Documentation ¶
Overview ¶
Package instruction contains all the instructions created by the compiler and executed by the VM.
Index ¶
- Constants
- Variables
- 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 Equal
- type EqualNumber
- type Finally
- type FinallyBlock
- type GreaterThanEqualNumber
- type GreaterThanEqualString
- type GreaterThanNumber
- type GreaterThanString
- type Instruction
- type InternalDefinition
- type Interpolate
- type Jump
- type JumpUnless
- type Len
- type LessThanEqualNumber
- type LessThanEqualString
- type LessThanNumber
- type LessThanString
- 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 Power
- type Print
- type Raise
- type Register
- type Registers
- type Remainder
- type Return
- type StringIndex
- type Subtract
- type VM
Constants ¶
const StateRegister = "0"
StateRegister is a reserved register for holding the map of the state. Effectively the instance or "this" context.
Variables ¶
var Lib map[string]*InternalDefinition
These are populated with the generated lib.go file. See Makefile.
var Packages map[string]bool
Functions ¶
This section is empty.
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 ¶
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]string // name: type Finally [][]Instruction }
func (*CompiledFunc) Append ¶
func (c *CompiledFunc) Append(instruction Instruction)
func (*CompiledFunc) NewVariable ¶
func (c *CompiledFunc) NewVariable(variableName string, kind string)
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 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 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 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 InternalDefinition ¶
type InternalDefinition struct { CompiledFunc *CompiledFunc FuncDef *ast.Func }
InternalDefinition is used by Lib to hold the definitions and compiled code for internal functions.
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 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 string }
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 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 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 string }
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 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 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 string ErrValue *ast.Literal // FinallyBlocks are stacked with stack. FinallyBlocks [][]*FinallyBlock // 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, pkg string) *VM
NewVM will create a new VM ready to run the provided instructions.
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
- equal.go
- error_scope.go
- finally.go
- func.go
- greater_than.go
- greater_than_equal.go
- instruction.go
- interpolate.go
- jump.go
- jump_unless.go
- len.go
- less_than.go
- less_than_equal.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
- or.go
- power.go
- print.go
- raise.go
- register.go
- remainder.go
- return.go
- string_index.go
- subtract.go
- vm.go