vm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

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

View Source
var ErrCompile = errors.New("compile error")

ErrCompile means error to compile AST into the instructions set.

View Source
var ErrRun = errors.New("run error")

ErrRun means error running the virtual machine on the instructions set.

Functions

func Run

func Run(iset *InstructionsSet, refcal func(string) (float64, error)) (float64, error)

Run executes the instructions set using a virtual machine (VM). Is uses refcal to calculate references.

Types

type BinOp

type BinOp int

BinOp enumerates supported binary operators.

const (

	// keep-sorted start
	BinOpDivide   BinOp // dividie
	BinOpMinus          // minus
	BinOpMultiply       // multiply
	BinOpPlus           // plus

)

func (BinOp) String

func (i BinOp) String() string

type Call

type Call struct {
	Func Function // function identifier
	Args int      // the number of arguments
}

Call is a function call, e.g. "SUM(...)".

type Engine

type Engine struct{}

Engine uses bytecode (instructions set) for intermediate representation in parsed cells.

func (Engine) Calculate

func (Engine) Calculate(data any, refcal func(string) (float64, error)) (float64, error)

Calculate run bytecode through Virtual Machine. It reports an error if IR is not bytecode.

func (Engine) Parse

func (Engine) Parse(s string) (any, error)

Parse parses cell content and returns bytecode.

func (Engine) WriteIR

func (Engine) WriteIR() bool

WriteIR adds intermediate representation to Sheet state in read/write.

type Function

type Function int

Function enumerates supported functions.

const (

	// keep-sorted start
	FuncSum Function // SUM

)

func (Function) String

func (i Function) String() string

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

)

func (InstType) String

func (i InstType) String() string

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.

func Compile

func Compile(n ast.Node) (InstructionsSet, error)

Compile converts AST to the instructions set.

type JumpOffset

type JumpOffset int

JumpOffset is the number of instructions to skip.

type Number

type Number float64

Number is a floating precision number.

type Ref

type Ref string

Ref is a cell reference.

type RelOp

type RelOp int

RelOp enumerates comparison operators.

const (

	// keep-sorted start
	RelOpEqual          RelOp // equal
	RelOpGreater              // greater
	RelOpGreaterOrEqual       // greaterorequal
	RelOpLess                 // less
	RelOpLessOrEqual          // lessorequal
	RelOpNotEqual             // notequal

)

func (RelOp) String

func (i RelOp) String() string

Jump to

Keyboard shortcuts

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