vfmt: []xxx; mut args

pull/3866/head
Alexander Medvednikov 2020-02-27 21:00:33 +01:00
parent 4f0d505c65
commit d619944cf5
4 changed files with 46 additions and 24 deletions

View File

@ -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 {

View File

@ -252,6 +252,12 @@ fn (f &Fmt) type_to_str(t table.Type) string {
fn (f mut Fmt) expr(node ast.Expr) {
match node {
ast.ArrayInit {
// `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 {
@ -265,6 +271,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
}
f.write(']')
}
}
ast.AssignExpr {
f.expr(it.left)
f.write(' $it.op.str() ')
@ -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()
}

View File

@ -13,10 +13,11 @@ 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()
@ -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 {

View File

@ -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{}