stmt

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 stmt defines the Statement IR — the ONLY representation that code generation emitters see. No emitter may import internal/core directly.

Statement IR is target-agnostic: it knows about variable declarations, if/switch control flow, function calls, and field access — concepts that exist in every target language. It does NOT know about Go's interface{}, Rust's Box<dyn Any>, or C's void*.

The IR is produced by lowering passes in internal/gen/lower/ that transform Core AST + CoreTypeInfo into this representation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(prog *Program) error

Validate checks IR invariants on a Program. Returns nil if valid, or an error describing the violation.

Types

type ADTConstructor

type ADTConstructor struct {
	TypeName string // The ADT type name (e.g., "Option")
	Tag      string // The variant tag (e.g., "Some")
	Args     []Expr // Constructor arguments
}

ADTConstructor constructs an ADT variant.

type ADTDecl

type ADTDecl struct {
	Variants []ADTVariant
}

ADTDecl declares an algebraic data type with variants.

type ADTVariant

type ADTVariant struct {
	Tag    string         // Constructor name (e.g., "Some", "None")
	Fields []ResolvedType // Field types (empty for nullary constructors)
}

ADTVariant is one constructor of an ADT.

type ArrayLit

type ArrayLit struct {
	ElemType ResolvedType
	Elems    []Expr
}

ArrayLit constructs an array value.

type AssignStmt

type AssignStmt struct {
	Name  string
	Value Expr
	Line  int // source line (0 = unknown)
}

AssignStmt assigns a value to an existing variable.

type BinOp

type BinOp struct {
	Op    BinOpKind
	Left  Expr
	Right Expr
}

BinOp is a binary operation.

type BinOpKind

type BinOpKind int

BinOpKind enumerates binary operators.

const (
	OpAdd BinOpKind = iota
	OpSub
	OpMul
	OpDiv
	OpMod
	OpEq
	OpNeq
	OpLt
	OpLte
	OpGt
	OpGte
	OpAnd
	OpOr
	OpConcat // string concatenation
)

type Binding

type Binding struct {
	Name       string       // Variable name to bind
	FieldIndex int          // Which field of the variant (0-based)
	Type       ResolvedType // Type of the extracted field
}

Binding captures a field extraction from an ADT match.

type BuiltinCall

type BuiltinCall struct {
	Name string // Builtin name (e.g., "_str_trim", "_list_map")
	Args []Expr
}

BuiltinCall calls a builtin/stdlib function by name.

type Call

type Call struct {
	Func Expr   // The function to call (VarRef, GlobalRef, or Lambda)
	Args []Expr // Arguments
}

Call invokes a function.

type Cons

type Cons struct {
	Head Expr
	Tail Expr
}

Cons prepends an element to a list.

type Expr

type Expr interface {
	// contains filtered or unexported methods
}

Expr is an expression that produces a value.

type ExprStmt

type ExprStmt struct {
	Value Expr
	Line  int // source line (0 = unknown)
}

ExprStmt wraps an expression used as a statement (e.g., function call for side effects).

type FieldAccess

type FieldAccess struct {
	Record      Expr
	Field       string
	KnownFields []string
}

FieldAccess accesses a field of a record.

KnownFields, when non-empty, lists the record's full field set (in lexicographic order) as inferred by the type checker. The bytecode compiler uses this to resolve the GET_FIELD index for anonymous / row-polymorphic records that never produced an explicit TypeDecl. Empty means "no type info available — fall back to recordTypes lookup". Added for M-BYTECODE-MULTIMODULE M3.

type FieldInit

type FieldInit struct {
	Name  string
	Value Expr
}

FieldInit initializes one field of a record literal.

type FuncDecl

type FuncDecl struct {
	Name       string       // Function name (already namespaced by module)
	Params     []Param      // Parameters
	ReturnType ResolvedType // Return type
	Body       []Stmt       // Function body statements
	Return     Expr         // Final return expression
	Exported   bool         // Whether this function is public
	Module     string       // Source module name
	File       string       // Source file path (for diagnostics; optional)
	Line       int          // Source line of the func declaration (for diagnostics; 0 = unknown)

	// LowerError is non-empty when the lower pass caught a panic or error
	// while lowering this function's body. The bytecode compiler will skip
	// normal compilation of such functions and mark the prototype as
	// EvalOnly with this reason, so the bridge dispatches the call to the
	// evaluator at runtime. Body/Return may be empty or partial in this
	// case — consumers should not traverse them.
	LowerError string
}

FuncDecl declares a function.

type FuncType

type FuncType struct {
	Params []ResolvedType
	Return ResolvedType
}

FuncType represents a function type.

func (FuncType) GoString

func (f FuncType) GoString() string

type GlobalRef

type GlobalRef struct {
	Module string // Source module
	Name   string // Function/value name
}

GlobalRef references a module-qualified function or value.

type IfExpr

type IfExpr struct {
	Cond Expr
	Then Expr
	Else Expr
}

IfExpr is a conditional expression (if/then/else that returns a value).

type IfStmt

type IfStmt struct {
	Cond Expr
	Then []Stmt
	Else []Stmt // may be empty or contain another IfStmt for else-if chains
	Line int    // source line (0 = unknown)
}

IfStmt is a conditional branch.

type ImportSpec

type ImportSpec struct {
	Path  string // e.g., "strings", "fmt"
	Alias string // optional alias
}

ImportSpec represents a required import.

type InterfaceType

type InterfaceType struct{}

InterfaceType represents an unresolved or dynamic type (interface{}). This should be used sparingly — only at boundaries where type info is lost.

