vm

package
v0.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2021 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package instruction contains all the instructions created by the compiler and executed by the VM.

Index

Constants

View Source
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"
)
View Source
const Directory = "/tmp/okc"

TODO(elliot): This should be configurable through an environment variable.

Variables

This section is empty.

Functions

func PathForPackage added in v0.19.0

func PathForPackage(packageName string) string

PathForPackage returns the absolute path of where the JSON file should exist for a package name. However, the path returned may not exist.

func Render added in v0.18.2

func Render(f io.Writer, x interface{}, indent string, vmPackage bool)

func Store added in v0.19.0

func Store(file *File, packageName string) error

Store will create or replace the okc file for the provided package name.

Types

type Add

type Add struct {
	Left, Right, Result Register
}

Add will sum two numbers.

func (*Add) Execute

func (ins *Add) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Add) String added in v0.16.0

func (ins *Add) String() string

String is the human-readable description of the instruction.

type And

type And struct {
	Left, Right, Result Register
}

And is a logical AND between two bools.

func (*And) Execute

func (ins *And) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*And) String added in v0.16.0

func (ins *And) String() string

String is the human-readable description of the instruction.

type Append added in v0.14.2

type Append struct {
	A, B, Result Register
}

Append returns an array by combining two other arrays.

func (*Append) Execute added in v0.14.2

func (ins *Append) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Append) String added in v0.16.0

func (ins *Append) String() string

String is the human-readable description of the instruction.

type ArrayAlloc added in v0.14.2

type ArrayAlloc struct {
	Size, Result Register
	Kind         TypeRegister
}

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.

func (*ArrayGet) Execute

func (ins *ArrayGet) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*ArrayGet) String added in v0.16.0

func (ins *ArrayGet) String() string

String is the human-readable description of the instruction.

type ArraySet

type ArraySet struct {
	Array, Index, Value Register
}

ArraySet sets a number value to an index.

func (*ArraySet) Execute

func (ins *ArraySet) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*ArraySet) String added in v0.16.0

func (ins *ArraySet) String() string

String is the human-readable description of the instruction.

type Assert

type Assert struct {
	Left, Right, Final Register
	Op                 string
	Pos                string
}

Assert is used in tests.

func (*Assert) Execute

func (ins *Assert) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Assert) String added in v0.16.0

func (ins *Assert) String() string

String is the human-readable description of the instruction.

type Assign

type Assign struct {
	Result   Register // Out (any)
	Register Register // In (any)
}

Assign sets a variable to the result of an expression.

func (*Assign) Execute

func (ins *Assign) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Assign) String added in v0.16.0

func (ins *Assign) String() string

type AssignFunc added in v0.23.7

type AssignFunc struct {
	Result     Register     // Out (any)
	Type       TypeRegister // In
	UniqueName string       // In
}

AssignFunc sets a function to a register.

func (*AssignFunc) Execute added in v0.23.7

func (ins *AssignFunc) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*AssignFunc) String added in v0.23.7

func (ins *AssignFunc) String() string

type AssignSymbol added in v0.23.7

type AssignSymbol struct {
	Result Register       // Out (any)
	Symbol SymbolRegister // In (any)
}

AssignSymbol sets a variable to the symbol.

func (*AssignSymbol) Execute added in v0.23.7

func (ins *AssignSymbol) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*AssignSymbol) String added in v0.23.7

func (ins *AssignSymbol) String() string

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 TypeRegister

	// Pos is used to append to the call stack.
	Pos string
}

Call tells the VM to jump to another function.

func (*Call) Execute

func (ins *Call) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Call) String added in v0.16.0

func (ins *Call) String() string

String is the human-readable description of the instruction.

type CastChar added in v0.14.2

type CastChar struct {
	X, Result Register
}

CastChar returns a char value of a value.

func (*CastChar) Execute added in v0.14.2

func (ins *CastChar) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*CastChar) String added in v0.16.0

