ast_node.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. AST_GAME,
  13. AST_EMPTY,
  14. // define a variable
  15. AST_DEFINITION,
  16. // assign an expression to a variable
  17. AST_ASSIGNMENT,
  18. // defining a function
  19. AST_FUNCTION_DEFINITION,
  20. AST_FUNCTION_CALL,
  21. AST_ARGUMENTS,
  22. AST_ARGUMENT,
  23. // symbol types
  24. AST_STRUCT,
  25. AST_STRUCT_PARENT,
  26. AST_GROUP,
  27. AST_VARTYPE_INT,
  28. AST_VARTYPE_FLOAT,
  29. AST_VARTYPE_VOID,
  30. AST_VARTYPE_STRUCT,
  31. // mathematical operators
  32. AST_ADDITION,
  33. AST_SUBSTRACTION,
  34. AST_MULTIPLICATION,
  35. AST_DIVISION,
  36. AST_LESS_THAN,
  37. AST_MORE_THAN,
  38. AST_EQUAL,
  39. AST_LESS_EQUAL,
  40. AST_MORE_EQUAL,
  41. AST_GROUP_EXPRESSIONS,
  42. /* variables - contain children that go through the struct hierarchy
  43. * example "this.x" results in a node AST_IDENTIFIER that points to the struct of "this" and
  44. * a child with another AST_IDENTIFIER that points to "x"
  45. */
  46. AST_IDENTIFIER,
  47. /* constants - have no children, are parsed as-is */
  48. AST_NUMBER,
  49. AST_STRING,
  50. AST_IF,
  51. };
  52. // Struct for a single node
  53. struct ast_node {
  54. enum AST_NODE_TYPE node_type;
  55. int value;
  56. struct dd_dynamic_array children;
  57. };
  58. // Actions
  59. struct ast_node *ast_create(enum AST_NODE_TYPE node_type, int value);
  60. void ast_child_add(struct ast_node *parent, struct ast_node *child);
  61. void ast_delete(struct ast_node *node);
  62. // Debug - Print node tree
  63. void ast_print(struct ast_node *node);
  64. int ast_push(struct ast_node *n);
  65. struct ast_node *ast_pop();
  66. #endif