From 852fca21512a551dad77f53db2f5e2a049bcf52e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 24 Jun 2020 12:54:39 +0200 Subject: [PATCH] all: call anon fns right away --- vlib/v/ast/ast.v | 1 + vlib/v/checker/checker.v | 2 +- vlib/v/fmt/fmt.v | 5 ++++- vlib/v/gen/cgen.v | 18 ++++++++++++------ vlib/v/parser/fn.v | 7 +++++++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 616154180f..2b53e416f9 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -211,6 +211,7 @@ pub: pub struct AnonFn { pub: decl FnDecl + is_called bool pub mut: typ table.Type } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 73ae3eacf3..5da6cca270 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1882,7 +1882,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { c.cur_fn = &node.decl c.stmts(node.decl.stmts) c.cur_fn = keep_fn - return node.typ + return if node.is_called { node.decl.return_type } else { node.typ } } ast.ArrayInit { return c.array_init(mut node) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6ff260fb10..6902b29c8f 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -568,7 +568,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { } f.write('\n') } - // Handle comments after last field + // Handle comments after last field for comment in node.end_comments { f.indent++ f.empty_line = true @@ -621,6 +621,9 @@ pub fn (mut f Fmt) expr(node ast.Expr) { match node { ast.AnonFn { f.fn_decl(node.decl) + if node.is_called { + f.write('()') + } } ast.ArrayInit { f.array_init(node) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 56f96ccc3a..a7b8a33eca 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1111,12 +1111,18 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.write('{') } ret_styp := g.typ(val.decl.return_type) - 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) + if val.is_called { + g.write('$ret_styp $ident.name = ') + g.expr(*val) + g.write('()') + } else { + 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(';') if blank_assign { g.write('}') diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 5573c84b39..07c2d22031 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -329,6 +329,12 @@ fn (mut p Parser) anon_fn() ast.AnonFn { return_type: return_type } 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 idx := p.table.find_or_register_fn_type(p.mod, func, true, false) typ := table.new_type(idx) @@ -347,6 +353,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn { pos: pos file: p.file_name } + is_called: is_called typ: typ } }