Skip to content

Commit 9183c6c

Browse files
committed
AST WIP
1 parent dbf728a commit 9183c6c

5 files changed

Lines changed: 193 additions & 169 deletions

File tree

include/ps_ast.h

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,86 +30,86 @@ extern "C"
3030
/** @brief Abstract Syntax Tree node kind */
3131
typedef enum enum_ps_ast_node_kind
3232
{
33-
PS_AST_PROGRAM, /** @brief Block: PROGRAM */
34-
PS_AST_PROCEDURE, /** @brief Block: PROCEDURE */
35-
PS_AST_FUNCTION, /** @brief Block: FUNCTION */
36-
PS_AST_UNIT, /** @brief Block: UNIT */
37-
PS_AST_STATEMENT_LIST, /** @brief Statement: List of statements */
38-
PS_AST_ASSIGNMENT, /** @brief Statement: Assignment */
39-
PS_AST_IF, /** @brief Statement: IF */
40-
PS_AST_WHILE, /** @brief Statement: WHILE */
41-
PS_AST_REPEAT, /** @brief Statement: REPEAT */
42-
PS_AST_FOR, /** @brief Statement: FOR */
43-
PS_AST_PROCEDURE_CALL, /** @brief Statement: PROCEDURE call */
44-
PS_AST_UNARY_OPERATION, /** @brief Expression: Unary operation */
45-
PS_AST_BINARY_OPERATION, /** @brief Expression: Binary operation */
46-
PS_AST_FUNCTION_CALL, /** @brief Expression: FUNCTION call */
47-
PS_AST_VALUE, /** @brief Expression: Value: integer, real, string, boolean, ... */
48-
PS_AST_VARIABLE_SIMPLE, /** @brief Expression: Simple variable (or constant) being accessed */
49-
PS_AST_VARIABLE_ARRAY, /** @brief Expression: Array element being accessed */
50-
PS_AST_LVALUE_SIMPLE, /** @brief Lvalue: Simple variable being written to */
51-
PS_AST_LVALUE_ARRAY, /** @brief Lvalue: Array element being written to */
33+
PS_AST_PROGRAM, /** @brief BLOCK: PROGRAM */
34+
PS_AST_PROCEDURE, /** @brief BLOCK: PROCEDURE */
35+
PS_AST_FUNCTION, /** @brief BLOCK: FUNCTION */
36+
PS_AST_UNIT, /** @brief BLOCK: UNIT */
37+
PS_AST_STATEMENT_LIST, /** @brief STATEMENT: List of statements */
38+
PS_AST_ASSIGNMENT, /** @brief STATEMENT: Assignment */
39+
PS_AST_IF, /** @brief STATEMENT: IF */
40+
PS_AST_WHILE, /** @brief STATEMENT: WHILE */
41+
PS_AST_REPEAT, /** @brief STATEMENT: REPEAT */
42+
PS_AST_FOR, /** @brief STATEMENT: FOR */
43+
PS_AST_PROCEDURE_CALL, /** @brief STATEMENT: PROCEDURE call */
44+
PS_AST_UNARY_OPERATION, /** @brief EXPRESSION: Unary operation */
45+
PS_AST_BINARY_OPERATION, /** @brief EXPRESSION: Binary operation */
46+
PS_AST_FUNCTION_CALL, /** @brief EXPRESSION: FUNCTION call */
47+
PS_AST_VALUE, /** @brief EXPRESSION: Value: integer, real, string, boolean, ... */
48+
PS_AST_VARIABLE_SIMPLE, /** @brief EXPRESSION: Simple variable (or constant) being accessed */
49+
PS_AST_VARIABLE_ARRAY, /** @brief EXPRESSION: Array element being accessed */
50+
PS_AST_LVALUE_SIMPLE, /** @brief LVALUE: Simple variable being written to */
51+
PS_AST_LVALUE_ARRAY, /** @brief LVALUE: Array element being written to */
5252
} __attribute__((__packed__)) ps_ast_node_kind;
5353

