Documentation
¶
Index ¶
- func CompileSource(source, filename string) (*runtime.CodeObject, []error)
- type CompileError
- type Compiler
- type LexError
- type Lexer
- type Optimizer
- func (o *Optimizer) EliminateDeadCode(stmts []model.Stmt) []model.Stmt
- func (o *Optimizer) FoldConstants(expr model.Expr) model.Expr
- func (o *Optimizer) OptimizeIf(s *model.If) []model.Stmt
- func (o *Optimizer) OptimizeLoop(stmts []model.Stmt) []model.Stmt
- func (o *Optimizer) PeepholeOptimize(code *runtime.CodeObject)
- func (o *Optimizer) ReduceStrength(expr model.Expr) model.Expr
- func (o *Optimizer) SpecializeBinaryOps(instrs []*instruction, code *runtime.CodeObject) bool
- type ParseError
- type Parser
- type ScopeType
- type Symbol
- type SymbolScope
- type SymbolTable
- func (st *SymbolTable) Define(name string) *Symbol
- func (st *SymbolTable) DefineGlobal(name string) *Symbol
- func (st *SymbolTable) DefineInEnclosingScope(name string) *Symbol
- func (st *SymbolTable) DefineNonlocal(name string) *Symbol
- func (st *SymbolTable) GetEnclosingScopeType() ScopeType
- func (st *SymbolTable) IsInsideClass() (*SymbolTable, bool)
- func (st *SymbolTable) MarkAsCell(name string)
- func (st *SymbolTable) Resolve(name string) (*Symbol, bool)
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 ¶
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 (*Compiler) Compile ¶
func (c *Compiler) Compile(module *model.Module) (*runtime.CodeObject, []CompileError)
Compile compiles a module to bytecode
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer tokenizes Python source code.
func NewLexerWithFilename ¶
NewLexerWithFilename creates a new lexer with a filename for error messages.
type Optimizer ¶
type Optimizer struct {
// contains filtered or unexported fields
}
Optimizer performs various compile-time optimizations
func (*Optimizer) EliminateDeadCode ¶
EliminateDeadCode removes unreachable code from statement list
func (*Optimizer) FoldConstants ¶
FoldConstants attempts to evaluate constant expressions in the AST Returns the simplified expression, or the original if not foldable
func (*Optimizer) OptimizeIf ¶
OptimizeIf optimizes if statements, removing dead branches
func (*Optimizer) OptimizeLoop ¶
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 ¶
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 ¶
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 NewParserWithFilename ¶
NewParserWithFilename creates a new parser with a filename for error messages.
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)