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 // 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) { if s.contains(pos) {
// binary search // binary search
mut first := 0 mut first := 0
@ -97,7 +98,8 @@ pub fn (s &Scope) innermost(pos int) ?&Scope {
} }
return s 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' { if name == 'filter' {
array_info := typ_sym.info as table.Array array_info := typ_sym.info as table.Array
elem_type_sym := c.table.get_type_symbol(array_info.elem_type) elem_type_sym := c.table.get_type_symbol(array_info.elem_type)
mut scope := c.file.scope.innermost(method_call_expr.pos.pos) or { mut scope := c.file.scope.innermost(method_call_expr.pos.pos)
c.file.scope
}
scope.override_var(ast.Var{ scope.override_var(ast.Var{
name: 'it' name: 'it'
typ: array_info.elem_type 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 { if right_sym.kind != .multi_return {
c.error('wrong number of vars', assign_stmt.pos) c.error('wrong number of vars', assign_stmt.pos)
} }
mut scope := c.file.scope.innermost(assign_stmt.pos.pos) or { mut scope := c.file.scope.innermost(assign_stmt.pos.pos)
c.file.scope
}
for i, _ in assign_stmt.left { for i, _ in assign_stmt.left {
mut ident := assign_stmt.left[i] mut ident := assign_stmt.left[i]
mut var_info := ident.var_info() 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 { if assign_stmt.left.len != assign_stmt.right.len {
c.error('wrong number of vars', assign_stmt.pos) 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 { for i, _ in assign_stmt.left {
mut ident := assign_stmt.left[i] mut ident := assign_stmt.left[i]
val := assign_stmt.right[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 ident.info = var_info
assign_stmt.left[i] = ident assign_stmt.left[i] = ident
} }
mut scope := c.file.scope.innermost(assign_stmt.pos.pos) or {
c.file.scope
}
scope.override_var(ast.Var{ scope.override_var(ast.Var{
name: ident.name name: ident.name
typ: val_type typ: val_type
@ -577,9 +571,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
c.assign_expr(it) c.assign_expr(it)
} }
ast.Assoc { ast.Assoc {
scope := c.file.scope.innermost(it.pos.pos) or { scope := c.file.scope.innermost(it.pos.pos)
c.file.scope
}
var := scope.find_var(it.var_name) or { var := scope.find_var(it.var_name) or {
panic(err) panic(err)
} }
@ -682,9 +674,7 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
if info.typ != 0 { if info.typ != 0 {
return info.typ return info.typ
} }
start_scope := c.file.scope.innermost(ident.pos.pos) or { start_scope := c.file.scope.innermost(ident.pos.pos)
c.file.scope
}
mut found := true mut found := true
mut var_scope := &ast.Scope(0) mut var_scope := &ast.Scope(0)
mut var := ast.Var{} mut var := ast.Var{}

View File

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

View File

@ -297,7 +297,12 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
for i, ident in assign_stmt.left { for i, ident in assign_stmt.left {
ident_var_info := ident.var_info() ident_var_info := ident.var_info()
var_type_sym := g.table.get_type_symbol(ident_var_info.typ) var_type_sym := g.table.get_type_symbol(ident_var_info.typ)
g.writeln('$var_type_sym.name $ident.name = $mr_var_name->arg[$i];') 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` // `a := 1` | `a,b := 1,2`
@ -306,8 +311,25 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
val := assign_stmt.right[i] val := assign_stmt.right[i]
ident_var_info := ident.var_info() ident_var_info := ident.var_info()
var_type_sym := g.table.get_type_symbol(ident_var_info.typ) var_type_sym := g.table.get_type_symbol(ident_var_info.typ)
g.write('$var_type_sym.name $ident.name = ') if ident.kind == .blank_ident {
g.expr(val) 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(';') g.writeln(';')
} }
} }