func (ins *CastChar) String() string

String is the human-readable description of the instruction.

type CastData added in v0.21.0

type CastData struct {
	X, Result Register
}

CastData returns a data value of a value.

func (*CastData) Execute added in v0.21.0

func (ins *CastData) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*CastData) String added in v0.21.0

func (ins *CastData) String() string

String is the human-readable description of the instruction.

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.

func (*Close) Execute added in v0.21.0

func (ins *Close) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Close) String added in v0.21.0

func (ins *Close) 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.

func (*Combine) Execute

func (ins *Combine) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Combine) String added in v0.16.0

func (ins *Combine) String() string

String is the human-readable description of the instruction.

type CompiledFunc

type CompiledFunc struct {
	Arguments    []string      `json:",omitempty"`
	Instructions *Instructions `json:",omitempty"`
	Registers    int

	Finally []*Instructions `json:",omitempty"`

	// These are copied from the function definition.
	// Name and Pos are used by the VM for stack traces.
	Type                  TypeRegister `json:",omitempty"`
	Name, UniqueName, Pos string       `json:",omitempty"`

	// These are only transient for the compiler.
	Parent                 *CompiledFunc           `json:"-"`
	DeferredFuncsToCompile []DeferredFunc          `json:"-"`
	Constants              map[string]*ast.Literal `json:"-"`
	// contains filtered or unexported fields
}

func NewCompiledFunc added in v0.23.2

func NewCompiledFunc(
	fn *ast.Func,
	parentFunc *CompiledFunc,
	constants map[string]*ast.Literal,
	file *File,
) *CompiledFunc

func (*CompiledFunc) Append

func (c *CompiledFunc) Append(instruction Instruction)

func (*CompiledFunc) GetTypeForVariable added in v0.23.2

func (c *CompiledFunc) GetTypeForVariable(
	variableName string,
	scopeOverrides map[string]*types.Type,
) (*types.Type, bool)

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.

func (*Concat) Execute

func (ins *Concat) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Concat) String added in v0.16.0

func (ins *Concat) String() string

String is the human-readable description of the instruction.

type DeferredFunc added in v0.21.2

type DeferredFunc struct {
	Register Register
	Func     *ast.Func
}

type Divide

type Divide struct {
	Left, Right, Result Register
}

Divide will multiply two numbers.

func (*Divide) Execute

func (ins *Divide) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Divide) String added in v0.16.0

func (ins *Divide) String() string

String is the human-readable description of the instruction.

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

	// Pos is used to append to the call stack.
	Pos string
}

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.

func (*EnvGet) Execute added in v0.22.0

func (ins *EnvGet) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*EnvGet) String added in v0.22.0

func (ins *EnvGet) String() string

String is the human-readable description of the instruction.

type EnvSet added in v0.22.0

type EnvSet struct {
	Name  Register // In string
	Value Register // In string
}

EnvSet sets an environment variable.

func (*EnvSet) Execute added in v0.22.0

func (ins *EnvSet) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*EnvSet) String added in v0.22.0

func (ins *EnvSet) String() string

String is the human-readable description of the instruction.

type EnvUnset added in v0.22.0

type EnvUnset struct {
	Name Register // In string
}

EnvUnset unsets an environment variable.

func (*EnvUnset) Execute added in v0.22.0

func (ins *EnvUnset) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*EnvUnset) String added in v0.22.0

func (ins *EnvUnset) 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.

func (*Equal) Execute

func (ins *Equal) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Equal) String added in v0.16.0

func (ins *Equal) String() string

String is the human-readable description of the instruction.

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.

func (*Exit) Execute added in v0.22.0

func (ins *Exit) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Exit) String added in v0.22.0

func (ins *Exit) String() string

String is the human-readable description of the instruction.

type File added in v0.19.0

