Astaroth  3.1
Scalable Multi-GPU Stencil Computations
Loading...
Searching...
No Matches
create_node.h
Go to the documentation of this file.
1#pragma once
2
3#include "ast.h" // Needs to come before tab.h to have the definition of ASTNode.
4
5#include "tab.h"
6
7static inline ASTNode*
8create_identifier_node(const char* identifier)
9{
10 ASTNode* identifier_node = astnode_create(NODE_UNKNOWN, NULL, NULL);
11 if(is_number(identifier))
12 identifier_node->token = NUMBER;
13 else
14 identifier_node->token = IDENTIFIER;
15 astnode_set_buffer(identifier,identifier_node);
16 return identifier_node;
17}
18static inline ASTNode*
19create_type_qualifier(const char* tqual)
20{
21 if(!tqual) return NULL;
22 ASTNode* tqual_identifier = astnode_create(NODE_UNKNOWN,NULL,NULL);
23 astnode_set_buffer(tqual,tqual_identifier);
24 ASTNode* type_qualifier = astnode_create(NODE_TQUAL,tqual_identifier,NULL);
25 return type_qualifier;
26}
27static inline ASTNode*
28create_type_qualifiers(const char* tqual)
29{
30 ASTNode* type_qualifiers = astnode_create(NODE_UNKNOWN,create_type_qualifier(tqual),NULL);
31 return type_qualifiers;
32}
33static inline ASTNode*
34create_tspec(const char* tspec_str)
35{
36 ASTNode* tspec_identifier = astnode_create(NODE_UNKNOWN,NULL,NULL);
37 astnode_set_buffer(tspec_str,tspec_identifier);
38 ASTNode* tspec = astnode_create(NODE_TSPEC,tspec_identifier,NULL);
39 return tspec;
40}
41static inline ASTNode*
42create_type_declaration_with_qualifiers(const ASTNode* qualifiers, const char* tspec)
43{
44
45 if(qualifiers && tspec)
46 return astnode_create(NODE_UNKNOWN,astnode_dup(qualifiers,NULL),create_tspec(tspec));
47 if(tspec)
48 return astnode_create(NODE_UNKNOWN,create_tspec(tspec),NULL);
49 return astnode_create(NODE_UNKNOWN,astnode_dup(qualifiers,NULL),NULL);
50
51}
52static inline ASTNode*
53create_type_declaration(const char* tqual, const char* tspec)
54{
55
56 if(tqual && tspec)
57 return astnode_create(NODE_UNKNOWN,create_type_qualifiers(tqual),create_tspec(tspec));
58 if(tspec)
59 return astnode_create(NODE_UNKNOWN,create_tspec(tspec),NULL);
60 return astnode_create(NODE_UNKNOWN,create_type_qualifiers(tqual),NULL);
61
62}
63static inline ASTNode*
64create_declaration(const char* identifier, const char* type, const char* tqual)
65{
66 ASTNode* type_decl = (type == NULL && tqual == NULL) ? NULL : create_type_declaration(tqual, type);
67 ASTNode* decl_vars = astnode_create(NODE_UNKNOWN,create_identifier_node(identifier),NULL);
68 ASTNode* declaration = astnode_create(NODE_DECLARATION,type_decl,decl_vars);
69 return declaration;
70}
71static inline ASTNode*
72create_binary_op_expr(const char* op)
73{
74 ASTNode* res = astnode_create(NODE_UNKNOWN,NULL,NULL);
75 res->token = BINARY_OP;
76 astnode_set_buffer(op,res);
77 return res;
78}
79static inline ASTNode*
80create_choose_node(ASTNode* lhs_value, ASTNode* rhs_value)
81{
82 ASTNode* res = astnode_create(NODE_UNKNOWN,lhs_value,rhs_value);
83 astnode_set_prefix("?", res);
84 astnode_set_prefix(": ",res->rhs);
85 return res;
86}
87static inline ASTNode*
88create_ternary_expr(ASTNode* conditional, ASTNode* lhs_value, ASTNode* rhs_value)
89{
90
91 return astnode_create(NODE_TERNARY_EXPRESSION,
92 astnode_dup(conditional,NULL),
93 create_choose_node(astnode_dup(lhs_value,NULL),astnode_dup(rhs_value,NULL))
94 );
95}
96static inline ASTNode*
97create_binary_expression(ASTNode* expression , ASTNode* unary_expression, const char* op)
98{
99
100 ASTNode* rhs = astnode_create(NODE_UNKNOWN,
101 create_binary_op_expr(op),
102 unary_expression);
103 return
104 astnode_create(NODE_BINARY_EXPRESSION,expression,rhs);
105}
106
107
108static inline ASTNode*
109create_assign_op(const char* op)
110{
111 ASTNode* res = astnode_create(NODE_UNKNOWN,NULL,NULL);
112 astnode_set_buffer(op,res);
113 res->token = ASSIGNOP;
114 return res;
115}
116
117static inline ASTNode*
118create_assignment_body(const ASTNode* assign_expr, const char* op)
119{
120 ASTNode* expression_list = astnode_create(NODE_UNKNOWN,astnode_dup(assign_expr,NULL),NULL);
121 return astnode_create(NODE_UNKNOWN,
122 astnode_dup(create_assign_op(op),NULL),
123 astnode_dup(expression_list,NULL)
124 );
125}
126
127static inline ASTNode*
128create_basic_statement(const ASTNode* statement)
129{
130 ASTNode* basic_statement = astnode_create(NODE_UNKNOWN,astnode_dup(statement,NULL),NULL);
131 basic_statement ->token = BASIC_STATEMENT;
132 ASTNode* res = astnode_create(NODE_UNKNOWN,basic_statement,NULL);
133 res->token = STATEMENT;
134 return res;
135}
136static inline ASTNode*
137create_variable_definition(const ASTNode* lhs)
138{
139 ASTNode* res = astnode_create(NODE_UNKNOWN,astnode_dup(lhs,NULL),NULL);
140 astnode_set_postfix(";",res);
141 return res;
142}
143static inline ASTNode*
144create_assignment(const ASTNode* lhs, const ASTNode* assign_expr, const char* op)
145{
146
147 return create_basic_statement(create_variable_definition
148 (
149 astnode_create(NODE_ASSIGNMENT,
150 astnode_dup(lhs,NULL),
151 create_assignment_body(assign_expr,op)
152 )
153 ));
154}
155static inline ASTNode* create_arr_initializer(const ASTNode* elems)
156{
157 ASTNode* arr_initializer = astnode_create(NODE_ARRAY_INITIALIZER,astnode_dup(elems,NULL),NULL);
158 astnode_set_prefix("{",arr_initializer);
159 astnode_set_postfix("}",arr_initializer);
160 return arr_initializer;
161}
162static inline ASTNode* create_struct_initializer(const ASTNode* elems)
163{
164 ASTNode* arr_initializer = astnode_create(NODE_STRUCT_INITIALIZER,astnode_dup(elems,NULL),NULL);
165 astnode_set_prefix("{",arr_initializer);
166 astnode_set_postfix("}",arr_initializer);
167 return arr_initializer;
168}
169static inline ASTNode* create_const_declaration(const ASTNode* rhs, const char* name, const ASTNode* type_declaration)
170{
171 ASTNode* expression = astnode_create(NODE_UNKNOWN,astnode_dup(rhs,NULL),NULL);
172 ASTNode* assign_expression = astnode_create(NODE_UNKNOWN,expression,NULL);
173 ASTNode* var_identifier = astnode_create(NODE_VARIABLE_ID,NULL,NULL);
174 astnode_sprintf(var_identifier,"%s",name);
175 var_identifier->token = IDENTIFIER;
176 ASTNode* assign_leaf = astnode_create(NODE_ASSIGNMENT, var_identifier,assign_expression);
177 ASTNode* assignment_list = astnode_create(NODE_UNKNOWN, assign_leaf,NULL);
178 ASTNode* var_definitions = astnode_create(NODE_ASSIGN_LIST,astnode_dup(type_declaration,NULL),assignment_list);
179 var_definitions -> type |= NODE_NO_OUT;
180 var_definitions -> type |= NODE_DECLARATION;
181 astnode_set_postfix(";",var_definitions);
182 return var_definitions;
183}
@ NODE_ASSIGNMENT
Definition ast.h:84
@ NODE_TSPEC
Definition ast.h:73
@ NODE_ASSIGN_LIST
Definition ast.h:94
@ NODE_ARRAY_INITIALIZER
Definition ast.h:79
@ NODE_TERNARY_EXPRESSION
Definition ast.h:104
@ NODE_DECLARATION
Definition ast.h:72
@ NODE_BINARY_EXPRESSION
Definition ast.h:103
@ NODE_VARIABLE_ID
Definition ast.h:78
@ NODE_NO_OUT
Definition ast.h:96
@ NODE_TQUAL
Definition ast.h:74
@ NODE_UNKNOWN
Definition ast.h:64
@ NODE_STRUCT_INITIALIZER
Definition ast.h:88
Definition ast.h:146
struct astnode_s * rhs
Definition ast.h:149
int token
Definition ast.h:156