compiler

package
v0.0.0-...-d353cf6 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompileSource

func CompileSource(source, filename string) (*runtime.CodeObject, []error)

CompileSource compiles Python source code to a code object

Types

type CompileError

type CompileError struct {
	Pos     model.Position
	Message string
}

CompileError represents a compilation error

func (CompileError) Error

func (e CompileError) Error() string

type Compiler

type Compiler struct {
	// contains filtered or unexported fields
}

Compiler compiles AST to bytecode

func NewCompiler

func NewCompiler(filename string) *Compiler

NewCompiler creates a new compiler

func (*Compiler) Compile

func (c *Compiler) Compile(module *model.Module) (*runtime.CodeObject, []CompileError)

Compile compiles a module to bytecode

type LexError

type LexError struct {
	Pos     model.Position
	Message string
}

LexError represents a lexical error.

func (LexError) Error

func (e LexError) Error() string

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes Python source code.

func NewLexer

func NewLexer(source string) *Lexer

NewLexer creates a new lexer for the given source.

func NewLexerWithFilename

func NewLexerWithFilename(source, filename string) *Lexer

NewLexerWithFilename creates a new lexer with a filename for error messages.

func (*Lexer) Errors

func (l *Lexer) Errors() []LexError

Errors returns any lexical errors encountered.

func (*Lexer) GetTokens

func (l *Lexer) GetTokens() []model.Token

GetTokens returns all tokens (for backwards compatibility).

func (*Lexer) NextToken

func (l *Lexer) NextToken() model.Token

NextToken returns the next token from the source.

func (*Lexer) Tokenize

func (l *Lexer) Tokenize() ([]model.Token, []LexError)

Tokenize scans the entire source and returns all tokens.

type Optimizer

type Optimizer struct {
	// contains filtered or unexported fields
}

Optimizer performs various compile-time optimizations

func NewOptimizer

func NewOptimizer() *Optimizer

NewOptimizer creates a new optimizer

func (*Optimizer) EliminateDeadCode

func (o *Optimizer) EliminateDeadCode(stmts []model.Stmt) []model.Stmt

EliminateDeadCode removes unreachable code from statement list

func (*Optimizer) FoldConstants

func (o *Optimizer) FoldConstants(expr model.Expr) model.Expr

FoldConstants attempts to evaluate constant expressions in the AST Returns the simplified expression, or the original if not foldable

func (*Optimizer) OptimizeIf

func (o *Optimizer) OptimizeIf(s *model.If) []model.Stmt

OptimizeIf optimizes if statements, removing dead branches

func (*Optimizer) OptimizeLoop

func (o *Optimizer) OptimizeLoop(stmts []model.Stmt) []model.Stmt

OptimizeLoop performs loop-invariant code motion on for/while loops It hoists expressions that don't depend on loop variables out of the loop

func (*Optimizer) PeepholeOptimize

func (o *Optimizer) PeepholeOptimize(code *runtime.CodeObject)

PeepholeOptimize optimizes the generated bytecode

func (*Optimizer) ReduceStrength

func (o *Optimizer) ReduceStrength(expr model.Expr) model.Expr

ReduceStrength applies strength reduction optimizations

func (*Optimizer) SpecializeBinaryOps

func (o *Optimizer) SpecializeBinaryOps(instrs []*instruction, code *runtime.CodeObject) bool

SpecializeBinaryOps converts generic binary ops to specialized int versions when we can prove or heuristically determine operands are integers

type ParseError

type ParseError struct {
	Pos     model.Position
	Message string
}

ParseError represents a parsing error.

func (ParseError) Error

func (e ParseError) Error() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses Python source code into an AST.

func NewParser

func NewParser(source string) *Parser

NewParser creates a new parser for the given source.

func NewParserWithFilename

func NewParserWithFilename(source, filename string) *Parser

NewParserWithFilename creates a new parser with a filename for error messages.

func (*Parser) Errors

func (p *Parser) Errors() []ParseError

Errors returns the parsing errors.

func (*Parser) Parse

func (p *Parser) Parse() (*model.Module, []ParseError)

Parse parses the source and returns the AST module.

type ScopeType

type ScopeType int

Scope types for variable resolution

const (
	ScopeModule ScopeType = iota
	ScopeFunction
	ScopeClass
	ScopeComprehension
)

type Symbol

type Symbol struct {
	Name          string
	Scope         SymbolScope
	Index         int
	OriginalIndex int // Original local index before becoming a cell (-1 if N/A)
}

Symbol represents a variable in a scope

type SymbolScope

type SymbolScope int

SymbolScope indicates where a variable is stored

const (
	ScopeLocal SymbolScope = iota
	ScopeGlobal
	ScopeBuiltin
	ScopeFree
	ScopeCell
)

type SymbolTable

type SymbolTable struct {
	// contains filtered or unexported fields
}

SymbolTable tracks variables in a scope

func NewSymbolTable

func NewSymbolTable(scopeType ScopeType, outer *SymbolTable) *SymbolTable

func (*SymbolTable) Define

func (st *SymbolTable) Define(name string) *Symbol

func (*SymbolTable) DefineGlobal

func (st *SymbolTable) DefineGlobal(name string) *Symbol

func (*SymbolTable) DefineInEnclosingScope

func (st *SymbolTable) DefineInEnclosingScope(name string) *Symbol

DefineInEnclosingScope defines a variable in the first non-comprehension outer scope. This is used for walrus operator (:=) in comprehensions, where the variable should be accessible in the enclosing scope per PEP 572.

func (*SymbolTable) DefineNonlocal

func (st *SymbolTable) DefineNonlocal(name string) *Symbol

func (*SymbolTable) GetEnclosingScopeType

func (st *SymbolTable) GetEnclosingScopeType() ScopeType

GetEnclosingScopeType returns the scope type of the first non-comprehension enclosing scope.

func (*SymbolTable) IsInsideClass

func (st *SymbolTable) IsInsideClass() (*SymbolTable, bool)

IsInsideClass returns true if this scope or any ancestor is a class scope. Also returns the class scope's SymbolTable if found.

func (*SymbolTable) MarkAsCell

func (st *SymbolTable) MarkAsCell(name string)

MarkAsCell marks a variable as needing to be stored in a cell (because it's captured by an inner scope)

func (*SymbolTable) Resolve

func (st *SymbolTable) Resolve(name string) (*Symbol, bool)

Jump to

Keyboard shortcuts

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