cgen: fix array.filter(anon_fn)
parent
12faf9fcfa
commit
acf0b84523
|
@ -529,6 +529,21 @@ fn test_filter() {
|
||||||
//assert arr.filter(arr % 2).len == 5
|
//assert arr.filter(arr % 2).len == 5
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_anon_fn_filter() {
|
||||||
|
filter_num := fn (i int) bool {
|
||||||
|
return i % 2 == 0
|
||||||
|
}
|
||||||
|
assert [1,2,3,4,5].filter(filter_num) == [2,4]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_anon_fn_arg_filter() {
|
||||||
|
a := [1,2,3,4].filter(fn (i int) bool {
|
||||||
|
return i % 2 == 0
|
||||||
|
})
|
||||||
|
|
||||||
|
assert a == [2,4]
|
||||||
|
}
|
||||||
|
|
||||||
fn map_test_helper_1(i int) int {
|
fn map_test_helper_1(i int) int {
|
||||||
return i * i
|
return i * i
|
||||||
}
|
}
|
||||||
|
|
|
@ -3245,10 +3245,28 @@ fn (mut g Gen) gen_filter(node ast.CallExpr) {
|
||||||
match node.args[0].expr {
|
match node.args[0].expr {
|
||||||
ast.Ident {
|
ast.Ident {
|
||||||
if it.kind == .function {
|
if it.kind == .function {
|
||||||
g.writeln('${node.args[0]}(it)')
|
g.write('${it.name}(it)')
|
||||||
|
} else if it.kind == .variable {
|
||||||
|
var_info := it.var_info()
|
||||||
|
sym_t := g.table.get_type_symbol(var_info.typ)
|
||||||
|
if sym_t.kind == .function {
|
||||||
|
g.write('${it.name}(it)')
|
||||||
} else {
|
} else {
|
||||||
g.expr(node.args[0].expr)
|
g.expr(node.args[0].expr)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
g.expr(node.args[0].expr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ast.AnonFn {
|
||||||
|
pos := g.out.len
|
||||||
|
def_pos := g.definitions.len
|
||||||
|
g.stmt(it.decl)
|
||||||
|
fn_body := g.out.after(pos)
|
||||||
|
g.out.go_back(fn_body.len)
|
||||||
|
g.definitions.go_back(g.definitions.len - def_pos)
|
||||||
|
g.definitions.write(fn_body)
|
||||||
|
g.write('${it.decl.name}(it)')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
g.expr(node.args[0].expr)
|
g.expr(node.args[0].expr)
|
||||||
|
|
Loading…
Reference in New Issue