From c9d30f78b7c72789952a3cf28d9e6c41119bf71d Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Tue, 10 Mar 2020 23:35:25 +1100 Subject: [PATCH] v: no optional for scope.innermost & more blank_ident --- vlib/v/ast/scope.v | 6 ++++-- vlib/v/checker/checker.v | 20 +++++--------------- vlib/v/fmt/fmt.v | 2 +- vlib/v/gen/cgen.v | 28 +++++++++++++++++++++++++--- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/vlib/v/ast/scope.v b/vlib/v/ast/scope.v index f7518cebc0..ebf88b8652 100644 --- a/vlib/v/ast/scope.v +++ b/vlib/v/ast/scope.v @@ -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 } /* diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index bec2d11cda..371bf25a8d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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{} diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index cd6ecd187c..5e7ba1efdf 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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(', ') } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 82facea3ca..960fe713dc 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -297,7 +297,12 @@ 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) - 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` @@ -306,8 +311,25 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { val := assign_stmt.right[i] ident_var_info := ident.var_info() var_type_sym := g.table.get_type_symbol(ident_var_info.typ) - g.write('$var_type_sym.name $ident.name = ') - g.expr(val) + 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(';') } }