Astaroth  2.2
ast.h
Go to the documentation of this file.
1 /*
2  Nodes for the Abstract Syntax Tree
3 
4  Statement: syntactic unit tha expresses some action.
5  May have internal components, expressions, which are evaluated
6 
7  Statements: return value
8  block
9 */
10 #include <assert.h>
11 #include <stdlib.h>
12 
13 #define BUFFER_SIZE (4096)
14 
15 #define GEN_ID(X) X
16 #define GEN_STR(X) #X
17 
18 // clang-format off
19 #define FOR_NODE_TYPES(FUNC) \
20  FUNC(NODE_UNKNOWN), \
21  FUNC(NODE_DEFINITION), \
22  FUNC(NODE_GLOBAL_DEFINITION), \
23  FUNC(NODE_ITERATION_STATEMENT), \
24  FUNC(NODE_DECLARATION), \
25  FUNC(NODE_DECLARATION_LIST), \
26  FUNC(NODE_ARRAY_DECLARATION), \
27  FUNC(NODE_TYPE_DECLARATION), \
28  FUNC(NODE_TYPE_QUALIFIER), \
29  FUNC(NODE_TYPE_SPECIFIER), \
30  FUNC(NODE_IDENTIFIER), \
31  FUNC(NODE_FUNCTION_DEFINITION), \
32  FUNC(NODE_FUNCTION_DECLARATION), \
33  FUNC(NODE_COMPOUND_STATEMENT), \
34  FUNC(NODE_FUNCTION_PARAMETER_DECLARATION), \
35  FUNC(NODE_MULTIDIM_SUBSCRIPT_EXPRESSION), \
36  FUNC(NODE_REAL_NUMBER), \
37  FUNC(NODE_FOR_EXPRESSION)
38 // clang-format on
39 
41 
42 typedef struct astnode_s {
43  int id;
44  struct astnode_s* parent;
45  struct astnode_s* lhs;
46  struct astnode_s* rhs;
47  NodeType type; // Type of the AST node
48  char* buffer; // Indentifiers and other strings (empty by default)
49 
50  int token; // Type of a terminal (that is not a simple char)
51  int prefix; // Tokens. Also makes the grammar since we don't have
52  int infix; // to divide it into max two-child rules
53  int postfix; // (which makes it much harder to read)
54 } ASTNode;
55 
56 static inline ASTNode*
57 astnode_create(const NodeType type, ASTNode* lhs, ASTNode* rhs)
58 {
59  ASTNode* node = calloc(1, sizeof(node[0]));
60 
61  static int id_counter = 0;
62  node->id = id_counter++;
63  node->type = type;
64  node->lhs = lhs;
65  node->rhs = rhs;
66  node->buffer = NULL;
67 
68  node->token = 0;
69  node->prefix = node->infix = node->postfix = 0;
70 
71  if (lhs)
72  node->lhs->parent = node;
73 
74  if (rhs)
75  node->rhs->parent = node;
76 
77  return node;
78 }
79 
80 static inline void
81 astnode_set_buffer(const char* buffer, ASTNode* node)
82 {
83  node->buffer = strdup(buffer);
84 }
85 
86 static inline void
87 astnode_destroy(ASTNode* node)
88 {
89  if (node->lhs)
90  astnode_destroy(node->lhs);
91  if (node->rhs)
92  astnode_destroy(node->rhs);
93  if (node->buffer)
94  free(node->buffer);
95  free(node);
96 }
97 
98 static inline void
99 astnode_print(const ASTNode* node)
100 {
101  const char* node_type_names[] = {FOR_NODE_TYPES(GEN_STR)};
102 
103  printf("%s (%p)\n", node_type_names[node->type], node);
104  printf("\tid: %d\n", node->id);
105  printf("\tparent: %p\n", node->parent);
106  printf("\tlhs: %p\n", node->lhs);
107  printf("\trhs: %p\n", node->rhs);
108  printf("\tbuffer: %s\n", node->buffer);
109  printf("\ttoken: %d\n", node->token);
110  printf("\tprefix: %d ('%c')\n", node->prefix, node->prefix);
111  printf("\tinfix: %d ('%c')\n", node->infix, node->infix);
112  printf("\tpostfix: %d ('%c')\n", node->postfix, node->postfix);
113 }
114 
115 extern ASTNode* root;
NodeType
NodeType
Definition: ast.h:40
GEN_STR
#define GEN_STR(X)
Definition: ast.h:16
astnode_s::token
int token
Definition: ast.h:50
root
ASTNode * root
Definition: code_generator.c:37
NUM_NODE_TYPES
@ NUM_NODE_TYPES
Definition: ast.h:40
astnode_s
Definition: ast.h:42
astnode_s::lhs
struct astnode_s * lhs
Definition: ast.h:45
astnode_s::prefix
int prefix
Definition: ast.h:51
astnode_s::infix
int infix
Definition: ast.h:52
astnode_s::type
NodeType type
Definition: ast.h:47
ASTNode
struct astnode_s ASTNode
astnode_s::parent
struct astnode_s * parent
Definition: ast.h:44
FOR_NODE_TYPES
#define FOR_NODE_TYPES(FUNC)
Definition: ast.h:19
astnode_s::id
int id
Definition: ast.h:43
astnode_s::rhs
struct astnode_s * rhs
Definition: ast.h:46
GEN_ID
#define GEN_ID(X)
Definition: ast.h:15
astnode_s::postfix
int postfix
Definition: ast.h:53
astnode_s::buffer
char * buffer
Definition: ast.h:48