Skip to content

Commit 77e4162

Browse files
committed
AST WIP
1 parent 9c9ea36 commit 77e4162

26 files changed

Lines changed: 589 additions & 505 deletions

include/ps_ast.h

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,32 @@ extern "C"
118118
ps_ast_node *condition; /** @brief Loop until condition is true */
119119
} ps_ast_repeat;
120120

121+
/** @brief Lvalue: simple variable */
122+
/** @example I, Total, ... */
123+
typedef struct s_ps_ast_variable_simple
124+
{
125+
PS_AST_NODE_COMMON
126+
ps_symbol *variable; /** @brief Symbol being referenced */
127+
} ps_ast_variable_simple;
128+
129+
/** @brief Lvalue: array value, like A[I], A[I, J, K], ... */
130+
typedef struct s_ps_ast_variable_array
131+
{
132+
PS_AST_NODE_COMMON
133+
ps_symbol *variable; /** @brief Array being referenced */
134+
size_t n_indexes; /** @brief Number of dimensions */
135+
ps_ast_node **indexes; /** @brief Values for each dimension */
136+
} ps_ast_variable_array;
137+
121138
/** @brief FOR statement */
122139
typedef struct s_ps_ast_for
123140
{
124141
PS_AST_NODE_COMMON
125-
ps_ast_node *variable; /** @brief Loop variable */
126-
ps_ast_node *start; /** @brief Start value */
127-
ps_ast_node *end; /** @brief End value */
128-
int step; /** @brief Step value: 1 for "TO", -1 for "DOWNTO" */
129-
ps_ast_statement_list *body; /** @brief Statements to execute for each value of loop variable */
142+
ps_ast_variable_simple *variable; /** @brief Loop variable *must* be a simple variable */
143+
ps_ast_node *start; /** @brief Start value */
144+
ps_ast_node *end; /** @brief End value */
145+
int step; /** @brief Step value: 1 for "TO", -1 for "DOWNTO" */
146+
ps_ast_statement_list *body; /** @brief Statements to execute for each value of loop variable */
130147
} ps_ast_for;
131148

132149
/** @brief Argument for procedure or function call */
@@ -160,23 +177,6 @@ extern "C"
160177
ps_value value;
161178
} ps_ast_value;
162179

163-
/** @brief Lvalue: simple variable */
164-
/** @example I, Total, ... */
165-
typedef struct s_ps_ast_variable_simple
166-
{
167-
PS_AST_NODE_COMMON
168-
ps_symbol *variable; /** @brief Symbol being referenced */
169-
} ps_ast_variable_simple;
170-
171-
/** @brief Lvalue: array value, like A[I], A[I, J, K], ... */
172-
typedef struct s_ps_ast_variable_array
173-
{
174-
PS_AST_NODE_COMMON
175-
ps_symbol *variable; /** @brief Array being referenced */
176-
size_t n_indexes; /** @brief Number of dimensions */
177-
ps_ast_node **indexes; /** @brief Values for each dimension */
178-
} ps_ast_variable_array;
179-
180180
/** @brief Unary operation: operator and operand */
181181
typedef struct s_ps_ast_unary_operation
182182
{
@@ -220,16 +220,17 @@ extern "C"
220220
ps_ast_block *ps_ast_create_block (uint16_t line, uint16_t column, ps_ast_node_kind kind, char *name );
221221
ps_ast_statement_list *ps_ast_create_statement_list (uint16_t line, uint16_t column, size_t count );
222222
ps_ast_assignment *ps_ast_create_assignment (uint16_t line, uint16_t column, ps_ast_node *lvalue,ps_ast_node *expression );
223-
ps_ast_if *ps_ast_create_if (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_ast_node *condition, ps_ast_statement_list *then_branch, ps_ast_statement_list *else_branch );
224-
ps_ast_while *ps_ast_create_while (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_ast_node *condition, ps_ast_statement_list *body );
223+
ps_ast_if *ps_ast_create_if (uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_statement_list *then_branch, ps_ast_statement_list *else_branch );
224+
ps_ast_while *ps_ast_create_while (uint16_t line, uint16_t column, ps_ast_node *condition, ps_ast_statement_list *body );
225225
ps_ast_repeat *ps_ast_create_repeat (uint16_t line, uint16_t column, ps_ast_statement_list *body, ps_ast_node *condition );
226-
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 );
226+
ps_ast_for *ps_ast_create_for (uint16_t line, uint16_t column, ps_ast_variable_simple *variable, ps_ast_node *start, ps_ast_node *end, int step, ps_ast_statement_list *body );
227227
ps_ast_call *ps_ast_create_call (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *executable, size_t n_args, ps_ast_argument *args[] );
228228
ps_ast_unary_operation *ps_ast_create_unary_operation (uint16_t line, uint16_t column, ps_operator_unary operator, ps_ast_node * operand );
229229
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 );
230230
ps_ast_value *ps_ast_create_rvalue_const (uint16_t line, uint16_t column, ps_value value );
231231
ps_ast_variable_simple *ps_ast_create_variable_simple (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *variable );
232-
ps_ast_variable_array *ps_ast_create_variable_array (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *symbol, size_t n_indexes, ps_ast_node *indexes );
232+
ps_ast_variable_array *ps_ast_create_variable_array (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_symbol *symbol, size_t n_indexes, ps_ast_node **indexes );
233+
ps_ast_argument *ps_ast_create_argument (uint16_t line, uint16_t column, ps_ast_node_kind kind, ps_ast_node *arg );
233234
// clang-format on
234235

