Skip to content

Commit d65d9a6

Browse files
committed
AST WIP
1 parent 6928a4f commit d65d9a6

7 files changed

Lines changed: 138 additions & 81 deletions

File tree

include/ps_ast.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ extern "C"
149149
ps_symbol *executable; /** @brief procedure of function being called */
150150
size_t n_args; /** @brief number of arguments, 0 if no arguments */
151151
ps_ast_node **args; /** @brief arguments, NULL if no arguments */
152-
uint16_t *widths; /** @brief format width of each argument for Write[Ln] */
153-
uint16_t *precisions; /** @brief format precision of each argument for Write[Ln] */
152+
int16_t *widths; /** @brief format width of each argument for Write[Ln] */
153+
int16_t *precisions; /** @brief format precision of each argument for Write[Ln] */
154154
} ps_ast_call;
155155

156156
/** @brief Assignment statement: LVALUE := EXPRESSION / RVALUE */
@@ -212,19 +212,19 @@ extern "C"
212212
size_t size);
213213

214214
// clang-format off
215-
ps_ast_block *ps_ast_create_block (uint16_t line, uint16_t column, ps_ast_block *parent, ps_ast_node_kind kind, const char *name );
216-
ps_ast_statement_list *ps_ast_create_statement_list (uint16_t line, uint16_t column, size_t count );
217-
ps_ast_assignment *ps_ast_create_assignment (uint16_t line, uint16_t column, ps_ast_node *lvalue,ps_ast_node *expression );
218-
ps_ast_if *ps_ast_create_if (uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_statement_list *then_branch, ps_ast_statement_list *else_branch );
219-
ps_ast_while *ps_ast_create_while (uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_statement_list *body );
220-
ps_ast_repeat *ps_ast_create_repeat (uint16_t line, uint16_t column, ps_ast_statement_list *body, ps_ast_node *condition );
221-
ps_ast_for *ps_ast_create_for (uint16_t line, uint16_t column, ps_ast_variable_simple *variable, ps_ast_node *start, ps_ast_node *end, int step, ps_ast_statement_list *body);
222-
ps_ast_call *ps_ast_create_call (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *executable, size_t n_args, ps_ast_node *args[] );
223-
ps_ast_unary_operation *ps_ast_create_unary_operation (uint16_t line, uint16_t column, ps_operator_unary operator, ps_ast_node * operand );
224-
ps_ast_binary_operation *ps_ast_create_binary_operation(uint16_t line, uint16_t column, ps_operator_binary operator, ps_ast_node * left, ps_ast_node *right );
225-
ps_ast_value *ps_ast_create_rvalue_const (uint16_t line, uint16_t column, ps_value value );
226-
ps_ast_variable_simple *ps_ast_create_variable_simple (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *variable );
227-
ps_ast_variable_array *ps_ast_create_variable_array (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *symbol, size_t n_indexes, ps_ast_node **indexes );
215+
ps_ast_block *ps_ast_create_block (uint16_t line, uint16_t column, ps_ast_block *parent, ps_ast_node_kind kind, const char *name );
216+
ps_ast_statement_list *ps_ast_create_statement_list (uint16_t line, uint16_t column, size_t count );
217+
ps_ast_assignment *ps_ast_create_assignment (uint16_t line, uint16_t column, ps_ast_node *lvalue,ps_ast_node *expression );
218+
ps_ast_if *ps_ast_create_if (uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_statement_list *then_branch, ps_ast_statement_list *else_branch );
219+
ps_ast_while *ps_ast_create_while (uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_statement_list *body );
220+
ps_ast_repeat *ps_ast_create_repeat (uint16_t line, uint16_t column, ps_ast_statement_list *body, ps_ast_node *condition );
221+
ps_ast_for *ps_ast_create_for (uint16_t line, uint16_t column, ps_ast_variable_simple *variable, ps_ast_node *start, ps_ast_node *end, int step, ps_ast_statement_list *body );
222+
ps_ast_call *ps_ast_create_call (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *executable, size_t n_args, ps_ast_node *args[], int16_t widths[], int16_t precisions []);
223+
ps_ast_unary_operation *ps_ast_create_unary_operation (uint16_t line, uint16_t column, ps_operator_unary operator, ps_ast_node * operand );
224+
ps_ast_binary_operation *ps_ast_create_binary_operation(uint16_t line, uint16_t column, ps_operator_binary operator, ps_ast_node * left, ps_ast_node *right );
225+
ps_ast_value *ps_ast_create_rvalue_const (uint16_t line, uint16_t column, ps_value value );
226+
ps_ast_variable_simple *ps_ast_create_variable_simple (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *variable );
227+
ps_ast_variable_array *ps_ast_create_variable_array (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *symbol, size_t n_indexes, ps_ast_node **indexes );
228228
// clang-format on
229229

