Skip to content

Commit 549f6a1

Browse files
committed
AST WIP
1 parent 9f6f7a0 commit 549f6a1

10 files changed

Lines changed: 147 additions & 144 deletions

include/ps_ast.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,11 @@ extern "C"
146146
typedef struct s_ps_ast_call
147147
{
148148
PS_AST_NODE_COMMON
149-
ps_symbol *executable; /** @brief procedure of function being called */
150-
size_t n_args; /** @brief number of arguments, 0 if no arguments */
151-
ps_ast_node **args; /** @brief arguments, NULL if no arguments */
149+
ps_symbol *executable; /** @brief procedure of function being called */
150+
size_t n_args; /** @brief number of arguments, 0 if no arguments */
151+
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] */
152154
} ps_ast_call;
153155

154156
/** @brief Assignment statement: LVALUE := EXPRESSION / RVALUE */

include/ps_executable.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77
#include <stdbool.h>
88
#include <stdlib.h>
99

10-
#include "ps_ast.h"
11-
#include "ps_value.h"
12-
1310
#ifndef _PS_EXECUTABLE_H
1411
#define _PS_EXECUTABLE_H
1512

13+
#include "ps_error.h"
14+
1615
#ifdef __cplusplus
1716
extern "C"
1817
{
1918
#endif
2019

2120
// Forward references
21+
typedef struct s_ps_interpreter ps_interpreter;
2222
typedef struct s_ps_value ps_value;
2323
typedef struct s_ps_symbol ps_symbol;
24+
typedef struct s_ps_ast_block ps_ast_block;
2425

2526
typedef ps_error (*ps_function_1arg)(ps_interpreter *i, const ps_value *v, ps_value *r);
2627
typedef ps_error (*ps_function_1arg_s)(ps_interpreter *i, ps_symbol *t, ps_value *r);

include/ps_parse.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ extern "C"
2222

2323
/* src/ps_parse_executable.c */
2424
ps_ast_node *ps_parse_procedure_or_function_declaration(ps_compiler *compiler, ps_symbol_kind kind);
25-
ps_ast_node *ps_parse_procedure_or_function_call(ps_compiler *compiler, ps_symbol *executable, ps_value *result);
25+
ps_ast_node *ps_parse_procedure_or_function_call(ps_compiler *compiler, ps_symbol *executable, ps_ast_block *block,
26+
ps_ast_node *expression);
2627
ps_ast_node *ps_parse_variable_reference(ps_compiler *compiler, ps_symbol **variable);
2728

2829
/* src/ps_parse_expression.c */
29-
ps_ast_node *ps_parse_expression(ps_compiler *compiler, ps_value *result);
30-
ps_ast_node *ps_parse_relational_expression(ps_compiler *compiler, ps_value *result);
31-
ps_ast_node *ps_parse_and_expression(ps_compiler *compiler, ps_value *result);
32-
ps_ast_node *ps_parse_or_expression(ps_compiler *compiler, ps_value *result);
33-
ps_ast_node *ps_parse_simple_expression(ps_compiler *compiler, ps_value *result);
34-
ps_ast_node *ps_parse_term(ps_compiler *compiler, ps_value *result);
35-
ps_ast_node *ps_parse_factor(ps_compiler *compiler, ps_value *result);
30+
ps_ast_node *ps_parse_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node *expression);
31+
ps_ast_node *ps_parse_relational_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node *expression);
32+
ps_ast_node *ps_parse_and_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node *expression);
33+
ps_ast_node *ps_parse_or_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node *expression);
34+
ps_ast_node *ps_parse_simple_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node *expression);
35+
ps_ast_node *ps_parse_term(ps_compiler *compiler, ps_ast_block *block, ps_ast_node *expression);
36+
ps_ast_node *ps_parse_factor(ps_compiler *compiler, ps_ast_block *block, ps_ast_node *expression);
3637
ps_ast_node *ps_parse_constant_expression(ps_compiler *compiler, ps_value *constant);
37-
ps_ast_node *ps_parse_function_call(ps_compiler *compiler, ps_symbol *function, ps_value *result);
38+
ps_ast_node *ps_parse_function_call(ps_compiler *compiler, ps_symbol *function, ps_ast_block *block,
39+
ps_ast_node *expression);
3840

