Skip to content

Commit d40d38f

Browse files
committed
AST WIP
1 parent e018e18 commit d40d38f

8 files changed

Lines changed: 262 additions & 224 deletions

include/ps_parse.h

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,16 @@ extern "C"
2626
ps_ast_node *expression);
2727
bool ps_parse_variable_reference(ps_compiler *compiler, ps_symbol **variable);
2828

29-
/* src/ps_parse_expression.c */
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_ast_block *block, ps_value *constant);
39-
bool ps_parse_function_call(ps_compiler *compiler, ps_ast_block *block, ps_symbol *function,
40-
ps_ast_node *expression);
41-
42-
/* src/ps_parse_type.c */
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);
52-
5329
#define PARSE_BEGIN(__PARSE__, __PLUS__) \
5430
ps_lexer *lexer = ps_parser_get_lexer(compiler->parser); \
5531
static char *visit = __PARSE__; \
5632
if (compiler->debug >= COMPILER_DEBUG_TRACE) \
5733
{ \
5834
fprintf(stderr, "BEGIN\t%-32s %-32s %-32s ", block->name, visit, __PLUS__); \
5935
ps_token_debug(stderr, "BEGIN", &lexer->current_token); \
60-
}
36+
} \
37+
uint16_t start_line = lexer->start_line; \
38+
uint16_t start_column = lexer->start_column;
6139