54-
#define PS_AST_NODE_COMMON_FIELDS \
55-
ps_ast_node_group group; /** @brief Node group */ \
56-
ps_ast_node_kind kind; /** @brief Node kind */ \
57-
uint16_t line; /** @brief Source code line number for error reporting, 0 if unknown */ \
54+
#define PS_AST_NODE_COMMON \
55+
ps_ast_node_group group; /** @brief Node group */ \
56+
ps_ast_node_kind kind; /** @brief Node kind */ \
57+
uint16_t line; /** @brief Source code line number for error reporting, 0 if unknown */ \
5858
uint16_t column; /** @brief Source code column number for error reporting, 0 if unknown */
5959

6060
/** @brief Abstract Syntax Tree node */
6161
typedef struct s_ps_ast_node
6262
{
63-
PS_AST_NODE_COMMON_FIELDS
63+
PS_AST_NODE_COMMON
6464
} ps_ast_node;
6565

6666
/** @brief Block is a program, procedure, function or unit */
6767
/** @details Units may be separated as they are special cases with interface and implementation */
6868
typedef struct s_ps_ast_block
6969
{
70-
PS_AST_NODE_COMMON_FIELDS
71-
ps_identifier name; /** @brief Every block has a name */
72-
size_t n_vars; /** @brief Number of variables to allocate at startup */
73-
ps_symbol_table *symbols; /** @brief Constants, types, variables, procedures and functions */
74-
ps_ast_node *statement_list; /** @brief Statements in this block */
75-
ps_formal_signature *signature; /** @brief Only for procedures and functions, empty otherwise */
76-
ps_symbol *result_type; /** @brief Only for functions, NULL otherwise */
77-
size_t n_executables; /** @brief 1 for PS_AST_PROCEDURE and PS_AST_FUNCTION */
78-
ps_ast_node **executables; /** @brief declarations of procedures and functions */
70+
PS_AST_NODE_COMMON
71+
ps_identifier name; /** @brief Every block has a name */
72+
size_t n_vars; /** @brief Number of variables to allocate at startup */
73+
ps_symbol_table *symbols; /** @brief Constants, types, variables, procedures and functions */
74+
size_t n_executables; /** @brief exactly 1 for procedure and function, 0 or more otherwise */
75+
ps_ast_node **executables; /** @brief declarations of procedures and functions */
76+
ps_ast_statement_list *statement_list; /** @brief Statements in this block */
77+
ps_formal_signature *signature; /** @brief Only for procedures and functions, empty otherwise */
78+
ps_symbol *result_type; /** @brief Only for functions, NULL otherwise */
7979
} ps_ast_block;
8080

8181
typedef struct s_ps_ast_statement_list
8282
{
83-
PS_AST_NODE_COMMON_FIELDS
83+
PS_AST_NODE_COMMON
8484
size_t count; /** @brief Number of statements */
8585
ps_ast_node **statements; /** @brief NULL if no statements */
8686
} ps_ast_statement_list;
8787

8888
typedef struct s_ps_ast_if
8989
{
90-
PS_AST_NODE_COMMON_FIELDS
90+
PS_AST_NODE_COMMON
9191
ps_ast_node *condition; /** @brief If condition, must be a boolean expression */
9292
ps_ast_statement_list *then_branch; /** @brief Statements to execute if condition is true, can be empty */
9393
ps_ast_statement_list *else_branch; /** @brief Statements to execute if condition is false, can be empty */
9494
} ps_ast_if;
9595

9696
typedef struct s_ps_ast_while
9797
{
98-
PS_AST_NODE_COMMON_FIELDS
98+
PS_AST_NODE_COMMON
9999
ps_ast_node *condition; /** @brief Loop while condition is true */
100100
ps_ast_statement_list *body; /** @brief Statements to execute while condition is true */
101101
} ps_ast_while;
102102

103103
typedef struct s_ps_ast_repeat
104104
{
105-
PS_AST_NODE_COMMON_FIELDS
105+
PS_AST_NODE_COMMON
106106
ps_ast_statement_list *body; /** @brief Statements to execute at least once */
107107
ps_ast_node *condition; /** @brief Loop until condition is true */
108108
} ps_ast_repeat;
109109

