Documentation ¶
Overview ¶
Package instruction contains all the instructions created by the compiler and executed by the VM.
Index ¶
- Constants
- Variables
- func PathForPackage(packageName string) string
- func Render(f io.Writer, x interface{}, indent string, vmPackage bool)
- func Store(file *File, packageName string) error
- type Add
- type And
- type Append
- type ArrayAlloc
- type ArrayGet
- type ArraySet
- type Assert
- type Assign
- type Call
- type CastChar
- type CastNumber
- type CastString
- type Combine
- type CompiledFunc
- type CompiledTest
- type Concat
- type Divide
- type DynamicCall
- type Equal
- type EqualNumber
- type File
- type Finally
- type FinallyBlock
- type Get
- type GreaterThanEqualNumber
- type GreaterThanEqualString
- type GreaterThanNumber
- type GreaterThanString
- type Instruction
- type Interface
- type Interpolate
- type Jump
- type JumpUnless
- type Len
- type LessThanEqualNumber
- type LessThanEqualString
- type LessThanNumber
- type LessThanString
- type LoadPackage
- type Log
- type MapAlloc
- type MapGet
- type MapSet
- type Multiply
- type NextArray
- type NextMap
- type NextString
- type Not
- type NotEqual
- type NotEqualNumber
- type On
- type Or
- type ParentScope
- type Power
- type Print
- type Props
- type Raise
- type Register
- type Registers
- type Remainder
- type Return
- type Set
- type StringIndex
- type Subtract
- type Type
- type VM
- func (vm *VM) Get(register Register) *ast.Literal
- func (vm *VM) LoadFile(pkgVariable string, file *File) error
- func (vm *VM) LoadPackage(pkgVariable, packageName string) error
- func (vm *VM) Raise(message string)
- func (vm *VM) Run() error
- func (vm *VM) RunTests(verbose bool, filter *regexp.Regexp) error
- func (vm *VM) Set(register Register, val *ast.Literal)
Constants ¶
const Directory = "/tmp/okc"
TODO(elliot): This should be configurable through an environment variable.
const StateRegister = "0"
StateRegister is a reserved register for holding the map of the state. Effectively the instance or "this" context.
Variables ¶
var Packages map[string]*File
These are populated with the generated lib.go file. See Makefile.
Functions ¶
func PathForPackage ¶ added in v0.19.0
PathForPackage returns the absolute path of where the okc file should exist for a package name. However, the path returned may not exist.
Types ¶
type Add ¶
type Add struct {
Left, Right, Result Register
}
Add will sum two numbers.
type And ¶
type And struct {
Left, Right, Result Register
}
And is a logical AND between two bools.
type Append ¶ added in v0.14.2
type Append struct {
A, B, Result Register
}
Append returns an array by combining two other arrays.
type ArrayAlloc ¶ added in v0.14.2
ArrayAlloc allocates an array of fixed size.
func (*ArrayAlloc) Execute ¶ added in v0.14.2
func (ins *ArrayAlloc) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*ArrayAlloc) String ¶ added in v0.16.0
func (ins *ArrayAlloc) String() string
String is the human-readable description of the instruction.
type ArrayGet ¶
type ArrayGet struct {
Array, Index, Result Register
}
ArrayGet gets a value from the array by its index.
type ArraySet ¶
type ArraySet struct {
Array, Index, Value Register
}
ArraySet sets a number value to an index.
type Assert ¶
Assert is used in tests.
type Assign ¶
type Assign struct { // VariableName is the destination. VariableName Register // Value or Register must be supplied, but not both. Value *ast.Literal Register Register }
Assign sets a variable to the result of an expression.
type Call ¶
Call tells the VM to jump to another function.
type CastChar ¶ added in v0.14.2
type CastChar struct {
X, Result Register
}
CastChar returns a char value of a value.
type CastNumber ¶ added in v0.14.2
type CastNumber struct {
X, Result Register
}
CastNumber returns a number value of a value.
func (*CastNumber) Execute ¶ added in v0.14.2
func (ins *CastNumber) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*CastNumber) String ¶ added in v0.16.0
func (ins *CastNumber) String() string
String is the human-readable description of the instruction.
type CastString ¶ added in v0.14.2
type CastString struct {
X, Result Register
}
CastString returns a string value of a value.
func (*CastString) Execute ¶ added in v0.14.2
func (ins *CastString) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*CastString) String ¶ added in v0.16.0
func (ins *CastString) String() string
String is the human-readable description of the instruction.
type Combine ¶
type Combine struct {
Left, Right, Result Register
}
Combine will create a new data by joining two other datas.
type CompiledFunc ¶
type CompiledFunc struct { Arguments []string Instructions []Instruction Registers int Variables map[string]*types.Type // name: type Finally [][]Instruction Interfaces map[string]map[string]*types.Type Type *types.Type }
func (*CompiledFunc) Append ¶
func (c *CompiledFunc) Append(instruction Instruction)
func (*CompiledFunc) NewVariable ¶
func (c *CompiledFunc) NewVariable(variableName string, kind *types.Type)
func (*CompiledFunc) NextRegister ¶
func (c *CompiledFunc) NextRegister() Register
type CompiledTest ¶
type CompiledTest struct { *CompiledFunc TestName string }
CompiledTest is a runnable test.
type Concat ¶
type Concat struct {
Left, Right, Result Register
}
Concat will create a new string by joining two other strings.
type Divide ¶
type Divide struct {
Left, Right, Result Register
}
Divide will multiply two numbers.
type DynamicCall ¶ added in v0.18.0
type DynamicCall struct { Variable Register // func literal Arguments Register // []any for arguments Results Register // []any for return values }
DynamicCall tells the VM to jump to another function.
func (*DynamicCall) Execute ¶ added in v0.18.0
func (ins *DynamicCall) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*DynamicCall) String ¶ added in v0.18.0
func (ins *DynamicCall) String() string
String is the human-readable description of the instruction.
type Equal ¶
type Equal struct {
Left, Right, Result Register
}
Equal will compare two non-numbers for equality. This works for every other type because every other type is stored as a string. When optimizations are made in the future this will need to be expanded to one instruction per type.
type EqualNumber ¶
type EqualNumber struct {
Left, Right, Result Register
}
EqualNumber will compare two numbers for equality.
func (*EqualNumber) Execute ¶
func (ins *EqualNumber) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*EqualNumber) String ¶ added in v0.16.0
func (ins *EqualNumber) String() string
String is the human-readable description of the instruction.
type File ¶ added in v0.19.0
type File struct { // Imports lists all the packages that this package relies on. Imports map[string]map[string]*types.Type Funcs map[string]*CompiledFunc FuncDefs map[string]*ast.Func Tests []*CompiledTest Interfaces map[string]map[string]*types.Type Constants map[string]*ast.Literal }
File is the root structure that will be serialized into the okc file.
type Finally ¶ added in v0.15.3
Finally will activate or deactivate a finally block.
type FinallyBlock ¶ added in v0.15.3
type FinallyBlock struct { Run bool Instructions []Instruction }
type Get ¶ added in v0.18.0
type Get struct {
Object, Prop, Result Register
}
Get is used to access array indexes, map keys and object properties at runtime.
type GreaterThanEqualNumber ¶
type GreaterThanEqualNumber struct {
Left, Right, Result Register
}
GreaterThanEqualNumber will compare two numbers.
func (*GreaterThanEqualNumber) Execute ¶
func (ins *GreaterThanEqualNumber) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*GreaterThanEqualNumber) String ¶ added in v0.16.0
func (ins *GreaterThanEqualNumber) String() string
String is the human-readable description of the instruction.
type GreaterThanEqualString ¶
type GreaterThanEqualString struct {
Left, Right, Result Register
}
GreaterThanEqualString will compare two strings.
func (*GreaterThanEqualString) Execute ¶
func (ins *GreaterThanEqualString) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*GreaterThanEqualString) String ¶ added in v0.16.0
func (ins *GreaterThanEqualString) String() string
String is the human-readable description of the instruction.
type GreaterThanNumber ¶
type GreaterThanNumber struct {
Left, Right, Result Register
}
GreaterThanNumber will compare two numbers.
func (*GreaterThanNumber) Execute ¶
func (ins *GreaterThanNumber) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*GreaterThanNumber) String ¶ added in v0.16.0
func (ins *GreaterThanNumber) String() string
String is the human-readable description of the instruction.
type GreaterThanString ¶
type GreaterThanString struct {
Left, Right, Result Register
}
GreaterThanString will compare two strings.
func (*GreaterThanString) Execute ¶
func (ins *GreaterThanString) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*GreaterThanString) String ¶ added in v0.16.0
func (ins *GreaterThanString) String() string
String is the human-readable description of the instruction.
type Instruction ¶
type Instruction interface { // Stringer provides human-readable descriptions of instructions. It's // helpful for debugging and used directly by "ok asm". fmt.Stringer Execute(i *int, vm *VM) error }
An Instruction can be executed by the VM.
type Interface ¶ added in v0.18.0
type Interface struct {
Value, Result Register
}
Interface assigns the runtime interface of a value to a string destination.
type Interpolate ¶ added in v0.13.2
Interpolate combines strings and expressions into one string result.
func (*Interpolate) Execute ¶ added in v0.13.2
func (ins *Interpolate) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*Interpolate) String ¶ added in v0.16.0
func (ins *Interpolate) String() string
String is the human-readable description of the instruction.
type Jump ¶
type Jump struct {
To int
}
Jump will jump to the instruction.
type JumpUnless ¶
JumpUnless will jump to the instruction if the expression is false.
func (*JumpUnless) Execute ¶
func (ins *JumpUnless) Execute(i *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*JumpUnless) String ¶ added in v0.16.0
func (ins *JumpUnless) String() string
String is the human-readable description of the instruction.
type Len ¶
type Len struct {
Argument, Result Register
}
Len is used to determine the size of an array or map.
type LessThanEqualNumber ¶
type LessThanEqualNumber struct {
Left, Right, Result Register
}
LessThanEqualNumber will compare two numbers.
func (*LessThanEqualNumber) Execute ¶
func (ins *LessThanEqualNumber) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*LessThanEqualNumber) String ¶ added in v0.16.0
func (ins *LessThanEqualNumber) String() string
String is the human-readable description of the instruction.
type LessThanEqualString ¶
type LessThanEqualString struct {
Left, Right, Result Register
}
LessThanEqualString will compare two strings.
func (*LessThanEqualString) Execute ¶
func (ins *LessThanEqualString) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*LessThanEqualString) String ¶ added in v0.16.0
func (ins *LessThanEqualString) String() string
String is the human-readable description of the instruction.
type LessThanNumber ¶
type LessThanNumber struct {
Left, Right, Result Register
}
LessThanNumber will compare two numbers.
func (*LessThanNumber) Execute ¶
func (ins *LessThanNumber) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*LessThanNumber) String ¶ added in v0.16.0
func (ins *LessThanNumber) String() string
String is the human-readable description of the instruction.
type LessThanString ¶
type LessThanString struct {
Left, Right, Result Register
}
LessThanString will compare two strings.
func (*LessThanString) Execute ¶
func (ins *LessThanString) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*LessThanString) String ¶ added in v0.16.0
func (ins *LessThanString) String() string
String is the human-readable description of the instruction.
type LoadPackage ¶ added in v0.19.3
LoadPackage loads a package at runtime into a register.
func (*LoadPackage) Execute ¶ added in v0.19.3
func (ins *LoadPackage) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*LoadPackage) String ¶ added in v0.19.3
func (ins *LoadPackage) String() string
type Log ¶ added in v0.12.1
type Log struct {
X, Result Register
}
Log is a natural logarithm (base e).
type MapAlloc ¶ added in v0.16.2
MapAlloc allocates a map of fixed size.
type MapGet ¶
type MapGet struct {
Map, Key, Result Register
}
MapGet gets a value from the map by its key.
type MapSet ¶
type MapSet struct {
Map, Key, Value Register
}
MapSet sets a number value to an index.
type Multiply ¶
type Multiply struct {
Left, Right, Result Register
}
Multiply will multiply two numbers.
type NextArray ¶
type NextArray struct { Array Register // In (array): Containing the iterating array. Cursor Register // In (number): Containing the current position. KeyResult Register // Out (any): Load the key into this register. ValueResult Register // Out (any): Load the value into this register. Result Register // Out (bool): Still more items? }
NextArray is used to tick an array iterator forward.
type NextMap ¶
type NextMap struct { Map Register // In (map): Containing the iterating map. Cursor Register // In (number): Containing the current position. KeyResult Register // Out (any): Load the key into this register. ValueResult Register // Out (any): Load the value into this register. Result Register // Out (bool): Still more items? }
NextMap is used to tick a map iterator forward.
type NextString ¶ added in v0.14.2
type NextString struct { Str Register // In (string): Containing the iterating string. Cursor Register // In (number): Containing the current position. KeyResult Register // Out (any): Load the key into this register. ValueResult Register // Out (any): Load the value into this register. Result Register // Out (bool): Still more items? }
NextString is used to tick a string iterator forward.
func (*NextString) Execute ¶ added in v0.14.2
func (ins *NextString) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*NextString) String ¶ added in v0.14.2
func (ins *NextString) String() string
String is the human-readable description of the instruction.
type Not ¶
type Not struct {
Left, Result Register
}
Not is a logical NOT of a bool.
type NotEqual ¶
type NotEqual struct {
Left, Right, Result Register
}
NotEqual will compare two non-numbers for non-equality. This works for every other type because every other type is stored as a string. When optimizations are made in the future this will need to be expanded to one instruction per type.
type NotEqualNumber ¶
type NotEqualNumber struct {
Left, Right, Result Register
}
NotEqualNumber will compare two numbers for equality.
func (*NotEqualNumber) Execute ¶
func (ins *NotEqualNumber) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*NotEqualNumber) String ¶ added in v0.16.0
func (ins *NotEqualNumber) String() string
String is the human-readable description of the instruction.
type On ¶ added in v0.15.0
type On struct { // Type will be the name of the error, or it can be empty to signal there // are no more errors to check. If the VM hits an empty Type it will return // and pass the error up to the caller. Type *types.Type }
On is a pragma for the vm to handle errors. It can also be used to indicate the end of the on's so that the VM can send the error up to the caller.
type Or ¶
type Or struct {
Left, Right, Result Register
}
Or is a logical OR between two bools.
type ParentScope ¶ added in v0.17.6
type ParentScope struct {
X Register
}
ParentScope sets the parent scope of a function literal.
func (*ParentScope) Execute ¶ added in v0.17.6
func (ins *ParentScope) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*ParentScope) String ¶ added in v0.17.6
func (ins *ParentScope) String() string
type Power ¶ added in v0.11.2
type Power struct {
Base, Power, Result Register
}
Power is Left to the power of Right.
type Print ¶
type Print struct {
Arguments Registers
}
Print will output a string to stdout.
type Props ¶ added in v0.18.0
type Props struct {
Object, Result Register
}
Props returns the property names of an object.
type Raise ¶ added in v0.15.0
type Raise struct { // Err is the register containing the error. Err Register // Type is used to match the handler. Type *types.Type }
Raise put the VM into an error mode. The VM will look for an error handler.
type Register ¶ added in v0.16.0
type Register string
Register is the name of a register. At the moment variable names can also be used as registers, but that will be removed in the future. When that happens these can be refactored into an int.
type Registers ¶ added in v0.16.0
type Registers []Register
Registers is multiple sequential registers. It might represents function arguments, for example.
type Remainder ¶
type Remainder struct {
Left, Right, Result Register
}
Remainder will return the remainder when dividing two numbers. This is not the same as a modulo. A remainder may be negative.
type Return ¶
type Return struct {
Results Registers
}
Return tells the VM to jump out of this function.
type Set ¶ added in v0.18.0
type Set struct {
Object, Prop, Value Register
// Result will always be true. This is just a way to make the instruction
// set easier, but it doesn't need to be here. Or at least return something
// more useful.
Result Register
}
Set is used to set by array indexes, map keys and object properties at runtime.
type StringIndex ¶ added in v0.14.2
type StringIndex struct {
Str, Index, Result Register
}
StringIndex returns a character from an index of a string.
func (*StringIndex) Execute ¶ added in v0.14.2
func (ins *StringIndex) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*StringIndex) String ¶ added in v0.14.2
func (ins *StringIndex) String() string
String is the human-readable description of the instruction.
type Subtract ¶
type Subtract struct {
Left, Right, Result Register
}
Subtract will subtract two numbers.
type Type ¶ added in v0.18.0
type Type struct {
Value, Result Register
}
Type assigns the runtime type of a value to a string destination.
type VM ¶
type VM struct { Return []Register Stack []map[Register]*ast.Literal Stdout io.Writer // Stats when running tests. TestsPass, TestsFailed int TotalAssertions int CurrentTestName string CurrentTestPassed bool // ErrType will be non-empty once an error is raised. It contains the type // to match for a handler. ErrValue contains the actual error. ErrType *types.Type ErrValue *ast.Literal // FinallyBlocks are stacked with stack. FinallyBlocks [][]*FinallyBlock // Interfaces describes all the interfaces types known by the VM. Interfaces map[string]map[string]*types.Type // contains filtered or unexported fields }
VM is an instance of a virtual machine to run ok instructions.
func NewVM ¶
func NewVM( fns map[string]*CompiledFunc, tests []*CompiledTest, interfaces map[string]map[string]*types.Type, pkg string, ) *VM
NewVM will create a new VM ready to run the provided instructions.
func (*VM) LoadPackage ¶ added in v0.19.0
func (*VM) Run ¶
Run will run the program. That is, execute the "main" function. If there is no main() method then nothing will run, but no error will be raised.
TODO(elliot): Change missing main into an error in the future. It was done
this way so I could use "ok run" like an "ok compile" (that didn't exist at the time) for compiling the standard libraries.
Source Files ¶
- add.go
- and.go
- append.go
- array_alloc.go
- array_get.go
- array_set.go
- assert.go
- assign.go
- call.go
- cast.go
- combine.go
- concat.go
- divide.go
- doc.go
- dynamic_call.go
- equal.go
- error_scope.go
- finally.go
- func.go
- get.go
- gob.go
- greater_than.go
- greater_than_equal.go
- instruction.go
- interface.go
- interpolate.go
- jump.go
- jump_unless.go
- len.go
- less_than.go
- less_than_equal.go
- lib.go
- load_package.go
- log.go
- map_alloc.go
- map_get.go
- map_set.go
- multiply.go
- next_array.go
- next_map.go
- next_string.go
- not.go
- not_equal.go
- okc.go
- or.go
- parent_scope.go
- path.go
- power.go
- print.go
- props.go
- raise.go
- register.go
- remainder.go
- render.go
- return.go
- set.go
- string_index.go
- subtract.go
- type.go
- vm.go