3941
/* src/ps_parse_type.c */
4042
ps_ast_node *ps_parse_type_definition(ps_compiler *compiler);
@@ -63,7 +65,7 @@ extern "C"
6365
return true; \
6466
}
6567

66-
#define READ_NEXT_TOKEN() \
68+
#define READ_NEXT_TOKEN \
6769
{ \
6870
if (!ps_lexer_read_token(lexer)) \
6971
return false; \

include/ps_parse_statement.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@
99

1010
#include <stdint.h>
1111

12+
#include "ps_ast.h"
1213
#include "ps_compiler.h"
13-
#include "ps_lexer.h"
1414

1515
#ifdef __cplusplus
1616
extern "C"
1717
{
1818
#endif
1919

20-
ps_ast_node *ps_parse_statement(ps_compiler *compiler);
21-
ps_ast_node *ps_parse_compound_statement(ps_compiler *compiler);
22-
ps_ast_node *ps_parse_assignment(ps_compiler *compiler, ps_symbol *variable);
23-
ps_ast_node *ps_parse_read_or_readln(ps_compiler *compiler, bool newline);
24-
ps_ast_node *ps_parse_write_or_writeln(ps_compiler *compiler, bool newline);
25-
ps_ast_node *ps_parse_assignment_or_procedure_call(ps_compiler *compiler);
26-
ps_ast_node *ps_parse_if_then_else(ps_compiler *compiler);
27-
ps_ast_node *ps_parse_repeat_until(ps_compiler *compiler);
28-
ps_ast_node *ps_parse_while_do(ps_compiler *compiler);
29-
ps_ast_node *ps_parse_for_do(ps_compiler *compiler);
30-
ps_ast_node *ps_parse_statement_list(ps_compiler *compiler, ps_token_type stop);
31-
ps_ast_node *ps_parse_statement_or_compound_statement(ps_compiler *compiler);
20+
// clang-format off
21+
bool ps_parse_statement (ps_compiler *compiler, ps_ast_block *block );
22+
bool ps_parse_compound_statement (ps_compiler *compiler, ps_ast_block *block );
23+
bool ps_parse_assignment (ps_compiler *compiler, ps_ast_block *block, ps_symbol *variable );
24+
bool ps_parse_read_or_readln (ps_compiler *compiler, ps_ast_block *block, bool newline );
25+
bool ps_parse_write_or_writeln (ps_compiler *compiler, ps_ast_block *block, bool newline );
26+
bool ps_parse_assignment_or_procedure_call (ps_compiler *compiler, ps_ast_block *block );
27+
bool ps_parse_if_then_else (ps_compiler *compiler, ps_ast_block *block );
28+
bool ps_parse_repeat_until (ps_compiler *compiler, ps_ast_block *block );
29+
bool ps_parse_while_do (ps_compiler *compiler, ps_ast_block *block );
30+
bool ps_parse_for_do (ps_compiler *compiler, ps_ast_block *block );
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 );
33+
// clang-format on
3234

3335
#ifdef __cplusplus
3436
}

src/ps_ast.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ ps_ast_block *ps_ast_create_block(uint16_t line, uint16_t column, ps_ast_block *
118118
block->result_type = NULL;
119119
block->n_vars = 0;
120120
block->statement_list = NULL;
121-
block->n_executables = 0;
122-
block->executables = NULL;
123121
return block;
124122
}
125123

@@ -134,9 +132,6 @@ ps_ast_node *ps_ast_free_block(ps_ast_block *block)
134132
ps_symbol_table_free(block->symbols);
135133
if (block->statement_list != NULL)
136134
ps_ast_free_statement_list(block->statement_list);
137-
for (size_t i = 0; i < block->n_executables; i++)
138-
block->executables[i] = ps_ast_free_block(block->executables[i]);
139-
ps_memory_free(PS_MEMORY_AST, block->executables);
140135
ps_memory_free(PS_MEMORY_AST, block);
141136
return NULL;
142137
}
@@ -341,17 +336,22 @@ ps_ast_call *ps_ast_create_call(uint16_t line, uint16_t column, ps_ast_node_kind
341336
return NULL;
342337
call->executable = executable;
343338
call->n_args = n_args;
344-
call->args = args;
339+
call->args = ps_memory_calloc(PS_MEMORY_AST, n_args, sizeof(ps_ast_node *));
340+
if (call->args == NULL)
341+
return ps_ast_free_call(call);
345342
return call;
346343
}
347344

