Skip to content

Commit ccdd561

Browse files
committed
AST WIP
1 parent 5b29f0c commit ccdd561

5 files changed

Lines changed: 79 additions & 73 deletions

File tree

include/ps_ast.h

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -201,57 +201,50 @@ extern "C"
201201
#define PS_AST_NODE_VARIABLE_ARRAY_SIZE sizeof(ps_ast_variable_array)
202202
// clang-format on
203203

204-
/** @brief Create a new AST node of the given kind */
204+
/** @brief Create a new AST node of the given group & kind */
205205
ps_ast_node *ps_ast_create_node(ps_ast_node_group group, ps_ast_node_kind kind, uint16_t line, uint16_t column,
206206
size_t size);
207207

208-
ps_ast_node *ps_ast_create_block(uint16_t line, uint16_t column, ps_ast_node_kind kind, char *name);
209-
ps_ast_node *ps_ast_create_statement_list(uint16_t line, uint16_t column, size_t count);
210-
ps_ast_node *ps_ast_create_assignment(uint16_t line, uint16_t column, ps_ast_node *variable,
211-
ps_ast_node *expression);
212-
ps_ast_node *ps_ast_create_if(uint16_t line, uint16_t column, ps_ast_node *condition,
213-
ps_ast_statement_list *then_branch, ps_ast_statement_list *else_branch);
214-
ps_ast_node *ps_ast_create_while(uint16_t line, uint16_t column, ps_ast_node *condition,
215-
ps_ast_statement_list *body);
216-
ps_ast_node *ps_ast_create_repeat(uint16_t line, uint16_t column, ps_ast_statement_list *body,
217-
ps_ast_node *condition);
218-
ps_ast_node *ps_ast_create_for(uint16_t line, uint16_t column, ps_ast_node *variable, ps_ast_node *start,
219-
ps_ast_node *end, int step, ps_ast_statement_list *body);
220-
ps_ast_node *ps_ast_create_procedure_call(uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args,
221-
ps_ast_argument *args);
222-
ps_ast_node *ps_ast_create_function_call(uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args,
223-
ps_ast_argument *args);
224-
ps_ast_node *ps_ast_create_unary_operation(uint16_t line, uint16_t column, ps_operator_unary operator,
225-
ps_ast_node * operand);
226-
ps_ast_node *ps_ast_create_binary_operation(uint16_t line, uint16_t column, ps_operator_binary operator,
227-
ps_ast_node * left, ps_ast_node *right);
228-
ps_ast_node *ps_ast_create_value(uint16_t line, uint16_t column, ps_value value);
229-
ps_ast_node *ps_ast_create_variable_simple(uint16_t line, uint16_t column, ps_symbol *variable);
230-
ps_ast_node *ps_ast_create_variable_array(uint16_t line, uint16_t column, ps_symbol *symbol, size_t n_indexes,
231-
ps_ast_node *indexes);
232-
ps_ast_node *ps_ast_create_lvalue_simple(uint16_t line, uint16_t column, ps_symbol *variable);
233-
ps_ast_node *ps_ast_create_lvalue_array(uint16_t line, uint16_t column, ps_symbol *symbol, size_t n_indexes,
234-
ps_ast_node *indexes);
208+
// clang-format off
209+
ps_ast_block *ps_ast_create_block (uint16_t line, uint16_t column, ps_ast_node_group group, ps_ast_node_kind kind, char *name );
210+
ps_ast_statement_list *ps_ast_create_statement_list (uint16_t line, uint16_t column, ps_ast_node_group group, ps_ast_node_kind kind, size_t count );
211+
ps_ast_assignment *ps_ast_create_assignment (uint16_t line, uint16_t column, ps_ast_node_group group, ps_ast_node_kind kind, ps_ast_node *variable,ps_ast_node *expression );
212+
ps_ast_if *ps_ast_create_if (uint16_t line, uint16_t column, ps_ast_node_group group, ps_ast_node_kind kind, ps_ast_node *condition, ps_ast_statement_list *then_branch, ps_ast_statement_list *else_branch );
213+
ps_ast_while *ps_ast_create_while (uint16_t line, uint16_t column, ps_ast_node_group group, ps_ast_node_kind kind, ps_ast_node *condition, ps_ast_statement_list *body );
214+
ps_ast_repeat *ps_ast_create_repeat (uint16_t line, uint16_t column, ps_ast_statement_list *body, ps_ast_node *condition );
215+
ps_ast_for *ps_ast_create_for (uint16_t line, uint16_t column, ps_ast_node_group group, ps_ast_node_kind kind, ps_ast_node *variable, ps_ast_node *start, ps_ast_node *end, int step, ps_ast_statement_list *body );
216+
ps_ast_call *ps_ast_create_procedure_call (uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args, ps_ast_argument *args );
217+
ps_ast_call *ps_ast_create_function_call (uint16_t line, uint16_t column, ps_symbol *executable, size_t n_args, ps_ast_argument *args );
218+
ps_ast_unary_operation *ps_ast_create_unary_operation (uint16_t line, uint16_t column, ps_operator_unary operator, ps_ast_node * operand );
219+
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 );
220+
ps_ast_value *ps_ast_create_value (uint16_t line, uint16_t column, ps_value value );
221+
ps_ast_variable_simple *ps_ast_create_variable_simple (uint16_t line, uint16_t column, ps_symbol *variable );
222+
ps_ast_variable_array *ps_ast_create_variable_array (uint16_t line, uint16_t column, ps_symbol *symbol, size_t n_indexes, ps_ast_node *indexes );
223+
ps_ast_variable_simple *ps_ast_create_lvalue_simple (uint16_t line, uint16_t column, ps_symbol *variable );
224+
ps_ast_variable_array *ps_ast_create_lvalue_array (uint16_t line, uint16_t column, ps_symbol *symbol, size_t n_indexes, ps_ast_node *indexes );
225+
// clang-format on
235226

