ast_node.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "ast_node.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "symtable.h"
  5. #include <string.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. new_node->arraySize = -1;
  12. new_node->isRef = 0;
  13. new_node->isIncluded = 0;
  14. new_node->lex[0] = '\0';
  15. dd_da_init(&new_node->children, sizeof(struct ast_node));
  16. new_node->parent = 0;
  17. return new_node;
  18. }
  19. int ast_child_add(struct ast_node *parent, struct ast_node *child) {
  20. dd_da_add(&parent->children, child);
  21. free(child);
  22. return parent->children.elements-1;
  23. }
  24. void ast_child_add_first(struct ast_node *parent, struct ast_node *child) {
  25. dd_da_add_first(&parent->children, child);
  26. free(child);
  27. }
  28. void ast_delete_child(struct ast_node *node) {
  29. // Delete children
  30. for (unsigned int i = 0; i < node->children.elements; i++) {
  31. struct ast_node *child = dd_da_get(&node->children, i);
  32. ast_delete_child(child);
  33. }
  34. dd_da_free(&node->children);
  35. }
  36. void ast_delete(struct ast_node *node) {
  37. ast_delete_child(node);
  38. free(node);
  39. }
  40. // Print whole node tree, meant for debugging only
  41. int tabs = 0;
  42. void ast_print(struct ast_node *node) {
  43. // Print tabs (if any)
  44. for (int i = 0; i < tabs; i++) {
  45. printf("\t");
  46. }
  47. if (tabs == 0) {
  48. printf("Abstract Syntax Tree:\n");
  49. printf("*** ");
  50. }
  51. else {
  52. printf("* ");
  53. }
  54. // Print actual node
  55. switch (node->node_type) {
  56. case AST_GAME: printf("GAME"); break;
  57. case AST_NUMBER: printf("NUMBER: %d", node->value); break;
  58. case AST_FLOAT: printf("FLOAT: %f", node->fvalue); break;
  59. case AST_STRING:
  60. printf("STRING: \"%s\"", node->lex);
  61. break;
  62. case AST_GROUP: printf("GROUP"); break;
  63. case AST_COMMAND_NATIVE: printf("COMMAND NATIVE: %s", node->lex); break;
  64. case AST_COMMAND_CUSTOM: printf("COMMAND CUSTOM: %s", node->lex); break;
  65. case AST_INCLUDE: printf("INCLUDE:"); break;
  66. case AST_IDENTIFIER:
  67. printf("IDENTIFIER: %s", node->lex);
  68. break;
  69. default:
  70. printf("%d | %d", node->node_type, node->value);
  71. break;
  72. }
  73. printf("\n");
  74. // Print children
  75. tabs++;
  76. for (unsigned int i = 0; i < node->children.elements; i++) {
  77. struct ast_node *child = dd_da_get(&node->children, i);
  78. ast_print(child);
  79. }
  80. tabs--;
  81. }
  82. // AST table
  83. #define AST_TABLE_MAX 1000
  84. struct ast_node* ast_table[AST_TABLE_MAX];
  85. int ast_table_lastentry;
  86. int ast_push(struct ast_node *n) {
  87. if (ast_table_lastentry +1 >= AST_TABLE_MAX) {
  88. return 0;
  89. }
  90. ast_table_lastentry++;
  91. ast_table[ast_table_lastentry] = n;
  92. // everything is ok, return a true value
  93. return 1;
  94. }
  95. struct ast_node *ast_pop() {
  96. if (ast_table_lastentry < 0) {
  97. return 0;
  98. }
  99. ast_table_lastentry--;
  100. return ast_table[ast_table_lastentry+1];
  101. }
  102. void ast_addLex(struct ast_node *n, const char *newLex) {
  103. if (strlen(newLex) > 499) {
  104. printf("lex is too long: %s\n", newLex);
  105. exit(-1);
  106. }
  107. strcpy(n->lex, newLex);
  108. n->lex[499] = '\0';
  109. }