Documentation
¶
Overview ¶
Package ast provides the abstract syntax tree types for M28. This layer sits between parsing and evaluation, enabling: - Multiple frontend languages (Python, Lisp, DSLs) - Source location tracking for error messages - Type annotation preservation - AST-based optimizations and tooling
Index ¶
- type ASTNode
- type AnnotatedAssignForm
- type AssertForm
- type AssignForm
- type BaseNode
- type BlockForm
- type BreakForm
- type CaseClause
- type ClassForm
- type ComprehensionClause
- type ComprehensionForm
- type ComprehensionKind
- type ContinueForm
- type DefForm
- type ExceptClause
- type ForForm
- type Identifier
- type IfForm
- type Literal
- type MatchForm
- type Parameter
- type PassForm
- type RaiseForm
- type ReturnForm
- type SExpr
- type SyntaxKind
- type TryForm
- type TypeInfo
- type WhileForm
- type WithForm
- type WithItem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ASTNode ¶
type ASTNode interface {
core.Value // Still a Value for backwards compatibility during migration
// Location returns the source location of this node
Location() *core.SourceLocation
// SyntaxKind returns which frontend produced this node
SyntaxKind() SyntaxKind
// Comments returns any comments attached to this node
Comments() []string
// TypeAnnotation returns the type annotation, if any
TypeAnnotation() *TypeInfo
// ToIR lowers this AST node to core.Value for evaluation
// This is where desugaring happens (Pythonic → Lisp)
ToIR() core.Value
}
ASTNode represents a node in the abstract syntax tree All AST nodes implement this interface
type AnnotatedAssignForm ¶
type AnnotatedAssignForm struct {
BaseNode
Target ASTNode // What to assign to (usually Identifier)
Annotation *TypeInfo // Type annotation
Value ASTNode // Value to assign (nil for annotation-only: x: int)
}
AnnotatedAssignForm represents an annotated assignment (PEP 526): x: int = 5
func NewAnnotatedAssignForm ¶
func NewAnnotatedAssignForm(target ASTNode, annotation *TypeInfo, value ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *AnnotatedAssignForm
NewAnnotatedAssignForm creates a new annotated assignment node
func (*AnnotatedAssignForm) String ¶
func (a *AnnotatedAssignForm) String() string
String implements core.Value.String
func (*AnnotatedAssignForm) ToIR ¶
func (a *AnnotatedAssignForm) ToIR() core.Value
ToIR implements ASTNode.ToIR
func (*AnnotatedAssignForm) Type ¶
func (a *AnnotatedAssignForm) Type() core.Type
Type implements core.Value.Type
type AssertForm ¶
type AssertForm struct {
BaseNode
Condition ASTNode // The condition to assert
Message ASTNode // Optional message (nil if not provided)
}
AssertForm represents the assert statement
func NewAssertForm ¶
func NewAssertForm(condition, message ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *AssertForm
NewAssertForm creates a new assert statement
func (*AssertForm) String ¶
func (a *AssertForm) String() string
String implements core.Value.String
type AssignForm ¶
type AssignForm struct {
BaseNode
Target ASTNode // What to assign to (usually Identifier)
Value ASTNode // Value to assign
}
AssignForm represents an assignment statement Examples:
Lisp: (= x 10) Python: x = 10
func NewAssignForm ¶
func NewAssignForm(target ASTNode, value ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *AssignForm
NewAssignForm creates a new assignment node
func (*AssignForm) String ¶
func (a *AssignForm) String() string
String implements core.Value.String
type BaseNode ¶
type BaseNode struct {
Loc *core.SourceLocation // Source location (file, line, column)
Syntax SyntaxKind // Which syntax produced this
Comment []string // Attached comments
Type *TypeInfo // Type annotation (optional)
}
BaseNode provides common fields that all AST nodes embed
func (*BaseNode) Location ¶
func (b *BaseNode) Location() *core.SourceLocation
Location implements ASTNode.Location
func (*BaseNode) SyntaxKind ¶
func (b *BaseNode) SyntaxKind() SyntaxKind
SyntaxKind implements ASTNode.SyntaxKind
func (*BaseNode) TypeAnnotation ¶
TypeAnnotation implements ASTNode.TypeAnnotation
type BlockForm ¶
BlockForm represents a sequence of statements (do block)
func NewBlockForm ¶
func NewBlockForm(statements []ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *BlockForm
NewBlockForm creates a new block statement
type BreakForm ¶
type BreakForm struct {
BaseNode
}
BreakForm represents the break statement
func NewBreakForm ¶
func NewBreakForm(loc *core.SourceLocation, syntax SyntaxKind) *BreakForm
NewBreakForm creates a new break statement
type CaseClause ¶
type CaseClause struct {
Pattern ASTNode // The pattern to match (can be literal, identifier, etc.)
Guard ASTNode // Optional if guard (can be nil)
Body []ASTNode // Statements to execute if matched
}
CaseClause represents a single case in a match statement
type ClassForm ¶
type ClassForm struct {
BaseNode
Name string
Bases []ASTNode // Base classes
Body []ASTNode // Methods and attributes
Decorators []ASTNode // Class decorators
Keywords []ASTNode // Keyword arguments (e.g., metaclass=...)
}
ClassForm represents a class definition
func NewClassForm ¶
func NewClassForm(name string, bases, body, decorators, keywords []ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *ClassForm
NewClassForm creates a new class definition
type ComprehensionClause ¶
type ComprehensionClause struct {
Variable string // Loop variable
Iterable ASTNode // What to iterate over
Condition ASTNode // Optional filter (nil if none)
}
ComprehensionClause represents a single for clause in a comprehension e.g., "for x in items if x > 0"
type ComprehensionForm ¶
type ComprehensionForm struct {
BaseNode
Kind ComprehensionKind
Element ASTNode // For list/set/generator: the expression
KeyExpr ASTNode // For dict: key expression
ValueExpr ASTNode // For dict: value expression
Clauses []ComprehensionClause // One or more for clauses
// Deprecated fields - kept for backward compatibility
Variable string // Use Clauses instead
Iterable ASTNode // Use Clauses instead
Condition ASTNode // Use Clauses instead
}
ComprehensionForm represents list/dict/set comprehensions and generator expressions Supports nested comprehensions via multiple clauses
func NewComprehensionForm ¶
func NewComprehensionForm(kind ComprehensionKind, element, keyExpr, valueExpr ASTNode, variable string, iterable, condition ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *ComprehensionForm
NewComprehensionForm creates a new comprehension (legacy single-clause version)
func NewComprehensionFormMulti ¶
func NewComprehensionFormMulti(kind ComprehensionKind, element, keyExpr, valueExpr ASTNode, clauses []ComprehensionClause, loc *core.SourceLocation, syntax SyntaxKind) *ComprehensionForm
NewComprehensionFormMulti creates a new comprehension with multiple clauses (for nested comprehensions)
func (*ComprehensionForm) String ¶
func (c *ComprehensionForm) String() string
String implements core.Value.String
func (*ComprehensionForm) ToIR ¶
func (c *ComprehensionForm) ToIR() core.Value
ToIR lowers comprehension to IR
func (*ComprehensionForm) Type ¶
func (c *ComprehensionForm) Type() core.Type
Type implements core.Value.Type
type ComprehensionKind ¶
type ComprehensionKind int
ComprehensionKind represents the type of comprehension
const ( ListComp ComprehensionKind = iota DictComp SetComp GeneratorComp )
func (ComprehensionKind) String ¶
func (ck ComprehensionKind) String() string
type ContinueForm ¶
type ContinueForm struct {
BaseNode
}
ContinueForm represents the continue statement
func NewContinueForm ¶
func NewContinueForm(loc *core.SourceLocation, syntax SyntaxKind) *ContinueForm
NewContinueForm creates a new continue statement
func (*ContinueForm) String ¶
func (c *ContinueForm) String() string
String implements core.Value.String
type DefForm ¶
type DefForm struct {
BaseNode
Name string // Function name
Params []Parameter // Parameters with optional types and defaults
Body ASTNode // Function body
ReturnType *TypeInfo // Return type annotation (optional)
Decorators []ASTNode // Function decorators (e.g., @property, @staticmethod)
IsAsync bool // Whether this is an async function
}
DefForm represents a function definition Examples:
Lisp: (def foo (x y) (+ x y)) Python: def foo(x: int, y: int) -> int: return x + y
func NewDefForm ¶
func NewDefForm(name string, params []Parameter, body ASTNode, returnType *TypeInfo, decorators []ASTNode, isAsync bool, loc *core.SourceLocation, syntax SyntaxKind) *DefForm
NewDefForm creates a new function definition node
type ExceptClause ¶
type ExceptClause struct {
ExceptionType string // Empty string for bare except (deprecated - use ExceptionTypeExpr)
ExceptionTypeExpr ASTNode // The actual exception type expression (identifier, tuple, etc.)
Variable string // Empty string if no 'as' clause
Body []ASTNode
}
ExceptClause represents an except clause in a try statement
type ForForm ¶
type ForForm struct {
BaseNode
Variable string // Deprecated: use Variables instead
Variables []string // Loop variables (for tuple unpacking)
Iterable ASTNode
Body []ASTNode
ElseBody []ASTNode // Optional else clause (Python feature)
}
ForForm represents a for loop
func NewForForm ¶
func NewForForm(variable string, iterable ASTNode, body, elseBody []ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *ForForm
NewForForm creates a new for loop with a single variable (backward compatible)
func NewForFormMulti ¶
func NewForFormMulti(variables []string, iterable ASTNode, body, elseBody []ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *ForForm
NewForFormMulti creates a new for loop with multiple variables (tuple unpacking)
type Identifier ¶
Identifier represents a symbol/variable name Examples: x, foo, print, +
func NewIdentifier ¶
func NewIdentifier(name string, loc *core.SourceLocation, syntax SyntaxKind) *Identifier
NewIdentifier creates a new identifier node
func (*Identifier) String ¶
func (i *Identifier) String() string
String implements core.Value.String
type IfForm ¶
type IfForm struct {
BaseNode
Condition ASTNode // Condition to test
ThenBranch ASTNode // Branch when condition is true
ElseBranch ASTNode // Branch when condition is false (optional)
}
IfForm represents a conditional expression Examples:
Lisp: (if condition then-branch else-branch) Python: if condition: then_branch else: else_branch
func NewIfForm ¶
func NewIfForm(condition ASTNode, thenBranch ASTNode, elseBranch ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *IfForm
NewIfForm creates a new if expression node
type Literal ¶
Literal represents a literal value (number, string, bool, nil) Examples: 42, "hello", True, None
func NewLiteral ¶
func NewLiteral(value core.Value, loc *core.SourceLocation, syntax SyntaxKind) *Literal
NewLiteral creates a new literal node
type MatchForm ¶
type MatchForm struct {
BaseNode
Subject ASTNode // The value being matched
Cases []CaseClause // List of case clauses
}
MatchForm represents a match statement
func NewMatchForm ¶
func NewMatchForm(subject ASTNode, cases []CaseClause, loc *core.SourceLocation, syntax SyntaxKind) *MatchForm
NewMatchForm creates a new match statement
type Parameter ¶
type Parameter struct {
Name string // Parameter name
Type *TypeInfo // Type annotation (optional)
Default ASTNode // Default value (optional)
IsVarArgs bool // True for *args parameters
IsKwargs bool // True for **kwargs parameters
}
Parameter represents a function parameter with optional type annotation and default value
type PassForm ¶
type PassForm struct {
BaseNode
}
PassForm represents the pass statement
func NewPassForm ¶
func NewPassForm(loc *core.SourceLocation, syntax SyntaxKind) *PassForm
NewPassForm creates a new pass statement
type RaiseForm ¶
type RaiseForm struct {
BaseNode
Exception ASTNode // nil for bare raise (re-raise)
Cause ASTNode // nil unless 'raise X from Y'
}
RaiseForm represents the raise statement
func NewRaiseForm ¶
func NewRaiseForm(exception, cause ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *RaiseForm
NewRaiseForm creates a new raise statement
type ReturnForm ¶
ReturnForm represents the return statement
func NewReturnForm ¶
func NewReturnForm(value ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *ReturnForm
NewReturnForm creates a new return statement
func (*ReturnForm) String ¶
func (r *ReturnForm) String() string
String implements core.Value.String
type SExpr ¶
SExpr represents an S-expression (function call, special form, list) Examples: (+ 1 2), (def foo (x) (* x 2)), (if x y z)
func NewSExpr ¶
func NewSExpr(elements []ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *SExpr
NewSExpr creates a new S-expression node
type SyntaxKind ¶
type SyntaxKind int
SyntaxKind identifies which frontend produced this AST node
const ( // SyntaxLisp indicates M28 Lisp syntax: (def foo (x) (* x 2)) SyntaxLisp SyntaxKind = iota // SyntaxPython indicates Python syntax: def foo(x): return x * 2 SyntaxPython // SyntaxJSON indicates JSON/configuration DSL syntax SyntaxJSON // SyntaxCustom indicates a custom DSL SyntaxCustom )
func (SyntaxKind) String ¶
func (sk SyntaxKind) String() string
type TryForm ¶
type TryForm struct {
BaseNode
TryBody []ASTNode
ExceptClauses []ExceptClause
ElseBody []ASTNode // Optional (runs if no exception)
FinallyBody []ASTNode // Optional
}
TryForm represents a try/except/finally statement
func NewTryForm ¶
func NewTryForm(tryBody []ASTNode, exceptClauses []ExceptClause, elseBody, finallyBody []ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *TryForm
NewTryForm creates a new try statement
type TypeInfo ¶
type TypeInfo struct {
Name string // "int", "str", "List", "Dict", etc.
Generic []*TypeInfo // For List[int], Dict[str, int]
IsOptional bool // For Optional[T]
}
TypeInfo represents type annotations (for gradual typing) Examples:
int → TypeInfo{Name: "int"}
List[int] → TypeInfo{Name: "List", Generic: []{Name: "int"}}
Dict[str, int] → TypeInfo{Name: "Dict", Generic: []{Name: "str"}, {Name: "int"}}
Optional[str] → TypeInfo{Name: "str", IsOptional: true}
type WhileForm ¶
type WhileForm struct {
BaseNode
Condition ASTNode
Body []ASTNode
ElseBody []ASTNode // Optional else clause
}
WhileForm represents a while loop
func NewWhileForm ¶
func NewWhileForm(condition ASTNode, body, elseBody []ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *WhileForm
NewWhileForm creates a new while loop
type WithForm ¶
WithForm represents a with statement (context manager)
func NewWithForm ¶
func NewWithForm(items []WithItem, body []ASTNode, loc *core.SourceLocation, syntax SyntaxKind) *WithForm
NewWithForm creates a new with statement