type File struct {
	Tests []*CompiledTest `json:",omitempty"`

	// Types contains the type descriptions that can be referenced by some
	// instructions at runtime.
	Types types.Registry `json:",omitempty"`

	// Symbols contains literal values that can be referenced by instructions.
	Symbols map[SymbolRegister]*Symbol `json:",omitempty"`

	// Globals describes the global registers and unique names of the functions
	// that will initialize each package.
	Globals map[string]string `json:",omitempty"`
}

File is the root structure that will be serialized into the okc file.

func Load added in v0.19.0

func Load(packageName string) (*File, error)

func Merge added in v0.24.0

func Merge(files ...*File) *File

Merge produces a new file with all symbols merged. Merge ignores imports and tests.

NOTE: This will mangle the input files so they cannot be used after going through this process.

func (*File) AddSymbolFunc added in v0.23.8

func (f *File) AddSymbolFunc(fn *CompiledFunc) SymbolRegister

func (*File) AddSymbolLiteral added in v0.23.7

func (f *File) AddSymbolLiteral(lit *ast.Literal) SymbolRegister

func (*File) AddType added in v0.23.6

func (f *File) AddType(ty *types.Type) TypeRegister

func (*File) FuncByName added in v0.19.8

func (f *File) FuncByName(name string) *CompiledFunc

type Finally added in v0.15.3

type Finally struct {
	Index int
	Run   bool
}

Finally will activate or deactivate a finally block.

func (*Finally) Execute added in v0.15.3

func (ins *Finally) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Finally) String added in v0.16.0

func (ins *Finally) String() string

String is the human-readable description of the instruction.

type FinallyBlock added in v0.15.3

type FinallyBlock struct {
	Run          bool
	Instructions *Instructions
}

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.

func (*FromUnix) Execute added in v0.20.0

func (ins *FromUnix) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*FromUnix) String added in v0.20.0

func (ins *FromUnix) String() string

String is the human-readable description of the 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.

func (*Get) Execute added in v0.18.0

func (ins *Get) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Get) String added in v0.18.0

func (ins *Get) String() string

String is the human-readable description of the 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 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.

func (*Info) Execute added in v0.21.0

func (ins *Info) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Info) String added in v0.21.0

func (ins *Info) 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 Instructions added in v0.23.9

type Instructions struct {
	Instructions []Instruction
}

func NewInstructions added in v0.23.9

func NewInstructions(instructions ...Instruction) *Instructions

func (*Instructions) MarshalJSON added in v0.23.9

func (ins *Instructions) MarshalJSON() ([]byte, error)

func (*Instructions) UnmarshalJSON added in v0.23.9

func (ins *Instructions) UnmarshalJSON(data []byte) error

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.

func (*Interface) Execute added in v0.18.0

func (ins *Interface) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Interface) String added in v0.18.0

func (ins *Interface) String() string

String is the human-readable description of the instruction.

type Interpolate added in v0.13.2

type Interpolate struct {
	Result Register
	Args   Registers
}

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

type Is struct {
	Value  Register // In any
	Type   Register // In string
	Result Register // Out bool
}

Is checks a type at runtime.

func (*Is) Execute added in v0.23.2

func (ins *Is) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Is) String added in v0.23.2

func (ins *Is) String() string

String is the human-readable description of the instruction.

type Jump

type Jump struct {
	To int
}

Jump will jump to the instruction.

func (*Jump) Execute

func (ins *Jump) Execute(i *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Jump) String added in v0.16.0

func (ins *Jump) String() string

String is the human-readable description of the instruction.

type JumpUnless

type JumpUnless struct {
	Condition Register
	To        int
}

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.

func (*Len) Execute

func (ins *Len) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Len) String added in v0.16.0

func (ins *Len) String() string

String is the human-readable description of the instruction.

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).

func (*Log) Execute added in v0.12.1

func (ins *Log) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Log) String added in v0.16.0

func (ins *Log) String() string

String is the human-readable description of the instruction.

type MapAlloc added in v0.16.2

type MapAlloc struct {
	Kind         TypeRegister
	Size, Result Register
}

