vm

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package vm contains a stack-based virtual machine that interprets Watson.

Index

Constants

View Source
const (
	DefaultStackSize = 1024 // the default size of the stack
)

Variables

View Source
var (
	ErrStackEmpty               = errors.New("stack is empty")
	ErrMaximumStackSizeExceeded = errors.New("maximum stack size exceeded")
	ErrTypeMismatch             = errors.New("type mismatch")
)

Functions

This section is empty.

Types

type Op

type Op int

Op is an instruction executed by VM. Each op just manipulates the stack.

const (
	// Integer Operations
	Inew Op = iota // push(0);
	Iinc           // v: int = pop(); push(v + 1);
	Ishl           // v: int = pop(); push(v << 1);
	Iadd           // b: int = pop(); a: int = pop(); push(a + b);
	Ineg           // v: int = pop(); push(-n);
	Isht           // b: int = pop(); a: int = pop(); push(a << b);
	Itof           // n: int = pop(); push(n interpreted as an IEEE-754 64-bit floating-point number)
	Itou           // n: int = pop(); push(n interpreted as 64-bit unsigned integer)

	// Float Operations
	Finf // push(Inf);
	Fnan // push(NaN);
	Fneg // x: float = pop(); push(-x);

	// String Operations
	Snew // push("");
	Sadd // n: int = pop(); s: str = pop(); c = n && 0xff; push(s + c);

	// Object Operations
	Onew // push({});
	Oadd // v: any = pop(); k: str = pop(); o: obj = pop(); o[k] = v; push(o);

	// Array Operations
	Anew // push([]);
	Aadd // x: any = pop(); a: array = pop(); a.append(x); push(a);

	// Bool Operations
	Bnew // push(false);
	Bneg // b: bool = pop(); push(!b);

	// Nil Operations
	Nnew // push(nil);

	// Generic Operations
	Gdup // x: any = pop(); push(x); push(x);
	Gpop // pop();
	Gswp // a: any = pop(); b: any = pop(); push(a); push(b);

)

func AllOps

func AllOps() []Op

Returns all defined Ops.

func (Op) GoString

func (op Op) GoString() string

type VM

type VM struct {
	// contains filtered or unexported fields
}

VM is a virtual machine that consists of a stack of values and a pointer to the top of the stack.

func NewVM

func NewVM(opts ...VMOption) *VM

Returns a new VM with its stack allocated. For more details see VMOption.

func (*VM) Feed

func (vm *VM) Feed(op Op) error

Feed takes a op and executes corresponding operation. This can fail in various ways; e.g. type mismatch, stack overflow, etc.

func (*VM) FeedMulti

func (vm *VM) FeedMulti(ops []Op) error

FeedMulti takes a series of Ops and executes them sequentially. If one of them fails, it stops execution and returns an error.

func (*VM) Top

func (vm *VM) Top() (*types.Value, error)

Top returns a value in the top of the stack. This returns ErrStackEmpty if the stack is empty.

type VMOption

type VMOption interface {
	// contains filtered or unexported methods
}

VMOption provides the way to build VMs with custom configurations.

func WithStackSize

func WithStackSize(size int) VMOption

WithStackSize sets the stack size of a VM to the given value. If given size is less than or equal to zero, DefaultStackSize will be used.

Jump to

Keyboard shortcuts

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