cgen: fix array.map(anon_fn)

pull/5297/head
yuyi 2020-06-09 16:14:10 +08:00 committed by GitHub
parent 895c7624e4
commit d472a27489
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -589,6 +589,13 @@ fn test_map() {
assert strs == ['v', 'is', 'awesome']
}
fn test_anon_fn_map() {
add_num := fn (i int) int {
return i + 1
}
assert [1,2,3].map(add_num) == [2,3,4]
}
fn test_array_str() {
numbers := [1, 2, 3]
assert numbers == [1,2,3]

View File

@ -3175,7 +3175,15 @@ fn (mut g Gen) gen_map(node ast.CallExpr) {
match node.args[0].expr {
ast.Ident {
if it.kind == .function {
g.writeln('${it.name}(it)')
g.write('${it.name}(it)')
} else if it.kind == .variable {
var_info := it.var_info()
sym := g.table.get_type_symbol(var_info.typ)
if sym.kind == .function {
g.write('${it.name}(it)')
} else {
g.expr(node.args[0].expr)
}
} else {
g.expr(node.args[0].expr)
}