110110
typedef struct s_ps_ast_for
111111
{
112-
PS_AST_NODE_COMMON_FIELDS
112+
PS_AST_NODE_COMMON
113113
ps_ast_node *variable; /** @brief Loop variable */
114114
ps_ast_node *start; /** @brief Start value */
115115
ps_ast_node *end; /** @brief End value */
@@ -119,7 +119,7 @@ extern "C"
119119

120120
typedef struct s_ps_ast_call
121121
{
122-
PS_AST_NODE_COMMON_FIELDS
122+
PS_AST_NODE_COMMON
123123
ps_symbol *executable; /** @brief procedure of function being called */
124124
size_t n_args; /** @brief number of arguments, 0 if no arguments */
125125
ps_ast_node_argument *args; /** @brief arguments, NULL if no arguments */
@@ -136,46 +136,46 @@ extern "C"
136136

137137
typedef struct s_ps_ast_assignment
138138
{
139-
PS_AST_NODE_COMMON_FIELDS
139+
PS_AST_NODE_COMMON
140140
ps_ast_node *lvalue; /** @brief variable being assigned to */
141141
ps_ast_node *expression; /** @brief expression to assign to variable */
142142
} ps_ast_assignment;
143143

144144
/** @brief Expression: literal value */
145145
typedef struct s_ps_ast_value
146146
{
147-
PS_AST_NODE_COMMON_FIELDS
147+
PS_AST_NODE_COMMON
148148
ps_value value;
149149
} ps_ast_value;
150150

151151
/** @brief Variable or Lvalue: simple variable */
152152
/** @example I, Total, ... */
153153
typedef struct s_ps_ast_variable_simple
154154
{
155-
PS_AST_NODE_COMMON_FIELDS
155+
PS_AST_NODE_COMMON
156156
ps_symbol *variable; /** @brief Symbol being referenced */
157157
} ps_ast_variable_simple;
158158

159159
/** @brief Variable or Lvalue: array */
160160
/** @example A[I], A[I, J, K], ... */
161161
typedef struct s_ps_ast_variable_array
162162
{
163-
PS_AST_NODE_COMMON_FIELDS
163+
PS_AST_NODE_COMMON
164164
ps_symbol *variable; /** @brief Symbol being referenced */
165165
size_t n_indexes; /** @brief For array access, 0 if not an array */
166166
ps_ast_node **indexes; /** @brief For array access, NULL if not an array */
167167
} ps_ast_variable_array;
168168

169169
typedef struct s_ps_ast_unary_operation
170170
{
171-
PS_AST_NODE_COMMON_FIELDS
171+
PS_AST_NODE_COMMON
172172
ps_operator_unary operator;
173173
ps_ast_node *operand;
174174
} ps_ast_unary_operation;
175175

176176
typedef struct s_ps_ast_binary_operation
177177
{
178-
PS_AST_NODE_COMMON_FIELDS
178+
PS_AST_NODE_COMMON
179179
ps_operator_binary operator; /** @brief Binary operator */
180180
ps_ast_node *left; /** @brief Left operand */
181181
ps_ast_node *right; /** @brief Right operand */
@@ -200,7 +200,7 @@ extern "C"
200200
// clang-format on
201201

202202
/** @brief Create a new AST node of the given kind */
203-
ps_ast_node *ps_ast_create_node(ps_ast_node_group group, ps_ast_node_kind kind, uint16_t line, uint16_t column);
203+
ps_ast_node *ps_ast_create_node(ps_ast_node_group group, ps_ast_node_kind kind, uint16_t line, uint16_t column, size_t size);
204204

205205
ps_ast_node *ps_ast_create_block(uint16_t line, uint16_t column, ps_ast_node_kind kind, char *name);
206206
ps_ast_node *ps_ast_create_statement_list(uint16_t line, uint16_t column, size_t count);

src/ps_ast.c

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ ps_ast_node *ps_ast_free_node(ps_ast_node *node)
5353
return ps_ast_free_repeat(node);
5454
case PS_AST_FOR:
5555
return ps_ast_free_for(node);
56-
case PS_AST_PROCEDURE_CALL:
57-
return ps_ast_free_procedure_call(node);
5856
case PS_AST_UNARY_OPERATION:
5957
return ps_ast_free_unary_operation(node);
6058
case PS_AST_BINARY_OPERATION:
6159
return ps_ast_free_binary_operation(node);
62-
case PS_AST_CALL:
63-
return ps_ast_free_function_call(node);
6460
case PS_AST_VALUE:
6561
return ps_ast_free_value(node);
62+
case PS_AST_PROCEDURE_CALL:
63+
case PS_AST_FUNCTION_CALL:
64+
return ps_ast_free_call(node);
6665
case PS_AST_VARIABLE_SIMPLE:
6766
case PS_AST_LVALUE_SIMPLE:
6867
return ps_ast_free_variable_simple(node);
@@ -73,17 +72,24 @@ ps_ast_node *ps_ast_free_node(ps_ast_node *node)
7372
}
7473

7574
// ============================================================================
76-
// ps_ast_block
75+
// PS_AST_BLOCK
7776
// ============================================================================
7877

7978
ps_ast_block *ps_ast_create_block(uint16_t line, uint16_t column, ps_ast_node_kind kind, char *name)
8079
{
8180
assert(kind == PS_AST_PROGRAM || kind == PS_AST_PROCEDURE || kind == PS_AST_FUNCTION || kind == PS_AST_UNIT);
8281
ps_ast_block *block =
83-
(ps_ast_block *)ps_ast_create_node(PS_AST_GROUP_BLOCK, kind, line, column, sizeof(ps_ast_node_block));
82+
(ps_ast_block *)ps_ast_create_node(PS_AST_GROUP_BLOCK, kind, line, column, sizeof(ps_ast_block));
8483
if (block == NULL)
8584
return NULL;
8685
snprintf(block->name, PS_IDENTIFIER_LEN, "%s", name);
86+
block->symbols = NULL;
87+
block->signature = NULL;
88+
block->result_type = NULL;
89+
block->n_vars = 0;
90+
block->statement_list = NULL;
91+
block->n_executables = 0;
92+
block->executables = NULL;
8793
return block;
8894
}
8995

@@ -104,7 +110,7 @@ ps_ast_node *ps_ast_free_block(ps_ast_node *node)
104110
}
105111

106112
// ============================================================================
107-
// ps_ast_statement_list
113+
// PS_AST_STATEMENT_LIST
108114
// ============================================================================
109115

110116
ps_ast_statement_list *ps_ast_create_statement_list(uint16_t line, uint16_t column, size_t count)
@@ -133,7 +139,7 @@ ps_ast_statement_list *ps_ast_free_statement_list(ps_ast_statement_list *list)
133139
}
134140

