Skip to content

Commit 46ece2a

Browse files
committed
AST WIP
1 parent d65d9a6 commit 46ece2a

8 files changed

Lines changed: 60 additions & 65 deletions

File tree

include/ps_executable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ extern "C"
6262

6363
ps_executable *ps_executable_alloc(ps_executable_kind kind, ps_ast_block *block);
6464
ps_executable *ps_executable_free(ps_executable *executable);
65+
char *ps_executable_get_kind_name(ps_executable_kind kind);
6566
void ps_executable_debug(FILE *output, char *message, ps_executable *executable);
6667

6768
#ifdef __cplusplus

include/ps_system.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,16 @@ extern "C"
2323
#define PS_SYSTEM_SYMBOL_TABLE_MORE 16
2424
#endif
2525

26-
#define PS_SYSTEM_FUNCTION(TYPE, VALUE, NAME, CALLABLE_FIELD, CALLABLE) \
27-
ps_executable ps_executable_##TYPE##_##VALUE = {CALLABLE_FIELD = CALLABLE, .formal_signature = NULL, .line = 0, \
28-
.column = 0}; \
26+
#define PS_SYSTEM_FUNCTION(TYPE, VALUE, NAME, KIND, CALLABLE_FIELD, CALLABLE) \
27+
ps_executable ps_executable_##TYPE##_##VALUE = {.kind = KIND, CALLABLE_FIELD = CALLABLE}; \
2928
ps_value ps_value_##TYPE##_##VALUE = {.type = &ps_system_##TYPE, .data = {.x = &ps_executable_##TYPE##_##VALUE}}; \
3029
ps_symbol ps_system_##TYPE##_##VALUE = {.kind = PS_SYMBOL_KIND_FUNCTION, \
3130
.name = NAME, \
3231
.value = &ps_value_##TYPE##_##VALUE, \
3332
.system = true, \
3433
.allocated = false}
35-
#define PS_SYSTEM_PROCEDURE(TYPE, VALUE, NAME, CALLABLE_FIELD, CALLABLE) \
36-
ps_executable ps_executable_##TYPE##_##VALUE = {CALLABLE_FIELD = CALLABLE, .formal_signature = NULL, .line = 0, \
37-
.column = 0}; \
34+
#define PS_SYSTEM_PROCEDURE(TYPE, VALUE, NAME, KIND, CALLABLE_FIELD, CALLABLE) \
35+
ps_executable ps_executable_##TYPE##_##VALUE = {.kind = KIND, CALLABLE_FIELD = CALLABLE}; \
3836
ps_value ps_value_##TYPE##_##VALUE = {.type = &ps_system_##TYPE, .data = {.x = &ps_executable_##TYPE##_##VALUE}}; \
3937
ps_symbol ps_system_##TYPE##_##VALUE = {.kind = PS_SYMBOL_KIND_PROCEDURE, \
4038
.name = NAME, \

src/ps_functions.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,37 @@
2525