func (InterfaceType) GoString

func (InterfaceType) GoString() string

type Lambda

type Lambda struct {
	Params []Param
	Body   []Stmt
	Return Expr
}

Lambda is an anonymous function.

type ListLit

type ListLit struct {
	ElemType ResolvedType // Element type
	Elems    []Expr
}

ListLit constructs a list value.

type LitBool

type LitBool struct{ Value bool }

LitBool is a boolean literal.

type LitFloat

type LitFloat struct{ Value float64 }

LitFloat is a float literal.

type LitInt

type LitInt struct{ Value int64 }

LitInt is an integer literal.

type LitString

type LitString struct{ Value string }

LitString is a string literal.

type LitUnit

type LitUnit struct{}

LitUnit is the unit value.

type MapType

type MapType struct{}

MapType represents a map[string]interface{} (AILANG's Map type).

func (MapType) GoString

func (MapType) GoString() string

type NamedType

type NamedType struct {
	Name    string // Type name (e.g., "Color", "Position")
	Pointer bool   // Whether to use pointer semantics (true for ADTs)
}

NamedType references a user-defined type (ADT or record) by name.

func (NamedType) GoString

func (n NamedType) GoString() string

type Param

type Param struct {
	Name string
	Type ResolvedType
}

Param is a function parameter.

type PrimitiveKind

type PrimitiveKind int

PrimitiveKind enumerates primitive types.

const (
	PrimInt PrimitiveKind = iota
	PrimFloat
	PrimBool
	PrimString
	PrimUnit
)

type PrimitiveType

type PrimitiveType struct {
	Kind PrimitiveKind
}

PrimitiveType represents a basic type.

func (PrimitiveType) GoString

func (p PrimitiveType) GoString() string

type Program

type Program struct {
	Package   string       // Target package/module name
	TypeDecls []TypeDecl   // ADT and record type declarations
	FuncDecls []FuncDecl   // Function declarations
	Imports   []ImportSpec // Required imports (e.g., "fmt", "strings")
}

Program represents a complete compilation unit ready for emission.

type RecordDecl

type RecordDecl struct {
	Fields []RecordField
}

RecordDecl declares a record (struct) type.

type RecordField

type RecordField struct {
	Name string
	Type ResolvedType
}

RecordField is a named field in a record.

type RecordLit

type RecordLit struct {
	TypeName string // The record type name
	Fields   []FieldInit
}

RecordLit constructs a record value.

type RecordUpdate

type RecordUpdate struct {
	Base   Expr
	Fields []FieldInit
}

RecordUpdate creates a new record with some fields changed.

type ResolvedType

type ResolvedType interface {
	GoString() string // Go type representation (for the Go emitter)
	// contains filtered or unexported methods
}

ResolvedType is a fully-resolved type with no type variables. Type projection is a pure function: same input always produces same output. If a type variable is encountered, projection MUST error (no silent defaults).

type ReturnStmt

type ReturnStmt struct {
	Value Expr
	Line  int // source line (0 = unknown)
}

ReturnStmt returns a value from a function.

type SliceType

type SliceType struct {
	Elem ResolvedType
}

SliceType represents a list/array as a Go slice.

func (SliceType) GoString

func (s SliceType) GoString() string

type Stmt

type Stmt interface {
	// contains filtered or unexported methods
}

Stmt is a statement in a function body.

type SwitchCase

type SwitchCase struct {
	Tag      string    // ADT variant tag to match
	Bindings []Binding // Variables bound from the matched variant's fields
	Body     []Stmt
}

SwitchCase is one arm of a switch.

type SwitchStmt

type SwitchStmt struct {
	Scrutinee Expr
	ADTName   string // The ADT type name (e.g., "Color") — helps emitters generate typed constants
	Cases     []SwitchCase
	Default   []Stmt // may be empty; if non-empty, must end in panic or return
	Line      int    // source line (0 = unknown)
}

SwitchStmt dispatches on a value (used for ADT tag matching).

type TupleLit

type TupleLit struct {
	Elems []Expr
}

TupleLit constructs a tuple value.

type TupleType

type TupleType struct {
	Elems []ResolvedType
}

TupleType represents a tuple (compiled as a struct).

func (TupleType) GoString

func (t TupleType) GoString() string

type TypeAliasDecl

type TypeAliasDecl struct {
	Target ResolvedType
}

TypeAliasDecl declares a type alias.

type TypeAssert

type TypeAssert struct {
	Value Expr
	Type  ResolvedType
}

TypeAssert asserts a value to a specific type (used at interface{} boundaries).

type TypeDecl

type TypeDecl struct {
	Name     string
	Kind     TypeDeclKind
	Exported bool
}

TypeDecl declares a named type.

type TypeDeclKind

type TypeDeclKind interface {
	// contains filtered or unexported methods
}

TypeDeclKind distinguishes ADTs from records.

type UnOp

type UnOp struct {
	Op      UnOpKind
	Operand Expr
}

UnOp is a unary operation.

type UnOpKind

type UnOpKind int

UnOpKind enumerates unary operators.

const (
	OpNeg UnOpKind = iota
	OpNot
)

type VarDecl

type VarDecl struct {
	Name  string
	Type  ResolvedType // may be nil if type is inferred
	Value Expr
	Line  int // source line (0 = unknown)
}

VarDecl declares a variable with an initial value.

type VarRef

type VarRef struct{ Name string }

VarRef references a local variable.

Jump to

Keyboard shortcuts

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