Fix right -> left associativity for assignment.

This commit is contained in:
Christoffer Lerno
2021-07-19 17:31:13 +02:00
parent 28a0dec343
commit 8568b9b079
2 changed files with 32 additions and 1 deletions

View File

@@ -391,7 +391,15 @@ static Expr *parse_binary(Context *context, Expr *left_side)
}
else
{
right_side = TRY_EXPR_OR(parse_precedence(context, rules[operator_type].precedence + 1), poisoned_expr);
// Assignment operators have precedence right -> left.
if (rules[operator_type].precedence == PREC_ASSIGNMENT)
{
right_side = TRY_EXPR_OR(parse_precedence(context, PREC_ASSIGNMENT), poisoned_expr);
}
else
{
right_side = TRY_EXPR_OR(parse_precedence(context, rules[operator_type].precedence + 1), poisoned_expr);
}
}
Expr *expr = EXPR_NEW_EXPR(EXPR_BINARY, left_side);

View File

@@ -0,0 +1,23 @@
module prec;
func void test()
{
int i = 1;
int j = 2;
int k = 3;
int l = i = j = k;
}
// #expect: prec.ll
%i = alloca i32, align 4
%j = alloca i32, align 4
%k = alloca i32, align 4
%l = alloca i32, align 4
store i32 1, i32* %i, align 4
store i32 2, i32* %j, align 4
store i32 3, i32* %k, align 4
%0 = load i32, i32* %k, align 4
store i32 %0, i32* %j, align 4
store i32 %0, i32* %i, align 4
store i32 %0, i32* %l, align 4