Skip to content

Commit 2f46d98

Browse files
committed
AST WIP
1 parent 5fa842c commit 2f46d98

4 files changed

Lines changed: 259 additions & 224 deletions

File tree

include/ps_ast_execute.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,33 @@ extern "C"
1515
#endif
1616

1717
/** @brief Run a Pascal program */
18-
bool ps_ast_run_program(ps_interpreter *interpreter, ps_ast_block *program);
18+
bool ps_ast_run_program(ps_interpreter *interpreter, const ps_ast_block *program);
1919
/** @brief Run a Pascal procedure */
20-
bool ps_ast_run_procedure(ps_interpreter *interpreter, ps_ast_block *procedure);
20+
bool ps_ast_run_procedure(ps_interpreter *interpreter, const ps_ast_block *procedure);
2121
/** @brief Run a Pascal function */
22-
bool ps_ast_run_function(ps_interpreter *interpreter, ps_ast_block *function);
22+
bool ps_ast_run_function(ps_interpreter *interpreter, const ps_ast_block *function);
2323
/** @brief Run a Pascal block */
24-
bool ps_ast_run_block(ps_interpreter *interpreter, ps_ast_block *block);
24+
bool ps_ast_run_block(ps_interpreter *interpreter, const ps_ast_block *block);
2525
/** @brief Run a list of Pascal statements */
26-
bool ps_ast_run_statement_list(ps_interpreter *interpreter, ps_ast_statement_list *statement_list);
26+
bool ps_ast_run_statement_list(ps_interpreter *interpreter, const ps_ast_statement_list *statement_list);
2727
/** @brief Run a Pascal statement */
28-
bool ps_ast_run_statement(ps_interpreter *interpreter, ps_ast_node *statement);
28+
bool ps_ast_run_statement(ps_interpreter *interpreter, const ps_ast_node *statement);
2929
/** @brief Run a Pascal assignment */
30-
bool ps_ast_run_assignment(ps_interpreter *interpreter, ps_ast_assignment *assignment);
30+
bool ps_ast_run_assignment(ps_interpreter *interpreter, const ps_ast_assignment *assignment);
3131
/** @brief Run a Pascal if statement */
32-
bool ps_ast_run_if(ps_interpreter *interpreter, ps_ast_if *if_statement);
32+
bool ps_ast_run_if(ps_interpreter *interpreter, const ps_ast_if *if_statement);
3333
/** @brief Run a Pascal while statement */
34-
bool ps_ast_run_while(ps_interpreter *interpreter, ps_ast_while *while_statement);
34+
bool ps_ast_run_while(ps_interpreter *interpreter, const ps_ast_while *while_statement);
3535
/** @brief Run a Pascal repeat statement */
36-
bool ps_ast_run_repeat(ps_interpreter *interpreter, ps_ast_repeat *repeat_statement);
36+
bool ps_ast_run_repeat(ps_interpreter *interpreter, const ps_ast_repeat *repeat_statement);
3737
/** @brief Run a Pascal for statement */
38-
bool ps_ast_run_for(ps_interpreter *interpreter, ps_ast_for *for_statement);
38+
bool ps_ast_run_for(ps_interpreter *interpreter, const ps_ast_for *for_statement);
3939
/** @brief Run a Pascal procedure call */
40-
bool ps_ast_run_procedure_call(ps_interpreter *interpreter, ps_ast_call *procedure_call);
40+
bool ps_ast_run_procedure_call(ps_interpreter *interpreter, const ps_ast_call *procedure_call);
4141
/** @brief Run a Pascal function call */
42-
bool ps_ast_run_function_call(ps_interpreter *interpreter, ps_ast_call *function_call,
43-
ps_ast_value *result);
42+
bool ps_ast_run_function_call(ps_interpreter *interpreter, const ps_ast_call *function_call, ps_ast_value *result);
4443
/** @brief Evaluate a Pascal expression */
45-
bool ps_ast_eval_expression(ps_interpreter *interpreter, ps_ast_node *expression, ps_ast_value *result);
44+
bool ps_ast_eval_expression(ps_interpreter *interpreter, const ps_ast_node *expression, ps_ast_value *result);
4645

4746
#ifdef __cplusplus
4847
}

