ast

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 3 Imported by: 0

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

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

func (*AssertForm) ToIR

func (a *AssertForm) ToIR() core.Value

ToIR lowers assert to IR

func (*AssertForm) Type

func (a *AssertForm) Type() core.Type

Type implements core.Value.Type

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

func (*AssignForm) ToIR

func (a *AssignForm) ToIR() core.Value

ToIR implements ASTNode.ToIR

func (*AssignForm) Type

func (a *AssignForm) Type() core.Type

Type implements core.Value.Type

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

func (b *BaseNode) Comments() []string

Comments implements ASTNode.Comments

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

func (b *BaseNode) TypeAnnotation() *TypeInfo

TypeAnnotation implements ASTNode.TypeAnnotation

type BlockForm

type BlockForm struct {
	BaseNode
	Statements []ASTNode
}

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

func (*BlockForm) String

func (b *BlockForm) String() string

String implements core.Value.String

func (*BlockForm) ToIR

func (b *BlockForm) ToIR() core.Value

ToIR lowers block to IR (do ...)

func (*BlockForm) Type

func (b *BlockForm) Type() core.Type

Type implements core.Value.Type

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

func (*BreakForm) String

func (b *BreakForm) String() string

String implements core.Value.String

func (*BreakForm) ToIR

func (b *BreakForm) ToIR() core.Value

ToIR lowers break to IR

func (*BreakForm) Type

func (b *BreakForm) Type() core.Type

Type implements core.Value.Type

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

func (*ClassForm) String

func (c *ClassForm) String() string

String implements core.Value.String

func (*ClassForm) ToIR

func (c *ClassForm) ToIR() core.Value

ToIR lowers class definition to IR

func (*ClassForm) Type

func (c *ClassForm) Type() core.Type

Type implements core.Value.Type

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

func (*ContinueForm) ToIR

func (c *ContinueForm) ToIR() core.Value

ToIR lowers continue to IR

func (*ContinueForm) Type

func (c *ContinueForm) Type() core.Type

Type implements core.Value.Type

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

func (*DefForm) String

func (d *DefForm) String() string

String implements core.Value.String

func (*DefForm) ToIR

func (d *DefForm) ToIR() core.Value

ToIR implements ASTNode.ToIR

func (*DefForm) Type

func (d *DefForm) Type() core.Type

Type implements core.Value.Type

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)

func (*ForForm) String

func (f *ForForm) String() string

String implements core.Value.String

func (*ForForm) ToIR

func (f *ForForm) ToIR() core.Value

ToIR lowers for loop to IR

func (*ForForm) Type

func (f *ForForm) Type() core.Type

Type implements core.Value.Type

type Identifier

type Identifier struct {
	BaseNode
	Name string
}

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

func (*Identifier) ToIR

func (i *Identifier) ToIR() core.Value

ToIR implements ASTNode.ToIR

func (*Identifier) Type

func (i *Identifier) Type() core.Type

Type implements core.Value.Type

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

func (*IfForm) String

func (i *IfForm) String() string

String implements core.Value.String

func (*IfForm) ToIR

func (i *IfForm) ToIR() core.Value

ToIR implements ASTNode.ToIR

func (*IfForm) Type

func (i *IfForm) Type() core.Type

Type implements core.Value.Type

type Literal

type Literal struct {
	BaseNode
	Value core.Value
}

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

func (*Literal) String

func (l *Literal) String() string

String implements core.Value.String

func (*Literal) ToIR

func (l *Literal) ToIR() core.Value

ToIR implements ASTNode.ToIR

func (*Literal) Type

func (l *Literal) Type() core.Type

Type implements core.Value.Type

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

func (*MatchForm) String

func (m *MatchForm) String() string

String implements core.Value.String

func (*MatchForm) ToIR

func (m *MatchForm) ToIR() core.Value

ToIR lowers match statement to IR For now, we'll convert to a series of if/elif statements

func (*MatchForm) Type

func (m *MatchForm) Type() core.Type

Type implements core.Value.Type

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

func (*PassForm) String

func (p *PassForm) String() string

String implements core.Value.String

func (*PassForm) ToIR

func (p *PassForm) ToIR() core.Value

ToIR lowers pass to IR (represented as None)

func (*PassForm) Type

func (p *PassForm) Type() core.Type

Type implements core.Value.Type

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

func (*RaiseForm) String

func (r *RaiseForm) String() string

String implements core.Value.String

func (*RaiseForm) ToIR

func (r *RaiseForm) ToIR() core.Value

ToIR lowers raise to IR

func (*RaiseForm) Type

func (r *RaiseForm) Type() core.Type

Type implements core.Value.Type

type ReturnForm

type ReturnForm struct {
	BaseNode
	Value ASTNode // nil for bare return
}

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

func (*ReturnForm) ToIR

func (r *ReturnForm) ToIR() core.Value

ToIR lowers return to IR

func (*ReturnForm) Type

func (r *ReturnForm) Type() core.Type

Type implements core.Value.Type

type SExpr

type SExpr struct {
	BaseNode
	Elements []ASTNode
}

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

func (*SExpr) String

func (s *SExpr) String() string

String implements core.Value.String

func (*SExpr) ToIR

func (s *SExpr) ToIR() core.Value

ToIR implements ASTNode.ToIR

func (*SExpr) Type

func (s *SExpr) Type() core.Type

Type implements core.Value.Type

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

func (*TryForm) String

func (t *TryForm) String() string

String implements core.Value.String

func (*TryForm) ToIR

func (t *TryForm) ToIR() core.Value

ToIR lowers try statement to IR

func (*TryForm) Type

func (t *TryForm) Type() core.Type

Type implements core.Value.Type

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}

func (*TypeInfo) String

func (ti *TypeInfo) String() string

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

func (*WhileForm) String

func (w *WhileForm) String() string

String implements core.Value.String

func (*WhileForm) ToIR

func (w *WhileForm) ToIR() core.Value

ToIR lowers while loop to IR

func (*WhileForm) Type

func (w *WhileForm) Type() core.Type

Type implements core.Value.Type

type WithForm

type WithForm struct {
	BaseNode
	Items []WithItem
	Body  []ASTNode
}

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

func (*WithForm) String

func (w *WithForm) String() string

String implements core.Value.String

func (*WithForm) ToIR

func (w *WithForm) ToIR() core.Value

ToIR lowers with statement to IR

func (*WithForm) Type

func (w *WithForm) Type() core.Type

Type implements core.Value.Type

type WithItem

type WithItem struct {
	Context  ASTNode
	Variable string // Empty string if no 'as' clause
}

WithItem represents a single context manager in a with statement

Jump to

Keyboard shortcuts

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