From b288bf2e7c724206950dd2d2f7462525177673b6 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Mar 2020 14:42:14 +0100 Subject: [PATCH] cgen: or{} for methods --- vlib/v/gen/cgen.v | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 7091c61c23..43a1cedcf4 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1006,7 +1006,7 @@ fn (g mut Gen) expr(node ast.Expr) { g.write('/*rec*/*') } g.expr(it.expr) - is_variadic := it.exp_arg_types.len > 0 && table.type_is_variadic(it.exp_arg_types[it.exp_arg_types.len-1]) + is_variadic := it.exp_arg_types.len > 0 && table.type_is_variadic(it.exp_arg_types[it.exp_arg_types.len - 1]) if it.args.len > 0 || is_variadic { g.write(', ') } @@ -1024,6 +1024,9 @@ fn (g mut Gen) expr(node ast.Expr) { // /////// g.call_args(it.args, it.exp_arg_types) g.write(')') + if it.or_block.stmts.len > 0 { + g.or_block(it.or_block.stmts, it.return_type) + } } ast.None { g.write('opt_none()') @@ -2107,25 +2110,29 @@ fn (g mut Gen) call_expr(it ast.CallExpr) { g.write(')') } if it.or_block.stmts.len > 0 { - // `foo() or { return }` - var_name := if g.expr_var_name != '' { g.expr_var_name } else { g.new_tmp_var() } - if g.expr_var_name == '' { - // The user is not using the optional return value. We need to use a temp var - // to access its fields (`.ok`, `.error` etc) - // `os.cp(...)` => `Option bool tmp = os__cp(...); if (!tmp.ok) { ... }` - styp := g.typ(it.return_type) - g.insert_before('$styp $var_name = ') - } - g.writeln(';') // or') - g.writeln('if (!${var_name}.ok) {') - g.writeln('string err = ${var_name}.v_error;') - g.writeln('int errcode = ${var_name}.ecode;') - g.stmts(it.or_block.stmts) - g.writeln('}') + g.or_block(it.or_block.stmts, it.return_type) } g.is_c_call = false } +fn (g mut Gen) or_block(stmts []ast.Stmt, return_type table.Type) { + // `foo() or { return }` + var_name := if g.expr_var_name != '' { g.expr_var_name } else { g.new_tmp_var() } + if g.expr_var_name == '' { + // The user is not using the optional return value. We need to use a temp var + // to access its fields (`.ok`, `.error` etc) + // `os.cp(...)` => `Option bool tmp = os__cp(...); if (!tmp.ok) { ... }` + styp := g.typ(return_type) + g.insert_before('$styp $var_name = ') + } + g.writeln(';') // or') + g.writeln('if (!${var_name}.ok) {') + g.writeln('string err = ${var_name}.v_error;') + g.writeln('int errcode = ${var_name}.ecode;') + g.stmts(stmts) + g.writeln('}') +} + // `a in [1,2,3]` => `a == 1 || a == 2 || a == 3` fn (g mut Gen) in_optimization(left ast.Expr, right ast.ArrayInit) { is_str := right.elem_type == table.string_type