diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 1b4ac9b1d3..f69709c783 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -886,6 +886,28 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { g.inside_ternary = prev_inside_ternary return } + } else if node.right is ast.PrefixExpr && g.inside_ternary == 0 { + prefix := node.right + if prefix.op == .not && prefix.right is ast.CallExpr { + call_expr := prefix.right as ast.CallExpr + if call_expr.or_block.kind != .absent { + prev_inside_ternary := g.inside_ternary + g.inside_ternary = 0 + tmp := g.new_tmp_var() + cur_line := g.go_before_stmt(0).trim_space() + g.empty_line = true + g.write('bool ${tmp} = (') + g.expr(node.left) + g.writeln(');') + g.set_current_pos_as_last_stmt_pos() + g.write('${cur_line} ${tmp} ${node.op.str()} ') + g.infix_left_var_name = '!${tmp}' + g.expr(node.right) + g.infix_left_var_name = '' + g.inside_ternary = prev_inside_ternary + return + } + } } g.gen_plain_infix_expr(node) } diff --git a/vlib/v/tests/inout/printing_result_in_infix_expr2.out b/vlib/v/tests/inout/printing_result_in_infix_expr2.out new file mode 100644 index 0000000000..cbf1c9810d --- /dev/null +++ b/vlib/v/tests/inout/printing_result_in_infix_expr2.out @@ -0,0 +1,4 @@ +True 2 +ok +True 3 +False 3 diff --git a/vlib/v/tests/inout/printing_result_in_infix_expr2.vv b/vlib/v/tests/inout/printing_result_in_infix_expr2.vv new file mode 100644 index 0000000000..8bc7ee9dc3 --- /dev/null +++ b/vlib/v/tests/inout/printing_result_in_infix_expr2.vv @@ -0,0 +1,29 @@ +fn false_(n int) !bool { + println('False ${n}') + return false +} + +fn true_(n int) !bool { + println('True ${n}') + return true +} + +fn true_2() !bool { + println('True') + return true +} + +fn test() ! { + if true_(2)! || !false_(2)! { + println('ok') + } + + if false { + } else if (true_(3)! || !false_(3)!) && false_(3)! { + println('ok2') + } +} + +fn main() { + test() or { panic(err) } +}