Documentation
¶
Overview ¶
Package vm implements a virtual machine (VM) to generate an instructions set from parsed cell's Abstract Syntax Tree (AST) for Intermediate Representation (IR) and execute these to calculate the cell value.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCompile = errors.New("compile error")
ErrCompile means error to compile AST into the instructions set.
var ErrRun = errors.New("run error")
ErrRun means error running the virtual machine on the instructions set.
Functions ¶
Types ¶
type BinOp ¶
type BinOp int
BinOp enumerates supported binary operators.
const ( // keep-sorted start BinOpDivide BinOp // dividie BinOpMinus // minus BinOpMultiply // multiply BinOpPlus // plus )
type Engine ¶
type Engine struct{}
Engine uses bytecode (instructions set) for intermediate representation in parsed cells.
func (Engine) Calculate ¶
Calculate run bytecode through Virtual Machine. It reports an error if IR is not bytecode.
type Function ¶
type Function int
Function enumerates supported functions.
const ( // keep-sorted start FuncSum Function // SUM )
type IfCall ¶
type IfCall struct {
RelOp // comparison operator used in the condition.
IfFail JumpOffset // jump instructions to reach fail-operand.
}
IfCall represents if-condition.
type Inst ¶
type Inst struct {
Type InstType // instruction type
Number float64 // number literal
BinOp // binary operator
Ref string // cell reference
*Call // function call
*IfCall // if
JumpOffset // jump offset
}
Inst is an instruction. It acts like a union: only one field is set depending on the instruction type.
type InstType ¶
type InstType int
InstType defines the instruction type.
const ( // keep-sorted start InstTypeBinOp InstType // operator InstTypeCall // function InstTypeIfCall // if InstTypeJump // jump InstTypeNumber // number InstTypeRef // reference )
type InstructionsSet ¶
type InstructionsSet struct {
Instructions []Inst // post-ordered AST instructions
}
InstructionsSet holds instructions for the virtual machine. It is pretty much a post-order AST.