Skip to content

Commit 2685eb8

Browse files
committed
AST WIP
1 parent 6664e57 commit 2685eb8

3 files changed

Lines changed: 30 additions & 28 deletions

File tree

src/ps_parse_executable.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,14 @@ bool ps_parse_procedure_or_function_declaration(ps_compiler *compiler, ps_ast_bl
261261
ps_ast_block **block_executable, ps_symbol_kind kind)
262262
{
263263
PARSE_BEGIN("EXECUTABLE", "PROCEDURE_OR_FUNCTION");
264+
(void)start_line;
265+
(void)start_column;
264266

265267
ps_identifier identifier;
266268
ps_symbol *executable_symbol = NULL;
267269
ps_value *value = NULL;
268270
ps_formal_signature *signature = NULL;
269271
ps_executable *executable = NULL;
270-
uint16_t line = 0;
271-
uint16_t column = 0;
272-
bool has_environment = false;
273272
ps_symbol *result_symbol = NULL;
274273
ps_value *result_value = NULL;
275274
bool result_symbol_added = false;
@@ -434,8 +433,8 @@ bool ps_parse_procedure_or_function_declaration(ps_compiler *compiler, ps_ast_bl
434433
cleanup:
435434
if (compiler->debug >= COMPILER_DEBUG_VERBOSE)
436435
fprintf(stderr, "INFO\tPROCEDURE_OR_FUNCTION: CLEANUP\n");
437-
if (has_environment)
438-
ps_compiler_exit_environment(compiler);
436+
// if (has_environment)
437+
// ps_compiler_exit_environment(compiler);
439438
if (compiler->debug >= COMPILER_DEBUG_VERBOSE)
440439
fprintf(stderr, "DEBUG\texecutable_symbol: %p%s\n", (void *)executable_symbol,
441440
executable_symbol_added ? " (added)" : " (not added)");

src/ps_parse_expression.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ bool ps_parse_factor(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **e
388388
PARSE_BEGIN("FACTOR", "");
389389

390390
ps_value factor_value = {.type = &ps_system_none, .data.v = NULL};
391-
ps_token_type unary_operator;
391+
ps_token_type unary_operator = PS_TOKEN_NONE;
392392

393393
switch (lexer->current_token.type)
394394
{
@@ -458,8 +458,8 @@ bool ps_parse_factor(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **e
458458
ps_ast_node **operand = NULL;
459459
if (!ps_parse_factor(compiler, block, operand))
460460
TRACE_ERROR("UNARY");
461-
ps_operator_unary unary_operator = ps_operator_unary_from_token(lexer->current_token.type);
462-
*expression = (ps_ast_node *)ps_ast_create_unary_operation(start_line, start_column, unary_operator, *operand);
461+
ps_operator_unary operator_unary = ps_operator_unary_from_token(unary_operator);
462+
*expression = (ps_ast_node *)ps_ast_create_unary_operation(start_line, start_column, operator_unary, *operand);
463463
if (*expression == NULL)
464464
RETURN_ERROR(PS_ERROR_OUT_OF_MEMORY)
465465
PARSE_END("OK UNARY")
@@ -477,13 +477,14 @@ bool ps_parse_factor(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **e
477477
PARSE_END("OK")
478478
}
479479

480-
bool ps_parse_function_call_random(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **expression, int *arg_count,
481-
ps_ast_node **arg1)
480+
bool ps_parse_function_call_random(ps_compiler *compiler, ps_ast_block *block, int *n_args, ps_ast_node **arg)
482481
{
483482
PARSE_BEGIN("FUNCTION_CALL", "RANDOM");
483+
(void)start_line;
484+
(void)start_column;
484485

485486
// Random function can be called with 3 signatures:
486-
// 1. Random or Random() => Real from 0.0 to 1.0 excluded
487+
// 1. Random or Random() => Real from 0.0 (included) to 1.0 (excluded)
487488
// 2. Random(Integer) => Integer
488489
// 3. Random(Unsigned) => Unsigned
489490
if (lexer->current_token.type == PS_TOKEN_LEFT_PARENTHESIS)
@@ -492,25 +493,25 @@ bool ps_parse_function_call_random(ps_compiler *compiler, ps_ast_block *block, p
492493
READ_NEXT_TOKEN
493494
if (lexer->current_token.type == PS_TOKEN_RIGHT_PARENTHESIS)
494495
{
495-
*arg_count = 0;
496+
*n_args = 0;
496497
// factor.type = &ps_system_real;
497498
READ_NEXT_TOKEN
498499
}
499500
else
500501
{
501-
*arg_count = 1;
502-
if (!ps_parse_expression(compiler, block, arg1))
502+
*n_args = 1;
503+
if (!ps_parse_expression(compiler, block, arg))
503504
TRACE_ERROR("PARAMETER");
504-
// if (arg1->type != &ps_system_integer && arg1->type != &ps_system_unsigned)
505+
// if (arg->type != &ps_system_integer && arg->type != &ps_system_unsigned)
505506
// RETURN_ERROR(PS_ERROR_UNEXPECTED_TYPE);
506507
EXPECT_TOKEN(PS_TOKEN_RIGHT_PARENTHESIS);
507-
// factor.type = arg1->type;
508+
// factor.type = arg->type;
508509
READ_NEXT_TOKEN
509510
}
510511
}
511512
else
512513
{
513-
*arg_count = 0;
514+
*n_args = 0;
514515
// factor.type = &ps_system_real;
515516
}
516517

@@ -520,14 +521,17 @@ bool ps_parse_function_call_random(ps_compiler *compiler, ps_ast_block *block, p
520521
bool ps_parse_function_call_low_high(ps_compiler *compiler, ps_ast_block *block, ps_symbol **symbol)
521522
{
522523
PARSE_BEGIN("FUNCTION_CALL", "LOW_HIGH")
524+
(void)start_line;
525+
(void)start_column;
526+
527+
ps_identifier identifier = {0};
523528

524529
// Low and High functions have one "symbolic" argument, i.e. Low(Days) or High(Day)
525530
EXPECT_TOKEN(PS_TOKEN_LEFT_PARENTHESIS)
526531
READ_NEXT_TOKEN
527532
if (lexer->current_token.type != PS_TOKEN_IDENTIFIER && lexer->current_token.type != PS_TOKEN_INTEGER &&
528533
lexer->current_token.type != PS_TOKEN_UNSIGNED && lexer->current_token.type != PS_TOKEN_CHAR)
529534
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
530-
ps_identifier identifier = {0};
531535
COPY_IDENTIFIER(identifier)
532536
*symbol = ps_compiler_find_symbol(compiler, block, identifier, false);
533537
if (*symbol == NULL)
@@ -539,22 +543,24 @@ bool ps_parse_function_call_low_high(ps_compiler *compiler, ps_ast_block *block,
539543
PARSE_END("OK")
540544
}
541545

542-
bool ps_parse_function_call_power(ps_compiler *compiler, ps_ast_block *block, ps_value *arg1, ps_value *arg2)
546+
bool ps_parse_function_call_power(ps_compiler *compiler, ps_ast_block *block, ps_ast_node **arg1, ps_ast_node **arg2)
543547
{
544548
PARSE_BEGIN("FUNCTION_CALL", "POWER");
549+
(void)start_line;
550+
(void)start_column;
545551

546552
EXPECT_TOKEN(PS_TOKEN_LEFT_PARENTHESIS)
547553
READ_NEXT_TOKEN
548554
if (!ps_parse_expression(compiler, block, arg1))
549555
TRACE_ERROR("ARG1")
550-
if (!ps_value_is_number(arg1) && !ps_value_is_real(arg1))
551-
RETURN_ERROR(PS_ERROR_EXPECTED_NUMBER)
556+
// if (!ps_value_is_number(arg1) && !ps_value_is_real(arg1))
557+
// RETURN_ERROR(PS_ERROR_EXPECTED_NUMBER)
552558
EXPECT_TOKEN(PS_TOKEN_COMMA)
553559
READ_NEXT_TOKEN
554560
if (!ps_parse_expression(compiler, block, arg2))
555561
TRACE_ERROR("ARG2")
556-
if (!ps_value_is_number(arg2) && !ps_value_is_real(arg2))
557-
RETURN_ERROR(PS_ERROR_EXPECTED_NUMBER)
562+
// if (!ps_value_is_number(arg2) && !ps_value_is_real(arg2))
563+
// RETURN_ERROR(PS_ERROR_EXPECTED_NUMBER)
558564
EXPECT_TOKEN(PS_TOKEN_RIGHT_PARENTHESIS)
559565
READ_NEXT_TOKEN
560566

@@ -576,11 +582,8 @@ bool ps_parse_function_call_system(ps_compiler *compiler, ps_ast_block *block, p
576582

577583
if (function == &ps_system_function_random)
578584
{
579-
ps_ast_node *expression = NULL;
580-
if (!ps_parse_function_call_random(compiler, block, &expression, &n_args, &expression))
585+
if (!ps_parse_function_call_random(compiler, block, &n_args, &args[0]))
581586
TRACE_ERROR("RANDOM")
582-
if (n_args == 1)
583-
args[0] = expression;
584587
}
585588
else if (function == &ps_system_function_get_tick_count)
586589
{

src/ps_parse_statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ bool ps_parse_for_do(ps_compiler *compiler, ps_ast_block *block, ps_ast_for **fo
627627
EXPECT_TOKEN(PS_TOKEN_IDENTIFIER);
628628
COPY_IDENTIFIER(identifier)
629629
READ_NEXT_TOKEN
630-
variable = ps_interpreter_find_symbol(compiler, identifier, true);
630+
variable = ps_compiler_find_symbol(compiler, block, identifier, true);
631631
if (variable == NULL)
632632
RETURN_ERROR(PS_ERROR_SYMBOL_NOT_FOUND);
633633
if (variable->kind != PS_SYMBOL_KIND_VARIABLE)

0 commit comments

Comments
 (0)