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

foo()? syntax

This commit is contained in:
Alexander Medvednikov
2019-11-13 21:47:05 +03:00
parent 72249ce889
commit b1fa0d25f0
5 changed files with 34 additions and 10 deletions

View File

@ -221,8 +221,6 @@ fn (p mut Parser) name_expr() string {
!p.table.known_fn(name) && !p.table.known_const(name) && !is_c
{
name = p.prepend_mod(name)
} else {
//println('$name')
}
// re-check
if p.known_var_check_new_var(name) {
@ -331,13 +329,29 @@ fn (p mut Parser) name_expr() string {
f = new_f
// optional function call `function() or {}`, no return assignment
is_or_else := p.tok == .key_orelse
if !p.is_var_decl && is_or_else {
is_or_else := p.tok == .key_orelse
if p.tok == .question {
// `files := os.ls('.')?`
if p.cur_fn.name != 'main__main' {
p.error('`func()?` syntax can only be used inside `fn main()` for now')
}
p.next()
tmp := p.get_tmp()
p.cgen.set_placeholder(fn_call_ph, '$f.typ $tmp = ')
p.genln(';')
p.genln('if (!${tmp}.ok) v_panic(${tmp}.error);')
typ := f.typ[7..] // option_xxx
p.gen('*($typ*) ${tmp}.data;')
return typ
}
else if !p.is_var_decl && is_or_else {
f.typ = p.gen_handle_option_or_else(f.typ, '', fn_call_ph)
p.print_tok()
}
else if !p.is_var_decl && !is_or_else && !p.inside_return_expr &&
else if !p.is_var_decl && !is_or_else && !p.inside_return_expr &&
f.typ.starts_with('Option_') {
opt_type := f.typ[7..]
p.print_tok()
p.error('unhandled option type: `?$opt_type`')
}