135141
// ============================================================================
136-
// ps_ast_assignment
142+
// PS_AST_ASSIGNMENT
137143
// ============================================================================
138144

139145
ps_ast_assignment *ps_ast_create_assignment(uint16_t line, uint16_t column, ps_ast_node *lvalue,
@@ -159,7 +165,7 @@ ps_ast_assignment *ps_ast_free_assignment(ps_ast_assignment *assignment)
159165
}
160166

161167
// ============================================================================
162-
// ps_ast_if
168+
// PS_AST_IF
163169
// ============================================================================
164170

165171
ps_ast_if *ps_ast_create_if(uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_node *then_branch,
@@ -185,7 +191,7 @@ ps_ast_if *ps_ast_free_if(ps_ast_if *if_statement)
185191
}
186192

187193
// ============================================================================
188-
// ps_ast_while
194+
// PS_AST_WHILE
189195
// ============================================================================
190196

191197
ps_ast_while *ps_ast_create_while(uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_node *body)
@@ -208,7 +214,7 @@ ps_ast_while *ps_ast_free_while(ps_ast_while *while_statement)
208214
}
209215

210216
// ============================================================================
211-
// ps_ast_repeat
217+
// PS_AST_REPEAT
212218
// ============================================================================
213219

214220
ps_ast_repeat *ps_ast_create_repeat(uint16_t line, uint16_t column, ps_ast_node *body, ps_ast_node *condition)
@@ -233,7 +239,7 @@ ps_ast_repeat *ps_ast_free_repeat(ps_ast_repeat *repeat_statement)
233239
}
234240

