1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: or{} for methods

This commit is contained in:
Alexander Medvednikov 2020-03-26 14:42:14 +01:00
parent 9c5de77f12
commit b288bf2e7c

View File

@ -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,24 +2110,28 @@ fn (g mut Gen) call_expr(it ast.CallExpr) {
g.write(')')
}
if it.or_block.stmts.len > 0 {
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(it.return_type)
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(it.or_block.stmts)
g.stmts(stmts)
g.writeln('}')
}
g.is_c_call = false
}
// `a in [1,2,3]` => `a == 1 || a == 2 || a == 3`
fn (g mut Gen) in_optimization(left ast.Expr, right ast.ArrayInit) {