236227
/** @brief Free an AST node and all its children */
237228
ps_ast_node *ps_ast_free_node(ps_ast_node *node);
238229

239-
ps_ast_node *ps_ast_free_block(ps_ast_node *node);
240-
ps_ast_node *ps_ast_free_statement_list(ps_ast_node *node);
241-
ps_ast_node *ps_ast_free_assignment(ps_ast_node *node);
242-
ps_ast_node *ps_ast_free_if(ps_ast_node *node);
243-
ps_ast_node *ps_ast_free_while(ps_ast_node *node);
244-
ps_ast_node *ps_ast_free_repeat(ps_ast_node *node);
245-
ps_ast_node *ps_ast_free_for(ps_ast_node *node);
246-
ps_ast_node *ps_ast_free_procedure_call(ps_ast_node *node);
247-
ps_ast_node *ps_ast_free_unary_operation(ps_ast_node *node);
248-
ps_ast_node *ps_ast_free_binary_operation(ps_ast_node *node);
249-
ps_ast_node *ps_ast_free_function_call(ps_ast_node *node);
250-
ps_ast_node *ps_ast_free_value(ps_ast_node *node);
251-
ps_ast_node *ps_ast_free_variable_simple(ps_ast_node *node);
252-
ps_ast_node *ps_ast_free_variable_array(ps_ast_node *node);
253-
ps_ast_node *ps_ast_free_lvalue_simple(ps_ast_node *node);
254-
ps_ast_node *ps_ast_free_lvalue_array(ps_ast_node *node);
230+
// clang-format off
231+
ps_ast_block *ps_ast_free_block (ps_ast_block *block );
232+
ps_ast_statement_list *ps_ast_free_statement_list (ps_ast_statement_list *list );
233+
ps_ast_assignment *ps_ast_free_assignment (ps_ast_assignment *assignment );
234+
ps_ast_if *ps_ast_free_if (ps_ast_if *if_statement );
235+
ps_ast_while *ps_ast_free_while (ps_ast_while *while_statement );
236+
ps_ast_repeat *ps_ast_free_repeat (ps_ast_repeat *repeat_statement );
237+
ps_ast_for *ps_ast_free_for (ps_ast_for *for_statement );
238+
ps_ast_call *ps_ast_free_procedure_call (ps_ast_call *call );
239+
ps_ast_unary_operation *ps_ast_free_unary_operation (ps_ast_unary_operation *operation );
240+
ps_ast_binary_operation *ps_ast_free_binary_operation (ps_ast_binary_operation *operation );
241+
ps_ast_call *ps_ast_free_function_call (ps_ast_call *call );
242+
ps_ast_value *ps_ast_free_value (ps_ast_value *value );
243+
ps_ast_variable_simple *ps_ast_free_variable_simple (ps_ast_variable_simple *variable );
244+
ps_ast_variable_array *ps_ast_free_variable_array (ps_ast_variable_array *variable );
245+
ps_ast_variable_simple *ps_ast_free_lvalue_simple (ps_ast_variable_simple *lvalue );
246+
ps_ast_variable_array *ps_ast_free_lvalue_array (ps_ast_variable_array *lvalue );
247+
// clang-format on
255248