6240
#define PARSE_END(__PLUS__) \
6341
{ \

include/ps_parse_expression.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
This file is part of the PascalScript Pascal interpreter.
3+
SPDX-FileCopyrightText: 2026 Christophe "CHiPs" Petit <chips44@gmail.com>
4+
SPDX-License-Identifier: LGPL-3.0-or-later
5+
*/
6+
7+
#ifndef _PS_PARSE_EXPRESSION_H
8+
#define _PS_PARSE_EXPRESSION_H
9+
10+
#include <stdint.h>
11+
12+
#include "ps_ast.h"
13+
#include "ps_compiler.h"
14+
#include "ps_symbol.h"
15+
#include "ps_value.h"
16+
17+
#ifdef __cplusplus
18+
extern "C"
19+
{
20+
#endif
21+
22+
// clang-format off
23+
bool ps_parse_expression (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
24+
bool ps_parse_relational_expression(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
25+
bool ps_parse_and_expression (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
26+
bool ps_parse_or_expression (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
27+
bool ps_parse_simple_expression (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
28+
bool ps_parse_term (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
29+
bool ps_parse_factor (ps_compiler *compiler, ps_ast_block *block, ps_ast_node **result);
30+
bool ps_parse_function_call (ps_compiler *compiler, ps_ast_block *block, ps_symbol *function, ps_ast_node *expression);
31+
bool ps_parse_constant_expression (ps_compiler *compiler, ps_ast_block *block, ps_value *constant);
32+
// clang-format on
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif /* _PS_PARSE_EXPRESSION_H */

include/ps_parse_statement.h

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

2020
// 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 );
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_node *statement );
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 );
2727
bool ps_parse_if_then_else (ps_compiler *compiler, ps_ast_block *block );
2828
bool ps_parse_repeat_until (ps_compiler *compiler, ps_ast_block *block );
2929
bool ps_parse_while_do (ps_compiler *compiler, ps_ast_block *block );

include/ps_parse_type.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
This file is part of the PascalScript Pascal interpreter.
3+
SPDX-FileCopyrightText: 2026 Christophe "CHiPs" Petit <chips44@gmail.com>
4+
SPDX-License-Identifier: LGPL-3.0-or-later
5+
*/
6+
7+
#ifndef _PS_PARSE_TYPE_H
8+
#define _PS_PARSE_TYPE_H
9+
10+
#include <stdint.h>
11+
12+
#include "ps_ast.h"
13+
#include "ps_compiler.h"
14+
#include "ps_symbol.h"
15+
16+
#ifdef __cplusplus
17+
extern "C"
18+
{
19+
#endif
20+
21+
// clang-format off
22+
bool ps_parse_type_definition (ps_compiler *compiler, ps_ast_block *block);
23+
bool ps_parse_type_reference (ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol, const char *type_name);
24+
bool ps_parse_type_reference_enum (ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol, const char *type_name);
25+
bool ps_parse_type_reference_subrange (ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol, const char *type_name);
26+
bool ps_parse_type_reference_array (ps_compiler *compiler, ps_ast_block *block, ps_symbol **type_symbol, const char *type_name);
27+
// clang-format on
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif /* _PS_PARSE_TYPE_H */

src/ps_compiler.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ ps_compiler *ps_compiler_alloc(bool range_check, bool bool_eval, bool io_check)
3636
return ps_compiler_free(compiler);
3737
// Allocate and initialize system environment
3838
compiler->system = ps_symbol_table_alloc(256, 0);
39+
if (compiler->system == NULL)
40+
return ps_compiler_free(compiler);
3941
if (!ps_system_init(compiler->system))
4042
return ps_compiler_free(compiler);
4143
return compiler;
@@ -79,7 +81,7 @@ bool ps_compiler_set_message(ps_compiler *compiler, char *format, ...) // NOSONA
7981
va_start(args, format);
8082
vsnprintf(compiler->message, sizeof(compiler->message), format, args); // NOSONAR
8183
va_end(args);
82-
return true;
84+
return false;
8385
}
8486

8587
ps_symbol *ps_compiler_find_symbol(ps_compiler *compiler, ps_ast_block *block, const char *name, bool local)

src/ps_parse_declaration.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ps_executable.h"
1010
#include "ps_memory.h"
1111
#include "ps_parse.h"
12+
#include "ps_parse_expression.h"
1213
#include "ps_parse_statement.h"
1314
#include "ps_system.h"
1415
#include "ps_token.h"
@@ -20,6 +21,8 @@
2021
static bool ps_parse_program_parameters(ps_compiler *compiler, ps_ast_block *block)
2122
{
2223
PARSE_BEGIN("PROGRAM", "PARAMETERS")
24+
(void)start_line;
25+
(void)start_column;
2326

2427
// Empty list?
2528
if (lexer->current_token.type == PS_TOKEN_LEFT_PARENTHESIS)
@@ -64,8 +67,6 @@ bool ps_parse_program(ps_compiler *compiler, ps_ast_block *block)
6467

6568
ps_identifier identifier = {0};
6669
ps_symbol *symbol_program = NULL;
67-
uint16_t start_line = lexer->start_line;
68-
uint16_t start_column = lexer->start_column;
6970

7071
// 'PROGRAM'
7172
EXPECT_TOKEN(PS_TOKEN_PROGRAM)
@@ -121,6 +122,8 @@ bool ps_parse_uses(ps_compiler *compiler, ps_ast_block *block)
121122
{
122123
(void)block;
123124
PARSE_BEGIN("USES", "")
125+
(void)start_line;
126+
(void)start_column;
124127

125128
if (lexer->current_token.type == PS_TOKEN_USES)
126129
{
@@ -164,6 +167,8 @@ bool ps_parse_uses(ps_compiler *compiler, ps_ast_block *block)
164167
bool ps_parse_block(ps_compiler *compiler, ps_ast_block *block)
165168
{
166169
PARSE_BEGIN("BLOCK", "")
170+
(void)start_line;
171+
(void)start_column;
167172

168173
bool loop = true;
169174
do
@@ -211,8 +216,10 @@ bool ps_parse_block(ps_compiler *compiler, ps_ast_block *block)
211216
}
212217
} while (loop);
213218

214-
if (!ps_parse_compound_statement(compiler, block))
219+
ps_ast_statement_list *statement_list = NULL;
220+
if (!ps_parse_compound_statement(compiler, block, &statement_list))
215221
TRACE_ERROR("COMPOUND")
222+
block->statement_list = statement_list;
216223

217224
PARSE_END("OK")
218225
}
@@ -226,7 +233,8 @@ bool ps_parse_block(ps_compiler *compiler, ps_ast_block *block)
226233
* Const
227234
* Foo = 42;
228235
* Bar = -3.14;
229-
* Baz = True;
236+
* Baz = -Bar;
237+
* Flag = True;
230238
* Hello = 'Hello, World!';
231239
* ImageWidth = 320;
232240
* ImageHeight = 200;
@@ -239,16 +247,15 @@ bool ps_parse_block(ps_compiler *compiler, ps_ast_block *block)
239247
* IDENTIFIER '=' IDENTIFIER | CONSTANT_EXPRESSION ';'
240248
* Examples:
241249
* Const
242-
* ImageWidth = 320;
243-
* ImageHeight = 200;
244-
* ImageDepth = 8;
245250
* ImagePixels = ImageWidth * ImageHeight;
246-
* ImageSize = (ImagePixels * ImageDepth) div 8;
251+
* ImageSize = (ImagePixels * ImageDepth) div ImageDepth;
247252
* Not implemented yet in lexer:
248253
* Lines = 'First line' #10 'Second line' #10 'Third line';
249254
*/
250255
bool ps_parse_const(ps_compiler *compiler, ps_ast_block *block)
251256
{
257+
// NB: adds symbols to current block symbol table, does not produce any AST nodes
258+
252259
PARSE_BEGIN("CONST", "")
253260

254261
ps_identifier identifier;
@@ -293,11 +300,13 @@ bool ps_parse_const(ps_compiler *compiler, ps_ast_block *block)
293300
*/
294301
bool ps_parse_type(ps_compiler *compiler, ps_ast_block *block)
295302
{
303+
// NB: adds symbols to current block symbol table, does not produce any AST nodes
304+
296305
PARSE_BEGIN("TYPE", "");
297306

298-
// RETURN_ERROR(PS_ERROR_NOT_IMPLEMENTED)
299307
EXPECT_TOKEN(PS_TOKEN_TYPE);
300308
READ_NEXT_TOKEN
309+
301310
if (lexer->current_token.type != PS_TOKEN_IDENTIFIER)
302311
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
303312
do

0 commit comments

Comments
 (0)