2626
/* clang-format off */
2727
// Math
28-
PS_SYSTEM_FUNCTION (function , abs , "ABS" , .func_1arg , &ps_function_abs );
29-
PS_SYSTEM_FUNCTION (function , arctan , "ARCTAN" , .func_1arg , &ps_function_arctan );
30-
PS_SYSTEM_FUNCTION (function , cos , "COS" , .func_1arg , &ps_function_cos );
31-
PS_SYSTEM_FUNCTION (function , even , "EVEN" , .func_1arg , &ps_function_even );
32-
PS_SYSTEM_FUNCTION (function , exp , "EXP" , .func_1arg , &ps_function_exp );
33-
PS_SYSTEM_FUNCTION (function , frac , "FRAC" , .func_1arg , &ps_function_frac );
34-
PS_SYSTEM_FUNCTION (function , int , "INT" , .func_1arg , &ps_function_int );
35-
PS_SYSTEM_FUNCTION (function , ln , "LN" , .func_1arg , &ps_function_ln );
36-
PS_SYSTEM_FUNCTION (function , log , "LOG" , .func_1arg , &ps_function_log );
37-
PS_SYSTEM_FUNCTION (function , odd , "ODD" , .func_1arg , &ps_function_odd );
38-
PS_SYSTEM_FUNCTION (function , power , "POWER" , .func_2args , &ps_function_power );
39-
PS_SYSTEM_FUNCTION (function , random , "RANDOM" , .func_1arg , &ps_function_random );
40-
PS_SYSTEM_FUNCTION (function , round , "ROUND" , .func_1arg , &ps_function_round );
41-
PS_SYSTEM_FUNCTION (function , sin , "SIN" , .func_1arg , &ps_function_sin );
42-
PS_SYSTEM_FUNCTION (function , sqr , "SQR" , .func_1arg , &ps_function_sqr );
43-
PS_SYSTEM_FUNCTION (function , sqrt , "SQRT" , .func_1arg , &ps_function_sqrt );
44-
PS_SYSTEM_FUNCTION (function , succ , "SUCC" , .func_1arg , &ps_function_succ );
45-
PS_SYSTEM_FUNCTION (function , tan , "TAN" , .func_1arg , &ps_function_tan );
46-
PS_SYSTEM_FUNCTION (function , trunc , "TRUNC" , .func_1arg , &ps_function_trunc );
28+
PS_SYSTEM_FUNCTION (function, abs , "ABS" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_abs );
29+
PS_SYSTEM_FUNCTION (function, arctan , "ARCTAN" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_arctan );
30+
PS_SYSTEM_FUNCTION (function, cos , "COS" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_cos );
31+
PS_SYSTEM_FUNCTION (function, even , "EVEN" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_even );
32+
PS_SYSTEM_FUNCTION (function, exp , "EXP" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_exp );
33+
PS_SYSTEM_FUNCTION (function, frac , "FRAC" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_frac );
34+
PS_SYSTEM_FUNCTION (function, int , "INT" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_int );
35+
PS_SYSTEM_FUNCTION (function, ln , "LN" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_ln );
36+
PS_SYSTEM_FUNCTION (function, log , "LOG" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_log );
37+
PS_SYSTEM_FUNCTION (function, odd , "ODD" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_odd );
38+
PS_SYSTEM_FUNCTION (function, power , "POWER" , PS_EXECUTABLE_FUNC_1ARG_S, .func_2args , &ps_function_power );
39+
PS_SYSTEM_FUNCTION (function, random , "RANDOM" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_random );
40+
PS_SYSTEM_FUNCTION (function, round , "ROUND" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_round );
41+
PS_SYSTEM_FUNCTION (function, sin , "SIN" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_sin );
42+
PS_SYSTEM_FUNCTION (function, sqr , "SQR" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_sqr );
43+
PS_SYSTEM_FUNCTION (function, sqrt , "SQRT" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_sqrt );
44+
PS_SYSTEM_FUNCTION (function, succ , "SUCC" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_succ );
45+
PS_SYSTEM_FUNCTION (function, tan , "TAN" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_tan );
46+
PS_SYSTEM_FUNCTION (function, trunc , "TRUNC" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_trunc );
4747
// Ordinal
48-
PS_SYSTEM_FUNCTION (function , chr , "CHR" , .func_1arg , &ps_function_chr );
49-
PS_SYSTEM_FUNCTION (function , high , "HIGH" , .func_1arg_s , &ps_function_high );
50-
PS_SYSTEM_FUNCTION (function , low , "LOW" , .func_1arg_s , &ps_function_low );
51-
PS_SYSTEM_FUNCTION (function , ord , "ORD" , .func_1arg , &ps_function_ord );
52-
PS_SYSTEM_FUNCTION (function , pred , "PRED" , .func_1arg , &ps_function_pred );
48+
PS_SYSTEM_FUNCTION (function, chr , "CHR" , PS_EXECUTABLE_FUNC_1ARG, .func_1arg , &ps_function_chr );
49+
PS_SYSTEM_FUNCTION (function, high , "HIGH" , PS_EXECUTABLE_FUNC_1ARG_S, .func_1arg_s, &ps_function_high );
50+
PS_SYSTEM_FUNCTION (function, low , "LOW" , PS_EXECUTABLE_FUNC_1ARG_S, .func_1arg_s, &ps_function_low );
51+
PS_SYSTEM_FUNCTION (function, ord , "ORD" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_ord );
52+
PS_SYSTEM_FUNCTION (function, pred , "PRED" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_pred );
5353
// String
54-
PS_SYSTEM_FUNCTION (function , length , "LENGTH" , .func_1arg , &ps_function_length );
55-
PS_SYSTEM_FUNCTION (function , lowercase , "LOWERCASE" , .func_1arg , &ps_function_lowercase );
56-
PS_SYSTEM_FUNCTION (function , uppercase , "UPPERCASE" , .func_1arg , &ps_function_uppercase );
54+
PS_SYSTEM_FUNCTION (function, length , "LENGTH" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_length );
55+
PS_SYSTEM_FUNCTION (function, lowercase , "LOWERCASE" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_lowercase );
56+
PS_SYSTEM_FUNCTION (function, uppercase , "UPPERCASE" , PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_uppercase );
5757
// System
58-
PS_SYSTEM_FUNCTION (function , get_tick_count, "GETTICKCOUNT", .func_1arg , &ps_function_get_tick_count );
58+
PS_SYSTEM_FUNCTION (function, get_tick_count, "GETTICKCOUNT", PS_EXECUTABLE_FUNC_1ARG , .func_1arg , &ps_function_get_tick_count);
5959
/* clang-format on */
6060