MapAlloc allocates a map of fixed size.

func (*MapAlloc) Execute added in v0.16.2

func (ins *MapAlloc) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*MapAlloc) String added in v0.16.2

func (ins *MapAlloc) String() string

String is the human-readable description of the instruction.

type MapGet

type MapGet struct {
	Map, Key, Result Register
}

MapGet gets a value from the map by its key.

func (*MapGet) Execute

func (ins *MapGet) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*MapGet) String added in v0.16.0

func (ins *MapGet) String() string

String is the human-readable description of the instruction.

type MapSet

type MapSet struct {
	Map, Key, Value Register
}

MapSet sets a number value to an index.

func (*MapSet) Execute

func (ins *MapSet) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*MapSet) String added in v0.16.0

func (ins *MapSet) String() string

String is the human-readable description of the instruction.

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.

func (*Mkdir) Execute added in v0.21.0

func (ins *Mkdir) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Mkdir) String added in v0.21.0

func (ins *Mkdir) String() string

String is the human-readable description of the instruction.

type Multiply

type Multiply struct {
	Left, Right, Result Register
}

Multiply will multiply two numbers.

func (*Multiply) Execute

func (ins *Multiply) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Multiply) String added in v0.16.0

func (ins *Multiply) String() string

String is the human-readable description of the instruction.

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.

func (*NextArray) Execute

func (ins *NextArray) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*NextArray) String added in v0.16.0

func (ins *NextArray) String() string

String is the human-readable description of the instruction.

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.

func (*NextMap) Execute

func (ins *NextMap) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*NextMap) String added in v0.16.0

func (ins *NextMap) String() string

String is the human-readable description of the instruction.

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.

func (*Not) Execute

func (ins *Not) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Not) String added in v0.16.0

func (ins *Not) String() string

String is the human-readable description of the instruction.

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.

func (*NotEqual) Execute

func (ins *NotEqual) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*NotEqual) String added in v0.16.0

func (ins *NotEqual) String() string

String is the human-readable description of the instruction.

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.

func (*Now) Execute added in v0.20.0

func (ins *Now) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Now) String added in v0.20.0

func (ins *Now) 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 should be ignored if Finished
	// is true to signal there are no more errors to check.
	Type TypeRegister

	// If the VM hits a Finished = true it will return and pass the error up to
	// the caller.
	Finished bool
}

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.

func (*On) Execute added in v0.15.0

func (ins *On) Execute(_ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

func (*On) String added in v0.16.0

func (ins *On) String() string

String is the human-readable description of the instruction.

type Open added in v0.21.0

type Open struct {
	Path, Result Register
}

Open opens a file.

func (*Open) Execute added in v0.21.0

func (ins *Open) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Open) String added in v0.21.0

func (ins *Open) String() string

String is the human-readable description of the instruction.

type Or

type Or struct {
	Left, Right, Result Register
}

Or is a logical OR between two bools.

func (*Or) Execute

func (ins *Or) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Or) String added in v0.16.0

func (ins *Or) String() string

String is the human-readable description of the instruction.

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.

func (*Power) Execute added in v0.11.2

func (ins *Power) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Power) String added in v0.16.0

func (ins *Power) String() string

String is the human-readable description of the instruction.

type Print

type Print struct {
	Arguments Registers
}

Print will output a string to stdout.

func (*Print) Execute

func (ins *Print) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Print) String added in v0.16.0

func (ins *Print) String() string

String is the human-readable description of the instruction.

type Props added in v0.18.0

type Props struct {
	Value  Register // In: object or map
	Result Register // Out: []string
}

Props returns the property names of an object.

func (*Props) Execute added in v0.18.0

func (ins *Props) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Props) String added in v0.18.0

func (ins *Props) String() string

String is the human-readable description of the instruction.

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 TypeRegister

	// Pos is used at the top of the stack trace to show where the error
	// originated from.
	Pos string
}

