From 09b7de1237075b5200c5bb118a55a2f739d97398 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 23 Nov 2024 13:38:34 +0300 Subject: [PATCH] fix return value == 2 --- src/c2v.v | 7 ++++++- tests/25.return_val_2.c | 7 +++++++ tests/25.return_val_2.out | 7 +++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/25.return_val_2.c create mode 100644 tests/25.return_val_2.out diff --git a/src/c2v.v b/src/c2v.v index 6fbf102..15a41b3 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -1102,6 +1102,7 @@ fn (mut c C2V) return_st(mut node Node) { if expr.kindof(.implicit_cast_expr) { if expr.ast_type.qualified == 'bool' { // Handle `return 1` which is actually `return true` + // TODO handle `return x == 2` c.returning_bool = true } } @@ -1755,7 +1756,7 @@ fn (mut c C2V) expr(_node &Node) string { return '' } if node.kindof(.integer_literal) { - if c.returning_bool { + if c.returning_bool && node.value in ['1', '0'] { if node.value == '1' { c.gen('true') } else { @@ -1801,6 +1802,7 @@ fn (mut c C2V) expr(_node &Node) string { // = + - * else if node.kindof(.binary_operator) { op := node.opcode + println("BINARY $op") mut first_expr := node.try_get_next_child() or { println(add_place_data_to_error(err)) bad_node @@ -1811,6 +1813,8 @@ fn (mut c C2V) expr(_node &Node) string { println(add_place_data_to_error(err)) bad_node } + println('second expr=') + println(second_expr) if second_expr.kindof(.binary_operator) && second_expr.opcode == '=' { // handle `a = b = c` => `a = c; b = c;` @@ -1831,6 +1835,7 @@ fn (mut c C2V) expr(_node &Node) string { c.gen('') second_expr.current_child_id = 0 } else { + println('doing second') c.expr(second_expr) } vprintln('done!') diff --git a/tests/25.return_val_2.c b/tests/25.return_val_2.c new file mode 100644 index 0000000..a9dbfdf --- /dev/null +++ b/tests/25.return_val_2.c @@ -0,0 +1,7 @@ +#include + +bool foo(int value) +{ + return value == 2; +} + diff --git a/tests/25.return_val_2.out b/tests/25.return_val_2.out new file mode 100644 index 0000000..ca17780 --- /dev/null +++ b/tests/25.return_val_2.out @@ -0,0 +1,7 @@ +@[translated] +module main + +fn foo(value int) bool { + return value == 2 +} +