6161
bool ps_functions_init(ps_symbol_table *system)
@@ -282,7 +282,7 @@ ps_error ps_function_low_or_high_subrange(const ps_symbol *type, ps_value *resul
282282
result->data.u = low ? type_def->def.g.u.min : type_def->def.g.u.max;
283283
break;
284284
case PS_TYPE_ENUM:
285-
result->type = type;
285+
result->type = (ps_symbol *)type;
286286
result->data.u = low ? type_def->def.g.e.min : type_def->def.g.e.max;
287287
break;
288288
default:

src/ps_parse_declaration.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,21 @@ bool ps_parse_type(ps_compiler *compiler, ps_ast_block *block)
308308
(void)start_line;
309309
(void)start_column;
310310

311-
EXPECT_TOKEN(PS_TOKEN_TYPE);
312-
READ_NEXT_TOKEN
313-
314-
if (lexer->current_token.type != PS_TOKEN_IDENTIFIER)
315-
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
316-
do
317-
{
318-
if (!ps_parse_type_definition(compiler, block))
319-
TRACE_ERROR("TYPE_DEFINITION");
320-
EXPECT_TOKEN(PS_TOKEN_SEMI_COLON);
321-
READ_NEXT_TOKEN
322-
} while (lexer->current_token.type == PS_TOKEN_IDENTIFIER);
323-
324-
PARSE_END("OK")
311+
RETURN_ERROR(PS_ERROR_NOT_IMPLEMENTED)
312+
// EXPECT_TOKEN(PS_TOKEN_TYPE);
313+
// READ_NEXT_TOKEN
314+
315+
// if (lexer->current_token.type != PS_TOKEN_IDENTIFIER)
316+
// RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
317+
// do
318+
// {
319+
// if (!ps_parse_type_definition(compiler, block))
320+
// TRACE_ERROR("TYPE_DEFINITION");
321+
// EXPECT_TOKEN(PS_TOKEN_SEMI_COLON);
322+
// READ_NEXT_TOKEN
323+
// } while (lexer->current_token.type == PS_TOKEN_IDENTIFIER);
324+
325+
// PARSE_END("OK")
325326
}
326327