256249
#ifdef __cplusplus
257250
}

include/ps_procedures.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ extern "C"
1919

2020
bool ps_procedures_init(ps_environment *system);
2121

22+
extern ps_symbol ps_system_procedure_dec;
23+
extern ps_symbol ps_system_procedure_inc;
24+
extern ps_symbol ps_system_procedure_randomize;
2225
extern ps_symbol ps_system_procedure_read;
2326
extern ps_symbol ps_system_procedure_readln;
2427
extern ps_symbol ps_system_procedure_write;
2528
extern ps_symbol ps_system_procedure_writeln;
26-
extern ps_symbol ps_system_procedure_randomize;
2729

2830
/* clang-format off */
29-
bool ps_procedure_read (ps_interpreter *interpreter, FILE *f, ps_value *value);
30-
bool ps_procedure_readln (ps_interpreter *interpreter, FILE *f, ps_value *value);
31+
bool ps_procedure_dec (ps_interpreter *interpreter, ps_value *value, const ps_value *decrement );
32+
bool ps_procedure_inc (ps_interpreter *interpreter, ps_value *value, const ps_value *increment );
33+
bool ps_procedure_randomize(ps_interpreter *interpreter, const ps_value *value );
34+
bool ps_procedure_read (ps_interpreter *interpreter, FILE *f, ps_value *value );
35+
bool ps_procedure_readln (ps_interpreter *interpreter, FILE *f, ps_value *value );
3136
bool ps_procedure_write (ps_interpreter *interpreter, FILE *f, const ps_value *value, int16_t width, int16_t precision);
3237
bool ps_procedure_writeln (ps_interpreter *interpreter, FILE *f, const ps_value *value, int16_t width, int16_t precision);
33-
bool ps_procedure_randomize(ps_interpreter *interpreter, const ps_value *value);
3438
/* clang-format on */
3539

3640
#ifdef __cplusplus

