ast: use multi return optional for find_scope_and_var

pull/4161/head
joe-conigliaro 2020-03-31 15:39:14 +11:00
parent 71ca553190
commit 602bc06bee
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 4 additions and 19 deletions

View File

@ -21,23 +21,13 @@ pub fn new_scope(parent &Scope, start_pos int) &Scope {
}
}
pub struct ScopeVar {
pub:
scope &Scope
var Var
}
// pub fn (s &Scope) find_scope_and_var(name string) ?(&Scope,Var) {
pub fn (s &Scope) find_scope_and_var(name string) ?ScopeVar {
pub fn (s &Scope) find_scope_and_var(name string) ?(&Scope,Var) {
if name in s.vars {
// return s,s.vars[name]
return ScopeVar{
s,s.vars[name]}
return s,s.vars[name]
}
for sc := s; !isnil(sc.parent); sc = sc.parent {
if name in sc.vars {
return ScopeVar{
sc,sc.vars[name]}
return sc,sc.vars[name]
}
}
return none

View File

@ -809,16 +809,11 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
}
start_scope := c.file.scope.innermost(ident.pos.pos)
mut found := true
mut var_scope := &ast.Scope(0)
mut var := ast.Var{}
// var_scope,var = start_scope.find_scope_and_var(ident.name) or {
mr := start_scope.find_scope_and_var(ident.name) or {
var_scope,var := start_scope.find_scope_and_var(ident.name) or {
found = false
c.error('not found: $ident.name - POS: $ident.pos.pos', ident.pos)
panic('')
}
var_scope = mr.scope
var = mr.var
if found {
// update the variable
// we need to do this here instead of var_decl since some