Documentation ¶
Index ¶
- Constants
- Variables
- func FormatInstructions(b []byte, posOffset int) []string
- func MakeInstruction(opcode Opcode, operands ...int) []byte
- func ReadOperands(numOperands []int, ins []byte) (operands []int, offset int)
- type Bytecode
- type CompilationScope
- type Compiler
- type EmittedInstruction
- type Error
- type Loop
- type ModuleLoader
- type Opcode
- type Symbol
- type SymbolScope
- type SymbolTable
- func (t *SymbolTable) BuiltinSymbols() []*Symbol
- func (t *SymbolTable) Define(name string) *Symbol
- func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol
- func (t *SymbolTable) Fork(block bool) *SymbolTable
- func (t *SymbolTable) FreeSymbols() []*Symbol
- func (t *SymbolTable) MaxSymbols() int
- func (t *SymbolTable) Names() []string
- func (t *SymbolTable) Parent(skipBlock bool) *SymbolTable
- func (t *SymbolTable) Resolve(name string) (symbol *Symbol, depth int, ok bool)
Constants ¶
const ( ScopeGlobal SymbolScope = "GLOBAL" ScopeLocal = "LOCAL" ScopeBuiltin = "BUILTIN" ScopeFree = "FREE" )
List of symbol scopes
Variables ¶
var OpcodeNames = [...]string{ OpConstant: "CONST", OpPop: "POP", OpTrue: "TRUE", OpFalse: "FALSE", OpAdd: "ADD", OpSub: "SUB", OpMul: "MUL", OpDiv: "DIV", OpRem: "REM", OpBAnd: "AND", OpBOr: "OR", OpBXor: "XOR", OpBAndNot: "ANDN", OpBShiftLeft: "SHL", OpBShiftRight: "SHR", OpBComplement: "NEG", OpEqual: "EQL", OpNotEqual: "NEQ", OpGreaterThan: "GTR", OpGreaterThanEqual: "GEQ", OpMinus: "NEG", OpLNot: "NOT", OpJumpFalsy: "JMPF", OpAndJump: "ANDJMP", OpOrJump: "ORJMP", OpJump: "JMP", OpNull: "NULL", OpGetGlobal: "GETG", OpSetGlobal: "SETG", OpSetSelGlobal: "SETSG", OpArray: "ARR", OpMap: "MAP", OpError: "ERROR", OpImmutable: "IMMUT", OpIndex: "INDEX", OpSliceIndex: "SLICE", OpCall: "CALL", OpReturn: "RET", OpReturnValue: "RETVAL", OpGetLocal: "GETL", OpSetLocal: "SETL", OpDefineLocal: "DEFL", OpSetSelLocal: "SETSL", OpGetBuiltin: "BUILTIN", OpClosure: "CLOSURE", OpGetFreePtr: "GETFP", OpGetFree: "GETF", OpSetFree: "SETF", OpGetLocalPtr: "GETLP", OpSetSelFree: "SETSF", OpIteratorInit: "ITER", OpIteratorNext: "ITNXT", OpIteratorKey: "ITKEY", OpIteratorValue: "ITVAL", }
OpcodeNames is opcode names.
var OpcodeOperands = [...][]int{ OpConstant: {2}, OpPop: {}, OpTrue: {}, OpFalse: {}, OpAdd: {}, OpSub: {}, OpMul: {}, OpDiv: {}, OpRem: {}, OpBAnd: {}, OpBOr: {}, OpBXor: {}, OpBAndNot: {}, OpBShiftLeft: {}, OpBShiftRight: {}, OpBComplement: {}, OpEqual: {}, OpNotEqual: {}, OpGreaterThan: {}, OpGreaterThanEqual: {}, OpMinus: {}, OpLNot: {}, OpJumpFalsy: {2}, OpAndJump: {2}, OpOrJump: {2}, OpJump: {2}, OpNull: {}, OpGetGlobal: {2}, OpSetGlobal: {2}, OpSetSelGlobal: {2, 1}, OpArray: {2}, OpMap: {2}, OpError: {}, OpImmutable: {}, OpIndex: {}, OpSliceIndex: {}, OpCall: {1}, OpReturn: {}, OpReturnValue: {}, OpGetLocal: {1}, OpSetLocal: {1}, OpDefineLocal: {1}, OpSetSelLocal: {1, 1}, OpGetBuiltin: {1}, OpClosure: {2, 1}, OpGetFreePtr: {1}, OpGetFree: {1}, OpSetFree: {1}, OpGetLocalPtr: {1}, OpSetSelFree: {1, 1}, OpIteratorInit: {}, OpIteratorNext: {}, OpIteratorKey: {}, OpIteratorValue: {}, }
OpcodeOperands is the number of operands.
Functions ¶
func FormatInstructions ¶
FormatInstructions returns string representation of bytecode instructions.
func MakeInstruction ¶
MakeInstruction returns a bytecode for an opcode and the operands.
Types ¶
type Bytecode ¶
type Bytecode struct { FileSet *source.FileSet MainFunction *objects.CompiledFunction Constants []objects.Object }
Bytecode is a compiled instructions and constants.
func (*Bytecode) CountObjects ¶ added in v1.13.0
CountObjects returns the number of objects found in Constants.
func (*Bytecode) FormatConstants ¶ added in v1.8.2
FormatConstants returns human readable string representations of compiled constants.
func (*Bytecode) FormatInstructions ¶ added in v1.8.2
FormatInstructions returns human readable string representations of compiled instructions.
func (*Bytecode) RemoveDuplicates ¶ added in v1.13.0
func (b *Bytecode) RemoveDuplicates()
RemoveDuplicates finds and remove the duplicate values in Constants. Note this function mutates Bytecode.
type CompilationScope ¶
type CompilationScope struct {
// contains filtered or unexported fields
}
CompilationScope represents a compiled instructions and the last two instructions that were emitted.
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler compiles the AST into a bytecode.
func NewCompiler ¶
func NewCompiler(file *source.File, symbolTable *SymbolTable, constants []objects.Object, modules *objects.ModuleMap, trace io.Writer) *Compiler
NewCompiler creates a Compiler.
func (*Compiler) EnableFileImport ¶ added in v1.17.0
EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.
type EmittedInstruction ¶
EmittedInstruction represents an opcode with its emitted position.
type Error ¶ added in v1.9.0
type Error struct {
// contains filtered or unexported fields
}
Error represents a compiler error.
type ModuleLoader ¶
ModuleLoader should take a module name and return the module data.
type Opcode ¶
type Opcode = byte
Opcode represents a single byte operation code.
const ( OpConstant Opcode = iota // Load constant OpAdd // Add OpSub // Sub OpMul // Multiply OpDiv // Divide OpRem // Remainder OpBAnd // bitwise AND OpBOr // bitwise OR OpBXor // bitwise XOR OpBShiftLeft // bitwise shift left OpBShiftRight // bitwise shift right OpBAndNot // bitwise AND NOT OpBComplement // bitwise complement OpPop // Pop OpTrue // Push true OpFalse // Push false OpEqual // Equal == OpNotEqual // Not equal != OpGreaterThan // Greater than >= OpGreaterThanEqual // Greater than or equal to >= OpMinus // Minus - OpLNot // Logical not ! OpJumpFalsy // Jump if falsy OpAndJump // Logical AND jump OpOrJump // Logical OR jump OpJump // Jump OpNull // Push null OpArray // Array object OpMap // Map object OpError // Error object OpImmutable // Immutable object OpIndex // Index operation OpSliceIndex // Slice operation OpCall // Call function OpReturn // Return OpReturnValue // Return value OpGetGlobal // Get global variable OpSetGlobal // Set global variable OpSetSelGlobal // Set global variable using selectors OpGetLocal // Get local variable OpSetLocal // Set local variable OpDefineLocal // Define local variable OpSetSelLocal // Set local variable using selectors OpGetFreePtr // Get free variable pointer object OpGetFree // Get free variables OpSetFree // Set free variables OpGetLocalPtr // Get local variable as a pointer OpSetSelFree // Set free variables using selectors OpGetBuiltin // Get builtin function OpClosure // Push closure OpIteratorInit // Iterator init OpIteratorNext // Iterator next OpIteratorKey // Iterator key OpIteratorValue // Iterator value )
List of opcodes
type Symbol ¶
type Symbol struct { Name string Scope SymbolScope Index int LocalAssigned bool // if the local symbol is assigned at least once }
Symbol represents a symbol in the symbol table.
type SymbolTable ¶
type SymbolTable struct {
// contains filtered or unexported fields
}
SymbolTable represents a symbol table.
func (*SymbolTable) BuiltinSymbols ¶ added in v1.10.1
func (t *SymbolTable) BuiltinSymbols() []*Symbol
BuiltinSymbols returns builtin symbols for the scope.
func (*SymbolTable) Define ¶
func (t *SymbolTable) Define(name string) *Symbol
Define adds a new symbol in the current scope.
func (*SymbolTable) DefineBuiltin ¶
func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol
DefineBuiltin adds a symbol for builtin function.
func (*SymbolTable) Fork ¶
func (t *SymbolTable) Fork(block bool) *SymbolTable
Fork creates a new symbol table for a new scope.
func (*SymbolTable) FreeSymbols ¶
func (t *SymbolTable) FreeSymbols() []*Symbol
FreeSymbols returns free symbols for the scope.
func (*SymbolTable) MaxSymbols ¶
func (t *SymbolTable) MaxSymbols() int
MaxSymbols returns the total number of symbols defined in the scope.
func (*SymbolTable) Names ¶
func (t *SymbolTable) Names() []string
Names returns the name of all the symbols.
func (*SymbolTable) Parent ¶
func (t *SymbolTable) Parent(skipBlock bool) *SymbolTable
Parent returns the outer scope of the current symbol table.
Source Files ¶
- bytecode.go
- bytecode_decode.go
- bytecode_optimize.go
- compilation_scope.go
- compiler.go
- compiler_assign.go
- compiler_for.go
- compiler_logical.go
- compiler_loops.go
- compiler_module.go
- compiler_scopes.go
- emitted_instruction.go
- error.go
- instructions.go
- loop.go
- module_loader.go
- opcodes.go
- symbol.go
- symbol_scopes.go
- symbol_table.go