v: no optional for scope.innermost & more blank_ident

pull/3984/head
Joe Conigliaro 2020-03-10 23:35:25 +11:00
parent ee72474971
commit c9d30f78b7
4 changed files with 35 additions and 21 deletions

View File

@ -72,7 +72,8 @@ pub fn (s &Scope) outermost() &Scope {
}
// returns the innermost scope containing pos
pub fn (s &Scope) innermost(pos int) ?&Scope {
// pub fn (s &Scope) innermost(pos int) ?&Scope {
pub fn (s &Scope) innermost(pos int) &Scope {
if s.contains(pos) {
// binary search
mut first := 0
@ -97,7 +98,8 @@ pub fn (s &Scope) innermost(pos int) ?&Scope {
}
return s
}
return none
// return none
return s
}
/*

View File

@ -257,9 +257,7 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
if name == 'filter' {
array_info := typ_sym.info as table.Array
elem_type_sym := c.table.get_type_symbol(array_info.elem_type)
mut scope := c.file.scope.innermost(method_call_expr.pos.pos) or {
c.file.scope
}
mut scope := c.file.scope.innermost(method_call_expr.pos.pos)
scope.override_var(ast.Var{
name: 'it'
typ: array_info.elem_type
@ -381,9 +379,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
if right_sym.kind != .multi_return {
c.error('wrong number of vars', assign_stmt.pos)
}
mut scope := c.file.scope.innermost(assign_stmt.pos.pos) or {
c.file.scope
}
mut scope := c.file.scope.innermost(assign_stmt.pos.pos)
for i, _ in assign_stmt.left {
mut ident := assign_stmt.left[i]
mut var_info := ident.var_info()
@ -409,6 +405,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
if assign_stmt.left.len != assign_stmt.right.len {
c.error('wrong number of vars', assign_stmt.pos)
}
mut scope := c.file.scope.innermost(assign_stmt.pos.pos)
for i, _ in assign_stmt.left {
mut ident := assign_stmt.left[i]
val := assign_stmt.right[i]
@ -427,9 +424,6 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
ident.info = var_info
assign_stmt.left[i] = ident
}
mut scope := c.file.scope.innermost(assign_stmt.pos.pos) or {
c.file.scope
}
scope.override_var(ast.Var{
name: ident.name
typ: val_type
@ -577,9 +571,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
c.assign_expr(it)
}
ast.Assoc {
scope := c.file.scope.innermost(it.pos.pos) or {
c.file.scope
}
scope := c.file.scope.innermost(it.pos.pos)
var := scope.find_var(it.var_name) or {
panic(err)
}
@ -682,9 +674,7 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
if info.typ != 0 {
return info.typ
}
start_scope := c.file.scope.innermost(ident.pos.pos) or {
c.file.scope
}
start_scope := c.file.scope.innermost(ident.pos.pos)
mut found := true
mut var_scope := &ast.Scope(0)
mut var := ast.Var{}

View File

@ -105,7 +105,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
if var_info.is_mut {
f.write('mut ')
}
f.write(ident.name)
f.expr(ident)
if i < it.left.len-1 {
f.write(', ')
}

View File

@ -297,17 +297,39 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
for i, ident in assign_stmt.left {
ident_var_info := ident.var_info()
var_type_sym := g.table.get_type_symbol(ident_var_info.typ)
if ident.kind == .blank_ident {
g.writeln('{$var_type_sym.name _ = $mr_var_name->arg[$i]};')
}
else {
g.writeln('$var_type_sym.name $ident.name = $mr_var_name->arg[$i];')
}
}
}
// `a := 1` | `a,b := 1,2`
else {
for i, ident in assign_stmt.left {
val := assign_stmt.right[i]
ident_var_info := ident.var_info()
var_type_sym := g.table.get_type_symbol(ident_var_info.typ)
if ident.kind == .blank_ident {
is_call := match val {
ast.CallExpr { true }
ast.MethodCallExpr { true }
else { false }
}
if is_call {
g.expr(val)
}
else {
g.write('{$var_type_sym.name _ = ')
g.expr(val)
g.write('}')
}
}
else {
g.write('$var_type_sym.name $ident.name = ')
g.expr(val)
}
g.writeln(';')
}
}