avdl_ast_node.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef AVDL_AST_NODE_H
  2. #define AVDL_AST_NODE_H
  3. #include "avdl_dynamic_array.h"
  4. /* abstract syntax tree
  5. * contains nodes that represent logic inside the game
  6. * each node has a unique "signature" (amount of children) to be considered
  7. * correct, and be able to parse itself properly to the target language
  8. */
  9. // ast node types
  10. enum AST_NODE_TYPE {
  11. /* parent of all nodes
  12. * children: statements
  13. */
  14. AST_GAME,
  15. AST_EMPTY,
  16. AST_GROUP,
  17. AST_COMMAND_NATIVE,
  18. AST_COMMAND_CUSTOM,
  19. /* variables - contain children that go through the struct hierarchy
  20. * example "this.x" results in a node AST_IDENTIFIER that points to the struct of "this" and
  21. * a child with another AST_IDENTIFIER that points to "x"
  22. */
  23. AST_IDENTIFIER,
  24. /* constants - have no children, are parsed as-is */
  25. AST_NUMBER,
  26. AST_FLOAT,
  27. AST_STRING,
  28. AST_INCLUDE,
  29. };
  30. // Struct for a single node
  31. struct ast_node {
  32. enum AST_NODE_TYPE node_type;
  33. union {
  34. int value;
  35. float fvalue;
  36. };
  37. int arraySize;
  38. int isRef;
  39. int isExtern;
  40. int isIncluded;
  41. struct dd_dynamic_array children;
  42. struct ast_node *parent;
  43. char lex[500];
  44. };
  45. // Actions
  46. struct ast_node *ast_create(enum AST_NODE_TYPE node_type, int value);
  47. int ast_child_add(struct ast_node *parent, struct ast_node *child);
  48. void ast_child_add_first(struct ast_node *parent, struct ast_node *child);
  49. void ast_delete(struct ast_node *node);
  50. void ast_addLex(struct ast_node *node, const char *newLex);
  51. // Debug - Print node tree
  52. void ast_print(struct ast_node *node);
  53. int ast_push(struct ast_node *n);
  54. struct ast_node *ast_pop();
  55. #endif