From 165dfe5fe0045642f1168045448a3533107a706f Mon Sep 17 00:00:00 2001 From: Henrixounez Date: Fri, 28 Jun 2019 19:10:32 +0200 Subject: [PATCH] compiler: allow mut passed as argument to be modified --- compiler/parser.v | 3 ++- compiler/scanner.v | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/compiler/parser.v b/compiler/parser.v index 2ac66ebbc1..bfb5102eff 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -1193,7 +1193,7 @@ fn (p mut Parser) name_expr() string { hack_lit := p.lit // amp ptr := p.tok == AMP - deref := p.tok == MUL + mut deref := p.tok == MUL if ptr || deref { p.next() } @@ -1237,6 +1237,7 @@ fn (p mut Parser) name_expr() string { // Variable v := p.cur_fn.find_var(name) if v.name.len != 0 { + deref = deref || (v.is_arg && v.is_mut && is_mutable_type(v.typ)) if ptr { p.gen('& /*vvar*/ ') } diff --git a/compiler/scanner.v b/compiler/scanner.v index eb4ba104a1..2391ee551d 100644 --- a/compiler/scanner.v +++ b/compiler/scanner.v @@ -713,3 +713,24 @@ fn (s mut Scanner) create_type_string(T Type, name string) { fn (p mut Parser) create_type_string(T Type, name string) { p.scanner.create_type_string(T, name) } + +//Checks if mut type can be dereferenced to be modified +fn is_mutable_type(typ string) bool { + if typ == 'bool*' { + return true + } + if typ == 'int*' || typ == 'rune*' || typ == 'i8*' || + typ == 'i16*' || typ == 'i32*' || typ == 'i64*' { + return true + } + if typ == 'byte*' || typ == 'u8*' || typ == 'u16*' || + typ == 'u32*' || typ == 'u64*' { + return true + } + if typ == 'f32*' || typ == 'f64*' { + return true + } + if typ == 'string*' { + return true + } +}