cgen: fix `for char in nums {}`

pull/4716/head
Delyan Angelov 2020-05-04 18:39:00 +03:00
parent b2157388d9
commit d7e7113047
2 changed files with 53 additions and 5 deletions

View File

@ -690,7 +690,7 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
g.expr(it.high)
g.writeln('; $i++) {')
if it.val_var != '_' {
g.writeln('\tint $it.val_var = $i;')
g.writeln('\tint ${c_name(it.val_var)} = $i;')
}
g.stmts(it.stmts)
g.writeln('}')
@ -708,7 +708,7 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
op_field := if cond_type_is_ptr { '->' } else { '.' }
g.writeln('for (int $i = 0; $i < ${atmp}${op_field}len; $i++) {')
if it.val_var != '_' {
g.writeln('\t$styp $it.val_var = (($styp*)${atmp}${op_field}data)[$i];')
g.writeln('\t$styp ${c_name(it.val_var)} = (($styp*)${atmp}${op_field}data)[$i];')
}
g.stmts(it.stmts)
g.writeln('}')
@ -727,7 +727,7 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
g.writeln('for (int $idx = 0; $idx < ${keys_tmp}.len; $idx++) {')
g.writeln('\t$key_styp $key = (($key_styp*)${keys_tmp}.data)[$idx];')
if it.val_var != '_' {
g.write('\t$val_styp $it.val_var = (*($val_styp*)map_get3(')
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)map_get3(')
g.expr(it.cond)
g.writeln(', $key, &($val_styp[]){ $zero }));')
}
@ -740,7 +740,7 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
g.write('for (int $i = 0; $i < ')
g.expr(it.cond)
g.writeln('.len; $i++) {')
g.write('$styp $it.val_var = ')
g.write('$styp ${c_name(it.val_var)} = ')
g.expr(it.cond)
g.writeln('.args[$i];')
g.stmts(it.stmts)
@ -751,7 +751,7 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
g.expr(it.cond)
g.writeln('.len; $i++) {')
if it.val_var != '_' {
g.write('byte $it.val_var = ')
g.write('byte ${c_name(it.val_var)} = ')
g.expr(it.cond)
g.writeln('.str[$i];')
}

View File

@ -0,0 +1,48 @@
const (
nums = [1, 2, 3]
)
fn test_for_char_in() {
mut sum := 0
for char in nums {
sum += char
}
assert sum == 6
}
fn test_for_char_in_range() {
mut sum := 0
for char in 0 .. nums.len {
sum += nums[char]
}
assert sum == 6
}
fn test_for_char_complex() {
mut sum := 0
for char := 0; char < nums.len; char++ {
sum += nums[char]
}
assert sum == 6
}
fn test_for_char_in_string() {
s := 'abcd'
mut sum := 0
for char in s {
sum += char
}
assert sum == 394 // ascii codes of `a` + `b` + `c` + `d`
}
fn test_for_char_in_map() {
m := {
'a': 'b'
'c': 'd'
}
mut acc := ''
for k, char in m {
acc += '$k: $char, '
}
assert acc == 'a: b, c: d, '
}