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 CastData
- type CastNumber
- type CastString
- type Close
- type Combine
- type CompiledFunc
- type CompiledTest
- type Concat
- type DeferredFunc
- type Divide
- type DynamicCall
- type EnvGet
- type EnvSet
- type EnvUnset
- type Equal
- type EqualNumber
- type Exit
- type File
- type Finally
- type FinallyBlock
- type FromUnix
- type Get
- type GreaterThanEqualNumber
- type GreaterThanEqualString
- type GreaterThanNumber
- type GreaterThanString
- type Info
- type Instruction
- type Interface
- type Interpolate
- type Is
- 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 Mkdir
- type Multiply
- type NextArray
- type NextMap
- type NextString
- type Not
- type NotEqual
- type NotEqualNumber
- type Now
- type On
- type Open
- type Or
- type ParentScope
- type Power
- type Print
- type Props
- type Raise
- type Rand
- type ReadData
- type ReadString
- type Register
- type Registers
- type Remainder
- type Remove
- type Rename
- type Return
- type Seek
- type Set
- type Sleep
- type Stack
- type StringIndex
- type Subtract
- type Type
- type Unix
- 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)
- type Write
Constants ¶
const ( // StateRegister is a reserved register for holding the map of the state. // Effectively the instance or "this" context. StateRegister = "0" // StackRegister holds the stack information. StackRegister = "__stack" )
const Directory = "/tmp/okc"
TODO(elliot): This should be configurable through an environment variable.
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 ¶
type Call struct { FunctionName string Arguments Registers Results Registers // Type is the resolved interface when calling constructors. In any other // case this should be types.Any. Type *types.Type }
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 CastData ¶ added in v0.21.0
type CastData struct {
X, Result Register
}
CastData returns a data 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 Close ¶ added in v0.21.0
type Close struct {
Fd Register
}
Close closes a file handle.
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 Finally [][]Instruction // These are copied from the function definition. // Name and Pos are used by the VM for stack traces. Type *types.Type Name, UniqueName, Pos string // These are only transient for the compiler, they will be nil when // serialized. Parent *CompiledFunc DeferredFuncsToCompile []DeferredFunc // contains filtered or unexported fields }
func NewCompiledFunc ¶ added in v0.23.2
func NewCompiledFunc(fn *ast.Func, parentFunc *CompiledFunc) *CompiledFunc
func (*CompiledFunc) Append ¶
func (c *CompiledFunc) Append(instruction Instruction)
func (*CompiledFunc) GetTypeForVariable ¶ added in v0.23.2
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 DeferredFunc ¶ added in v0.21.2
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 EnvGet ¶ added in v0.22.0
type EnvGet struct { Name Register // In string Value Register // Out string Exists Register // Out bool }
EnvGet gets an environment variable.
type EnvSet ¶ added in v0.22.0
EnvSet sets an environment variable.
type EnvUnset ¶ added in v0.22.0
type EnvUnset struct {
Name Register // In string
}
EnvUnset unsets an environment variable.
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 Exit ¶ added in v0.22.0
type Exit struct {
Status Register // In
}
Exit is the process exit with status code.
type File ¶ added in v0.19.0
type File struct { // Imports lists all the packages that this package relies on. Imports map[string]*types.Type Funcs map[string]*CompiledFunc Tests []*CompiledTest // Constants contains the package-level constants. These would also appear // as values within PackageFunc, however it's not trivial for the compiler // to extract these. Also, since constants cannot be modified, there's no // need to create some complex logic to retrieved them at compile or // runtime. // // It's totally fine (and probably the best choice) to have the compiler // substitute these values as literals into expressions. Especially for when // the compiler starts supporting simplifying expressions at compile time. // // Constants can only be scalars and all scalars. Constants map[string]*ast.Literal PackageFunc *CompiledFunc }
File is the root structure that will be serialized into the okc file.
func (*File) FuncByName ¶ added in v0.19.8
func (f *File) FuncByName(name string) *CompiledFunc
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 FromUnix ¶ added in v0.20.0
type FromUnix struct { Seconds Register // in Year, Month, Day, Hour, Minute, Second Register // out }
FromUnix returns a time components from a unix timestamp.
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 Info ¶ added in v0.21.0
type Info struct { Path Register // In Name Register // Out Size Register // Out Mode Register // Out ModTime []Register // Out (6 elements) IsDir Register // Out }
Info fetches file information or raises and error if the file does not exist.
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 Is ¶ added in v0.23.2
Is checks a type at runtime.
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 Mkdir ¶ added in v0.21.0
type Mkdir struct {
Path Register // In
}
Mkdir creates a directory for Path and any directories needed along the way.
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 Now ¶ added in v0.20.0
type Now struct {
Year, Month, Day, Hour, Minute, Second Register // out
}
Now sets the Result to the current time as a time.Time.
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 Open ¶ added in v0.21.0
type Open struct {
Path, Result Register
}
Open opens a file.
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
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 Rand ¶ added in v0.20.1
type Rand struct {
Result Register
}
Rand returns a random number between 0 and 1.
type ReadData ¶ added in v0.21.0
ReadData reads upto a fixed amount of bytes from a file.
type ReadString ¶ added in v0.21.0
ReadString reads upto a fixed amount of characters from a file.
func (*ReadString) Execute ¶ added in v0.21.0
func (ins *ReadString) Execute(_ *int, vm *VM) error
Execute implements the Instruction interface for the VM.
func (*ReadString) String ¶ added in v0.21.0
func (ins *ReadString) String() string
String is the human-readable description of the instruction.
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 Remove ¶ added in v0.21.0
type Remove struct {
Path Register // In
}
Remove removes (unlinks) a file.
type Rename ¶ added in v0.21.0
Rename renames (moves) a file.
type Return ¶
type Return struct {
Results Registers
}
Return tells the VM to jump out of this function.
type Seek ¶ added in v0.21.0
Seek moves the cursor of a file handle.
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 Sleep ¶ added in v0.20.0
type Sleep struct {
Seconds Register
}
Sleep will sleep for a fractional amount of seconds.
type Stack ¶ added in v0.22.0
type Stack struct {
Stack Register // Out
}
Stack returns the current stack trace.
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 Unix ¶ added in v0.20.0
type Unix struct {
Time, Result Register
}
Unix returns a unix timestamp from a time.Time.
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 // contains filtered or unexported fields }
VM is an instance of a virtual machine to run ok 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
- close.go
- combine.go
- concat.go
- divide.go
- doc.go
- dynamic_call.go
- env.go
- equal.go
- error_scope.go
- exit.go
- finally.go
- from_unix.go
- func.go
- get.go
- gob.go
- greater_than.go
- greater_than_equal.go
- info.go
- instruction.go
- interface.go
- interpolate.go
- is.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
- mkdir.go
- multiply.go
- next_array.go
- next_map.go
- next_string.go
- not.go
- not_equal.go
- now.go
- okc.go
- open.go
- or.go
- parent_scope.go
- path.go
- power.go
- print.go
- props.go
- raise.go
- rand.go
- read.go
- register.go
- remainder.go
- remove.go
- rename.go
- render.go
- return.go
- seek.go
- set.go
- sleep.go
- stack.go
- string_index.go
- subtract.go
- type.go
- unix.go
- vm.go
- write.go