mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: allows compound assignment operators on array
This commit is contained in:
parent
ffb6c6f5b4
commit
9b3b22d6b3
@ -2000,6 +2000,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
||||
p.error('strings are immutable')
|
||||
}
|
||||
assign_pos := p.cgen.cur_line.len
|
||||
is_cao := p.tok != .assign
|
||||
p.assigned_type = typ
|
||||
p.expected_type = typ
|
||||
p.assign_statement(v, fn_ph, is_indexer && (is_map || is_arr))
|
||||
@ -2013,19 +2014,33 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
||||
tmp_val := p.cgen.cur_line.right(assign_pos)
|
||||
p.cgen.resetln(p.cgen.cur_line.left(assign_pos))
|
||||
// val := p.cgen.end_tmp()
|
||||
mut cao_tmp := p.cgen.cur_line
|
||||
if is_map {
|
||||
p.cgen.set_placeholder(fn_ph, 'map__set(&')
|
||||
// CAO on map is a bit more complicated as it loads
|
||||
// the value inside a pointer instead of returning it.
|
||||
}
|
||||
else {
|
||||
if is_ptr {
|
||||
p.cgen.set_placeholder(fn_ph, 'array_set(')
|
||||
if is_cao {
|
||||
cao_tmp = '*($p.expected_type *) array__get(*$cao_tmp)'
|
||||
}
|
||||
}
|
||||
else {
|
||||
p.cgen.set_placeholder(fn_ph, 'array_set(&/*q*/')
|
||||
if is_cao {
|
||||
cao_tmp = '*($p.expected_type *) array__get($cao_tmp)'
|
||||
}
|
||||
}
|
||||
}
|
||||
p.gen(', & $tmp)')
|
||||
p.cgen.insert_before('$typ $tmp = $tmp_val;')
|
||||
if !is_cao {
|
||||
p.cgen.insert_before('$typ $tmp = $tmp_val;')
|
||||
}
|
||||
else {
|
||||
p.cgen.insert_before('$typ $tmp = $cao_tmp ' + tmp_val.all_before('=') + tmp_val.all_after('=') + ';')
|
||||
}
|
||||
}
|
||||
return typ
|
||||
}
|
||||
|
@ -199,3 +199,10 @@ fn test_clone() {
|
||||
assert nums.slice(1, 3).str() == '[2, 3]'
|
||||
}
|
||||
|
||||
fn test_doubling() {
|
||||
mut nums := [1, 2, 3, 4, 5]
|
||||
for i := 0; i < nums.len; i++ {
|
||||
nums[i] *= 2
|
||||
}
|
||||
assert nums.str() == '[2, 4, 6, 8, 10]'
|
||||
}
|
Loading…
Reference in New Issue
Block a user