235241
// ============================================================================
236-
// ps_ast_for
242+
// PS_AST_FOR
237243
// ============================================================================
238244

239245
ps_ast_for *ps_ast_create_for(uint16_t line, uint16_t column, ps_ast_node *variable, ps_ast_node *start,
@@ -262,7 +268,7 @@ ps_ast_for *ps_ast_free_for(ps_ast_for *for_statement)
262268
}
263269

264270
// ============================================================================
265-
// ps_ast_procedure_call
271+
// PS_AST_PROCEDURE_CALL
266272
// ============================================================================
267273

268274
ps_ast_call *ps_ast_create_procedure_call(uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args,
@@ -288,14 +294,14 @@ ps_ast_call *ps_ast_free_procedure_call(ps_ast_call *procedure_call)
288294
}
289295

290296
// ============================================================================
291-
// ps_ast_unary_operation
297+
// PS_AST_UNARY_OPERATION
292298
// ============================================================================
293299

294300
ps_ast_unary_operation *ps_ast_create_unary_operation(uint16_t line, uint16_t column, ps_operator_unary operator,
295301
ps_ast_node *operand)
296302
{
297-
ps_ast_unary_operation *unary_operation =
298-
ps_ast_create_node(PS_AST_GROUP_EXPRESSION, PS_AST_UNARY_OPERATION, line, column);
303+
ps_ast_unary_operation *unary_operation = ps_ast_create_node(PS_AST_GROUP_EXPRESSION, PS_AST_UNARY_OPERATION, line,
304+
column, sizeof(ps_ast_unary_operation));
299305
if (unary_operation == NULL)
300306
return NULL;
301307
unary_operation->operator = operator;
@@ -311,7 +317,7 @@ ps_ast_unary_operation *ps_ast_free_unary_operation(ps_ast_unary_operation *unar
311317
}
312318

313319
// ============================================================================
314-
// ps_ast_binary_operation
320+
// PS_AST_BINARY_OPERATION
315321
// ============================================================================
316322

317323
ps_ast_binary_operation *ps_ast_create_binary_operation(uint16_t line, uint16_t column, ps_operator_binary operator,
@@ -336,7 +342,7 @@ ps_ast_binary_operation *ps_ast_free_binary_operation(ps_ast_binary_operation *b
336342
}
337343

338344
// ============================================================================
339-
// ps_ast_value
345+
// PS_AST_VALUE
340346
// ============================================================================
341347

342348
ps_ast_value *ps_ast_create_value(uint16_t line, uint16_t column, ps_value literal)
@@ -351,12 +357,13 @@ ps_ast_value *ps_ast_create_value(uint16_t line, uint16_t column, ps_value liter
351357

352358
ps_ast_value *ps_ast_free_value(ps_ast_value *value)
353359
{
360+
ps_value_free(&value->value);
354361
ps_memory_free(PS_MEMORY_AST, value);
355362
return NULL;
356363
}
357364

358365
// ============================================================================
359-
// ps_ast_variable_simple
366+
// PS_AST_VARIABLE_SIMPLE
360367
// ============================================================================
361368

362369
ps_ast_variable_simple *ps_ast_create_variable_simple(uint16_t line, uint16_t column, ps_symbol *variable)
@@ -376,7 +383,7 @@ ps_ast_variable_simple *ps_ast_free_variable_simple(ps_ast_variable_simple *vari
376383
}
377384

378385
// ============================================================================
379-
// ps_ast_variable_array
386+
// PS_AST_VARIABLE_ARRAY
380387
// ============================================================================
381388

382389
ps_ast_variable_array *ps_ast_create_variable_array(uint16_t line, uint16_t column, ps_symbol *variable,

0 commit comments

Comments
 (0)