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.
This commit is contained in:
Jacob Signorovitch 2025-02-04 17:28:06 -05:00
parent 6903aeb383
commit 408a5a46c0
2 changed files with 37 additions and 7 deletions

View File

@ -56,15 +56,41 @@
%type<argarr> argstart; %type<argarr> argstart;
%type<exps> blockstart; %type<exps> blockstart;
%type<exps> block; %type<exps> block;
%type<exps> inputstart
%type<exps> input
%start inputend // This makes no sense but w/e.
%% %%
input: inputstart:
%empty exp {
| exp { root = $1; } DList* exps = dlist_init();
| input EXPSEP exp { root = $3; } 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: argstart:
exp { exp {
ArgArr* argarr = argarr_init(); ArgArr* argarr = argarr_init();
@ -103,7 +129,7 @@ exp:
//| BLOCKS exp BLOCKE { $$ = $2; } //| BLOCKS exp BLOCKE { $$ = $2; }
| BLOCKS block BLOCKE { | 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 { | SUB exp {

View File

@ -100,8 +100,12 @@ bin() { ./scl.out $1 | tail -n1; }
run bin "x=1" run bin "x=1"
[ "$output" = "= 1.000000" ] [ "$output" = "= 1.000000" ]
# run bin "x = 1; x + 1" run bin "x=1;x+1"
# [ "$output" = "= 2.000000" ] [ "$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" { #@test "function definition" {