348345
ps_ast_node *ps_ast_free_call(ps_ast_call *call)
349346
{
350347
assert(call != NULL);
351348
assert(call->kind == PS_AST_PROCEDURE_CALL || call->kind == PS_AST_FUNCTION_CALL);
352-
for (size_t i = 0; i < call->n_args; i++)
353-
ps_ast_free_node(call->args[i]);
354-
ps_memory_free(PS_MEMORY_AST, call->args);
349+
if (call->args != NULL)
350+
{
351+
for (size_t i = 0; i < call->n_args; i++)
352+
ps_ast_free_node(call->args[i]);
353+
ps_memory_free(PS_MEMORY_AST, call->args);
354+
}
355355
ps_memory_free(PS_MEMORY_AST, call);
356356
return NULL;
357357
}

src/ps_ast_debug.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ void ps_ast_debug_block(size_t margin, const ps_ast_block *block)
155155
ps_ast_debug_line(margin, "%s %s", ps_ast_node_get_kind_name(block->kind), block->name);
156156
// ps_ast_debug_line(margin, " - Number of symbols: %zu", block->symbols ? block->symbols->used : 0);
157157
// ps_ast_debug_line(margin, " - Number of variables: %zu", block->n_vars);
158-
// ps_ast_debug_line(margin, " - Number of executables: %zu", block->n_executables);
159158
// ps_ast_debug_line(margin, " - Number of statements: %zu",
160159
// block->statement_list ? block->statement_list->count : 0);
161160
// ps_ast_debug_line(margin, " - Number of parameters: %zu",

