From 408a5a46c0c36d9cf1b84b6f12656c853cdd7c95 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Tue, 4 Feb 2025 17:28:06 -0500 Subject: [PATCH] Can set and ref vars in main scope. One can now assign and reference variables in the main scope, as it has been put in a block through the parser. Also added some tests for this purpose. --- src/grammar.y | 36 +++++++++++++++++++++++++++++++----- test/val/test.bats | 8 ++++++-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index a0903d2..321859b 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -56,15 +56,41 @@ %type argstart; %type blockstart; %type block; +%type inputstart +%type input + +%start inputend // This makes no sense but w/e. %% -input: - %empty - | exp { root = $1; } - | input EXPSEP exp { root = $3; } +inputstart: + exp { + DList* exps = dlist_init(); + dlist_append(exps, $1); + $$ = exps; + } ; + +input: + inputstart { + $$ = $1; + } + | input EXPSEP exp { + dlist_append($1, $3); + $$ = $1; + } + ; + +inputend: + %empty + | input { + root = ast_init(AST_TYPE_BLOCK, ast_block_data_init($1->buf, $1->ln)); + } + ; + + + argstart: exp { ArgArr* argarr = argarr_init(); @@ -103,7 +129,7 @@ exp: //| BLOCKS exp BLOCKE { $$ = $2; } | BLOCKS block BLOCKE { - $$ = ast_init(AST_TYPE_BLOCK, ast_block_data_init($2->buf, $2->ln)); + $$ = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $2->buf, $2->ln)); } | SUB exp { diff --git a/test/val/test.bats b/test/val/test.bats index e54d287..6ac83e4 100644 --- a/test/val/test.bats +++ b/test/val/test.bats @@ -100,8 +100,12 @@ bin() { ./scl.out $1 | tail -n1; } run bin "x=1" [ "$output" = "= 1.000000" ] -# run bin "x = 1; x + 1" -# [ "$output" = "= 2.000000" ] + run bin "x=1;x+1" + [ "$output" = "= 2.000000" ] + + run bin "h=7;j=2;k=8;l=4;h*h-l+j*k" + echo $output + [ "$output" = "= 61.000000" ] } #@test "function definition" {