230230
/** @brief Free an AST node and all its children */

include/ps_parse_statement.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ extern "C"
1818
#endif
1919

2020
// clang-format off
21-
bool ps_parse_statement (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **statement );
22-
bool ps_parse_compound_statement (ps_compiler *compiler, ps_ast_block *block, ps_ast_statement_list **statement_list );
23-
bool ps_parse_assignment (ps_compiler *compiler, ps_ast_block *block, ps_ast_assignment **assignment , ps_symbol *variable );
24-
bool ps_parse_read_or_readln (ps_compiler *compiler, ps_ast_block *block, ps_ast_call **call , bool newline );
25-
bool ps_parse_write_or_writeln (ps_compiler *compiler, ps_ast_block *block, ps_ast_call **call , bool newline );
26-
bool ps_parse_assignment_or_procedure_call (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **statement );
27-
bool ps_parse_if_then_else (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **statement );
28-
bool ps_parse_repeat_until (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **statement );
29-
bool ps_parse_while_do (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **statement );
30-
bool ps_parse_for_do (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **statement );
31-
bool ps_parse_statement_list (ps_compiler *compiler, ps_ast_block *block, ps_token_type stop );
32-
bool ps_parse_statement_or_compound_statement(ps_compiler *compiler, ps_ast_block *block );
21+
bool ps_parse_statement (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **statement );
22+
bool ps_parse_compound_statement (ps_compiler *compiler, ps_ast_block *block, ps_ast_statement_list **statement_list );
23+
bool ps_parse_assignment (ps_compiler *compiler, ps_ast_block *block, ps_ast_assignment **assignment , ps_symbol *variable);
24+
bool ps_parse_read_or_readln (ps_compiler *compiler, ps_ast_block *block, ps_ast_call **call , bool newline );
25+
bool ps_parse_write_or_writeln (ps_compiler *compiler, ps_ast_block *block, ps_ast_call **call , bool newline );
26+
bool ps_parse_assignment_or_procedure_call (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **statement );
27+
bool ps_parse_if_then_else (ps_compiler *compiler, ps_ast_block *block, ps_ast_if **if_statement );
28+
bool ps_parse_repeat_until (ps_compiler *compiler, ps_ast_block *block, ps_ast_repeat **repeat_statement );
29+
bool ps_parse_while_do (ps_compiler *compiler, ps_ast_block *block, ps_ast_while **while_statement );
30+
bool ps_parse_for_do (ps_compiler *compiler, ps_ast_block *block, ps_ast_for **for_statement );
31+
bool ps_parse_statement_list (ps_compiler *compiler, ps_ast_block *block, ps_ast_statement_list **statement_list, ps_token_type stop );
32+
bool ps_parse_statement_or_compound_statement(ps_compiler *compiler, ps_ast_block *block, ps_ast_statement_list **statement_list );
3333
// clang-format on
3434

3535
#ifdef __cplusplus

src/ps_ast.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ ps_ast_node *ps_ast_free_for(ps_ast_for *for_statement)
325325
// =============================================================================
326326

