avdl_ast_node.c 3.0 KB

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