From d619944cf5a27fa6b54b7cc6c98261039447dc3b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 27 Feb 2020 21:00:33 +0100 Subject: [PATCH] vfmt: []xxx; mut args --- vlib/v/ast/ast.v | 2 ++ vlib/v/fmt/fmt.v | 35 ++++++++++++++++++++++++----------- vlib/v/parser/fn.v | 14 ++++++++++---- vlib/v/parser/parser.v | 19 ++++++++++--------- 4 files changed, 46 insertions(+), 24 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 236fb895b3..870cc175bc 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -153,6 +153,7 @@ mut: name string args []Expr is_c bool + muts []bool } pub struct MethodCallExpr { @@ -162,6 +163,7 @@ pub: expr Expr name string args []Expr + muts []bool } pub struct Return { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 1114a0282c..455cb4e667 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -252,18 +252,25 @@ fn (f &Fmt) type_to_str(t table.Type) string { fn (f mut Fmt) expr(node ast.Expr) { match node { ast.ArrayInit { - // type_sym := f.table.get_type_symbol(it.typ) - f.write('[') - for i, expr in it.exprs { - if i > 0 && it.exprs.len > 1 { - f.wrap_long_line() - } - f.expr(expr) - if i < it.exprs.len - 1 { - f.write(', ') - } + // `x := []string` + if it.exprs.len == 0 && it.typ != 0 { + f.write(f.table.type_to_str(it.typ)) + } + // `[1,2,3]` + else { + // type_sym := f.table.get_type_symbol(it.typ) + f.write('[') + for i, expr in it.exprs { + if i > 0 && it.exprs.len > 1 { + f.wrap_long_line() + } + f.expr(expr) + if i < it.exprs.len - 1 { + f.write(', ') + } + } + f.write(']') } - f.write(']') } ast.AssignExpr { f.expr(it.left) @@ -294,6 +301,9 @@ fn (f mut Fmt) expr(node ast.Expr) { ast.CallExpr { f.write('${it.name}(') for i, expr in it.args { + if it.muts[i] { + f.write('mut ') + } f.expr(expr) if i != it.args.len - 1 { f.write(', ') @@ -383,6 +393,9 @@ fn (f mut Fmt) expr(node ast.Expr) { f.expr(it.expr) f.write('.' + it.name + '(') for i, arg in it.args { + if it.muts[i] { + f.write('mut ') + } if i > 0 { f.wrap_long_line() } diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index cbc9fa21a3..ebf67a5a18 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -13,12 +13,13 @@ pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr { name := p.check_name() fn_name := if mod.len > 0 { '${mod}.$name' } else { name } p.check(.lpar) - args := p.call_args() + args, muts := p.call_args() node := ast.CallExpr{ name: fn_name args: args + muts: muts // tok: tok - + pos: tok.position() is_c: is_c } @@ -29,11 +30,16 @@ pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr { return node } -pub fn (p mut Parser) call_args() []ast.Expr { +pub fn (p mut Parser) call_args() ([]ast.Expr, []bool) { mut args := []ast.Expr + mut muts := []bool for p.tok.kind != .rpar { if p.tok.kind == .key_mut { p.check(.key_mut) + muts << true + } + else { + muts << false } e,_ := p.expr(0) args << e @@ -42,7 +48,7 @@ pub fn (p mut Parser) call_args() []ast.Expr { } } p.check(.rpar) - return args // ,table.void_ti + return args, muts } fn (p mut Parser) fn_decl() ast.FnDecl { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index bf3e823896..d43d9cb85a 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -57,7 +57,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt { pref: &pref.Preferences{} scope: scope // scope: &ast.Scope{start_pos: 0, parent: 0} - + } p.init_parse_fns() p.read_first_token() @@ -333,7 +333,7 @@ pub fn (p mut Parser) stmt() ast.Stmt { return ast.ExprStmt{ expr: expr // typ: typ - + } } } @@ -632,7 +632,7 @@ pub fn (p mut Parser) name_expr() ast.Expr { p.expr_mod = '' return ast.EnumVal{ enum_name: enum_name // lp.prepend_mod(enum_name) - + val: val pos: p.tok.position() } @@ -894,7 +894,7 @@ fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) ast.Expr { // Method call if p.tok.kind == .lpar { p.next() - args := p.call_args() + args,muts := p.call_args() if p.tok.kind == .key_orelse { p.next() p.parse_block() @@ -903,6 +903,7 @@ fn (p mut Parser) dot_expr(left ast.Expr, left_type table.Type) ast.Expr { expr: left name: field_name args: args + muts:muts pos: p.tok.position() } mut node := ast.Expr{} @@ -1178,11 +1179,11 @@ fn (p mut Parser) if_expr() ast.Expr { stmts: stmts else_stmts: else_stmts // typ: typ - + pos: pos has_else: has_else // left: left - + } return node } @@ -1603,10 +1604,10 @@ fn (p mut Parser) var_decl() ast.VarDecl { node := ast.VarDecl{ name: name expr: expr // p.expr(token.lowest_prec) - + is_mut: is_mut // typ: typ - + pos: p.tok.position() } p.scope.register_var(node) @@ -1725,7 +1726,7 @@ fn (p mut Parser) match_expr() ast.Expr { blocks: blocks match_exprs: match_exprs // typ: typ - + cond: cond } return node