ast_node.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "ast_node.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "yacc.tab.h"
  5. #include "symtable.h"
  6. // Create node with given token and value - no children
  7. struct ast_node *ast_create(enum AST_NODE_TYPE node_type, int value) {
  8. struct ast_node *new_node = malloc(sizeof(struct ast_node));
  9. new_node->node_type = node_type;
  10. new_node->value = value;
  11. dd_da_init(&new_node->children, sizeof(struct ast_node));
  12. return new_node;
  13. }
  14. void ast_child_add(struct ast_node *parent, struct ast_node *child) {
  15. dd_da_add(&parent->children, child);
  16. free(child);
  17. }
  18. void ast_child_add_first(struct ast_node *parent, struct ast_node *child) {
  19. dd_da_add_first(&parent->children, child);
  20. free(child);
  21. }
  22. void ast_delete_child(struct ast_node *node) {
  23. // Delete children
  24. for (int i = 0; i < node->children.elements; i++) {
  25. struct ast_node *child = dd_da_get(&node->children, i);
  26. ast_delete_child(child);
  27. }
  28. dd_da_free(&node->children);
  29. }
  30. void ast_delete(struct ast_node *node) {
  31. ast_delete_child(node);
  32. free(node);
  33. }
  34. // Print whole node tree, meant for debugging only
  35. int tabs = 0;
  36. void ast_print(struct ast_node *node) {
  37. // Print tabs (if any)
  38. for (int i = 0; i < tabs; i++) {
  39. printf("\t");
  40. }
  41. if (tabs == 0) {
  42. printf("Abstract Syntax Tree:\n");
  43. printf("*** ");
  44. }
  45. else {
  46. printf("* ");
  47. }
  48. // Print actual node
  49. char *type;
  50. struct entry *e = symtable_entryat(node->value);
  51. switch (node->node_type) {
  52. case AST_GAME: printf("GAME"); break;
  53. case AST_NUMBER: printf("NUMBER: %d", node->value); break;
  54. case AST_STRING:
  55. printf("STRING: %s", e->lexptr);
  56. break;
  57. case AST_GROUP: printf("GROUP"); break;
  58. case AST_COMMAND_NATIVE: printf("COMMAND NATIVE: %s", e->lexptr); break;
  59. case AST_COMMAND_CUSTOM: printf("COMMAND CUSTOM:"); break;
  60. case AST_IDENTIFIER:
  61. e = symtable_entryat(node->value);
  62. printf("IDENTIFIER: %s", e->lexptr);
  63. break;
  64. default:
  65. printf("%d | %d", node->node_type, node->value);
  66. break;
  67. }
  68. printf("\n");
  69. // Print children
  70. tabs++;
  71. for (int i = 0; i < node->children.elements; i++) {
  72. struct ast_node *child = dd_da_get(&node->children, i);
  73. ast_print(child);
  74. }
  75. tabs--;
  76. }
  77. // AST table
  78. #define AST_TABLE_MAX 1000
  79. struct ast_node* ast_table[AST_TABLE_MAX];
  80. int ast_table_lastentry;
  81. int ast_push(struct ast_node *n) {
  82. if (ast_table_lastentry +1 >= AST_TABLE_MAX) {
  83. return 0;
  84. }
  85. ast_table_lastentry++;
  86. ast_table[ast_table_lastentry] = n;
  87. }
  88. struct ast_node *ast_pop() {
  89. if (ast_table_lastentry < 0) {
  90. return 0;
  91. }
  92. ast_table_lastentry--;
  93. return ast_table[ast_table_lastentry+1];
  94. }