src/ps_ast_debug.c

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,25 @@ void ps_ast_debug_line(const char *format, ...) // NOSONAR
9999
return;
100100
va_list args;
101101
va_start(args, format);
102-
fprintf(stderr, "AST_DEBUG\t");
102+
// fprintf(stderr, "AST_DEBUG\t");
103103
vfprintf(stderr, format, args); // NOSONAR
104104
fprintf(stderr, "\n");
105105
va_end(args);
106106
}
107107

108+
void ps_ast_debug_word(const char *format, ...) // NOSONAR
109+
{
110+
if (!ps_ast_debug)
111+
return;
112+
va_list args;
113+
va_start(args, format);
114+
vfprintf(stderr, format, args); // NOSONAR
115+
va_end(args);
116+
}
117+
108118
void ps_ast_debug_value(const ps_ast_value *value_node)
109119
{
110-
ps_ast_debug_line("VALUE: %s\n", ps_value_get_debug_string(&value_node->value));
120+
ps_ast_debug_line("VALUE: %s", ps_value_get_debug_string(&value_node->value));
111121
}
112122

113123
void ps_ast_debug_variable_simple(const ps_ast_variable_simple *variable_simple)
@@ -117,29 +127,26 @@ void ps_ast_debug_variable_simple(const ps_ast_variable_simple *variable_simple)
117127

118128
void ps_ast_debug_variable_array(const ps_ast_variable_array *variable_array)
119129
{
120-
ps_ast_debug_line("VARIABLE_ARRAY: %s\n", variable_array->variable->name);
121-
ps_ast_debug_line(" - Number of indexes: %zu\n", variable_array->n_indexes);
130+
ps_ast_debug_line("VARIABLE_ARRAY: indexes=%zu %s[\n", variable_array->n_indexes, variable_array->variable->name);
122131
for (size_t i = 0; i < variable_array->n_indexes; i++)
123132
{
124-
ps_ast_debug_line(" - Index %zu:\n", i);
133+
ps_ast_debug_word("%zu", i);
125134
ps_ast_debug_node(variable_array->indexes[i]);
135+
if (i < variable_array->n_indexes - 1)
136+
ps_ast_debug_word(", ");
126137
}
127-
}
128-
129-
void ps_ast_debug_block(const ps_ast_block *block)
130-
{
131-
ps_ast_debug_line("BLOCK kind: %s, name: %s\n", ps_ast_node_get_kind_name(block->kind), block->name);
132-
ps_ast_debug_line(" - Number of symbols: %zu", block->symbols ? block->symbols->used : 0);
133-
ps_ast_debug_line(" - Number of variables: %zu", block->n_vars);
134-
ps_ast_debug_line(" - Number of executables: %zu", block->n_executables);
135-
ps_ast_debug_line(" - Number of statements: %zu", block->statement_list ? block->statement_list->count : 0);
136-
ps_ast_debug_line(" - Number of parameters: %zu", block->signature ? block->signature->parameter_count : 0);
137-
ps_ast_debug_line(" - Result type: %s", block->result_type ? block->result_type->name : "none");
138+
ps_ast_debug_word("]\n");
138139
}
139140

140141
void ps_ast_debug_statement_list(const ps_ast_statement_list *statement_list)
141142
{
142143
ps_ast_debug_line("STATEMENT_LIST");
144+
if (statement_list == NULL || statement_list->statements == NULL)
145+
{
146+
ps_ast_debug_line(" - Count: 0");
147+
ps_ast_debug_line(" - Statements: NULL");
148+
return;
149+
}
143150
ps_ast_debug_line(" - Count: %zu", statement_list->count);
144151
ps_ast_debug_line(" - Statements:");
145152
for (size_t i = 0; i < statement_list->count; i++)
@@ -148,18 +155,30 @@ void ps_ast_debug_statement_list(const ps_ast_statement_list *statement_list)
148155
}
149156
}
150157

