Skip to content

Commit 85afd16

Browse files
committed
AST WIP
1 parent e8fc969 commit 85afd16

5 files changed

Lines changed: 41 additions & 42 deletions

File tree

include/ps_parse.h

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

2020
/* src/ps_parse.c */
21-
ps_ast_node *ps_parse_start(ps_compiler *compiler);
21+
bool ps_parse_start(ps_compiler *compiler, ps_ast_block *block_program);
2222

2323
/* src/ps_parse_executable.c */
24-
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_ast_block *block,
26-
ps_ast_node *expression);
27-
ps_ast_node *ps_parse_variable_reference(ps_compiler *compiler, ps_symbol **variable);
24+
bool ps_parse_procedure_or_function_declaration(ps_compiler *compiler, ps_symbol_kind kind);
25+
bool ps_parse_procedure_or_function_call(ps_compiler *compiler, ps_symbol *executable, ps_ast_block *block,
26+
ps_ast_node *expression);
27+
bool ps_parse_variable_reference(ps_compiler *compiler, ps_symbol **variable);
2828

2929
/* src/ps_parse_expression.c */
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);
37-
ps_ast_node *ps_parse_constant_expression(ps_compiler *compiler, ps_value *constant);
38-
ps_ast_node *ps_parse_function_call(ps_compiler *compiler, ps_ast_block *block, ps_symbol *function,
39-
ps_ast_node *expression);
30+
bool ps_parse_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
31+
32+
bool ps_parse_relational_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
33+
bool ps_parse_and_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
34+
bool ps_parse_or_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
35+
bool ps_parse_simple_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
36+
bool ps_parse_term(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
37+
bool ps_parse_factor(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
38+
bool ps_parse_constant_expression(ps_compiler *compiler, ps_value *constant);
39+
bool ps_parse_function_call(ps_compiler *compiler, ps_ast_block *block, ps_symbol *function,
40+
ps_ast_node *expression);
4041

4142
/* src/ps_parse_type.c */
42-
ps_ast_node *ps_parse_type_definition(ps_compiler *compiler, ps_ast_block *block);
43-
ps_ast_node *ps_parse_type_reference(ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol,
44-
const char *type_name);
45-
ps_ast_node *ps_parse_type_reference_enum(ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol,
46-
const char *type_name);
47-
ps_ast_node *ps_parse_type_reference_subrange(ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol,
48-
const char *type_name);
49-
ps_ast_node *ps_parse_type_reference_array(ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol,
50-
const char *type_name);
43+
bool ps_parse_type_definition(ps_compiler *compiler, ps_ast_block *block);
44+
bool ps_parse_type_reference(ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol,
45+
const char *type_name);
46+
bool ps_parse_type_reference_enum(ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol,
47+
const char *type_name);
48+
bool ps_parse_type_reference_subrange(ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol,
49+
const char *type_name);
50+
bool ps_parse_type_reference_array(ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol,
51+
const char *type_name);
5152

5253
#define PARSE_BEGIN(__PARSE__, __PLUS__) \
5354
ps_lexer *lexer = ps_parser_get_lexer(compiler->parser); \

src/ps_interpreter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ps_interpreter *ps_interpreter_free(ps_interpreter *interpreter)
5555
{
5656
if (interpreter->string_heap != NULL)
5757
interpreter->string_heap = ps_string_heap_free(interpreter->string_heap);
58-
ps_system_done(interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM]->symbols<);
58+
ps_system_done(interpreter->environments[PS_INTERPRETER_ENVIRONMENT_SYSTEM]->symbols);
5959
for (size_t i = 0; i < PS_INTERPRETER_ENVIRONMENTS; i++)
6060
{
6161
if (interpreter->environments[i] != NULL)

src/ps_parse_declaration.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ static bool ps_parse_program_parameters(ps_compiler *compiler)
5858
* PROGRAM IDENTIFIER [ '(' [ IDENTIFIER [ ',' IDENTIFIER ]* ] ')'] ';'
5959
* BLOCK '.'
6060
*/
61-
bool ps_parse_program(ps_compiler *compiler, ps_ast_node *ast)
61+
bool ps_parse_program(ps_compiler *compiler, ps_ast_block *block)
6262
{
6363
PARSE_BEGIN("PROGRAM", "")
6464

6565
ps_identifier identifier = {0};
6666
ps_symbol *symbol_program = NULL;
67+
uint16_t start_line = lexer->start_line;
68+
uint16_t start_column = lexer->start_column;
6769

6870
// 'PROGRAM'
6971
EXPECT_TOKEN(PS_TOKEN_PROGRAM)
70-
uint16_t start_line = lexer->start_line;
71-
uint16_t start_column = lexer->start_column;
7272
READ_NEXT_TOKEN
7373

7474
// IDENTIFIER
@@ -111,7 +111,6 @@ bool ps_parse_program(ps_compiler *compiler, ps_ast_node *ast)
111111
// NB: text after '.' is not analyzed and has not to be
112112
EXPECT_TOKEN(PS_TOKEN_DOT)
113113

114-
ast = (ps_ast_node *)program;
115114
PARSE_END("OK")
116115
}
117116

src/ps_parse_expression.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,7 @@ bool ps_parse_function_call(ps_compiler *compiler, ps_ast_block *block, ps_symbo
687687
* | [ '-' ] REAL_VALUE
688688
* | BOOLEAN_VALUE
689689
* | [ '-' ] IDENTIFIER
690-
* Next steps:
691690
* | STRING_VALUE
692-
* | NIL
693-
* | CONSTANT_EXPRESSION
694691
*/
695692
bool ps_parse_constant_expression(ps_compiler *compiler, ps_ast_block *block, ps_value *constant)
696693
{
@@ -752,7 +749,7 @@ bool ps_parse_constant_expression(ps_compiler *compiler, ps_ast_block *block, ps
752749
break;
753750
case PS_TOKEN_IDENTIFIER:
754751
COPY_IDENTIFIER(identifier)
755-
symbol = ps_compiler_find_symbol(compiler, identifier, false);
752+
symbol = ps_compiler_find_symbol(compiler, block, identifier, false);
756753
if (symbol == NULL)
757754
RETURN_ERROR(PS_ERROR_SYMBOL_NOT_FOUND);
758755
if (symbol->kind != PS_SYMBOL_KIND_CONSTANT)

src/ps_parse_statement.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool ps_parse_compound_statement(ps_compiler *compiler, ps_ast_block *block)
7373

7474
EXPECT_TOKEN(PS_TOKEN_BEGIN);
7575
READ_NEXT_TOKEN
76-
if (lexer->current_token.type != PS_TOKEN_END && !ps_parse_statement_list(compiler, PS_TOKEN_END))
76+
if (lexer->current_token.type != PS_TOKEN_END && !ps_parse_statement_list(compiler, block, PS_TOKEN_END))
7777
TRACE_ERROR("STATEMENT_LIST")
7878
EXPECT_TOKEN(PS_TOKEN_END)
7979
READ_NEXT_TOKEN
@@ -158,6 +158,7 @@ bool ps_parse_assignment_array(ps_compiler *compiler, ps_ast_block *block, ps_sy
158158

159159
/**
160160
* Parse assignment:
161+
* Simple:
161162
* IDENTIFIER := EXPRESSION
162163
* Next steps:
163164
* Array access:
@@ -175,16 +176,16 @@ bool ps_parse_assignment(ps_compiler *compiler, ps_ast_block *block, ps_symbol *
175176
if (variable->kind == PS_SYMBOL_KIND_CONSTANT)
176177
{
177178
compiler->error = PS_ERROR_ASSIGN_TO_CONST;
178-
ps_interpreter_set_message(compiler, "Constant '%s' cannot be assigned", variable->name);
179+
ps_compiler_set_message(compiler, "Constant '%s' cannot be assigned", variable->name);
179180
TRACE_ERROR("CONSTANT");
180181
}
181182
if (variable->kind != PS_SYMBOL_KIND_VARIABLE)
182183
{
183184
compiler->error = PS_ERROR_EXPECTED_VARIABLE;
184-
ps_interpreter_set_message(compiler, "Symbol '%s' is not a variable", variable->name);
185+
ps_compiler_set_message(compiler, "Symbol '%s' is not a variable", variable->name);
185186
TRACE_ERROR("VARIABLE");
186187
}
187-
if (compiler->debug >= DEBUG_VERBOSE)
188+
if (compiler->debug >= COMPILER_DEBUG_VERBOSE)
188189
fprintf(stderr, "\nINFO\tASSIGNMENT: #1 variable '%s' type is '%s'\n", variable->name,
189190
ps_type_definition_get_name(variable->value->type->value->data.t));
190191
if (ps_value_get_type(variable->value) == PS_TYPE_ARRAY)
@@ -200,7 +201,7 @@ bool ps_parse_assignment(ps_compiler *compiler, ps_ast_block *block, ps_symbol *
200201
result.type = variable->value->type;
201202
if (!ps_parse_expression(compiler, &result))
202203
TRACE_ERROR("EXPRESSION1");
203-
if (compiler->debug >= DEBUG_VERBOSE)
204+
if (compiler->debug >= COMPILER_DEBUG_VERBOSE)
204205
fprintf(stderr, "\nINFO\tASSIGNMENT: #2 variable '%s' type is '%s'\n", variable->name,
205206
ps_type_definition_get_name(variable->value->type->value->data.t));
206207
// if (mode == MODE_EXEC && !ps_interpreter_copy_value(compiler, &result, variable->value))
@@ -244,6 +245,7 @@ bool ps_parse_write_or_writeln(ps_compiler *compiler, ps_ast_block *block, bool
244245
bool loop = true;
245246
int16_t width = 0;
246247
int16_t precision = 0;
248+
ps_ast_node *expression = NULL;
247249

248250
// "Write[Ln];" or "Write[Ln] Else|End|Until"?
249251
// (Write without parameters is legal but is a no-op)
@@ -266,11 +268,11 @@ bool ps_parse_write_or_writeln(ps_compiler *compiler, ps_ast_block *block, bool
266268

267269
while (loop)
268270
{
269-
if (compiler->debug >= DEBUG_VERBOSE)
271+
if (compiler->debug >= COMPILER_DEBUG_VERBOSE)
270272
fprintf(stderr, "\nINFO\tWRITE_OR_WRITELN: expecting expression of type 'ANY'\n");
271273
if (!ps_parse_expression(compiler, block, expression))
272274
TRACE_ERROR("EXPRESSION")
273-
// retrieve numeric format
275+
// retrieve string/numeric format
274276
width = 0;
275277
precision = 0;
276278
if (lexer->current_token.type == PS_TOKEN_COLON)
@@ -334,7 +336,7 @@ bool ps_parse_assignment_or_procedure_call(ps_compiler *compiler, ps_ast_block *
334336
symbol = ps_environment_find_symbol(environment->parent, identifier, true);
335337
if (symbol != NULL && symbol->kind == PS_SYMBOL_KIND_FUNCTION && strcmp((char *)identifier, environment->name) == 0)
336338
{
337-
if (compiler->debug >= DEBUG_VERBOSE)
339+
if (compiler->debug >= COMPILER_DEBUG_VERBOSE)
338340
fprintf(stderr, "%cINFO\tAssignment to current function '%s' as Result\n", (char *)identifier);
339341
// Assign to the not so implicit "Result" local variable
340342
symbol = ps_interpreter_find_symbol(compiler, result_identifier, false);
@@ -357,7 +359,7 @@ bool ps_parse_assignment_or_procedure_call(ps_compiler *compiler, ps_ast_block *
357359
TRACE_ERROR("ASSIGNMENT")
358360
break;
359361
case PS_SYMBOL_KIND_CONSTANT:
360-
ps_interpreter_set_message(compiler, "Constant '%s' cannot be assigned", symbol->name);
362+
ps_compiler_set_message(compiler, "Constant '%s' cannot be assigned", symbol->name);
361363
RETURN_ERROR(PS_ERROR_ASSIGN_TO_CONST)
362364
case PS_SYMBOL_KIND_PROCEDURE:
363365
if (!ps_parse_procedure_or_function_call(compiler, symbol, NULL))

0 commit comments

Comments
 (0)