ast_node.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef _PGMPREFIX_AST_NODE_H_
  2. #define _PGMPREFIX_AST_NODE_H_
  3. #include "dd_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_STRING,
  27. };
  28. // Struct for a single node
  29. struct ast_node {
  30. enum AST_NODE_TYPE node_type;
  31. int value;
  32. struct dd_dynamic_array children;
  33. };
  34. // Actions
  35. struct ast_node *ast_create(enum AST_NODE_TYPE node_type, int value);
  36. void ast_child_add(struct ast_node *parent, struct ast_node *child);
  37. void ast_child_add_first(struct ast_node *parent, struct ast_node *child);
  38. void ast_delete(struct ast_node *node);
  39. // Debug - Print node tree
  40. void ast_print(struct ast_node *node);
  41. int ast_push(struct ast_node *n);
  42. struct ast_node *ast_pop();
  43. #endif