235236
/** @brief Free an AST node and all its children */

include/ps_ast_debug.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,9 @@ extern "C"
2727

2828
/** @brief Print debug message to stderr (printf-style) */
2929
void ps_ast_debug_line(const char *format, ...); // NOSONAR
30-
/** @brief Debug print information about a program node */
31-
void ps_ast_debug_program(ps_ast_node *node);
32-
/** @brief Debug print information about a procedure node */
33-
void ps_ast_debug_procedure(ps_ast_node *node);
34-
/** @brief Debug print information about a function node */
35-
void ps_ast_debug_function(ps_ast_node *node);
36-
/** @brief Debug print information about a unit node */
37-
void ps_ast_debug_unit(ps_ast_node *node);
38-
/** @brief Debug print information about a statement list */
39-
void ps_ast_debug_statement_list(ps_ast_node *node);
40-
/** @brief Debug print information about an assignment node */
41-
void ps_ast_debug_assignment(ps_ast_node *node);
42-
/** @brief Debug print information about an if statement node */
43-
void ps_ast_debug_if(ps_ast_node *node);
30+
4431
/** @brief Debug print information about any AST node */
45-
void ps_ast_debug_node(ps_ast_node *node);
32+
void ps_ast_debug_node(const ps_ast_node *node);
4633

4734
#ifdef __cplusplus
4835
}

include/ps_ast_execute.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ extern "C"
1919
/** @brief Check if an AST node has a specific kind */
2020
bool ps_ast_check_kind(const ps_ast_node *node, ps_ast_node_kind expected_kind);
2121
/** @brief Run a Pascal program */
22-
bool ps_ast_run_program(ps_interpreter *interpreter, ps_ast_node *program);
22+
bool ps_ast_run_program(ps_interpreter *interpreter, ps_ast_block *program);
2323
/** @brief Run a Pascal procedure */
24-
bool ps_ast_run_procedure(ps_interpreter *interpreter, ps_ast_node *procedure);
24+
bool ps_ast_run_procedure(ps_interpreter *interpreter, ps_ast_block *procedure);
2525
/** @brief Run a Pascal function */
26-
bool ps_ast_run_function(ps_interpreter *interpreter, ps_ast_node *function);
26+
bool ps_ast_run_function(ps_interpreter *interpreter, ps_ast_block *function);
2727
/** @brief Run a Pascal block */
28-
bool ps_ast_run_block(ps_interpreter *interpreter, ps_ast_node *node);
28+
bool ps_ast_run_block(ps_interpreter *interpreter, ps_ast_block *block);
2929
/** @brief Run a list of Pascal statements */
3030
bool ps_ast_run_statement_list(ps_interpreter *interpreter, ps_ast_statement_list *statement_list);
3131
/** @brief Run a Pascal statement */

include/ps_ast_test.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_AST_TEST_H
8+
#define _PS_AST_TEST_H
9+
10+
#ifdef __cplusplus
11+
extern "C"
12+
{
13+
#endif
14+
15+
bool ps_ast_test_minimal();
16+
bool ps_ast_test_hello();
17+
18+
#ifdef __cplusplus
19+
}
20+
#endif
21+
22+
#endif /* _PS_AST_TEST_H */

include/ps_interpreter.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,6 @@ extern "C"
103103
*/
104104
bool ps_interpreter_copy_value(ps_interpreter *interpreter, ps_value *from, ps_value *to);
105105

106-
/** @brief Load source code from string */
107-
bool ps_interpreter_load_string(ps_interpreter *interpreter, char *source, size_t length);
108-
109-
/** @brief Load source code from file */
110-
bool ps_interpreter_load_file(ps_interpreter *interpreter, const char *filename);
111-
112-
/** @brief Run the interpreter on the loaded source code, with option to execute or just parse */
113-
bool ps_interpreter_run(ps_interpreter *interpreter, bool exec);
114-
115106
#ifdef __cplusplus
116107
}
117108
#endif