158+
void ps_ast_debug_block(const ps_ast_block *block)
159+
{
160+
ps_ast_debug_line("BLOCK kind: %s, name: %s", ps_ast_node_get_kind_name(block->kind), block->name);
161+
ps_ast_debug_line(" - Number of symbols: %zu", block->symbols ? block->symbols->used : 0);
162+
ps_ast_debug_line(" - Number of variables: %zu", block->n_vars);
163+
ps_ast_debug_line(" - Number of executables: %zu", block->n_executables);
164+
ps_ast_debug_line(" - Number of statements: %zu", block->statement_list ? block->statement_list->count : 0);
165+
ps_ast_debug_line(" - Number of parameters: %zu", block->signature ? block->signature->parameter_count : 0);
166+
ps_ast_debug_line(" - Result type: %s", block->result_type ? block->result_type->name : "none");
167+
ps_ast_debug_statement_list(block->statement_list);
168+
}
169+
151170
void ps_ast_debug_assignment(const ps_ast_assignment *assignment)
152171
{
153172
if (assignment->lvalue->kind == PS_AST_LVALUE_SIMPLE)
154173
{
155174
ps_ast_variable_simple *variable_simple = (ps_ast_variable_simple *)assignment->lvalue;
156-
ps_ast_debug_line("ASSIGNMENT variable: %s\n", variable_simple->variable->name);
175+
ps_ast_debug_line("ASSIGNMENT variable: %s", variable_simple->variable->name);
157176
}
158177
else if (assignment->lvalue->kind == PS_AST_LVALUE_ARRAY)
159178
{
160179
ps_ast_variable_array *variable_array = (ps_ast_variable_array *)assignment->lvalue;
161-
ps_ast_debug_line("ASSIGNMENT variable: %s\n", variable_array->variable->name);
162-
ps_ast_debug_line(" - Number of indexes: %zu\n", variable_array->n_indexes);
180+
ps_ast_debug_line("ASSIGNMENT variable: %s", variable_array->variable->name);
181+
ps_ast_debug_line(" - Number of indexes: %zu", variable_array->n_indexes);
163182
}
164183
else
165184
{
@@ -172,14 +191,14 @@ void ps_ast_debug_assignment(const ps_ast_assignment *assignment)
172191

173192
void ps_ast_debug_unary_operation(const ps_ast_unary_operation *unary_operation)
174193
{
175-
ps_ast_debug_line("UNARY_OPERATION operator: %s\n", ps_operator_unary_get_name(unary_operation->operator));
194+
ps_ast_debug_line("UNARY_OPERATION operator: %s", ps_operator_unary_get_name(unary_operation->operator));
176195
ps_ast_debug_line(" - Operand:");
177196
ps_ast_debug_node(unary_operation->operand);
178197
}
179198

180199
void ps_ast_debug_binary_operation(const ps_ast_binary_operation *binary_operation)
181200
{
182-
ps_ast_debug_line("BINARY_OPERATION operator: %s\n", ps_operator_binary_get_name(binary_operation->operator));
201+
ps_ast_debug_line("BINARY_OPERATION operator: %s", ps_operator_binary_get_name(binary_operation->operator));
183202
ps_ast_debug_line(" - Left operand:");
184203
ps_ast_debug_node(binary_operation->left);
185204
ps_ast_debug_line(" - Right operand:");
@@ -188,44 +207,44 @@ void ps_ast_debug_binary_operation(const ps_ast_binary_operation *binary_operati
188207

189208
void ps_ast_debug_if(const ps_ast_if *if_statement)
190209
{
191-
ps_ast_debug_line("IF statement\n");
192-
ps_ast_debug_line(" - Condition:\n");
210+
ps_ast_debug_line("IF statement");
211+
ps_ast_debug_line(" - Condition:");
193212
ps_ast_debug_node(if_statement->condition);
194-
ps_ast_debug_line(" - Then branch:\n");
213+
ps_ast_debug_line(" - Then branch:");
195214
ps_ast_debug_statement_list(if_statement->then_branch);
196-
ps_ast_debug_line(" - Else branch:\n");
215+
ps_ast_debug_line(" - Else branch:");
197216
ps_ast_debug_statement_list(if_statement->else_branch);
198217
}
199218

200219
void ps_ast_debug_while(const ps_ast_while *while_statement)
201220
{
202-
ps_ast_debug_line("WHILE statement\n");
203-
ps_ast_debug_line(" - Condition:\n");
221+
ps_ast_debug_line("WHILE statement");
222+
ps_ast_debug_line(" - Condition:");
204223
ps_ast_debug_node(while_statement->condition);
205-
ps_ast_debug_line(" - Body:\n");
224+
ps_ast_debug_line(" - Body:");
206225
ps_ast_debug_statement_list(while_statement->body);
207226
}
208227

209228
void ps_ast_debug_repeat(const ps_ast_repeat *repeat_statement)
210229
{
211230
ps_ast_debug_line("REPEAT statement\n");
212-
ps_ast_debug_line(" - Body:\n");
231+
ps_ast_debug_line(" - Body:");
213232
ps_ast_debug_statement_list(repeat_statement->body);
214-
ps_ast_debug_line(" - Condition:\n");
233+
ps_ast_debug_line(" - Condition:");
215234
ps_ast_debug_node(repeat_statement->condition);
216235
}
217236

218237
void ps_ast_debug_for(const ps_ast_for *for_statement)
219238
{
220-
ps_ast_debug_line("FOR statement\n");
221-
ps_ast_debug_line(" - Variable:\n");
239+
ps_ast_debug_line("FOR statement");
240+
ps_ast_debug_line(" - Variable:");
222241
ps_ast_debug_variable_simple(for_statement->variable);
223-
ps_ast_debug_line(" - Start:\n");
242+
ps_ast_debug_line(" - Start:");
224243
ps_ast_debug_node(for_statement->start);
225-
ps_ast_debug_line(" - End:\n");
244+
ps_ast_debug_line(" - End:");
226245
ps_ast_debug_node(for_statement->end);
227-
ps_ast_debug_line(" - Step: %d\n", for_statement->step);
228-
ps_ast_debug_line(" - Body:\n");
246+
ps_ast_debug_line(" - Step: %d", for_statement->step);
247+
ps_ast_debug_line(" - Body:");
229248
ps_ast_debug_statement_list(for_statement->body);
230249
}
231250

@@ -239,22 +258,22 @@ void ps_ast_debug_argument(const ps_ast_argument *argument)
239258

240259
void ps_ast_debug_procedure_call(const ps_ast_call *call)
241260
{
242-
ps_ast_debug_line("PROCEDURE_CALL: %s\n", call->executable->name);
243-
ps_ast_debug_line(" - Number of arguments: %zu\n", call->n_args);
261+
ps_ast_debug_line("PROCEDURE_CALL: %s", call->executable->name);
262+
ps_ast_debug_line(" - Number of arguments: %zu", call->n_args);
244263
for (size_t i = 0; i < call->n_args; i++)
245264
{
246-
ps_ast_debug_line(" - Argument %zu:\n", i);
265+
ps_ast_debug_line(" - Argument %zu:", i);
247266
ps_ast_debug_argument(call->args[i]);
248267
}
249268
}
250269

251270
void ps_ast_debug_function_call(const ps_ast_call *call)
252271
{
253-
ps_ast_debug_line("FUNCTION_CALL: %s\n", call->executable->name);
254-
ps_ast_debug_line(" - Number of arguments: %zu\n", call->n_args);
272+
ps_ast_debug_line("FUNCTION_CALL: %s", call->executable->name);
273+
ps_ast_debug_line(" - Number of arguments: %zu", call->n_args);
255274
for (size_t i = 0; i < call->n_args; i++)
256275
{
257-
ps_ast_debug_line(" - Argument %zu:\n", i);
276+
ps_ast_debug_line(" - Argument %zu:", i);
258277
ps_ast_debug_argument(call->args[i]);
259278
}
260279
}
@@ -266,7 +285,7 @@ void ps_ast_debug_node(const ps_ast_node *node)
266285
ps_ast_debug_line("NODE: NULL");
267286
return;
268287
}
269-
ps_ast_debug_line("NODE kind: %s (%d)\n", ps_ast_node_get_kind_name(node->kind), node->kind);
288+
ps_ast_debug_line("NODE kind: %s (%d)", ps_ast_node_get_kind_name(node->kind), node->kind);
270289
switch (node->kind)
271290
{
272291
case PS_AST_KIND_UNKNOWN:
@@ -300,7 +319,7 @@ void ps_ast_debug_node(const ps_ast_node *node)
300319
ps_ast_debug_if((const ps_ast_if *)node);
301320
break;
302321
case PS_AST_CASE:
303-
ps_ast_debug_line("CASE statement (not implemented yet)\n");
322+
ps_ast_debug_line("CASE statement (not implemented yet)");
304323
break;
305324
case PS_AST_WHILE:
306325
ps_ast_debug_while((const ps_ast_while *)node);
@@ -328,6 +347,6 @@ void ps_ast_debug_node(const ps_ast_node *node)
328347
ps_ast_debug_argument((const ps_ast_argument *)node);
329348
break;
330349
default:
331-
ps_ast_debug_line("Error: unknown AST node kind %d\n", node->kind);
350+
ps_ast_debug_line("Error: unknown AST node kind %d", node->kind);
332351
}
333352
}

0 commit comments

Comments
 (0)