1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix passing high order function with body

This commit is contained in:
joe-conigliaro 2020-04-21 22:45:20 +10:00
parent bc4a576c54
commit b9c0d2d362
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 19 additions and 9 deletions

View File

@ -782,15 +782,15 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
return_type = it.return_type
}
ast.AnonFn {
g.expr(*it)
// TODO: no buffer fiddling
fsym := g.table.get_type_symbol(it.typ)
ret_styp := g.typ(it.decl.return_type)
g.write('$ret_styp (*$ident.name) (')
def_pos := g.definitions.len
g.fn_args(it.decl.args, it.decl.is_variadic)
g.definitions.go_back(g.definitions.len - def_pos)
g.writeln(') = &${fsym.name};')
g.write(') = ')
g.expr(*it)
g.writeln(';')
continue
}
else {}
@ -1140,9 +1140,11 @@ fn (mut g Gen) expr(node ast.Expr) {
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.out.go_back(fn_body.len)
fsym := g.table.get_type_symbol(it.typ)
g.write('&${fsym.name}')
}
else {
// #printf("node=%d\n", node.typ);

View File

@ -107,6 +107,10 @@ fn high_fn(f fn(int) int) {
}
fn high_fn_no_ret(f fn(int)) {
f(111)
}
fn high_fn_array(f fn(a []int) []int) {
}
@ -135,12 +139,16 @@ fn test_anon_fn() {
}
f2(1)
/*
high_fn(fn (x int) int {
println('hello')
return x + 1
// TODO: fix return
// high_fn(fn (x int) int {
// println('hello')
// return x + 1
// })
high_fn_no_ret(fn (x int) {
println('hello $x')
})
*/
}
fn assert_in_bool_fn(v int) bool {