cgen: fix mutable arrays

pull/4232/head
krischerven 2020-04-04 08:08:38 -04:00 committed by GitHub
parent 133842bf95
commit 4c6db7a64e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 2 deletions

View File

@ -479,10 +479,21 @@ fn (g mut Gen) for_in(it ast.ForInStmt) {
styp := g.typ(it.val_type) styp := g.typ(it.val_type)
g.write('for (int $i = 0; $i < ') g.write('for (int $i = 0; $i < ')
g.expr(it.cond) g.expr(it.cond)
g.writeln('.len; $i++) {') cond_type_is_ptr := table.type_is_ptr(it.cond_type)
if cond_type_is_ptr {
g.writeln('->')
} else {
g.writeln('.')
}
g.write('len; $i++) {')
g.write('\t$styp $it.val_var = (($styp*)') g.write('\t$styp $it.val_var = (($styp*)')
g.expr(it.cond) g.expr(it.cond)
g.writeln('.data)[$i];') if cond_type_is_ptr {
g.writeln('->')
} else {
g.writeln('.')
}
g.write('data)[$i];')
g.stmts(it.stmts) g.stmts(it.stmts)
g.writeln('}') g.writeln('}')
} }
@ -1705,6 +1716,9 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
// `x[0] *= y` // `x[0] *= y`
if g.assign_op in [.mult_assign] { if g.assign_op in [.mult_assign] {
g.write('*($elem_type_str*)array_get(') g.write('*($elem_type_str*)array_get(')
if left_is_ptr {
g.write('*')
}
g.expr(node.left) g.expr(node.left)
g.write(', ') g.write(', ')
g.expr(node.index) g.expr(node.index)
@ -1721,6 +1735,9 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
} }
else { else {
g.write('(*($elem_type_str*)array_get(') g.write('(*($elem_type_str*)array_get(')
if left_is_ptr {
g.write('*')
}
g.expr(node.left) g.expr(node.left)
g.write(', ') g.write(', ')
g.expr(node.index) g.expr(node.index)