include/ps_operator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "ps_interpreter.h"
1313
#include "ps_value.h"
14+
#include "ps_token.h"
1415

1516
#ifdef __cplusplus
1617
extern "C"

include/ps_parse.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ extern "C"
3434
bool ps_parse_variable_reference(ps_compiler *compiler, ps_symbol **variable);
3535

3636
/* src/ps_parse_expression.c */
37-
bool ps_parse_and_expression(ps_compiler *compiler, ps_value *result);
3837
bool ps_parse_expression(ps_compiler *compiler, ps_value *result);
39-
bool ps_parse_factor(ps_compiler *compiler, ps_value *result);
40-
bool ps_parse_function_call(ps_compiler *compiler, ps_symbol *function, ps_value *result);
41-
bool ps_parse_or_expression(ps_compiler *compiler, ps_value *result);
4238
bool ps_parse_relational_expression(ps_compiler *compiler, ps_value *result);
39+
bool ps_parse_and_expression(ps_compiler *compiler, ps_value *result);
40+
bool ps_parse_or_expression(ps_compiler *compiler, ps_value *result);
4341
bool ps_parse_simple_expression(ps_compiler *compiler, ps_value *result);
4442
bool ps_parse_term(ps_compiler *compiler, ps_value *result);
43+
bool ps_parse_factor(ps_compiler *compiler, ps_value *result);
4544
bool ps_parse_constant_expression(ps_compiler *compiler, ps_value *constant);
45+
bool ps_parse_function_call(ps_compiler *compiler, ps_symbol *function, ps_value *result);
4646

4747
/* src/ps_parse_statement.c */
4848
bool ps_parse_assignment_or_procedure_call(ps_compiler *compiler);
@@ -129,7 +129,7 @@ extern "C"
129129
ps_token_type_dump_value(__PS_TOKEN_TYPE__, "UNKNOWN")); \
130130
ps_token_debug(stderr, "NEXT", &lexer->current_token); \
131131
} \
132-
ps_compiler_set_message(compiler, "Expected '%s'", ps_token_get_keyword(__PS_TOKEN_TYPE__)); \
132+
ps_compiler_set_message(compiler, "Expected '%s'", ps_token_get_keyword(__PS_TOKEN_TYPE__)); \
133133
goto cleanup; \
134134
}
135135

@@ -143,7 +143,7 @@ extern "C"
143143
fprintf(stderr, "RETURN\t%-32s %-8d ", visit, __PS_ERROR__); \
144144
ps_token_debug(stderr, "RETURN", &lexer->current_token); \
145145
} \
146-
return ps_compiler_return_false(compiler, __PS_ERROR__); \
146+
return ps_compiler_return_false(compiler, __PS_ERROR__); \
147147
}
148148

149149
#define GOTO_CLEANUP(__PS_ERROR__) \

include/ps_procedures.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ 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;
22+
// extern ps_symbol ps_system_procedure_dec;
23+
// extern ps_symbol ps_system_procedure_inc;
2424
extern ps_symbol ps_system_procedure_randomize;
2525
extern ps_symbol ps_system_procedure_read;
2626
extern ps_symbol ps_system_procedure_readln;
2727
extern ps_symbol ps_system_procedure_write;
2828
extern ps_symbol ps_system_procedure_writeln;
2929

3030
/* clang-format off */
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 );
31+
// bool ps_procedure_dec (ps_interpreter *interpreter, ps_value *value );
32+
// bool ps_procedure_inc (ps_interpreter *interpreter, ps_value *value );
3333
bool ps_procedure_randomize(ps_interpreter *interpreter, const ps_value *value );
3434
bool ps_procedure_read (ps_interpreter *interpreter, FILE *f, ps_value *value );
3535
bool ps_procedure_readln (ps_interpreter *interpreter, FILE *f, ps_value *value );

include/ps_signature.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ extern "C"
4747
ps_formal_signature *ps_formal_signature_free(ps_formal_signature *signature);
4848

4949
/** @brief Find a parameter in a formal signature by name */
50-
ps_formal_parameter *ps_formal_signature_find_parameter(ps_formal_signature *signature, char *name);
50+
ps_formal_parameter *ps_formal_signature_find_parameter(ps_formal_signature *signature, const char *name);
5151

5252
/** @brief Add parameter to formal signature */
53-
bool ps_formal_signature_add_parameter(ps_formal_signature *signature, bool byref, char *name,
54-
ps_symbol *type);
53+
bool ps_formal_signature_add_parameter(ps_formal_signature *signature, bool byref, const char *name, ps_symbol *type);
5554

5655
/** @brief Dump formal signature */
5756
void ps_formal_signature_dump(FILE *output, char *message, ps_formal_signature *signature);

0 commit comments

Comments
 (0)