diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-09-28 15:30:11 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-09-28 15:30:11 +0900 |
| commit | 5a16856f8ff4dbf801b4a622ca7053b77e8a0214 (patch) | |
| tree | a5f15bccc7b34802da8aa655d26c28848a832cff | |
| parent | 1b208f7f0a3b7d6b72c2a431d9fcd6e18fe76f5d (diff) | |
| download | ducc-5a16856f8ff4dbf801b4a622ca7053b77e8a0214.tar.gz ducc-5a16856f8ff4dbf801b4a622ca7053b77e8a0214.tar.zst ducc-5a16856f8ff4dbf801b4a622ca7053b77e8a0214.zip | |
feat: implement bitwise not operator
| -rw-r--r-- | src/codegen.c | 4 | ||||
| -rw-r--r-- | src/parse.c | 3 | ||||
| -rw-r--r-- | tests/test_bitwise_operators.sh | 11 |
3 files changed, 18 insertions, 0 deletions
diff --git a/src/codegen.c b/src/codegen.c index 34f6c23..f02a38c 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -87,6 +87,10 @@ static void codegen_unary_expr(CodeGen* g, AstNode* ast) { fprintf(g->out, " sete al\n"); fprintf(g->out, " movzb rax, al\n"); fprintf(g->out, " push rax\n"); + } else if (ast->node_op == TokenKind_tilde) { + fprintf(g->out, " pop rax\n"); + fprintf(g->out, " not rax\n"); + fprintf(g->out, " push rax\n"); } else { unreachable(); } diff --git a/src/parse.c b/src/parse.c index 80c7aac..b82c45d 100644 --- a/src/parse.c +++ b/src/parse.c @@ -547,6 +547,9 @@ static AstNode* parse_prefix_expr(Parser* p) { } else if (consume_token_if(p, TokenKind_not)) { AstNode* operand = parse_prefix_expr(p); return ast_new_unary_expr(op, operand); + } else if (consume_token_if(p, TokenKind_tilde)) { + AstNode* operand = parse_prefix_expr(p); + return ast_new_unary_expr(op, operand); } else if (consume_token_if(p, TokenKind_and)) { AstNode* operand = parse_prefix_expr(p); return ast_new_ref_expr(operand); diff --git a/tests/test_bitwise_operators.sh b/tests/test_bitwise_operators.sh index fe1f245..f683099 100644 --- a/tests/test_bitwise_operators.sh +++ b/tests/test_bitwise_operators.sh @@ -25,5 +25,16 @@ int main() { ASSERT_EQ(2, d); ASSERT_EQ(3, e); ASSERT_EQ(0, f); + + ASSERT_EQ(-1, ~0); + ASSERT_EQ(-2, ~1); + ASSERT_EQ(-6, ~5); + ASSERT_EQ(0, ~(-1)); + ASSERT_EQ(5, ~(-6)); + + int x = 10; + ASSERT_EQ(-11, ~x); + ASSERT_EQ(-1, ~(x & 0)); + ASSERT_EQ(-16, ~(x | 5)); } EOF |
