vm

package
v0.19.5 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: MIT Imports: 16 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 Directory = "/tmp/okc"

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

View Source
const StateRegister = "0"

StateRegister is a reserved register for holding the map of the state. Effectively the instance or "this" context.

Variables

View Source
var Packages map[string]*File

These are populated with the generated lib.go file. See Makefile.

Functions

func PathForPackage added in v0.19.0

func PathForPackage(packageName string) string

PathForPackage returns the absolute path of where the okc 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         *types.Type
}

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 {
	// 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.

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 Call

type Call struct {
	FunctionName string
	Arguments    Registers
	Results      Registers
}

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

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

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 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
}

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.

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

func Load added in v0.19.0

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

func (*File) ResolveType added in v0.19.1

func (f *File) ResolveType(t *types.Type) *types.Type

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 []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 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.

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 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 LoadPackage added in v0.19.3

type LoadPackage struct {
	Result      Register
	PackageName string
}

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

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         *types.Type
	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 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 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.

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 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 {
	Object, Result Register
}

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 *types.Type
}

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 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 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 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 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 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 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) 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(pkgVariable string, file *File) error

func (*VM) LoadPackage added in v0.19.0

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

func (*VM) Raise added in v0.18.0

func (vm *VM) Raise(message string)

func (*VM) Run

func (vm *VM) Run() 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) error

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

Jump to

Keyboard shortcuts

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