ast_node.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_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 isIncluded;
  40. struct dd_dynamic_array children;
  41. struct ast_node *parent;
  42. };
  43. // Actions
  44. struct ast_node *ast_create(enum AST_NODE_TYPE node_type, int value);
  45. void ast_child_add(struct ast_node *parent, struct ast_node *child);
  46. void ast_child_add_first(struct ast_node *parent, struct ast_node *child);
  47. void ast_delete(struct ast_node *node);
  48. // Debug - Print node tree
  49. void ast_print(struct ast_node *node);
  50. int ast_push(struct ast_node *n);
  51. struct ast_node *ast_pop();
  52. #endif