diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 9f3d86f84c..b04dbc84f4 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -602,11 +602,15 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) { // styp := g.typ(node.return_type) elem_type_str := g.typ(info.elem_type) g.empty_line = true + g.writeln('bool $tmp = false;') + if g.infix_left_var_name.len > 0 { + g.writeln('if ($g.infix_left_var_name) {') + g.indent++ + } g.write('${g.typ(node.left_type)} ${tmp}_orig = ') g.expr(node.left) g.writeln(';') g.writeln('int ${tmp}_len = ${tmp}_orig.len;') - g.writeln('bool $tmp = false;') i := g.new_tmp_var() g.writeln('for (int $i = 0; $i < ${tmp}_len; ++$i) {') g.writeln('\t$elem_type_str it = (($elem_type_str*) ${tmp}_orig.data)[$i];') @@ -654,6 +658,10 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) { g.stmt_path_pos << g.out.len } g.write('\n') + if g.infix_left_var_name.len > 0 { + g.indent-- + g.writeln('}') + } g.write(s) g.write(tmp) } @@ -666,11 +674,15 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) { // styp := g.typ(node.return_type) elem_type_str := g.typ(info.elem_type) g.empty_line = true + g.writeln('bool $tmp = true;') + if g.infix_left_var_name.len > 0 { + g.writeln('if ($g.infix_left_var_name) {') + g.indent++ + } g.write('${g.typ(node.left_type)} ${tmp}_orig = ') g.expr(node.left) g.writeln(';') g.writeln('int ${tmp}_len = ${tmp}_orig.len;') - g.writeln('bool $tmp = true;') i := g.new_tmp_var() g.writeln('for (int $i = 0; $i < ${tmp}_len; ++$i) {') g.writeln('\t$elem_type_str it = (($elem_type_str*) ${tmp}_orig.data)[$i];') @@ -718,6 +730,10 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) { g.stmt_path_pos << g.out.len } g.write('\n') + if g.infix_left_var_name.len > 0 { + g.indent-- + g.writeln('}') + } g.write(s) g.write(tmp) } diff --git a/vlib/v/gen/c/infix_expr.v b/vlib/v/gen/c/infix_expr.v index f57d4b2f9f..4678ed9a79 100644 --- a/vlib/v/gen/c/infix_expr.v +++ b/vlib/v/gen/c/infix_expr.v @@ -630,6 +630,24 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { return } g.inside_ternary = prev_inside_ternary + } else if node.right is ast.CallExpr { + if node.right.left_type != 0 { + sym := g.table.get_type_symbol(node.right.left_type) + if sym.kind == .array && node.right.name in ['map', 'filter', 'all', 'any'] { + 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.stmt_path_pos << g.out.len + g.write('$cur_line $tmp $node.op.str() ') + g.infix_left_var_name = if node.op == .and { tmp } else { '!$tmp' } + g.expr(node.right) + g.infix_left_var_name = '' + return + } + } } g.gen_plain_infix_expr(node) } diff --git a/vlib/v/tests/if_expr_with_array_call_test.v b/vlib/v/tests/if_expr_with_array_call_test.v new file mode 100644 index 0000000000..89866ce563 --- /dev/null +++ b/vlib/v/tests/if_expr_with_array_call_test.v @@ -0,0 +1,17 @@ +fn test_if_expr_with_array_all_any() { + arr := [''] + + for i in arr { + if i.len == 0 || i[1..].bytes().all(it.is_letter()) { + println('empty or all char from second is letter!') + assert true + } + } + + for i in arr { + if i.len == 0 || i[1..].bytes().any(it.is_letter()) { + println('empty or all char from second is letter!') + assert true + } + } +}