src/ps_ast.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,45 @@ ps_ast_node *ps_ast_free_node(ps_ast_node *node)
3636
return NULL;
3737
switch (node->kind)
3838
{
39+
case PS_AST_KIND_UNKNOWN:
40+
ps_memory_free(PS_MEMORY_AST, node);
41+
return NULL;
3942
case PS_AST_PROGRAM:
4043
case PS_AST_PROCEDURE:
4144
case PS_AST_FUNCTION:
4245
case PS_AST_UNIT:
43-
return ps_ast_free_block(node);
46+
return ps_ast_free_block((ps_ast_block *)node);
4447
case PS_AST_STATEMENT_LIST:
45-
return ps_ast_free_statement_list(node);
48+
return ps_ast_free_statement_list((ps_ast_statement_list *)node);
4649
case PS_AST_ASSIGNMENT:
47-
return ps_ast_free_assignment(node);
50+
return ps_ast_free_assignment((ps_ast_assignment *)node);
4851
case PS_AST_IF:
49-
return ps_ast_free_if(node);
52+
return ps_ast_free_if((ps_ast_if *)node);
5053
case PS_AST_WHILE:
51-
return ps_ast_free_while(node);
54+
return ps_ast_free_while((ps_ast_while *)node);
5255
case PS_AST_REPEAT:
53-
return ps_ast_free_repeat(node);
56+
return ps_ast_free_repeat((ps_ast_repeat *)node);
5457
case PS_AST_FOR:
55-
return ps_ast_free_for(node);
58+
return ps_ast_free_for((ps_ast_for *)node);
5659
case PS_AST_UNARY_OPERATION:
57-
return ps_ast_free_unary_operation(node);
60+
return ps_ast_free_unary_operation((ps_ast_unary_operation *)node);
5861
case PS_AST_BINARY_OPERATION:
59-
return ps_ast_free_binary_operation(node);
62+
return ps_ast_free_binary_operation((ps_ast_binary_operation *)node);
6063
case PS_AST_VALUE:
61-
return ps_ast_free_value(node);
64+
return ps_ast_free_value((ps_ast_value *)node);
6265
case PS_AST_PROCEDURE_CALL:
6366
case PS_AST_FUNCTION_CALL:
64-
return ps_ast_free_call(node);
67+
return ps_ast_free_call((ps_ast_call *)node);
6568
case PS_AST_VARIABLE_SIMPLE:
6669
case PS_AST_LVALUE_SIMPLE:
67-
return ps_ast_free_variable_simple(node);
70+
return ps_ast_free_variable_simple((ps_ast_variable_simple *)node);
6871
case PS_AST_VARIABLE_ARRAY:
6972
case PS_AST_LVALUE_ARRAY:
70-
return ps_ast_free_variable_array(node);
73+
return ps_ast_free_variable_array((ps_ast_variable_array *)node);
74+
case PS_AST_ARG_EXPR:
75+
case PS_AST_ARG_VAR_BY_VAL:
76+
case PS_AST_ARG_VAR_BY_REF:
77+
return ps_ast_free_argument((ps_ast_argument *)node);
7178
}
7279
}
7380

@@ -92,9 +99,8 @@ ps_ast_block *ps_ast_create_block(uint16_t line, uint16_t column, ps_ast_node_ki
9299
return block;
93100
}
94101

95-
ps_ast_node *ps_ast_free_block(ps_ast_node *node)
102+
ps_ast_block *ps_ast_free_block(ps_ast_block *block)
96103
{
97-
ps_ast_block *block = (ps_ast_block *)node;
98104
if (block->signature != NULL)
99105
ps_signature_free(block->signature);
100106
if (block->symbols != NULL)
@@ -104,7 +110,7 @@ ps_ast_node *ps_ast_free_block(ps_ast_node *node)
104110
for (size_t i = 0; i < block->n_executables; i++)
105111
block->executables[i] = ps_ast_free_node(block->executables[i]);
106112
ps_memory_free(PS_MEMORY_AST, block->executables);
107-
ps_memory_free(PS_MEMORY_AST, node);
113+
ps_memory_free(PS_MEMORY_AST, block);
108114
return NULL;
109115
}
110116

