block

package
v0.14.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

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

func Lower(expr core.CoreExpr) *Block

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

func LowerLetRec(letrec *core.LetRec) *Block

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).

func (*Block) IsEmpty

func (b *Block) IsEmpty() bool

IsEmpty returns true if the block has no statements (just a final expression).

func (*Block) Len

func (b *Block) Len() int

Len returns the number of statements in the block.

type Stmt

type Stmt struct {
	// Name is the variable name being bound.
	Name string

	// Value is the expression whose result is bound to Name.
	Value core.CoreExpr
}

Stmt represents a single variable binding: var name = value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL