aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/codegen.c4
-rw-r--r--src/parse.c3
-rw-r--r--tests/test_bitwise_operators.sh11
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