Raise put the VM into an error mode. The VM will look for an error handler.

func (*Raise) Execute added in v0.15.0

func (ins *Raise) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Raise) String added in v0.16.0

func (ins *Raise) String() string

String is the human-readable description of the instruction.

type Rand added in v0.20.1

type Rand struct {
	Result Register
}

Rand returns a random number between 0 and 1.

func (*Rand) Execute added in v0.20.1

func (ins *Rand) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Rand) String added in v0.20.1

func (ins *Rand) String() string

String is the human-readable description of the instruction.

type ReadData added in v0.21.0

type ReadData struct {
	Fd, Size Register // In
	Data     Register // Out
}

ReadData reads upto a fixed amount of bytes from a file.

func (*ReadData) Execute added in v0.21.0

func (ins *ReadData) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*ReadData) String added in v0.21.0

func (ins *ReadData) String() string

String is the human-readable description of the instruction.

type ReadString added in v0.21.0

type ReadString struct {
	Fd, Size Register // In
	Str      Register // Out
}

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.

func (Register) String added in v0.16.0

func (r Register) String() string

String returns either "rX" or the name of the variable. See Register for details.

type Registers added in v0.16.0

type Registers []Register

Registers is multiple sequential registers. It might represents function arguments, for example.

func (Registers) String added in v0.16.0

func (rs Registers) String() string

String returns the registers as a set, like "(r0, r1)". The values will be wrapped with parenthesis even if there are one or zero elements.

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.

func (*Remainder) Execute

func (ins *Remainder) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Remainder) String added in v0.16.0

func (ins *Remainder) String() string

String is the human-readable description of the instruction.

type Remove added in v0.21.0

type Remove struct {
	Path Register // In
}

Remove removes (unlinks) a file.

func (*Remove) Execute added in v0.21.0

func (ins *Remove) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Remove) String added in v0.21.0

func (ins *Remove) String() string

String is the human-readable description of the instruction.

type Rename added in v0.21.0

type Rename struct {
	OldPath Register // In
	NewPath Register // In
}

Rename renames (moves) a file.

func (*Rename) Execute added in v0.21.0

func (ins *Rename) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Rename) String added in v0.21.0

func (ins *Rename) String() string

String is the human-readable description of the instruction.

type Return

type Return struct {
	Results Registers
}

Return tells the VM to jump out of this function.

func (*Return) Execute

func (ins *Return) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Return) String added in v0.16.0

func (ins *Return) String() string

String is the human-readable description of the instruction.

type Seek added in v0.21.0

type Seek struct {
	Fd, Offset, Whence Register // in
	NewOffset          Register // out
}

Seek moves the cursor of a file handle.

func (*Seek) Execute added in v0.21.0

func (ins *Seek) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Seek) String added in v0.21.0

func (ins *Seek) String() string

String is the human-readable description of the instruction.

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.

func (*Set) Execute added in v0.18.0

func (ins *Set) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Set) String added in v0.18.0

func (ins *Set) String() string

String is the human-readable description of the instruction.

type Sleep added in v0.20.0

type Sleep struct {
	Seconds Register
}

Sleep will sleep for a fractional amount of seconds.

func (*Sleep) Execute added in v0.20.0

func (ins *Sleep) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Sleep) String added in v0.20.0

func (ins *Sleep) String() string

String is the human-readable description of the instruction.

type Stack added in v0.22.0

type Stack struct {
	Stack Register // Out
}

Stack returns the current stack trace.

func (*Stack) Execute added in v0.22.0

func (ins *Stack) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Stack) String added in v0.22.0

func (ins *Stack) String() string

String is the human-readable description of the instruction.

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.

func (*Subtract) Execute

func (ins *Subtract) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Subtract) String added in v0.16.0

func (ins *Subtract) String() string

String is the human-readable description of the instruction.

type Symbol added in v0.23.7

