Skip to content

Commit

Permalink
Make binary ops left assoc
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Oct 15, 2024
1 parent 8635398 commit 198c39f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 81 deletions.
2 changes: 1 addition & 1 deletion parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ parser_node *parse_expr_prec(typed_token **tkns_ptr, parser_node *lhs, int min_p
while (1)
{
int look_prec = op_prec(tkn->type_id);
if (look_prec < prec)
if (look_prec <= prec) // <= for left assoc and < for right assoc
{
break;
}
Expand Down
48 changes: 24 additions & 24 deletions tests/output/inp2.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,69 +53,69 @@ mov rbp, rsp
sub rsp, __main_size
mov [rsp+0], rdi
mov [rsp+8], rsi
mov rax, __temp_str_3
mov rbx, __temp_str_4
add rax, rbx
mov [rsp + 24], rax
mov rax, __temp_str_5
mov rbx, __temp_str_6
mul rbx
mov [rsp + 24], rax
mov rax, __temp_str_7
mov rbx, __temp_str_8
add rax, rbx
mov [rsp + 32], rax
mov rax, [rsp + 24]
mov rbx, [rsp + 32]
add rax, rbx
mov [rsp + 40], rax
mov rax, __temp_str_4
mov rbx, [rsp + 40]
mov rax, [rsp + 40]
mov rbx, __temp_str_7
add rax, rbx
mov [rsp + 48], rax
mov rax, __temp_str_3
mov rbx, [rsp + 48]
mov rax, [rsp + 48]
mov rbx, __temp_str_8
add rax, rbx
mov [rsp + 56], rax
mov rax, [rsp + 56]
mov [rsp+16], rax
mov rax, __temp_str_11
mov rbx, __temp_str_12
mov rax, __temp_str_9
mov rbx, __temp_str_10
add rax, rbx
mov [rsp + 72], rax
mov rax, __temp_str_10
mov rbx, [rsp + 72]
mov rax, __temp_str_11
mov rbx, __temp_str_12
add rax, rbx
mov [rsp + 80], rax
mov rax, __temp_str_9
mov rax, [rsp + 72]
mov rbx, [rsp + 80]
add rax, rbx
mov [rsp + 88], rax
mov rax, [rsp + 88]
mov [rsp+64], rax
mov rax, __temp_str_15
mov rbx, __temp_str_16
mov rax, __temp_str_13
mov rbx, __temp_str_14
add rax, rbx
mov [rsp + 96], rax
mov rax, __temp_str_14
mov rbx, [rsp + 96]
mov rax, __temp_str_15
mov rbx, __temp_str_16
add rax, rbx
mov [rsp + 104], rax
mov rax, __temp_str_13
mov rax, [rsp + 96]
mov rbx, [rsp + 104]
add rax, rbx
mov [rsp + 112], rax
mov rax, [rsp + 112]
mov [rsp+64], rax
call hey
mov [rsp + 120], rax
call func
mov [rsp + 128], rax
mov rax, __temp_str_18
mov rbx, [rsp + 128]
mov rax, [rsp + 120]
mov rbx, __temp_str_17
add rax, rbx
mov [rsp + 128], rax
call func
mov [rsp + 136], rax
mov rax, __temp_str_17
mov rax, __temp_str_18
mov rbx, [rsp + 136]
add rax, rbx
mov [rsp + 144], rax
mov rax, [rsp + 120]
mov rax, [rsp + 128]
mov rbx, [rsp + 144]
add rax, rbx
mov [rsp + 152], rax
Expand Down
66 changes: 33 additions & 33 deletions tests/output/inp2.c_tree_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,73 +52,73 @@ Program(
Value:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 1)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 2)
Right:
BinaryOp(Op: 72)
Left:
BinaryOp(Op: 78)
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 3)
Literal(Type: 33, Value: 1)
Right:
Literal(Type: 33, Value: 4)
Literal(Type: 33, Value: 2)
Right:
BinaryOp(Op: 72)
BinaryOp(Op: 78)
Left:
Literal(Type: 33, Value: 5)
Literal(Type: 33, Value: 3)
Right:
Literal(Type: 33, Value: 6)
Literal(Type: 33, Value: 4)
Right:
Literal(Type: 33, Value: 5)
Right:
Literal(Type: 33, Value: 6)
VarDecl(b):
Type(TKN_INT)
Value:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 234)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 234)
Right:
Literal(Type: 33, Value: 2)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 23)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 23)
Right:
Literal(Type: 33, Value: 1)
Literal(Type: 33, Value: 1)
Assign(b):
Value:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 2)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 2)
Right:
Literal(Type: 33, Value: 3)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 4)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: 4)
Right:
Literal(Type: 33, Value: 5)
Literal(Type: 33, Value: 5)
Assign(a):
Value:
BinaryOp(Op: 72)
Left:
FunctionCall(Name: hey)
Args:
Right:
BinaryOp(Op: 72)
Left:
FunctionCall(Name: hey)
Args:
Right:
Literal(Type: 33, Value: ab)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: cd)
Right:
BinaryOp(Op: 72)
Left:
Literal(Type: 33, Value: cd)
Right:
FunctionCall(Name: func)
Args:
FunctionCall(Name: func)
Args:
FunctionCall(Name: salam)
Args:
CompoundStatement
Expand Down
20 changes: 10 additions & 10 deletions tests/output/inp3.c_asm_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,23 @@ mov rax, [rsp + 120]
mov [rsp+104], rax
mov rax, __temp_str_8
mov [rsp + 128], rax
mov rax, [rsp + 80]
mov rbx, [rsp + 72]
mov rax, [rsp + 88]
mov rbx, [rsp + 88]
add rax, rbx
mov [rsp + 136], rax
mov rax, [rsp + 136]
mov rbx, [rsp + 72]
mul rbx
mov rbx, [rsp + 104]
add rax, rbx
mov [rsp + 144], rax
mov rax, [rsp + 104]
mov rbx, [rsp + 144]
mov rax, [rsp + 80]
mov rbx, [rsp + 72]
add rax, rbx
mov [rsp + 152], rax
mov rax, [rsp + 88]
mov rbx, [rsp + 152]
add rax, rbx
mov rax, [rsp + 152]
mov rbx, [rsp + 72]
mul rbx
mov [rsp + 160], rax
mov rax, [rsp + 88]
mov rax, [rsp + 144]
mov rbx, [rsp + 160]
add rax, rbx
mov [rsp + 168], rax
Expand Down
26 changes: 13 additions & 13 deletions tests/output/inp3.c_tree_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,25 @@ Program(
)
BinaryOp(Op: 72)
Left:
Variable(c)
Right:
BinaryOp(Op: 72)
Left:
Variable(c)
BinaryOp(Op: 72)
Left:
Variable(c)
Right:
Variable(c)
Right:
Variable(d)
Right:
BinaryOp(Op: 78)
Left:
BinaryOp(Op: 72)
Left:
Variable(d)
Variable(b)
Right:
BinaryOp(Op: 78)
Left:
BinaryOp(Op: 72)
Left:
Variable(b)
Right:
Variable(a)
Right:
Variable(a)
Variable(a)
Right:
Variable(a)
VarDecl(cc):
Type(TKN_INT*)
Value:
Expand Down

0 comments on commit 198c39f

Please sign in to comment.