Generate produces Go code for the lambda expression. Output format: func(param1 type1, param2 type2, ...) returnType { body } For expression bodies, wraps in { return ... }. For block bodies, uses as-is. Source mappings track: - Lambda start position → entire generated function
()
| 36 | // Source mappings track: |
| 37 | // - Lambda start position → entire generated function |
| 38 | func (g *LambdaCodeGen) Generate() ast.CodeGenResult { |
| 39 | if g.expr == nil { |
| 40 | return ast.CodeGenResult{} |
| 41 | } |
| 42 | |
| 43 | // func( |
| 44 | g.Write("func(") |
| 45 | |
| 46 | // Parameters |
| 47 | g.generateParams() |
| 48 | |
| 49 | // ) |
| 50 | g.WriteByte(')') |
| 51 | |
| 52 | // Return type |
| 53 | // - If explicitly specified: use it |
| 54 | // - If expression body (has return stmt): default to 'any' as placeholder |
| 55 | // - If block body: NO DEFAULT (may be void) |
| 56 | // The type inferrer will replace 'any' with actual types when in call context |
| 57 | if g.expr.ReturnType != "" { |
| 58 | g.WriteByte(' ') |
| 59 | g.Write(g.expr.ReturnType) |
| 60 | } else if !g.expr.IsBlock { |
| 61 | // Expression body - add 'any' return type so { return ... } is valid |
| 62 | g.Write(" any") |
| 63 | } |
| 64 | // Block bodies have no default return type (may be void) |
| 65 | |
| 66 | // Body |
| 67 | g.WriteByte(' ') |
| 68 | g.generateBody() |
| 69 | |
| 70 | return g.Result() |
| 71 | } |
| 72 | |
| 73 | // generateParams generates the parameter list. |
| 74 | // |