327327
ps_ast_call *ps_ast_create_call(uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *executable,
328-
size_t n_args, ps_ast_node *args[])
328+
size_t n_args, ps_ast_node *args[], int16_t widths[], int16_t precisions[])
329329
{
330330
assert(kind == PS_AST_PROCEDURE_CALL || kind == PS_AST_FUNCTION_CALL);
331331
assert(executable != NULL);
@@ -339,6 +339,21 @@ ps_ast_call *ps_ast_create_call(uint16_t line, uint16_t column, ps_ast_node_kind
339339
call->args = ps_memory_calloc(PS_MEMORY_AST, n_args, sizeof(ps_ast_node *));
340340
if (call->args == NULL)
341341
return (ps_ast_call *)ps_ast_free_call(call);
342+
memcpy(call->args, args, n_args * sizeof(ps_ast_node *));
343+
if (widths != NULL)
344+
{
345+
call->widths = ps_memory_calloc(PS_MEMORY_AST, n_args, sizeof(int16_t));
346+
if (call->widths == NULL)
347+
return (ps_ast_call *)ps_ast_free_call(call);
348+
memcpy(call->widths, widths, n_args * sizeof(int16_t));
349+
}
350+
if (precisions != NULL)
351+
{
352+
call->precisions = ps_memory_calloc(PS_MEMORY_AST, n_args, sizeof(int16_t));
353+
if (call->precisions == NULL)
354+
return (ps_ast_call *)ps_ast_free_call(call);
355+
memcpy(call->precisions, precisions, n_args * sizeof(int16_t));
356+
}
342357
return call;
343358
}
344359

src/ps_ast_test.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ bool ps_ast_test_hello()
772772
args1[0] = (ps_ast_node *)argument_hello;
773773

774774
ps_ast_debug_line(0, "Create the first PROCEDURE CALL statement");
775-
ps_ast_call *statement1 = ps_ast_create_call(3, 5, PS_AST_PROCEDURE_CALL, &ps_system_procedure_writeln, 1, args1);
775+
ps_ast_call *statement1 =
776+
ps_ast_create_call(3, 5, PS_AST_PROCEDURE_CALL, &ps_system_procedure_writeln, 1, args1, NULL, NULL);
776777
ASSERT_RETURN_FALSE(statement1 != NULL);
777778

778779
ps_ast_debug_line(0, "Create the argument list for the second procedure call");
@@ -781,7 +782,8 @@ bool ps_ast_test_hello()
781782
args2[0] = (ps_ast_node *)argument_i_42;
782783

783784
ps_ast_debug_line(0, "Create the second PROCEDURE CALL statement");
784-
ps_ast_call *statement2 = ps_ast_create_call(3, 5, PS_AST_PROCEDURE_CALL, &ps_system_procedure_writeln, 1, args2);
785+
ps_ast_call *statement2 =
786+
ps_ast_create_call(3, 5, PS_AST_PROCEDURE_CALL, &ps_system_procedure_writeln, 1, args2, NULL, NULL);
785787
ASSERT_RETURN_FALSE(statement2 != NULL);
786788

787789
ps_ast_debug_line(0, "Add the statements to the statement list");

src/ps_parse_declaration.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ bool ps_parse_const(ps_compiler *compiler, ps_ast_block *block)
257257
// NB: adds symbols to current block symbol table, does not produce any AST nodes
258258

259259
PARSE_BEGIN("CONST", "")
260+
(void)start_line;
261+
(void)start_column;
260262

261263
ps_identifier identifier;
262264
ps_value *value;
@@ -303,6 +305,8 @@ bool ps_parse_type(ps_compiler *compiler, ps_ast_block *block)
303305
// NB: adds symbols to current block symbol table, does not produce any AST nodes
304306

305307
PARSE_BEGIN("TYPE", "");
308+
(void)start_line;
309+
(void)start_column;
306310

307311
EXPECT_TOKEN(PS_TOKEN_TYPE);
308312
READ_NEXT_TOKEN

src/ps_parse_expression.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ bool ps_parse_factor_identifier_array(ps_compiler *compiler, ps_ast_block *block
282282
(void)compiler;
283283
(void)block;
284284
(void)symbol;
285+
(void)result;
285286
RETURN_ERROR(PS_ERROR_NOT_IMPLEMENTED)
286287
// const ps_type_definition *type_def = ps_array_get_type_def(symbol->value->type);
287288
// if (type_def == NULL)
@@ -337,16 +338,17 @@ bool ps_parse_factor_identifier(ps_compiler *compiler, ps_ast_block *block, cons
337338
{
338339
case PS_SYMBOL_KIND_CONSTANT:
339340
case PS_SYMBOL_KIND_VARIABLE:
340-
if (compiler->debug >= DEBUG_VERBOSE)
341+
if (compiler->debug >= COMPILER_DEBUG_VERBOSE)
341342
{
342343
fprintf(stderr, "INFO\tFACTOR: identifier '%s' is a '%s' of type '%s'\n", symbol->name,
343344
ps_symbol_get_kind_name(symbol->kind),
344345
ps_type_definition_get_name(symbol->value->type->value->data.t));
345346
}
346347
if (ps_value_is_array(symbol->value))
347348
{
348-
if (!ps_parse_factor_identifier_array(compiler, block, symbol, factor))
349-
TRACE_ERROR("ARRAY")
349+
RETURN_ERROR(PS_ERROR_NOT_IMPLEMENTED)
350+
// if (!ps_parse_factor_identifier_array(compiler, block, symbol, factor))
351+
// TRACE_ERROR("ARRAY")
350352
}
351353
else
352354
{

0 commit comments

Comments
 (0)