Documentation
¶
Overview ¶
Package block provides a language-neutral Block IR for flattening nested let bindings. This intermediate representation sits between Core AST and target language emission (e.g., Go), enabling flat code generation without deeply nested closures.
M-CODEGEN-V2: Eliminate nested IIFEs by lowering let chains to flat blocks.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Block ¶
type Block struct {
// Stmts contains variable bindings in evaluation order.
// Each Stmt declares a variable and its value expression.
Stmts []Stmt
// FinalExpr is the body expression after all bindings are established.
// This is what the block evaluates to.
FinalExpr core.CoreExpr
}
Block represents a sequence of variable bindings followed by a final expression. This is the result of flattening nested let expressions from Core AST.
Example Core:
Let{x, 1, Let{y, 2, Let{z, 3, x + y + z}}}
Becomes Block:
Block{
Stmts: [{x, 1}, {y, 2}, {z, 3}],
FinalExpr: x + y + z
}
func Lower ¶
Lower transforms a Core expression into a Block by extracting top-level Let bindings into a flat sequence of statements.
This ONLY extracts consecutive top-level Let bindings. Nested Let expressions inside Value positions are preserved - they will become IIFEs when generated, but at most ONE level deep (not O(n) nesting).
Evaluation order is preserved: bindings appear in the order they were declared in the original Core AST.
Examples:
Lower(Let{x, 1, Let{y, 2, x+y}})
→ Block{Stmts: [{x,1}, {y,2}], FinalExpr: x+y}
Lower(Let{x, Let{y, 1, y}, x}) // Let inside Value
→ Block{Stmts: [{x, Let{y,1,y}}], FinalExpr: x} // Inner Let preserved!
Lower(App{f, x}) // Non-let expression
→ Block{Stmts: [], FinalExpr: App{f, x}}
func LowerLetRec ¶
LowerLetRec transforms a LetRec expression into a Block. LetRec bindings are all added as statements, preserving mutual recursion capability.
Note: LetRec bindings may reference each other, so order matters for codegen (forward declaration pattern in Go).