diff --git a/codegen.c b/codegen.c index c27a3fe..89a09ec 100644 --- a/codegen.c +++ b/codegen.c @@ -884,6 +884,13 @@ void codegen_generate_normal_unary(struct node *node, struct history *history) { "eax", STACK_FRAME_ELEMENT_TYPE_PUSHED_VALUE, "result_value", 0, &(struct stack_frame_data){.dtype = last_dtype}); } + } else if (S_EQ(node->unary.op, "!")) { + asm_push("cmp eax, 0"); + asm_push("sete al"); + asm_push("movzx eax, al"); + asm_push_ins_push_with_data( + "eax", STACK_FRAME_ELEMENT_TYPE_PUSHED_VALUE, "result_value", 0, + &(struct stack_frame_data){.dtype = last_dtype}); } } diff --git a/expressionable.c b/expressionable.c index 7b130d6..fb224d7 100644 --- a/expressionable.c +++ b/expressionable.c @@ -306,7 +306,7 @@ void expressionable_parse_parentheses(struct expressionable *expressionable) { void expressionable_parse_for_normal_unary( struct expressionable *expressionable) { - const char *unary_op = expressionable_peek_next(expressionable)->sval; + const char *unary_op = expressionable_token_next(expressionable)->sval; expressionable_parse(expressionable); void *unary_operand_node = expressionable_node_pop(expressionable); diff --git a/preprocessor/preprocessor.c b/preprocessor/preprocessor.c index ddcfe9f..4965efd 100644 --- a/preprocessor/preprocessor.c +++ b/preprocessor/preprocessor.c @@ -1338,6 +1338,24 @@ int preprocessor_evaluate_exp(struct compile_process *compiler, node->exp.op); } +int preprocessor_evaluate_unary(struct compile_process *compiler, + struct preprocessor_node *node) { + int res = 0; + const char *op = node->unary_node.op; + struct preprocessor_node *right_operand = node->unary_node.operand; + if (S_EQ(op, "!")) { + res = !preprocessor_evaluate(compiler, right_operand); + } else if (S_EQ(op, "~")) { + res = ~preprocessor_evaluate(compiler, right_operand); + } else if (S_EQ(op, "-")) { + res = -preprocessor_evaluate(compiler, right_operand); + } else { + compiler_error(compiler, "unknown unary operator"); + } + + return res; +} + int preprocessor_evaluate(struct compile_process *compiler, struct preprocessor_node *root_node) { struct preprocessor_node *current = root_node; @@ -1354,6 +1372,10 @@ int preprocessor_evaluate(struct compile_process *compiler, case PREPROCESSOR_EXPRESSION_NODE: res = preprocessor_evaluate_exp(compiler, current); break; + + case PREPROCESSOR_UNARY_NODE: + res = preprocessor_evaluate_unary(compiler, current); + break; } return res; diff --git a/test.c b/test.c index a6432df..53132f9 100644 --- a/test.c +++ b/test.c @@ -1,8 +1,6 @@ -#define TEST_FUNC(s) #s +#define ABC !1 +#if ABC > 0 +#error "ABC > 0" +#endif -int printf(const char *format, ...); - -int main() { - const char *s = TEST_FUNC(Hello World !); - printf("%s\n", s); -} +int main() { return !0; }