327328
/**

src/ps_parse_expression.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ bool ps_parse_factor_identifier(ps_compiler *compiler, ps_ast_block *block, cons
353353
else
354354
{
355355
// factor.type = symbol->value->type;
356-
*factor = ps_ast_create_variable_simple(start_line, start_column, PS_AST_RVALUE_SIMPLE, symbol);
356+
*factor =
357+
(ps_ast_node *)ps_ast_create_variable_simple(start_line, start_column, PS_AST_RVALUE_SIMPLE, symbol);
357358
if (*factor == NULL)
358359
RETURN_ERROR(PS_ERROR_OUT_OF_MEMORY)
359360
READ_NEXT_TOKEN

src/ps_parse_statement.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "ps_parse_statement.h"
88
#include "ps_array.h"
9+
#include "ps_ast.h"
910
#include "ps_functions.h"
1011
#include "ps_parse.h"
1112
#include "ps_parse_expression.h"
@@ -364,7 +365,7 @@ bool ps_parse_write_or_writeln(ps_compiler *compiler, ps_ast_block *block, ps_as
364365
loop = false;
365366
}
366367

367-
*call = ps_ast_create_call(lexer->start_line, lexer->start_column, PS_AST_PROCEDURE_CALL,
368+
*call = ps_ast_create_call(start_line, start_column, PS_AST_PROCEDURE_CALL,
368369
newline ? &ps_system_procedure_writeln : &ps_system_procedure_write, n_args,
369370
n_args > 0 ? args : NULL, widths, precisions);
370371
if (*call == NULL)
@@ -394,7 +395,7 @@ bool ps_parse_assignment_or_procedure_call(ps_compiler *compiler, ps_ast_block *
394395
if (symbol != NULL && symbol->kind == PS_SYMBOL_KIND_FUNCTION && strcmp((char *)identifier, block->name) == 0)
395396
{
396397
if (compiler->debug >= COMPILER_DEBUG_VERBOSE)
397-
fprintf(stderr, "%cINFO\tAssignment to current function '%s' as Result\n", (char *)identifier);
398+
fprintf(stderr, "INFO\tAssignment to current function '%s' as Result\n", (char *)identifier);
398399
// Assign to the not so implicit "Result" local variable
399400
symbol = ps_compiler_find_symbol(compiler, block, result_identifier, false);
400401
}

src/ps_procedures.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
/* clang-format off */
2525
// PS_SYSTEM_PROCEDURE(procedure, dec , "DEC" , .proc_1arg , &ps_procedure_dec );
2626
// PS_SYSTEM_PROCEDURE(procedure, inc , "INC" , .proc_1arg , &ps_procedure_inc );
27-
PS_SYSTEM_PROCEDURE(procedure, randomize, "RANDOMIZE", .proc_1arg , &ps_procedure_randomize);
28-
PS_SYSTEM_PROCEDURE(procedure, read , "READ" , .proc_file_read , &ps_procedure_read );
29-
PS_SYSTEM_PROCEDURE(procedure, readln , "READLN" , .proc_file_read , &ps_procedure_readln );
30-
PS_SYSTEM_PROCEDURE(procedure, write , "WRITE" , .proc_file_write, &ps_procedure_write );
31-
PS_SYSTEM_PROCEDURE(procedure, writeln , "WRITELN" , .proc_file_write, &ps_procedure_writeln );
27+
PS_SYSTEM_PROCEDURE(procedure, randomize, "RANDOMIZE", PS_EXECUTABLE_PROC_1ARG , .proc_1arg , &ps_procedure_randomize);
28+
PS_SYSTEM_PROCEDURE(procedure, read , "READ" , PS_EXECUTABLE_PROC_FILE_READ , .proc_file_read , &ps_procedure_read );
29+
PS_SYSTEM_PROCEDURE(procedure, readln , "READLN" , PS_EXECUTABLE_PROC_FILE_READ , .proc_file_read , &ps_procedure_readln );
30+
PS_SYSTEM_PROCEDURE(procedure, write , "WRITE" , PS_EXECUTABLE_PROC_FILE_WRITE, .proc_file_write, &ps_procedure_write );
31+
PS_SYSTEM_PROCEDURE(procedure, writeln , "WRITELN" , PS_EXECUTABLE_PROC_FILE_WRITE, .proc_file_write, &ps_procedure_writeln );
3232
/* clang-format on */
3333

3434
bool ps_procedures_init(ps_environment *system)

src/ps_value.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,8 @@ char *ps_value_get_enum(const ps_value *value)
435435
const ps_executable *executable = value->data.x; \
436436
if (executable == NULL) \
437437
snprintf(buffer, sizeof(buffer) - 1, "NULL!"); \
438-
else if (executable->address != NULL) \
439-
snprintf(buffer, sizeof(buffer) - 1, "SYSTEM@%p", executable->address); \
440438
else \
441-
snprintf(buffer, sizeof(buffer) - 1, "%s@L:%05d/C:%03d", \
442-
executable->formal_signature->result_type == NULL || \
443-
executable->formal_signature->result_type == &ps_system_none \
444-
? "PROCEDURE" \
445-
: "FUNCTION", \
446-
executable->line + 1, executable->column + 1);
439+
snprintf(buffer, sizeof(buffer) - 1, "%s@%p", ps_executable_get_kind_name(executable), executable->address);
447440

448441
#define ARRAY_VALUE \
449442
if (debug) \

0 commit comments

Comments
 (0)