Skip to content

Commit 80bb3e3

Browse files
committed
AST WIP
1 parent 9183c6c commit 80bb3e3

9 files changed

Lines changed: 133 additions & 120 deletions

File tree

include/ps_ast.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ extern "C"
2121
/** @brief Abstract Syntax Tree node group */
2222
typedef enum e_ps_ast_node_group
2323
{
24-
PS_AST_GROUP_BLOCK,
25-
PS_AST_GROUP_STATEMENT,
26-
PS_AST_GROUP_EXPRESSION,
27-
PS_AST_GROUP_LVALUE
24+
PS_AST_GROUP_UNKNOWN = 0,
25+
PS_AST_BLOCK,
26+
PS_AST_STATEMENT,
27+
PS_AST_EXPRESSION,
28+
PS_AST_LVALUE,
29+
PS_AST_ARGUMENT,
2830
} __attribute__((__packed__)) ps_ast_node_group;
2931

3032
/** @brief Abstract Syntax Tree node kind */
3133
typedef enum enum_ps_ast_node_kind
3234
{
35+
PS_AST_KIND_UNKNOWN = 0, /** @brief Unknown node kind */
3336
PS_AST_PROGRAM, /** @brief BLOCK: PROGRAM */
3437
PS_AST_PROCEDURE, /** @brief BLOCK: PROCEDURE */
3538
PS_AST_FUNCTION, /** @brief BLOCK: FUNCTION */
@@ -49,13 +52,16 @@ extern "C"
4952
PS_AST_VARIABLE_ARRAY, /** @brief EXPRESSION: Array element being accessed */
5053
PS_AST_LVALUE_SIMPLE, /** @brief LVALUE: Simple variable being written to */
5154
PS_AST_LVALUE_ARRAY, /** @brief LVALUE: Array element being written to */
55+
PS_AST_ARG_EXPR, /** @brief ARGUMENT: Expression passed by value */
56+
PS_AST_ARG_VAR_BY_VAL, /** @brief ARGUMENT: Variable passed by value */
57+
PS_AST_ARG_VAR_BY_REF, /** @brief ARGUMENT: Variable passed by reference */
5258
} __attribute__((__packed__)) ps_ast_node_kind;
5359

5460
#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 */ \
58-
uint16_t column; /** @brief Source code column number for error reporting, 0 if unknown */
61+
ps_ast_node_group group; /** @brief Node group */ \
62+
ps_ast_node_kind kind; /** @brief Node kind */ \
63+
uint16_t line; /** @brief Source code start line number for error reporting, 0 if unknown */ \
64+
uint16_t column; /** @brief Source code start column number for error reporting, 0 if unknown */
5965

6066
/** @brief Abstract Syntax Tree node */
6167
typedef struct s_ps_ast_node
@@ -67,13 +73,12 @@ extern "C"
6773
/** @details Units may be separated as they are special cases with interface and implementation */
6874
typedef struct s_ps_ast_block
6975
{
70-
PS_AST_NODE_COMMON
71-
ps_identifier name; /** @brief Every block has a name */
76+
PS_AST_NODE_COMMON ps_identifier name; /** @brief Every block has a name */
7277
size_t n_vars; /** @brief Number of variables to allocate at startup */
7378
ps_symbol_table *symbols; /** @brief Constants, types, variables, procedures and functions */
7479
size_t n_executables; /** @brief exactly 1 for procedure and function, 0 or more otherwise */
7580
ps_ast_node **executables; /** @brief declarations of procedures and functions */
76-
ps_ast_statement_list *statement_list; /** @brief Statements in this block */
81+
ps_ast_statement_list *statement_list; /** @brief Statements in this block */
7782
ps_formal_signature *signature; /** @brief Only for procedures and functions, empty otherwise */
7883
ps_symbol *result_type; /** @brief Only for functions, NULL otherwise */
7984
} ps_ast_block;
@@ -120,19 +125,16 @@ extern "C"
120125
typedef struct s_ps_ast_call
121126
{
122127
PS_AST_NODE_COMMON
123-
ps_symbol *executable; /** @brief procedure of function being called */
124-
size_t n_args; /** @brief number of arguments, 0 if no arguments */
125-
ps_ast_node_argument *args; /** @brief arguments, NULL if no arguments */
128+
ps_symbol *executable; /** @brief procedure of function being called */
129+
size_t n_args; /** @brief number of arguments, 0 if no arguments */
130+
ps_ast_argument **args; /** @brief arguments, NULL if no arguments */
126131
} ps_ast_call;
127132

