cgen: fix passing high order function with body

pull/4522/head^2
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 return_type = it.return_type
} }
ast.AnonFn { ast.AnonFn {
g.expr(*it)
// TODO: no buffer fiddling // TODO: no buffer fiddling
fsym := g.table.get_type_symbol(it.typ)
ret_styp := g.typ(it.decl.return_type) ret_styp := g.typ(it.decl.return_type)
g.write('$ret_styp (*$ident.name) (') g.write('$ret_styp (*$ident.name) (')
def_pos := g.definitions.len def_pos := g.definitions.len
g.fn_args(it.decl.args, it.decl.is_variadic) g.fn_args(it.decl.args, it.decl.is_variadic)
g.definitions.go_back(g.definitions.len - def_pos) g.definitions.go_back(g.definitions.len - def_pos)
g.writeln(') = &${fsym.name};') g.write(') = ')
g.expr(*it)
g.writeln(';')
continue continue
} }
else {} else {}
@ -1140,9 +1140,11 @@ fn (mut g Gen) expr(node ast.Expr) {
def_pos := g.definitions.len def_pos := g.definitions.len
g.stmt(it.decl) g.stmt(it.decl)
fn_body := g.out.after(pos) fn_body := g.out.after(pos)
g.out.go_back(fn_body.len)
g.definitions.go_back(g.definitions.len - def_pos) g.definitions.go_back(g.definitions.len - def_pos)
g.definitions.write(fn_body) 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 { else {
// #printf("node=%d\n", node.typ); // #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) { fn high_fn_array(f fn(a []int) []int) {
} }
@ -135,12 +139,16 @@ fn test_anon_fn() {
} }
f2(1) f2(1)
/*
high_fn(fn (x int) int { // TODO: fix return
println('hello') // high_fn(fn (x int) int {
return x + 1 // println('hello')
// return x + 1
// })
high_fn_no_ret(fn (x int) {
println('hello $x')
}) })
*/
} }
fn assert_in_bool_fn(v int) bool { fn assert_in_bool_fn(v int) bool {