diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-09-28 10:35:44 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-09-28 10:35:44 +0900 |
| commit | 931cbe657ccdcfefe4077cd7371f1ea4ad4e5b53 (patch) | |
| tree | fe0950e80e3ff1a6a559ac4b169a6991131816a9 | |
| parent | 86ae353d6267387bf216d5fd760a7ced7bced402 (diff) | |
| download | ducc-931cbe657ccdcfefe4077cd7371f1ea4ad4e5b53.tar.gz ducc-931cbe657ccdcfefe4077cd7371f1ea4ad4e5b53.tar.zst ducc-931cbe657ccdcfefe4077cd7371f1ea4ad4e5b53.zip | |
feat: implement |=, &=, ^=, <<= and >>= operators
| -rw-r--r-- | src/codegen.c | 12 | ||||
| -rw-r--r-- | src/parse.c | 3 | ||||
| -rw-r--r-- | tests/test_assignment_operators.sh | 40 |
3 files changed, 53 insertions, 2 deletions
diff --git a/src/codegen.c b/src/codegen.c index 716660d..cc3f9a7 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -292,6 +292,18 @@ static void codegen_assign_expr_helper(CodeGen* g, AstNode* ast) { fprintf(g->out, " cqo\n"); fprintf(g->out, " idiv rdi\n"); fprintf(g->out, " mov rax, rdx\n"); + } else if (ast->node_op == TokenKind_assign_or) { + fprintf(g->out, " or rax, rdi\n"); + } else if (ast->node_op == TokenKind_assign_and) { + fprintf(g->out, " and rax, rdi\n"); + } else if (ast->node_op == TokenKind_assign_xor) { + fprintf(g->out, " xor rax, rdi\n"); + } else if (ast->node_op == TokenKind_assign_lshift) { + fprintf(g->out, " mov rcx, rdi\n"); + fprintf(g->out, " shl rax, cl\n"); + } else if (ast->node_op == TokenKind_assign_rshift) { + fprintf(g->out, " mov rcx, rdi\n"); + fprintf(g->out, " sar rax, cl\n"); } else { unreachable(); } diff --git a/src/parse.c b/src/parse.c index da1345d..ba9b7e5 100644 --- a/src/parse.c +++ b/src/parse.c @@ -801,7 +801,8 @@ static AstNode* parse_assignment_expr(Parser* p) { TokenKind op = peek_token(p)->kind; // TODO: check if the lhs is unary expression. if (op == TokenKind_assign || op == TokenKind_assign_mul || op == TokenKind_assign_div || - op == TokenKind_assign_mod) { + op == TokenKind_assign_mod || op == TokenKind_assign_or || op == TokenKind_assign_and || + op == TokenKind_assign_xor || op == TokenKind_assign_lshift || op == TokenKind_assign_rshift) { next_token(p); AstNode* rhs = parse_assignment_expr(p); lhs = ast_new_assign_expr(op, lhs, rhs); diff --git a/tests/test_assignment_operators.sh b/tests/test_assignment_operators.sh index 541abc8..9b3a33e 100644 --- a/tests/test_assignment_operators.sh +++ b/tests/test_assignment_operators.sh @@ -21,6 +21,44 @@ int main() { z %= 7; ASSERT_EQ(3, z); - return 0; + int a = 0x05; + a |= 0x0A; + ASSERT_EQ(0x0F, a); + + int b = 0x0F; + b &= 0x0A; + ASSERT_EQ(0x0A, b); + + int c = 7; + c |= 8; + ASSERT_EQ(15, c); + + int d = 15; + d &= 6; + ASSERT_EQ(6, d); + + int e = 0x0F; + e ^= 0x05; + ASSERT_EQ(0x0A, e); + + int f = 3; + f <<= 2; + ASSERT_EQ(12, f); + + int g = 16; + g >>= 2; + ASSERT_EQ(4, g); + + int h = -16; + h >>= 2; + ASSERT_EQ(-4, h); + + int j = 1; + j <<= 4; + ASSERT_EQ(16, j); + + int k = 64; + k >>= 3; + ASSERT_EQ(8, k); } EOF |