128133
typedef struct s_ps_ast_argument
129134
{
130-
bool is_reference; /** @brief true if argument is passed by reference */
131-
union {
132-
ps_ast_node *expression; /** @brief expression argument (by value) */
133-
ps_ast_node *symbol_reference; /** @brief variable argument (by value or by reference) */
134-
};
135-
} ps_ast_node_argument;
135+
PS_AST_NODE_COMMON
136+
ps_ast_node *arg; /** @brief expression (by value) or variable (by value or by reference)*/
137+
} ps_ast_argument;
136138

137139
typedef struct s_ps_ast_assignment
138140
{
@@ -190,7 +192,7 @@ extern "C"
190192
#define PS_AST_NODE_REPEAT_SIZE sizeof(ps_ast_repeat)
191193
#define PS_AST_NODE_FOR_SIZE sizeof(ps_ast_for)
192194
#define PS_AST_NODE_CALL_SIZE sizeof(ps_ast_call)
193-
#define PS_AST_NODE_ARGUMENT_SIZE sizeof(ps_ast_node_argument)
195+
#define PS_AST_NODE_ARGUMENT_SIZE sizeof(ps_ast_argument)
194196
#define PS_AST_NODE_ASSIGNMENT_SIZE sizeof(ps_ast_assignment)
195197
#define PS_AST_NODE_VALUE_SIZE sizeof(ps_ast_value)
196198
#define PS_AST_NODE_UNARY_OPERATION_SIZE sizeof(ps_ast_unary_operation)
@@ -200,7 +202,8 @@ extern "C"
200202
// clang-format on
201203

202204
/** @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, size_t size);
205+
ps_ast_node *ps_ast_create_node(ps_ast_node_group group, ps_ast_node_kind kind, uint16_t line, uint16_t column,
206+
size_t size);
204207

205208
ps_ast_node *ps_ast_create_block(uint16_t line, uint16_t column, ps_ast_node_kind kind, char *name);
206209
ps_ast_node *ps_ast_create_statement_list(uint16_t line, uint16_t column, size_t count);
@@ -215,9 +218,9 @@ extern "C"
215218
ps_ast_node *ps_ast_create_for(uint16_t line, uint16_t column, ps_ast_node *variable, ps_ast_node *start,
216219
ps_ast_node *end, int step, ps_ast_statement_list *body);
217220
ps_ast_node *ps_ast_create_procedure_call(uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args,
218-
ps_ast_node_argument *args);
221+
ps_ast_argument *args);
219222
ps_ast_node *ps_ast_create_function_call(uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args,
220-
ps_ast_node_argument *args);
223+
ps_ast_argument *args);
221224
ps_ast_node *ps_ast_create_unary_operation(uint16_t line, uint16_t column, ps_operator_unary operator,
222225
ps_ast_node * operand);
223226
ps_ast_node *ps_ast_create_binary_operation(uint16_t line, uint16_t column, ps_operator_binary operator,

include/ps_value.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ extern "C"
3434
/** @brief Free allocated value */
3535
ps_value *ps_value_free(ps_value *value);
3636

37-
/** @brief Scalar is: Integer, Unsigned, Integer or Unsigned subrange */
37+
/** @brief Scalar is: Ordinal, Real, *FUTURE* Pointer */
3838
bool ps_value_is_scalar(const ps_value *value);
39-
/** @brief Ordinal is: Boolean, Char or Enum */
39+
/** @brief Ordinal is: Integer, Unsigned, Integer or Unsigned subrange, Boolean, Char or Enum */
4040
bool ps_value_is_ordinal(const ps_value *value);
41-
/** @brief Number is: Integer, Unsigned, Real, Integer or Unsigned subrange */
41+
/** @brief Number is: Real, Integer, Unsigned, Integer or Unsigned subrange */
4242
bool ps_value_is_number(const ps_value *value);
4343
/** @brief is Real? */
4444
bool ps_value_is_real(const ps_value *value);

src/ps_ast.c

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ ps_ast_node *ps_ast_free_node(ps_ast_node *node)
7878
ps_ast_block *ps_ast_create_block(uint16_t line, uint16_t column, ps_ast_node_kind kind, char *name)
7979
{
8080
assert(kind == PS_AST_PROGRAM || kind == PS_AST_PROCEDURE || kind == PS_AST_FUNCTION || kind == PS_AST_UNIT);
81-
ps_ast_block *block =
82-
(ps_ast_block *)ps_ast_create_node(PS_AST_GROUP_BLOCK, kind, line, column, sizeof(ps_ast_block));
81+
ps_ast_block *block = (ps_ast_block *)ps_ast_create_node(PS_AST_BLOCK, kind, line, column, sizeof(ps_ast_block));
8382
if (block == NULL)
8483
return NULL;
8584
snprintf(block->name, PS_IDENTIFIER_LEN, "%s", name);
@@ -116,7 +115,7 @@ ps_ast_node *ps_ast_free_block(ps_ast_node *node)
116115
ps_ast_statement_list *ps_ast_create_statement_list(uint16_t line, uint16_t column, size_t count)
117116
{
118117
ps_ast_statement_list *list = (ps_ast_statement_list *)ps_ast_create_node(
119-
PS_AST_GROUP_STATEMENT, PS_AST_STATEMENT_LIST, line, column, sizeof(ps_ast_statement_list));
118+
PS_AST_STATEMENT, PS_AST_STATEMENT_LIST, line, column, sizeof(ps_ast_statement_list));
120119
if (list == NULL)
121120
return NULL;
122121
list->count = count;
@@ -145,10 +144,10 @@ ps_ast_statement_list *ps_ast_free_statement_list(ps_ast_statement_list *list)
145144
ps_ast_assignment *ps_ast_create_assignment(uint16_t line, uint16_t column, ps_ast_node *lvalue,
146145
ps_ast_node *expression)
147146
{
148-
assert(lvalue != NULL && ps_ast_check_group(lvalue, PS_AST_GROUP_LVALUE));
149-
assert(expression != NULL && ps_ast_check_group(expression, PS_AST_GROUP_EXPRESSION));
150-
ps_ast_assignment *assignment = (ps_ast_assignment *)ps_ast_create_node(PS_AST_GROUP_STATEMENT, PS_AST_ASSIGNMENT,
151-
line, column, sizeof(ps_ast_assignment));
147+
assert(lvalue != NULL && ps_ast_check_group(lvalue, PS_AST_LVALUE));
148+
assert(expression != NULL && ps_ast_check_group(expression, PS_AST_EXPRESSION));
149+
ps_ast_assignment *assignment = (ps_ast_assignment *)ps_ast_create_node(PS_AST_STATEMENT, PS_AST_ASSIGNMENT, line,
150+
column, sizeof(ps_ast_assignment));
152151
if (assignment == NULL)
153152
return NULL;
154153
assignment->lvalue = lvalue;
@@ -172,7 +171,7 @@ ps_ast_if *ps_ast_create_if(uint16_t line, uint16_t column, ps_ast_node *conditi
172171
ps_ast_node *else_branch)
173172
{
174173
ps_ast_if *if_statement =
175-
(ps_ast_if *)ps_ast_create_node(PS_AST_GROUP_STATEMENT, PS_AST_IF, line, column, sizeof(ps_ast_if));
174+
(ps_ast_if *)ps_ast_create_node(PS_AST_STATEMENT, PS_AST_IF, line, column, sizeof(ps_ast_if));
176175
if (if_statement == NULL)
177176
return NULL;
178177
if_statement->condition = condition;
@@ -197,7 +196,7 @@ ps_ast_if *ps_ast_free_if(ps_ast_if *if_statement)
197196
ps_ast_while *ps_ast_create_while(uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_node *body)
198197
{
199198
ps_ast_while *while_statement =
200-
(ps_ast_while *)ps_ast_create_node(PS_AST_GROUP_STATEMENT, PS_AST_WHILE, line, column, sizeof(ps_ast_while));
199+
(ps_ast_while *)ps_ast_create_node(PS_AST_STATEMENT, PS_AST_WHILE, line, column, sizeof(ps_ast_while));
201200
if (while_statement == NULL)
202201
return NULL;
203202
while_statement->condition = condition;
@@ -220,7 +219,7 @@ ps_ast_while *ps_ast_free_while(ps_ast_while *while_statement)
220219
ps_ast_repeat *ps_ast_create_repeat(uint16_t line, uint16_t column, ps_ast_node *body, ps_ast_node *condition)
221220
{
222221
ps_ast_repeat *repeat_statement =
223-
(ps_ast_repeat *)ps_ast_create_node(PS_AST_GROUP_STATEMENT, PS_AST_REPEAT, line, column, sizeof(ps_ast_repeat));
222+
(ps_ast_repeat *)ps_ast_create_node(PS_AST_STATEMENT, PS_AST_REPEAT, line, column, sizeof(ps_ast_repeat));
224223
if (repeat_statement == NULL)
225224
return NULL;
226225
assert(body != NULL);
@@ -242,11 +241,11 @@ ps_ast_repeat *ps_ast_free_repeat(ps_ast_repeat *repeat_statement)
242241
// PS_AST_FOR
243242
// ============================================================================
244243

245-
ps_ast_for *ps_ast_create_for(uint16_t line, uint16_t column, ps_ast_node *variable, ps_ast_node *start,
244+
ps_ast_for *ps_ast_create_for(uint16_t line, uint16_t column, ps_ast_variable_simple *variable, ps_ast_node *start,
246245
ps_ast_node *end, int step, ps_ast_node *body)
247246
{
248247
ps_ast_for *for_statement =
249-
(ps_ast_for *)ps_ast_create_node(PS_AST_GROUP_STATEMENT, PS_AST_FOR, line, column, sizeof(ps_ast_for));
248+
(ps_ast_for *)ps_ast_create_node(PS_AST_STATEMENT, PS_AST_FOR, line, column, sizeof(ps_ast_for));
250249
if (for_statement == NULL)
251250
return NULL;
252251
for_statement->variable = variable;
@@ -272,10 +271,10 @@ ps_ast_for *ps_ast_free_for(ps_ast_for *for_statement)
272271
// ============================================================================
273272

274273
ps_ast_call *ps_ast_create_procedure_call(uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args,
275-
ps_ast_node_argument *args)
274+
ps_ast_argument *args[])
276275
{
277-
ps_ast_call *procedure_call = (ps_ast_call *)ps_ast_create_node(PS_AST_GROUP_STATEMENT, PS_AST_PROCEDURE_CALL, line,
278-
column, sizeof(ps_ast_call));
276+
ps_ast_call *procedure_call =
277+
(ps_ast_call *)ps_ast_create_node(PS_AST_STATEMENT, PS_AST_PROCEDURE_CALL, line, column, sizeof(ps_ast_call));
279278
if (procedure_call == NULL)
280279
return NULL;
281280
procedure_call->executable = executable;
@@ -293,15 +292,36 @@ ps_ast_call *ps_ast_free_procedure_call(ps_ast_call *procedure_call)
293292
return NULL;
294293
}
295294

295+
// ============================================================================
296+
// PS_AST_ARGUMENT
297+
// ============================================================================
298+
299+
ps_ast_argument *ps_ast_create_argument(uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_ast_node *arg)
300+
{
301+
ps_ast_argument *argument =
302+
(ps_ast_argument *)ps_ast_create_node(PS_AST_ARGUMENT, kind, line, column, sizeof(ps_ast_argument));
303+
if (argument == NULL)
304+
return NULL;
305+
argument->arg = arg;
306+
return argument;
307+
}
308+
309+
ps_ast_argument *ps_ast_free_argument(ps_ast_argument *argument)
310+
{
311+
argument->arg = ps_ast_free_node(argument->arg);
312+
ps_memory_free(PS_MEMORY_AST, argument);
313+
return NULL;
314+
}
315+
296316
// ============================================================================
297317
// PS_AST_UNARY_OPERATION
298318
// ============================================================================
299319

300320
ps_ast_unary_operation *ps_ast_create_unary_operation(uint16_t line, uint16_t column, ps_operator_unary operator,
301321
ps_ast_node *operand)
302322
{
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));
323+
ps_ast_unary_operation *unary_operation =
324+
ps_ast_create_node(PS_AST_EXPRESSION, PS_AST_UNARY_OPERATION, line, column, sizeof(ps_ast_unary_operation));
305325
if (unary_operation == NULL)
306326
return NULL;
307327
unary_operation->operator = operator;
@@ -324,7 +344,7 @@ ps_ast_binary_operation *ps_ast_create_binary_operation(uint16_t line, uint16_t
324344
ps_ast_node *left, ps_ast_node *right)
325345
{
326346
ps_ast_binary_operation *binary_operation = (ps_ast_binary_operation *)ps_ast_create_node(
327-
PS_AST_GROUP_EXPRESSION, PS_AST_BINARY_OPERATION, line, column, sizeof(ps_ast_binary_operation));
347+
PS_AST_EXPRESSION, PS_AST_BINARY_OPERATION, line, column, sizeof(ps_ast_binary_operation));
328348
if (binary_operation == NULL)
329349
return NULL;
330350
binary_operation->operator = operator;
@@ -348,7 +368,7 @@ ps_ast_binary_operation *ps_ast_free_binary_operation(ps_ast_binary_operation *b
348368
ps_ast_value *ps_ast_create_value(uint16_t line, uint16_t column, ps_value literal)
349369
{
350370
ps_ast_value *value =
351-
(ps_ast_value *)ps_ast_create_node(PS_AST_GROUP_EXPRESSION, PS_AST_VALUE, line, column, sizeof(ps_ast_value));
371+
(ps_ast_value *)ps_ast_create_node(PS_AST_EXPRESSION, PS_AST_VALUE, line, column, sizeof(ps_ast_value));
352372
if (value == NULL)
353373
return NULL;
354374
value->value = literal;
@@ -369,7 +389,7 @@ ps_ast_value *ps_ast_free_value(ps_ast_value *value)
369389
ps_ast_variable_simple *ps_ast_create_variable_simple(uint16_t line, uint16_t column, ps_symbol *variable)
370390
{
371391
ps_ast_variable_simple *variable_simple = (ps_ast_variable_simple *)ps_ast_create_node(
372-
PS_AST_GROUP_EXPRESSION, PS_AST_VARIABLE_SIMPLE, line, column, sizeof(ps_ast_variable_simple));
392+
PS_AST_EXPRESSION, PS_AST_VARIABLE_SIMPLE, line, column, sizeof(ps_ast_variable_simple));
373393
if (variable_simple == NULL)
374394
return NULL;
375395
variable_simple->variable = variable;
@@ -390,7 +410,7 @@ ps_ast_variable_array *ps_ast_create_variable_array(uint16_t line, uint16_t colu
390410
size_t n_indexes, ps_ast_node *indexes)
391411
{
392412
ps_ast_variable_array *variable_array = (ps_ast_variable_array *)ps_ast_create_node(
393-
PS_AST_GROUP_EXPRESSION, PS_AST_VARIABLE_ARRAY, line, column, sizeof(ps_ast_variable_array));
413+
PS_AST_EXPRESSION, PS_AST_VARIABLE_ARRAY, line, column, sizeof(ps_ast_variable_array));
394414
if (variable_array == NULL)
395415
return NULL;
396416
variable_array->variable = variable;
@@ -416,10 +436,10 @@ ps_ast_variable_array *ps_ast_free_variable_array(ps_ast_variable_array *variabl
416436
// ============================================================================
417437

418438
ps_ast_call *ps_ast_create_function_call(uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args,
419-
ps_ast_node_argument *args)
439+
ps_ast_argument *args)
420440
{
421-
ps_ast_call *function_call = (ps_ast_call *)ps_ast_create_node(PS_AST_GROUP_EXPRESSION, PS_AST_FUNCTION_CALL, line,
422-
column, sizeof(ps_ast_call));
441+
ps_ast_call *function_call =
442+
(ps_ast_call *)ps_ast_create_node(PS_AST_EXPRESSION, PS_AST_FUNCTION_CALL, line, column, sizeof(ps_ast_call));
423443
if (function_call == NULL)
424444
return NULL;
425445
function_call->executable = executable;

src/ps_ast_debug.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ char *ps_ast_node_get_group_name(ps_ast_node_group group)
2020
{
2121
switch (group)
2222
{
23-
case PS_AST_GROUP_BLOCK:
23+
case PS_AST_BLOCK:
2424
return "BLOCK";
25-
case PS_AST_GROUP_STATEMENT:
25+
case PS_AST_STATEMENT:
2626
return "STATEMENT";
27-
case PS_AST_GROUP_EXPRESSION:
27+
case PS_AST_EXPRESSION:
2828
return "EXPRESSION";
29-
case PS_AST_GROUP_LVALUE:
29+
case PS_AST_LVALUE:
3030
return "LVALUE";
3131
default:
3232
ps_ast_debug_line("Error: unknown AST node group %d\n", group);

0 commit comments

Comments
 (0)