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 Loop
- type ModuleLoader
- type Opcode
- type Symbol
- type SymbolScope
- type SymbolTable
- 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", OpExport: "EXPORT", OpGetLocal: "GETL", OpSetLocal: "SETL", OpDefineLocal: "DEFL", OpSetSelLocal: "SETSL", OpGetBuiltin: "BUILTIN", OpClosure: "CLOSURE", OpGetFree: "GETF", OpSetFree: "SETF", 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: {}, OpExport: {}, OpGetLocal: {1}, OpSetLocal: {1}, OpDefineLocal: {1}, OpSetSelLocal: {1, 1}, OpGetBuiltin: {1}, OpClosure: {2, 1}, OpGetFree: {1}, OpSetFree: {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 ¶
Bytecode is a compiled instructions and constants.
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(symbolTable *SymbolTable, stdModules map[string]*objects.ImmutableMap, trace io.Writer) *Compiler
NewCompiler creates a Compiler. User can optionally provide the symbol table if one wants to add or remove some global- or builtin- scope symbols. If not (nil), Compile will create a new symbol table and use the default builtin functions. Likewise, standard modules can be explicitly provided if user wants to add or remove some modules. By default, Compile will use all the standard modules otherwise.
func (*Compiler) SetModuleLoader ¶
func (c *Compiler) SetModuleLoader(moduleLoader ModuleLoader)
SetModuleLoader sets or replaces the current module loader. Note that the module loader is used for user modules, not for the standard modules.
type EmittedInstruction ¶
EmittedInstruction represents an opcode with its emitted position.
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 OpExport // Export 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 OpGetFree // Get free variables OpSetFree // Set free variables 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) 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.