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 ¶
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); )
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 (*VM) Feed ¶
Feed takes a op and executes corresponding operation. This can fail in various ways; e.g. type mismatch, stack overflow, etc.
type VMOption ¶
type VMOption interface {
// contains filtered or unexported methods
}
VMOption provides the way to build VMs with custom configurations.
func WithStackSize ¶
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.
Click to show internal directories.
Click to hide internal directories.