Skip to content

Commit 3e115be

Browse files
committed
AST WIP
1 parent faac90f commit 3e115be

6 files changed

Lines changed: 22 additions & 32 deletions

File tree

include/ps_ast.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,14 @@ extern "C"
7979
/** @details Units may be separated as they are special cases with interface and implementation */
8080
typedef struct s_ps_ast_block
8181
{
82-
PS_AST_NODE_COMMON ps_identifier name; /** @brief Every block has a name */
83-
struct s_ps_ast_block *parent; /** @brief Parent block, NULL for PROGRAM */
84-
size_t n_vars; /** @brief Number of variables to allocate at startup */
85-
ps_symbol_table *symbols; /** @brief Constants, types, variables, procedures and functions */
86-
size_t n_executables; /** @brief exactly 1 for procedure and function, 0 or more otherwise */
87-
struct s_ps_ast_block **executables; /** @brief declarations of procedures and functions */
88-
ps_ast_statement_list *statement_list; /** @brief Statements for this block */
89-
ps_formal_signature *signature; /** @brief Only for procedures and functions, NULL otherwise */
90-
ps_symbol *result_type; /** @brief Only for functions, NULL otherwise */
82+
PS_AST_NODE_COMMON
83+
ps_identifier name; /** @brief Every block has a name */
84+
struct s_ps_ast_block *parent; /** @brief Parent block, NULL for PROGRAM */
85+
size_t n_vars; /** @brief Number of variables to allocate at startup */
86+
ps_symbol_table *symbols; /** @brief Constants, types, variables, procedures and functions */
87+
ps_ast_statement_list *statement_list; /** @brief Statements for this block */
88+
ps_formal_signature *signature; /** @brief Only for procedures and functions, NULL otherwise */
89+
ps_symbol *result_type; /** @brief Only for functions, NULL otherwise */
9190
} ps_ast_block;
9291

9392
/** @brief IF statement */

include/ps_compiler.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern "C"
3333
COMPILER_DEBUG_NONE, /** @brief No debug */
3434
COMPILER_DEBUG_TRACE, /** @brief Output messages to stderr */
3535
COMPILER_DEBUG_VERBOSE, /** @brief More traces */
36-
} ps_compiler_debug;
36+
} __attribute__((__packed__)) ps_compiler_debug;
3737

3838
typedef struct s_ps_compiler
3939
{
@@ -46,9 +46,6 @@ extern "C"
4646
bool range_check; /** @brief Range checking for integer and real values */
4747
bool bool_eval; /** @brief *FUTURE* Short circuit boolean evaluation */
4848
bool io_check; /** @brief *FUTURE* stop or set IOResult on I/O error */
49-
ps_environment
50-
*environments[PS_COMPILER_ENVIRONMENTS]; /** @brief Environments with enough levels for some recursion */
51-
ps_ast_node *ast; /** @brief AST */
5249
} /*__attribute__((__packed__))*/ ps_compiler;
5350

5451
#define PS_COMPILER_SIZEOF sizeof(ps_compiler)

include/ps_parse_declaration.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ extern "C"
1717
{
1818
#endif
1919

20-
/* src/ps_parse_declaration.c */
21-
ps_ast_node *ps_parse_program(ps_compiler *compiler);
22-
ps_ast_node *ps_parse_uses(ps_compiler *compiler);
23-
ps_ast_node *ps_parse_const(ps_compiler *compiler);
24-
ps_ast_node *ps_parse_type(ps_compiler *compiler);
25-
ps_ast_node *ps_parse_var(ps_compiler *compiler);
26-
ps_ast_node *ps_parse_block(ps_compiler *compiler);
20+
// clang-format off
21+
bool ps_parse_program(ps_compiler *compiler, ps_ast_block *block);
22+
bool ps_parse_uses (ps_compiler *compiler, ps_ast_block *block);
23+
bool ps_parse_const (ps_compiler *compiler, ps_ast_block *block);
24+
bool ps_parse_type (ps_compiler *compiler, ps_ast_block *block);
25+
bool ps_parse_var (ps_compiler *compiler, ps_ast_block *block);
26+
bool ps_parse_block (ps_compiler *compiler, ps_ast_block *block);
27+
// clang-format on
2728

2829
#ifdef __cplusplus
2930
}

src/ps_ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ ps_ast_block *ps_ast_create_block(uint16_t line, uint16_t column, ps_ast_block *
113113
return NULL;
114114
snprintf(block->name, PS_IDENTIFIER_LEN, "%s", name);
115115
block->parent = parent;
116-
block->symbols = NULL;
116+
block->symbols = ps_symbol_table_alloc(0, 0);
117117
block->signature = NULL;
118118
block->result_type = NULL;
119119
block->n_vars = 0;

src/ps_ast_test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ ps_ast_block *ps_ast_test_create_block_program(const char *name)
6767
ASSERT_GOTO_CLEANUP(block_program->column == 1);
6868
ASSERT_GOTO_CLEANUP(strcmp(block_program->name, name) == 0);
6969
ASSERT_GOTO_CLEANUP(block_program->n_vars == 0);
70-
ASSERT_GOTO_CLEANUP(block_program->n_executables == 0);
71-
ASSERT_GOTO_CLEANUP(block_program->executables == NULL);
7270
ASSERT_GOTO_CLEANUP(block_program->statement_list == NULL);
7371
ASSERT_GOTO_CLEANUP(block_program->signature == NULL);
7472
ASSERT_GOTO_CLEANUP(block_program->result_type == NULL);

src/ps_parse.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,26 @@
66

77
#include <string.h>
88

9+
#include "ps_ast.h"
910
#include "ps_compiler.h"
10-
#include "ps_functions.h"
1111
#include "ps_parse.h"
1212
#include "ps_parse_declaration.h"
13-
#include "ps_parser.h"
14-
#include "ps_procedures.h"
15-
#include "ps_string.h"
16-
#include "ps_system.h"
1713

1814
/**
1915
* Parse
2016
* PROGRAM IDENTIFIER [ '(' [ IDENTIFIER { ',' IDENTIFIER } ] ')' ] ';'
2117
* BLOCK
2218
* '.'
2319
*/
24-
ps_ast_node *ps_parse_start(ps_compiler *compiler)
20+
ps_ast_block *ps_parse_start(ps_compiler *compiler, ps_ast_block *block_program)
2521
{
2622
PARSE_BEGIN("START", "")
2723

28-
READ_NEXT_TOKEN
24+
READ_NEXT_TOKEN()
2925
switch (lexer->current_token.type)
3026
{
3127
case PS_TOKEN_PROGRAM:
32-
ast = ps_parse_program(compiler);
33-
if (NULL == ast)
28+
if (!ps_parse_program(compiler, block_program))
3429
TRACE_ERROR("PROGRAM")
3530
break;
3631
case PS_TOKEN_UNIT:

0 commit comments

Comments
 (0)