type Symbol struct {
	Type TypeRegister

	// Value can only be used when Func is nil.
	Value string `json:",omitempty"`

	Func *CompiledFunc `json:",omitempty"`

	// Interface will only be set when Func is provided.
	Interface string `json:",omitempty"`
}

A Symbol contains a literal value that can be referenced by instructions.

func (*Symbol) Literal added in v0.23.7

func (s *Symbol) Literal(registry types.Registry) *ast.Literal

type SymbolRegister added in v0.23.7

type SymbolRegister string

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.

func (*Type) Execute added in v0.18.0

func (ins *Type) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Type) String added in v0.18.0

func (ins *Type) String() string

String is the human-readable description of the instruction.

type TypeRegister added in v0.23.6

type TypeRegister string

type UnicodeIs added in v0.25.0

type UnicodeIs struct {
	Op     Register // in: string
	Char   Register // in: char
	Result Register // out: bool
}

UnicodeIs is a collection of unicode instructions that provides OK with basic unicode functionality without having to build all the tables into the source code, yet.

It was put into a single instruction as to not bloat the VM instructions since this is intended to be removed in the future.

func (*UnicodeIs) Execute added in v0.25.0

func (ins *UnicodeIs) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*UnicodeIs) String added in v0.25.0

func (ins *UnicodeIs) String() string

String is the human-readable description of the instruction.

type UnicodeTo added in v0.25.0

type UnicodeTo struct {
	Op     Register // in: string
	Char   Register // in: char
	Result Register // out: char
}

UnicodeTo works the same way as UnicodeIs, but we need a separate instruction to handle returning a character rather than a bool.

func (*UnicodeTo) Execute added in v0.25.0

func (ins *UnicodeTo) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*UnicodeTo) String added in v0.25.0

func (ins *UnicodeTo) String() string

String is the human-readable description of the instruction.

type Unix added in v0.20.0

type Unix struct {
	Time, Result Register
}

Unix returns a unix timestamp from a time.Time.

func (*Unix) Execute added in v0.20.0

func (ins *Unix) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Unix) String added in v0.20.0

func (ins *Unix) String() string

String is the human-readable description of the instruction.

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, and ErrStack
	// contains the descriptive stack of where the error was originally raised.
	ErrType  *types.Type
	ErrValue *ast.Literal
	ErrStack []string

	// FinallyBlocks are stacked with stack.
	FinallyBlocks [][]*FinallyBlock

	// Types can be referenced by instructions.
	Types map[TypeRegister]*types.Type

	// Symbols contain literals that can be referenced by AssignSymbol.
	Symbols map[SymbolRegister]*ast.Literal

	Globals       map[string]*ast.Literal
	GlobalsToLoad map[string]string
	// contains filtered or unexported fields
}

VM is an instance of a virtual machine to run ok instructions.

func NewVM

func NewVM(pkg string) *VM

NewVM will create a new VM ready to run the provided instructions.

func (*VM) Get added in v0.16.1

func (vm *VM) Get(register Register) *ast.Literal

Get will get a register.

func (*VM) LoadFile added in v0.19.0

func (vm *VM) LoadFile(file *File) error

func (*VM) LoadPackage added in v0.19.0

func (vm *VM) LoadPackage(packageName string) error

func (*VM) Raise added in v0.18.0

func (vm *VM) Raise(message string)

func (*VM) Run

func (vm *VM) Run(mainPackage string) error

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.

func (*VM) RunTests

func (vm *VM) RunTests(verbose bool, filter *regexp.Regexp, packageName string) error

RunTests will run the tests only.

func (*VM) Set added in v0.16.1

func (vm *VM) Set(register Register, val *ast.Literal)

Set will set a register.

type Write added in v0.21.0

type Write struct {
	Data, Fd Register
}

Write writes data to a file.

func (*Write) Execute added in v0.21.0

func (ins *Write) Execute(_ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

func (*Write) String added in v0.21.0

func (ins *Write) String() string

String is the human-readable description of the instruction.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL