1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fix mutable array args

This commit is contained in:
Alexander Medvednikov 2019-07-24 13:04:57 +02:00
parent 32aae80a64
commit 7ea688aa43
3 changed files with 34 additions and 19 deletions

View File

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

View File

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

View File

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