Documentation
¶
Index ¶
- Constants
- Variables
- func CountObjects(o core.Value) (c int)
- func FormatInstructions(b []byte, posOffset int) []string
- func MakeInstruction(opcode core.Opcode, operands ...int) []byte
- type Bytecode
- func (b *Bytecode) CountObjects() int
- func (b *Bytecode) Decode(alloc *core.Arena, r io.Reader, modules *ModuleMap) error
- func (b *Bytecode) Encode(w io.Writer) error
- func (b *Bytecode) FormatConstants() (output []string)
- func (b *Bytecode) FormatInstructions() []string
- func (b *Bytecode) RemoveDuplicates()
- func (b *Bytecode) Size() int64
- type Importable
- type Module
- type ModuleGetter
- type ModuleMap
- func (m *ModuleMap) Add(name string, module Importable)
- func (m *ModuleMap) AddBuiltinModule(name string, attrs map[string]core.Value)
- func (m *ModuleMap) AddMap(o *ModuleMap)
- func (m *ModuleMap) AddSourceModule(name string, src []byte)
- func (m *ModuleMap) Copy() *ModuleMap
- func (m *ModuleMap) Get(name string) Importable
- func (m *ModuleMap) GetBuiltinModule(name string) *Module
- func (m *ModuleMap) GetSourceModule(name string) *SourceModule
- func (m *ModuleMap) Len() int
- func (m *ModuleMap) Remove(name string)
- type SourceModule
- 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, recur bool) (*Symbol, int, bool)
- type VM
- func (v *VM) Abort()
- func (v *VM) Allocator() *core.Arena
- func (v *VM) Call(fn *core.CompiledFunction, args []core.Value) (core.Value, error)
- func (v *VM) Clear()
- func (v *VM) IsStackEmpty() bool
- func (v *VM) Reset(alloc *core.Arena, bytecode *Bytecode, globals []core.Value)
- func (v *VM) Run() (err error)
Constants ¶
const ( // GlobalsSize is the maximum number of global variables for a VM. GlobalsSize = 1024 // DefaultStackSize is the maximum stack size for a VM. DefaultStackSize = 2048 // DefaultMaxFrames is the maximum number of function frames for a VM. DefaultMaxFrames = 1024 )
Variables ¶
var BuiltinFuncs = map[int]core.Value{ 7: core.NewBuiltinFunctionValue("bool", builtinBool, 0, true), 38: core.NewBuiltinFunctionValue("byte", builtinByte, 0, true), 9: core.NewBuiltinFunctionValue("rune", builtinRune, 0, true), 6: core.NewBuiltinFunctionValue("int", builtinInt, 0, true), 8: core.NewBuiltinFunctionValue("float", builtinFloat, 0, true), 34: core.NewBuiltinFunctionValue("decimal", builtinDecimal, 0, true), 11: core.NewBuiltinFunctionValue("time", builtinTime, 0, true), 5: core.NewBuiltinFunctionValue("string", builtinString, 0, true), 36: core.NewBuiltinFunctionValue("runes", builtinRunes, 0, true), 10: core.NewBuiltinFunctionValue("bytes", builtinBytes, 0, true), 21: core.NewBuiltinFunctionValue("dict", builtinDict, 0, true), 30: core.NewBuiltinFunctionValue("range", builtinRange, 2, true), 33: core.NewBuiltinFunctionValue("error", builtinError, 0, true), 15: core.NewBuiltinFunctionValue("is_bool", builtinIsBool, 1, false), 39: core.NewBuiltinFunctionValue("is_byte", builtinIsByte, 1, false), 16: core.NewBuiltinFunctionValue("is_rune", builtinIsRune, 1, false), 12: core.NewBuiltinFunctionValue("is_int", builtinIsInt, 1, false), 13: core.NewBuiltinFunctionValue("is_float", builtinIsFloat, 1, false), 35: core.NewBuiltinFunctionValue("is_decimal", builtinIsDecimal, 1, false), 23: core.NewBuiltinFunctionValue("is_time", builtinIsTime, 1, false), 14: core.NewBuiltinFunctionValue("is_string", builtinIsString, 1, false), 37: core.NewBuiltinFunctionValue("is_runes", builtinIsRunes, 1, false), 17: core.NewBuiltinFunctionValue("is_bytes", builtinIsBytes, 1, false), 18: core.NewBuiltinFunctionValue("is_array", builtinIsArray, 1, false), 31: core.NewBuiltinFunctionValue("is_dict", builtinIsDict, 1, false), 20: core.NewBuiltinFunctionValue("is_record", builtinIsRecord, 1, false), 32: core.NewBuiltinFunctionValue("is_range", builtinIsRange, 1, false), 24: core.NewBuiltinFunctionValue("is_error", builtinIsError, 1, false), 25: core.NewBuiltinFunctionValue("is_undefined", builtinIsUndefined, 1, false), 26: core.NewBuiltinFunctionValue("is_function", builtinIsFunction, 1, false), 27: core.NewBuiltinFunctionValue("is_callable", builtinIsCallable, 1, false), 22: core.NewBuiltinFunctionValue("is_iterable", builtinIsIterable, 1, false), 19: core.NewBuiltinFunctionValue("is_immutable", builtinIsImmutable, 1, false), 0: core.NewBuiltinFunctionValue("len", builtinLen, 1, false), 1: core.NewBuiltinFunctionValue("copy", builtinCopy, 1, false), 2: core.NewBuiltinFunctionValue("append", builtinAppend, 2, true), 3: core.NewBuiltinFunctionValue("delete", builtinDelete, 2, false), 4: core.NewBuiltinFunctionValue("splice", builtinSplice, 1, true), 29: core.NewBuiltinFunctionValue("format", builtinFormat, 1, true), 28: core.NewBuiltinFunctionValue("type_name", builtinTypeName, 1, false), }
do not change builtin function indexes as it will break compatibility 40..99 are reserved for future builtin functions
Functions ¶
func CountObjects ¶
CountObjects returns the number of objects that a given object o contains. For scalar value types, it will always be 1. For compound value types, this will include its elements and all of their elements recursively.
func FormatInstructions ¶
FormatInstructions returns string representation of bytecode instructions.
Types ¶
type Bytecode ¶
type Bytecode struct {
FileSet *parser.SourceFileSet
MainFunction *core.CompiledFunction
Constants []core.Value
}
Bytecode is a compiled instructions and constants.
func (*Bytecode) CountObjects ¶
CountObjects returns the number of objects found in Constants.
func (*Bytecode) FormatConstants ¶
FormatConstants returns human readable string representations of compiled constants.
func (*Bytecode) FormatInstructions ¶
FormatInstructions returns human readable string representations of compiled instructions.
func (*Bytecode) RemoveDuplicates ¶
func (b *Bytecode) RemoveDuplicates()
RemoveDuplicates finds and remove the duplicate values in Constants. Note this function mutates Bytecode.
type Importable ¶
type Importable interface {
// Import should return either an Object or module source code ([]byte).
Import(alloc *core.Arena, moduleName string) (any, error)
}
Importable interface represents importable module instance.
type Module ¶
func (*Module) AsImmutableRecord ¶
AsImmutableRecord converts builtin module into an immutable record.
type ModuleGetter ¶
type ModuleGetter interface {
Get(name string) Importable
}
ModuleGetter enables implementing dynamic module loading.
type ModuleMap ¶
type ModuleMap struct {
// contains filtered or unexported fields
}
ModuleMap represents a set of named modules. Use NewModuleMap to create a new module map.
func (*ModuleMap) Add ¶
func (m *ModuleMap) Add(name string, module Importable)
Add adds an import module.
func (*ModuleMap) AddBuiltinModule ¶
AddBuiltinModule adds a builtin module.
func (*ModuleMap) AddSourceModule ¶
AddSourceModule adds a source module.
func (*ModuleMap) Get ¶
func (m *ModuleMap) Get(name string) Importable
Get returns an import module identified by name. It returns if the name is not found.
func (*ModuleMap) GetBuiltinModule ¶
GetBuiltinModule returns a builtin module identified by name. It returns if the name is not found or the module is not a builtin module.
func (*ModuleMap) GetSourceModule ¶
func (m *ModuleMap) GetSourceModule(name string) *SourceModule
GetSourceModule returns a source module identified by name. It returns if the name is not found or the module is not a source module.
type SourceModule ¶
type SourceModule struct {
Src []byte
}
SourceModule is an importable module that's written in Kavun.
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 SymbolScope ¶
type SymbolScope string
SymbolScope represents a symbol scope.
const ( ScopeGlobal SymbolScope = "GLOBAL" ScopeLocal SymbolScope = "LOCAL" ScopeBuiltin SymbolScope = "BUILTIN" ScopeFree SymbolScope = "FREE" )
List of symbol scopes
type SymbolTable ¶
type SymbolTable struct {
// contains filtered or unexported fields
}
SymbolTable represents a symbol table.
func (*SymbolTable) BuiltinSymbols ¶
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.
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
VM is a virtual machine that executes the bytecode compiled by Compiler. VM must be used in a single-threaded context only.
func (*VM) Clear ¶ added in v0.0.8
func (v *VM) Clear()
Clear clears the remaining stack and frames to release references to heap objects and help GC. This step is not strictly necessary for correctness, but can help reduce memory usage and GC overhead when the same VM is reused for multiple runs.
func (*VM) IsStackEmpty ¶
IsStackEmpty tests if the stack is empty or not.