diff --git a/src/grammar.y b/src/grammar.y index 1a9a2be..d34cf02 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -17,7 +17,6 @@ double fval; char* strval; AST* ast; - ArgArr* argarr; } %define parse.error verbose @@ -42,8 +41,6 @@ %type num; %type exp; -%type arg; -%type argstart; %% @@ -52,40 +49,24 @@ input: | 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: - 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) - // name(thing) - // name() + | SUB exp { + AST** argv = calloc(2, sizeof(AST*)); + 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 rgroup - // marg: exp sep marg - // exp rgroup + | LGROUP exp RGROUP { $$ = $2; } - | CALL LGROUP arg RGROUP { - $$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, $3->ln, $3->buf)); + // name(thing, thing) + | 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 { diff --git a/test/validation/test.bats b/test/validation/test.bats index 7f5a2c3..24ae0a0 100644 --- a/test/validation/test.bats +++ b/test/validation/test.bats @@ -72,14 +72,13 @@ bin() { ./scl.out $1 | tail -n1; } [ "$output" = "= -2.000000" ] } -# Doesn't run without hanging for now. -# @test "order of operations with parenthesis" { -# run bin "(1+2)*3" -# [ "$output" = "= 9.000000" ] -# -# run bin "-(1+2*3)" -# [ "$output" = "= -7.000000" ] -# -# run bin "-(-(1+2)*3)" -# [ "$output" = "= 9.000000" ] -# } +@test "order of operations with parenthesis" { + run bin "(1+2)*3" + [ "$output" = "= 9.000000" ] + + run bin "-(1+2*3)" + [ "$output" = "= -7.000000" ] + + run bin "-(-(1+2)*3)" + [ "$output" = "= 9.000000" ] +}