src/ps_interpreter.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ ps_interpreter *ps_interpreter_alloc(bool range_check, bool bool_eval, bool io_c
4444
if (interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM] == NULL)
4545
return ps_interpreter_free(interpreter);
4646
// Initialize system environment
47-
if (!ps_system_init(interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM]))
48-
return ps_interpreter_free(interpreter);
49-
if (!ps_procedures_init(interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM]))
50-
return ps_interpreter_free(interpreter);
51-
if (!ps_functions_init(interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM]))
47+
if (!ps_system_init(interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM]->symbols))
5248
return ps_interpreter_free(interpreter);
5349
return interpreter;
5450
}
@@ -59,7 +55,7 @@ ps_interpreter *ps_interpreter_free(ps_interpreter *interpreter)
5955
{
6056
if (interpreter->string_heap != NULL)
6157
interpreter->string_heap = ps_string_heap_free(interpreter->string_heap);
62-
ps_system_done(interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM]);
58+
ps_system_done(interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM]->symbols<);
6359
for (size_t i = 0; i < PS_INTERPRETER_ENVIRONMENTS; i++)
6460
{
6561
if (interpreter->environments[i] != NULL)

src/ps_parse.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
* BLOCK
1818
* '.'
1919
*/
20-
ps_ast_block *ps_parse_start(ps_compiler *compiler, ps_ast_block *block_program)
20+
bool ps_parse_start(ps_compiler *compiler, ps_ast_block *block_program)
2121
{
2222
PARSE_BEGIN("START", "")
2323

24-
READ_NEXT_TOKEN()
24+
READ_NEXT_TOKEN
2525
switch (lexer->current_token.type)
2626
{
2727
case PS_TOKEN_PROGRAM:

src/ps_parse_declaration.c

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ static bool ps_parse_program_parameters(ps_compiler *compiler)
2424
// Empty list?
2525
if (lexer->current_token.type == PS_TOKEN_LEFT_PARENTHESIS)
2626
{
27-
READ_NEXT_TOKEN()
27+
READ_NEXT_TOKEN
2828
if (lexer->current_token.type == PS_TOKEN_RIGHT_PARENTHESIS)
2929
{
30-
READ_NEXT_TOKEN()
30+
READ_NEXT_TOKEN
3131
PARSE_END("OK")
3232
}
3333
}
3434
bool loop = true;
3535
do
3636
{
3737
EXPECT_TOKEN(PS_TOKEN_IDENTIFIER)
38-
READ_NEXT_TOKEN()
38+
READ_NEXT_TOKEN
3939
switch (lexer->current_token.type)
4040
{
4141
case PS_TOKEN_COMMA:
42-
READ_NEXT_TOKEN()
42+
READ_NEXT_TOKEN
4343
break;
4444
case PS_TOKEN_RIGHT_PARENTHESIS:
45-
READ_NEXT_TOKEN()
45+
READ_NEXT_TOKEN
4646
loop = false;
4747
break;
4848
default:
@@ -69,20 +69,20 @@ bool ps_parse_program(ps_compiler *compiler, ps_ast_node *ast)
6969
EXPECT_TOKEN(PS_TOKEN_PROGRAM)
7070
uint16_t start_line = lexer->start_line;
7171
uint16_t start_column = lexer->start_column;
72-
READ_NEXT_TOKEN()
72+
READ_NEXT_TOKEN
7373

7474
// IDENTIFIER
7575
EXPECT_TOKEN(PS_TOKEN_IDENTIFIER)
7676
COPY_IDENTIFIER(identifier)
77-
READ_NEXT_TOKEN()
77+
READ_NEXT_TOKEN
7878

7979
// Skip optional parameters enclosed in parentheses
8080
if (!ps_parse_program_parameters(compiler))
8181
TRACE_ERROR("PARAMETERS")
8282

8383
// ';'
8484
EXPECT_TOKEN(PS_TOKEN_SEMI_COLON)
85-
READ_NEXT_TOKEN()
85+
READ_NEXT_TOKEN
8686

8787
ps_ast_block *program = ps_ast_create_block(start_line, start_column, NULL, PS_AST_PROGRAM, identifier);
8888
if (NULL == program)
@@ -124,20 +124,20 @@ bool ps_parse_uses(ps_compiler *compiler, ps_ast_block *block)
124124

125125
if (lexer->current_token.type == PS_TOKEN_USES)
126126
{
127-
READ_NEXT_TOKEN()
127+
READ_NEXT_TOKEN
128128
bool loop = true;
129129
do
130130
{
131131
if (lexer->current_token.type != PS_TOKEN_IDENTIFIER)
132132
RETURN_ERROR(PS_ERROR_EXPECTED_IDENTIFIER)
133-
READ_NEXT_TOKEN()
133+
READ_NEXT_TOKEN
134134
switch (lexer->current_token.type)
135135
{
136136
case PS_TOKEN_COMMA:
137-
READ_NEXT_TOKEN()
137+
READ_NEXT_TOKEN
138138
break;
139139
case PS_TOKEN_SEMI_COLON:
140-
READ_NEXT_TOKEN()
140+
READ_NEXT_TOKEN
141141
loop = false;
142142
break;
143143
default:
@@ -193,8 +193,6 @@ bool ps_parse_block(ps_compiler *compiler, ps_ast_block *block)
193193
// RETURN_ERROR(PS_ERROR_OUT_OF_MEMORY)
194194
// if (!ps_parse_procedure_or_function_declaration(compiler, procedure, PS_SYMBOL_KIND_PROCEDURE))
195195
// TRACE_ERROR("PROCEDURE")
196-
// block->executables[block->n_executables] = procedure;
197-
// block->n_executables += 1;
198196
// break;
199197
case PS_TOKEN_FUNCTION:
200198
RETURN_ERROR(PS_ERROR_NOT_IMPLEMENTED)
@@ -204,8 +202,6 @@ bool ps_parse_block(ps_compiler *compiler, ps_ast_block *block)
204202
// RETURN_ERROR(PS_ERROR_OUT_OF_MEMORY)
205203
// if (!ps_parse_procedure_or_function_declaration(compiler, function, PS_SYMBOL_KIND_FUNCTION))
206204
// TRACE_ERROR("FUNCTION")
207-
// block->executables[block->n_executables] = function;
208-
// block->n_executables += 1;
209205
// break;
210206
case PS_TOKEN_BEGIN:
211207
loop = false;
@@ -262,14 +258,14 @@ bool ps_parse_const(ps_compiler *compiler, ps_ast_block *block)
262258
ps_symbol *constant;
263259

264260
EXPECT_TOKEN(PS_TOKEN_CONST)
265-
READ_NEXT_TOKEN()
261+
READ_NEXT_TOKEN
266262
do
267263
{
268264
EXPECT_TOKEN(PS_TOKEN_IDENTIFIER)
269265
COPY_IDENTIFIER(identifier)
270-
READ_NEXT_TOKEN()
266+
READ_NEXT_TOKEN
271267
EXPECT_TOKEN(PS_TOKEN_EQ)
272-
READ_NEXT_TOKEN()
268+
READ_NEXT_TOKEN
273269
value = ps_value_alloc(&ps_system_none, data);
274270
if (value == NULL)
275271
RETURN_ERROR(PS_ERROR_OUT_OF_MEMORY)
@@ -279,7 +275,7 @@ bool ps_parse_const(ps_compiler *compiler, ps_ast_block *block)
279275
TRACE_ERROR("CONSTANT_EXPRESSION")
280276
}
281277
EXPECT_TOKEN(PS_TOKEN_SEMI_COLON);
282-
READ_NEXT_TOKEN()
278+
READ_NEXT_TOKEN
283279
constant = ps_symbol_alloc(PS_SYMBOL_KIND_CONSTANT, identifier, value);
284280
if (constant == NULL)
285281
RETURN_ERROR(PS_ERROR_OUT_OF_MEMORY)
@@ -302,15 +298,15 @@ bool ps_parse_type(ps_compiler *compiler, ps_ast_block *block)
302298

303299
// RETURN_ERROR(PS_ERROR_NOT_IMPLEMENTED)
304300
EXPECT_TOKEN(PS_TOKEN_TYPE);
305-
READ_NEXT_TOKEN()
301+
READ_NEXT_TOKEN
306302
if (lexer->current_token.type != PS_TOKEN_IDENTIFIER)
307303
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
308304
do
309305
{
310306
if (!ps_parse_type_definition(compiler, block))
311307
TRACE_ERROR("TYPE_DEFINITION");
312308
EXPECT_TOKEN(PS_TOKEN_SEMI_COLON);
313-
READ_NEXT_TOKEN()
309+
READ_NEXT_TOKEN
314310
} while (lexer->current_token.type == PS_TOKEN_IDENTIFIER);
315311

316312
PARSE_END("OK")
@@ -331,12 +327,12 @@ static bool ps_parse_var_identifier_list(ps_compiler *compiler, ps_identifier *i
331327
const ps_symbol *variable = ps_compiler_find_symbol(compiler, identifier[*var_count], true);
332328
if (variable != NULL)
333329
RETURN_ERROR(PS_ERROR_SYMBOL_EXISTS)
334-
READ_NEXT_TOKEN()
330+
READ_NEXT_TOKEN
335331
if (lexer->current_token.type == PS_TOKEN_COLON)
336332
break;
337333
if (lexer->current_token.type != PS_TOKEN_COMMA)
338334
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
339-
READ_NEXT_TOKEN()
335+
READ_NEXT_TOKEN
340336
*var_count += 1;
341337
if (*var_count == 8)
342338
RETURN_ERROR(PS_ERROR_TOO_MANY_VARIABLES)
@@ -363,12 +359,12 @@ bool ps_parse_var(ps_compiler *compiler, ps_ast_block *block)
363359
ps_symbol *type_symbol = NULL;
364360

365361
EXPECT_TOKEN(PS_TOKEN_VAR)
366-
READ_NEXT_TOKEN()
362+
READ_NEXT_TOKEN
367363
do
368364
{
369365
if (!ps_parse_var_identifier_list(compiler, identifier, &var_count))
370366
TRACE_ERROR("VARIABLE IDENTIFIER LIST")
371-
READ_NEXT_TOKEN()
367+
READ_NEXT_TOKEN
372368
if (!ps_parse_type_reference(compiler, &type_symbol, NULL))
373369
TRACE_ERROR("TYPE REFERENCE")
374370
EXPECT_TOKEN(PS_TOKEN_SEMI_COLON)
@@ -377,7 +373,7 @@ bool ps_parse_var(ps_compiler *compiler, ps_ast_block *block)
377373
if (!ps_compiler_add_variable(compiler, block, identifier[i], type_symbol))
378374
TRACE_ERROR("ADD VARIABLE")
379375
}
380-
READ_NEXT_TOKEN()
376+
READ_NEXT_TOKEN
381377
} while (lexer->current_token.type == PS_TOKEN_IDENTIFIER);
382378

383379
PARSE_END("OK")

0 commit comments

Comments
 (0)