mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: optionals default value
This commit is contained in:

committed by
Alexander Medvednikov

parent
4e64a58ac1
commit
df5faf35e5
@ -11,12 +11,15 @@ fn (p mut Parser) gen_or_else(pos int) string {
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
// returns the type of the new variable
|
||||
fn (p mut Parser) gen_var_decl(name string, is_static bool) string {
|
||||
// Generate expression to tmp because we need its type first
|
||||
// `[typ] [name] = bool_expression();`
|
||||
pos := p.cgen.add_placeholder()
|
||||
p.is_var_decl = true
|
||||
mut typ := p.bool_expression()
|
||||
p.is_var_decl = false
|
||||
if typ.starts_with('...') { typ = typ[3..] }
|
||||
//p.gen('/*after expr*/')
|
||||
// Option check ? or {
|
||||
@ -68,10 +71,13 @@ fn (p mut Parser) gen_blank_identifier_assign() {
|
||||
is_indexer := p.peek() == .lsbr
|
||||
is_fn_call, next_expr := p.is_next_expr_fn_call()
|
||||
pos := p.cgen.add_placeholder()
|
||||
p.is_var_decl = true
|
||||
typ := p.bool_expression()
|
||||
p.is_var_decl = false
|
||||
if !is_indexer && !is_fn_call {
|
||||
p.error_with_token_index('assigning `$next_expr` to `_` is redundant', assign_error_tok_idx)
|
||||
}
|
||||
// handle or
|
||||
if p.tok == .key_orelse {
|
||||
p.gen_handle_option_or_else(typ, '', pos)
|
||||
} else {
|
||||
@ -93,6 +99,7 @@ fn (p mut Parser) gen_handle_option_or_else(_typ, name string, fn_call_ph int) s
|
||||
p.cgen.set_placeholder(fn_call_ph, '$typ $tmp = ')
|
||||
typ = typ[7..]
|
||||
p.genln(';')
|
||||
or_tok_idx := p.token_idx
|
||||
p.check(.key_orelse)
|
||||
p.check(.lcbr)
|
||||
p.register_var(Var {
|
||||
@ -107,15 +114,26 @@ fn (p mut Parser) gen_handle_option_or_else(_typ, name string, fn_call_ph int) s
|
||||
is_mut: false
|
||||
is_used: true
|
||||
})
|
||||
if is_assign {
|
||||
p.genln('$typ $name;')
|
||||
}
|
||||
p.genln('if (!$tmp .ok) {')
|
||||
p.genln('string err = $tmp . error;')
|
||||
p.genln('int errcode = $tmp . ecode;')
|
||||
p.statements()
|
||||
if is_assign {
|
||||
p.genln('$typ $name = *($typ*) $tmp . data;')
|
||||
last_ph := p.cgen.add_placeholder()
|
||||
last_typ := p.statements()
|
||||
if is_assign && last_typ == typ {
|
||||
expr_line := p.cgen.lines[p.cgen.lines.len-3]
|
||||
last_expr := expr_line[last_ph..]
|
||||
p.cgen.lines[p.cgen.lines.len-3] = ''
|
||||
p.genln('if (!$tmp .ok) {')
|
||||
p.genln('$name = $last_expr;')
|
||||
p.genln('}')
|
||||
} else if is_assign {
|
||||
p.genln('$name = *($typ*) $tmp . data;')
|
||||
}
|
||||
if !p.returns && p.prev_tok2 != .key_continue && p.prev_tok2 != .key_break {
|
||||
p.error('`or` block must return/exit/continue/break/panic')
|
||||
if !p.returns && last_typ != typ && is_assign && p.prev_tok2 != .key_continue && p.prev_tok2 != .key_break {
|
||||
p.error_with_token_index('`or` block must provide a default value or return/exit/continue/break/panic', or_tok_idx)
|
||||
}
|
||||
p.returns = false
|
||||
return typ
|
||||
|
Reference in New Issue
Block a user