From 7ea688aa43612614b00ddab4ec463cbed4a2ad70 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 24 Jul 2019 13:04:57 +0200 Subject: [PATCH] fix mutable array args --- compiler/fn.v | 3 +-- compiler/parser.v | 32 +++++++++++++++----------------- compiler/tests/fn_test.v | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/compiler/fn.v b/compiler/fn.v index 66441e8b6c..a5005859a8 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -631,8 +631,7 @@ fn (p mut Parser) fn_args(f mut Fn) { if !p.first_run() && !p.table.known_type(typ2) { p.error('fn_args: unknown type $typ2') } - if is_mut { - // && !typ2.starts_with('array_') { + if is_mut { typ2 += '*' } v := Var { diff --git a/compiler/parser.v b/compiler/parser.v index 3364a380f5..e23a507acb 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -1736,11 +1736,12 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string { } fn (p mut Parser) index_expr(typ string, fn_ph int) string { - //if p.fileis('main.v') { - //println('index expr typ=$typ') - //} // a[0] v := p.expr_var + //if p.fileis('fn_test.v') { + //println('index expr typ=$typ') + //println(v.name) + //} is_map := typ.starts_with('map_') is_str := typ == 'string' is_arr0 := typ.starts_with('array_') @@ -1912,7 +1913,11 @@ fn (p mut Parser) index_expr(typ string, fn_ph int) string { p.gen('$index_expr ]') } else { - p.gen('( *($typ*) array__get($index_expr) )') + if is_ptr { + p.gen('( *($typ*) array__get(* $index_expr) )') + } else { + p.gen('( *($typ*) array__get($index_expr) )') + } } } else if is_str && !p.builtin_pkg { @@ -2076,13 +2081,13 @@ fn (p mut Parser) expression() string { fn (p mut Parser) term() string { line_nr := p.scanner.line_nr - if p.fileis('fn_test') { - println('\nterm() $line_nr') - } + //if p.fileis('fn_test') { + //println('\nterm() $line_nr') + //} typ := p.unary() - if p.fileis('fn_test') { - println('2: $line_nr') - } + //if p.fileis('fn_test') { + //println('2: $line_nr') + //} // `*` on a newline? Can't be multiplication, only dereference if p.tok == .mul && line_nr != p.scanner.line_nr { return typ @@ -3117,13 +3122,6 @@ fn (p mut Parser) for_st() { // TODO don't call map_get() for each key, fetch values while traversing // the tree (replace `map_keys()` above with `map_key_vals()`) p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);') - - /* - p.genln('for (int l = 0; l < $tmp . entries.len; l++) {') - p.genln('Entry entry = *((Entry*) (array__get($tmp .entries, l)));') - p.genln('string $i = entry.key;') - p.genln('$var_typ $val; map_get($tmp, $i, & $val);') - */ } } // `for val in vals` diff --git a/compiler/tests/fn_test.v b/compiler/tests/fn_test.v index 6279b62088..bf9cd2f33b 100644 --- a/compiler/tests/fn_test.v +++ b/compiler/tests/fn_test.v @@ -63,6 +63,24 @@ fn myprint(s string, ..) { println('/* /* comment */ */') } +fn modify_array(a mut []int) { + a[0] = 10 + for i in 0..a.len { + a[i] = a[i] * 2 + } + a << 888 +} + +fn test_mut() { + mut nums := [1, 2, 3] + modify_array(mut nums) + assert nums.len == 4 + assert nums[0] == 20 + assert nums[1] == 4 + assert nums[2] == 6 + assert nums[3] == 888 +} + fn test_fns() { // no asserts for now, just test function declarations above }