vm

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

Lib is populated with the generated lib.go file. See Makefile.

Functions

This section is empty.

Types

type Add

type Add struct {
	Left, Right, Result string
}

Add will sum two numbers.

func (*Add) Execute

func (ins *Add) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type And

type And struct {
	Left, Right, Result string
}

And is a logical AND between two bools.

func (*And) Execute

func (ins *And) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type ArrayAllocNumber

type ArrayAllocNumber struct {
	Size, Result string
}

ArrayAllocNumber allocates a "[]number" of fixed size.

func (*ArrayAllocNumber) Execute

func (ins *ArrayAllocNumber) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type ArrayGet

type ArrayGet struct {
	Array, Index, Result string
}

ArrayGet gets a value from the array by its index.

func (*ArrayGet) Execute

func (ins *ArrayGet) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type ArraySet

type ArraySet struct {
	Array, Index, Value string
}

ArraySet sets a number value to an index.

func (*ArraySet) Execute

func (ins *ArraySet) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Assert

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

Assert is used in tests.

func (*Assert) Execute

func (ins *Assert) Execute(registers map[string]*ast.Literal, _ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

type Assign

type Assign struct {
	VariableName string
	Value        *ast.Literal
	Register     string
}

Assign sets a variable to the result of an expression.

func (*Assign) Execute

func (ins *Assign) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Call

type Call struct {
	FunctionName string
	Arguments    []string
	Results      []string
}

Call tells the VM to jump to another function.

func (*Call) Execute

func (ins *Call) Execute(registers map[string]*ast.Literal, _ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

type Combine

type Combine struct {
	Left, Right, Result string
}

Combine will create a new data by joining two other datas.

func (*Combine) Execute

func (ins *Combine) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type CompiledFunc

type CompiledFunc struct {
	Arguments    []string
	Instructions []Instruction
	Registers    int
	Variables    map[string]string
}

func (*CompiledFunc) Append

func (c *CompiledFunc) Append(instruction Instruction)

func (*CompiledFunc) NewVariable

func (c *CompiledFunc) NewVariable(variableName string, kind string)

func (*CompiledFunc) NextRegister

func (c *CompiledFunc) NextRegister() string

type CompiledTest

type CompiledTest struct {
	*CompiledFunc
	TestName string
}

CompiledTest is a runnable test.

type Concat

type Concat struct {
	Left, Right, Result string
}

Concat will create a new string by joining two other strings.

func (*Concat) Execute

func (ins *Concat) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Divide

type Divide struct {
	Left, Right, Result string
}

Divide will multiply two numbers.

func (*Divide) Execute

func (ins *Divide) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Equal

type Equal struct {
	Left, Right, Result string
}

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(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type EqualNumber

type EqualNumber struct {
	Left, Right, Result string
}

EqualNumber will compare two numbers for equality.

func (*EqualNumber) Execute

func (ins *EqualNumber) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type GreaterThanEqualNumber

type GreaterThanEqualNumber struct {
	Left, Right, Result string
}

GreaterThanEqualNumber will compare two numbers.

func (*GreaterThanEqualNumber) Execute

func (ins *GreaterThanEqualNumber) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type GreaterThanEqualString

type GreaterThanEqualString struct {
	Left, Right, Result string
}

GreaterThanEqualString will compare two strings.

func (*GreaterThanEqualString) Execute

func (ins *GreaterThanEqualString) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type GreaterThanNumber

type GreaterThanNumber struct {
	Left, Right, Result string
}

GreaterThanNumber will compare two numbers.

func (*GreaterThanNumber) Execute

func (ins *GreaterThanNumber) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type GreaterThanString

type GreaterThanString struct {
	Left, Right, Result string
}

GreaterThanString will compare two strings.

func (*GreaterThanString) Execute

func (ins *GreaterThanString) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Instruction

type Instruction interface {
	Execute(registers map[string]*ast.Literal, currentInstruction *int, vm *VM) error
}

An Instruction can be executed by the VM.

type InternalDefinition

type InternalDefinition struct {
	CompiledFunc *CompiledFunc
	FuncDef      *ast.Func
}

InternalDefinition is used by Lib to hold the definitions and compiled code for internal functions.

type Jump

type Jump struct {
	To int
}

Jump will jump to the instruction.

func (*Jump) Execute

func (ins *Jump) Execute(registers map[string]*ast.Literal, i *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type JumpUnless

type JumpUnless struct {
	Condition string
	To        int
}

JumpUnless will jump to the instruction if the expression is false.

func (*JumpUnless) Execute

func (ins *JumpUnless) Execute(registers map[string]*ast.Literal, i *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Len

type Len struct {
	Argument, Result string
}

Len is used to determine the size of an array or map.

func (*Len) Execute

func (ins *Len) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type LessThanEqualNumber

type LessThanEqualNumber struct {
	Left, Right, Result string
}

LessThanEqualNumber will compare two numbers.

func (*LessThanEqualNumber) Execute

func (ins *LessThanEqualNumber) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type LessThanEqualString

type LessThanEqualString struct {
	Left, Right, Result string
}

LessThanEqualString will compare two strings.

func (*LessThanEqualString) Execute

func (ins *LessThanEqualString) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type LessThanNumber

type LessThanNumber struct {
	Left, Right, Result string
}

LessThanNumber will compare two numbers.

func (*LessThanNumber) Execute

func (ins *LessThanNumber) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type LessThanString

type LessThanString struct {
	Left, Right, Result string
}

LessThanString will compare two strings.

func (*LessThanString) Execute

func (ins *LessThanString) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type MapAllocNumber

type MapAllocNumber struct {
	Size, Result string
}

MapAllocNumber allocates a "{}number" of fixed size.

func (*MapAllocNumber) Execute

func (ins *MapAllocNumber) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type MapGet

type MapGet struct {
	Map, Key, Result string
}

MapGet gets a value from the map by its key.

func (*MapGet) Execute

func (ins *MapGet) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type MapSet

type MapSet struct {
	Map, Key, Value string
}

MapSet sets a number value to an index.

func (*MapSet) Execute

func (ins *MapSet) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Multiply

type Multiply struct {
	Left, Right, Result string
}

Multiply will multiply two numbers.

func (*Multiply) Execute

func (ins *Multiply) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type NextArray

type NextArray struct {
	Array       string // Register In (array): Containing the iterating array.
	Cursor      string // Register In (number): Containing the current position.
	KeyResult   string // Register Out (any): Load the key into this register.
	ValueResult string // Register Out (any): Load the value into this register.
	Result      string // Register Out (bool): Still more items?
}

NextArray is used to tick an array iterator forward.

func (*NextArray) Execute

func (ins *NextArray) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type NextMap

type NextMap struct {
	Map         string // Register In (map): Containing the iterating map.
	Cursor      string // Register In (number): Containing the current position.
	KeyResult   string // Register Out (any): Load the key into this register.
	ValueResult string // Register Out (any): Load the value into this register.
	Result      string // Register Out (bool): Still more items?
}

NextMap is used to tick a map iterator forward.

func (*NextMap) Execute

func (ins *NextMap) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Not

type Not struct {
	Left, Result string
}

Not is a logical NOT of a bool.

func (*Not) Execute

func (ins *Not) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type NotEqual

type NotEqual struct {
	Left, Right, Result string
}

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(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type NotEqualNumber

type NotEqualNumber struct {
	Left, Right, Result string
}

NotEqualNumber will compare two numbers for equality.

func (*NotEqualNumber) Execute

func (ins *NotEqualNumber) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Or

type Or struct {
	Left, Right, Result string
}

Or is a logical OR between two bools.

func (*Or) Execute

func (ins *Or) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Print

type Print struct {
	Arguments []string
	Stdout    io.Writer
}

Print will output a string to stdout.

func (*Print) Execute

func (ins *Print) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Remainder

type Remainder struct {
	Left, Right, Result string
}

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(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type Return

type Return struct {
	Results []string
}

Return tells the VM to jump out of this function.

func (*Return) Execute

func (ins *Return) Execute(registers map[string]*ast.Literal, _ *int, vm *VM) error

Execute implements the Instruction interface for the VM.

type Subtract

type Subtract struct {
	Left, Right, Result string
}

Subtract will subtract two numbers.

func (*Subtract) Execute

func (ins *Subtract) Execute(registers map[string]*ast.Literal, _ *int, _ *VM) error

Execute implements the Instruction interface for the VM.

type VM

type VM struct {
	Return []string

	// Stats when running tests.
	TestsPass, TestsFailed int
	TotalAssertions        int
	CurrentTestName        string
	CurrentTestPassed      bool
	// contains filtered or unexported fields
}

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

func NewVM

func NewVM(fns map[string]*CompiledFunc, tests []*CompiledTest, pkg string) *VM

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

func (*VM) Run

func (vm *VM) Run() error

Run will run the program.

func (*VM) RunTests

func (vm *VM) RunTests() error

Run will run the tests only.

Jump to

Keyboard shortcuts

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