Added parenthesis.

Also updated some grammar rules for negatives to be more general.
This commit is contained in:
Jacob Signorovitch 2025-01-14 13:59:26 -05:00
parent 0b1905429c
commit 1098fa252f
2 changed files with 24 additions and 44 deletions

View File

@ -17,7 +17,6 @@
double fval; double fval;
char* strval; char* strval;
AST* ast; AST* ast;
ArgArr* argarr;
} }
%define parse.error verbose %define parse.error verbose
@ -42,8 +41,6 @@
%type<fval> num; %type<fval> num;
%type<ast> exp; %type<ast> exp;
%type<argarr> arg;
%type<argarr> argstart;
%% %%
@ -52,40 +49,24 @@ input:
| exp { root = $1; } | exp { root = $1; }
; ;
num:
NUM { $$ = $1; }
| SUB NUM { $$ = -$2; } %prec NEG
;
argstart:
exp {
ArgArr* argarr = argarr_init();
argarr_add(argarr, $1);
$$ = argarr;
}
arg:
argstart { $$ = $1; }
| arg SEP exp {
argarr_add($1, $3);
$$ = $1;
}
;
exp: exp:
num { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); } NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
// name(thing-1, thing-2, ..., thing-n) | SUB exp {
// name(thing) AST** argv = calloc(2, sizeof(AST*));
// name() argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
argv[1] = $2;
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("mul", 2, argv));
}
// larg: lgroup marg | LGROUP exp RGROUP { $$ = $2; }
// lgroup rgroup
// marg: exp sep marg
// exp rgroup
| CALL LGROUP arg RGROUP { // name(thing, thing)
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, $3->ln, $3->buf)); | CALL LGROUP exp SEP exp RGROUP {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = $3;
argv[1] = $5;
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, 2, argv));
} }
| exp PLUS exp { | exp PLUS exp {

View File

@ -72,14 +72,13 @@ bin() { ./scl.out $1 | tail -n1; }
[ "$output" = "= -2.000000" ] [ "$output" = "= -2.000000" ]
} }
# Doesn't run without hanging for now. @test "order of operations with parenthesis" {
# @test "order of operations with parenthesis" { run bin "(1+2)*3"
# run bin "(1+2)*3" [ "$output" = "= 9.000000" ]
# [ "$output" = "= 9.000000" ]
# run bin "-(1+2*3)"
# run bin "-(1+2*3)" [ "$output" = "= -7.000000" ]
# [ "$output" = "= -7.000000" ]
# run bin "-(-(1+2)*3)"
# run bin "-(-(1+2)*3)" [ "$output" = "= 9.000000" ]
# [ "$output" = "= 9.000000" ] }
# }