all: call anon fns right away

pull/5477/head
Daniel Däschle 2020-06-24 12:54:39 +02:00 committed by GitHub
parent cf7ac7be7f
commit 852fca2151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 8 deletions

View File

@ -211,6 +211,7 @@ pub:
pub struct AnonFn { pub struct AnonFn {
pub: pub:
decl FnDecl decl FnDecl
is_called bool
pub mut: pub mut:
typ table.Type typ table.Type
} }

View File

@ -1882,7 +1882,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
c.cur_fn = &node.decl c.cur_fn = &node.decl
c.stmts(node.decl.stmts) c.stmts(node.decl.stmts)
c.cur_fn = keep_fn c.cur_fn = keep_fn
return node.typ return if node.is_called { node.decl.return_type } else { node.typ }
} }
ast.ArrayInit { ast.ArrayInit {
return c.array_init(mut node) return c.array_init(mut node)

View File

@ -568,7 +568,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
} }
f.write('\n') f.write('\n')
} }
// Handle comments after last field // Handle comments after last field
for comment in node.end_comments { for comment in node.end_comments {
f.indent++ f.indent++
f.empty_line = true f.empty_line = true
@ -621,6 +621,9 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
match node { match node {
ast.AnonFn { ast.AnonFn {
f.fn_decl(node.decl) f.fn_decl(node.decl)
if node.is_called {
f.write('()')
}
} }
ast.ArrayInit { ast.ArrayInit {
f.array_init(node) f.array_init(node)

View File

@ -1111,12 +1111,18 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
g.write('{') g.write('{')
} }
ret_styp := g.typ(val.decl.return_type) ret_styp := g.typ(val.decl.return_type)
g.write('$ret_styp (*$ident.name) (') if val.is_called {
def_pos := g.definitions.len g.write('$ret_styp $ident.name = ')
g.fn_args(val.decl.args, val.decl.is_variadic) g.expr(*val)
g.definitions.go_back(g.definitions.len - def_pos) g.write('()')
g.write(') = ') } else {
g.expr(*val) g.write('$ret_styp (*$ident.name) (')
def_pos := g.definitions.len
g.fn_args(val.decl.args, val.decl.is_variadic)
g.definitions.go_back(g.definitions.len - def_pos)
g.write(') = ')
g.expr(*val)
}
g.writeln(';') g.writeln(';')
if blank_assign { if blank_assign {
g.write('}') g.write('}')

View File

@ -329,6 +329,12 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
return_type: return_type return_type: return_type
} }
name := 'anon_${p.tok.pos}_$func.signature()' name := 'anon_${p.tok.pos}_$func.signature()'
mut is_called := false
if p.tok.kind == .lpar {
is_called = true
p.check(.lpar)
p.check(.rpar)
}
func.name = name func.name = name
idx := p.table.find_or_register_fn_type(p.mod, func, true, false) idx := p.table.find_or_register_fn_type(p.mod, func, true, false)
typ := table.new_type(idx) typ := table.new_type(idx)
@ -347,6 +353,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
pos: pos pos: pos
file: p.file_name file: p.file_name
} }
is_called: is_called
typ: typ typ: typ
} }
} }