cgen: fix `for k,_ in map { x << k }`
parent
77bde09c13
commit
ca1f47a742
|
@ -290,3 +290,16 @@ fn test_plus_assign_string() {
|
||||||
assert m.len == 1
|
assert m.len == 1
|
||||||
assert m['one'] == '1'
|
assert m['one'] == '1'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn test_map_keys_to_array() {
|
||||||
|
m := {'a': 'b', 'c': 'd'}
|
||||||
|
mut arr := []string{}
|
||||||
|
for k, _ in m {
|
||||||
|
arr << k
|
||||||
|
}
|
||||||
|
sarr := arr.str()
|
||||||
|
println(sarr)
|
||||||
|
assert sarr == "['a', 'c']"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -864,9 +864,14 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
|
||||||
g.writeln(';')
|
g.writeln(';')
|
||||||
g.writeln('array_$key_styp $keys_tmp = map_keys(&$atmp);')
|
g.writeln('array_$key_styp $keys_tmp = map_keys(&$atmp);')
|
||||||
g.writeln('for (int $idx = 0; $idx < ${keys_tmp}.len; ++$idx) {')
|
g.writeln('for (int $idx = 0; $idx < ${keys_tmp}.len; ++$idx) {')
|
||||||
g.writeln('\t$key_styp $key = (($key_styp*)${keys_tmp}.data)[$idx];')
|
// TODO: analyze whether it.key_type has a .clone() method and call .clone() for all types:
|
||||||
|
if it.key_type == table.string_type {
|
||||||
|
g.writeln('\t$key_styp $key = /*kkkk*/ string_clone( (($key_styp*)${keys_tmp}.data)[$idx] );')
|
||||||
|
} else {
|
||||||
|
g.writeln('\t$key_styp $key = /*kkkk*/ (($key_styp*)${keys_tmp}.data)[$idx];')
|
||||||
|
}
|
||||||
if it.val_var != '_' {
|
if it.val_var != '_' {
|
||||||
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)map_get($atmp')
|
g.write('\t$val_styp ${c_name(it.val_var)} = /*vvv*/ (*($val_styp*)map_get($atmp')
|
||||||
g.writeln(', $key, &($val_styp[]){ $zero }));')
|
g.writeln(', $key, &($val_styp[]){ $zero }));')
|
||||||
}
|
}
|
||||||
g.stmts(it.stmts)
|
g.stmts(it.stmts)
|
||||||
|
|
Loading…
Reference in New Issue