src/ps_ast_execute.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ bool ps_ast_run_procedure_call(ps_interpreter *interpreter, ps_ast_call *procedu
255255
bool ps_ast_run_function_call(ps_interpreter *interpreter, ps_ast_call *function_call, ps_ast_value *result)
256256
{
257257
ps_ast_debug_line("FUNCTION CALL %s", function_call->executable->name);
258+
result->value.type = &ps_system_none;
259+
result->value.data = (ps_value_data){0};
258260
ps_interpreter_set_message(interpreter, "Function calls not implemented yet");
259261
interpreter->error = PS_ERROR_NOT_IMPLEMENTED;
260262
return false;

src/ps_procedures.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
/**********************************************************************************************************************/
2323

2424
/* clang-format off */
25-
PS_SYSTEM_PROCEDURE(procedure, inc , "INC" , .proc_1arg , &ps_procedure_inc );
2625
PS_SYSTEM_PROCEDURE(procedure, dec , "DEC" , .proc_1arg , &ps_procedure_dec );
26+
PS_SYSTEM_PROCEDURE(procedure, inc , "INC" , .proc_1arg , &ps_procedure_inc );
2727
PS_SYSTEM_PROCEDURE(procedure, randomize, "RANDOMIZE", .proc_1arg , &ps_procedure_randomize);
2828
PS_SYSTEM_PROCEDURE(procedure, read , "READ" , .proc_file_read , &ps_procedure_read );
2929
PS_SYSTEM_PROCEDURE(procedure, readln , "READLN" , .proc_file_read , &ps_procedure_readln );
@@ -34,8 +34,8 @@ PS_SYSTEM_PROCEDURE(procedure, writeln , "WRITELN" , .proc_file_write, &ps_pro
3434
bool ps_procedures_init(ps_environment *system)
3535
{
3636
(void)system;
37-
ADD_SYSTEM_SYMBOL(ps_system_procedure_inc)
3837
ADD_SYSTEM_SYMBOL(ps_system_procedure_dec)
38+
ADD_SYSTEM_SYMBOL(ps_system_procedure_inc)
3939
ADD_SYSTEM_SYMBOL(ps_system_procedure_randomize)
4040
ADD_SYSTEM_SYMBOL(ps_system_procedure_read)
4141
ADD_SYSTEM_SYMBOL(ps_system_procedure_readln)
@@ -46,12 +46,13 @@ bool ps_procedures_init(ps_environment *system)
4646
return false;
4747
}
4848

49-
bool ps_procedure_inc_or_dec(ps_interpreter *interpreter, ps_value *value, const ps_value *offset, bool is_inc)
49+
bool ps_procedure_inc_or_dec(ps_interpreter *interpreter, ps_value *value, bool is_inc)
5050
{
51-
if (value == NULL || value->type == NULL || value->type->value == NULL || !ps_value_is_ordinal(value))
51+
if (!ps_value_is_ordinal(value))
5252
return ps_interpreter_return_false(interpreter, PS_ERROR_UNEXPECTED_TYPE);
5353
ps_value new_value = {.allocated = false, .type = value->type, .data = {0}};
54-
switch (value->type->value->data.t->base)
54+
ps_value_type base = ps_value_get_base(value);
55+
switch (base)
5556
{
5657
case PS_TYPE_CHAR:
5758
case PS_TYPE_INTEGER:
@@ -72,14 +73,14 @@ bool ps_procedure_inc_or_dec(ps_interpreter *interpreter, ps_value *value, const
7273
return true;
7374
}
7475

75-
bool ps_procedure_inc(ps_interpreter *interpreter, ps_value *value, const ps_value *increment)
76+
bool ps_procedure_dec(ps_interpreter *interpreter, ps_value *value)
7677
{
77-
return ps_procedure_inc_or_dec(interpreter, value, increment, true);
78+
return ps_procedure_inc_or_dec(interpreter, value, false);
7879
}
7980

80-
bool ps_procedure_dec(ps_interpreter *interpreter, ps_value *value, const ps_value *decrement)
81+
bool ps_procedure_inc(ps_interpreter *interpreter, ps_value *value)
8182
{
82-
return ps_procedure_inc_or_dec(interpreter, value, decrement, false);
83+
return ps_procedure_inc_or_dec(interpreter, value, true);
8384
}
8485

8586
bool ps_procedure_randomize(ps_interpreter *interpreter, const ps_value *value)

0 commit comments

Comments
 (0)