Skip to content

Commit

Permalink
fix return value == 2
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Nov 23, 2024
1 parent 134090f commit 09b7de1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/c2v.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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;`
Expand All @@ -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!')
Expand Down
7 changes: 7 additions & 0 deletions tests/25.return_val_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdbool.h>

bool foo(int value)
{
return value == 2;
}

7 changes: 7 additions & 0 deletions tests/25.return_val_2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@[translated]
module main

fn foo(value int) bool {
return value == 2
}

0 comments on commit 09b7de1

Please sign in to comment.