vm

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package vm handles the line-by-line execution of vise bytecode.

Index

Constants

View Source
const (
	NOOP   = 0
	CATCH  = 1
	CROAK  = 2
	LOAD   = 3
	RELOAD = 4
	MAP    = 5
	MOVE   = 6
	HALT   = 7
	INCMP  = 8
	MSINK  = 9
	MOUT   = 10
	MNEXT  = 11
	MPREV  = 12
)

VM Opcodes

View Source
const VERSION = 0

Variables

View Source
var (
	OpcodeString = map[Opcode]string{
		NOOP:   "NOOP",
		CATCH:  "CATCH",
		CROAK:  "CROAK",
		LOAD:   "LOAD",
		RELOAD: "RELOAD",
		MAP:    "MAP",
		MOVE:   "MOVE",
		HALT:   "HALT",
		INCMP:  "INCMP",
		MSINK:  "MSINK",
		MOUT:   "MOUT",
		MNEXT:  "MNEXT",
		MPREV:  "MPREV",
	}

	OpcodeIndex = map[string]Opcode{
		"NOOP":   NOOP,
		"CATCH":  CATCH,
		"CROAK":  CROAK,
		"LOAD":   LOAD,
		"RELOAD": RELOAD,
		"MAP":    MAP,
		"MOVE":   MOVE,
		"HALT":   HALT,
		"INCMP":  INCMP,
		"MSINK":  MSINK,
		"MOUT":   MOUT,
		"MNEXT":  MNEXT,
		"MPREV":  MPREV,
	}
)

Functions

func CheckTarget

func CheckTarget(target []byte, st *state.State) (bool, error)

CheckTarget tests whether the navigation state transition is available in the current state.

Fails if target is formally invalid, or if navigation is unavailable.

func NewInvalidInputError

func NewInvalidInputError(input string) error

NewInvalidInputError creates a new InvalidInputError

func NewLine

func NewLine(instructionList []byte, instruction uint16, strargs []string, byteargs []byte, numargs []uint8) []byte

NewLine creates a new instruction line for the VM.

func ParseCatch

func ParseCatch(b []byte) (string, uint32, bool, []byte, error)

ParseCatch parses and extracts the expected argument portion of a CATCH instruction

func ParseCroak

func ParseCroak(b []byte) (uint32, bool, []byte, error)

ParseCroak parses and extracts the expected argument portion of a CROAK instruction

func ParseHalt

func ParseHalt(b []byte) ([]byte, error)

ParseHalt parses and extracts the expected argument portion of a HALT instruction

func ParseInCmp

func ParseInCmp(b []byte) (string, string, []byte, error)

ParseInCmp parses and extracts the expected argument portion of a INCMP instruction

func ParseLoad

func ParseLoad(b []byte) (string, uint32, []byte, error)

ParseLoad parses and extracts the expected argument portion of a LOAD instruction

func ParseMNext

func ParseMNext(b []byte) (string, string, []byte, error)

ParseMNext parses and extracts the expected argument portion of a MNEXT instruction

func ParseMOut

func ParseMOut(b []byte) (string, string, []byte, error)

ParseMOut parses and extracts the expected argument portion of a MOUT instruction

func ParseMPrev

func ParseMPrev(b []byte) (string, string, []byte, error)

ParseMPrev parses and extracts the expected argument portion of a MPREV instruction

func ParseMSink

func ParseMSink(b []byte) ([]byte, error)

ParseMSink parses and extracts the expected argument portion of a MSINK instruction

func ParseMap

func ParseMap(b []byte) (string, []byte, error)

ParseMap parses and extracts the expected argument portion of a MAP instruction

func ParseMove

func ParseMove(b []byte) (string, []byte, error)

ParseMove parses and extracts the expected argument portion of a MOVE instruction

func ParseReload

func ParseReload(b []byte) (string, []byte, error)

ParseReload parses and extracts the expected argument portion of a RELOAD instruction

func RegisterInputValidator added in v0.2.0

func RegisterInputValidator(k int, v string) error

func Rewind added in v0.3.0

func Rewind(sym string, st *state.State, ca cache.Memory) (string, error)

func ValidInput

func ValidInput(input []byte) (int, error)

CheckInput validates the given byte string as client input.

func ValidSym

func ValidSym(input []byte) error

CheckSym validates the given byte string as a node symbol.

Types

type ExternalCodeError

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

ExternalCodeError indicates an error that occurred when resolving an external code symbol (LOAD, RELOAD).

func NewExternalCodeError

func NewExternalCodeError(sym string, err error) *ExternalCodeError

NewExternalCodeError creates a new ExternalCodeError.

func (ExternalCodeError) Error

func (e ExternalCodeError) Error() string

Error implements the Error interface.

func (*ExternalCodeError) WithCode

func (e *ExternalCodeError) WithCode(code int) *ExternalCodeError

type InvalidInputError

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

InvalidInputError indicates client input that was unhandled by the bytecode (INCMP fallthrough)

func (InvalidInputError) Error

func (e InvalidInputError) Error() string

Error implements the Error interface.

type Opcode

type Opcode uint16

func ParseOp

func ParseOp(b []byte) (Opcode, []byte, error)

ParseOp verifies and extracts the expected opcode portion of an instruction

type ParseHandler added in v0.3.0

type ParseHandler struct {
	Catch  func(string, uint32, bool) error
	Croak  func(uint32, bool) error
	Load   func(string, uint32) error
	Reload func(string) error
	Map    func(string) error
	Move   func(string) error
	Halt   func() error
	InCmp  func(string, string) error
	MOut   func(string, string) error
	MSink  func() error
	MNext  func(string, string) error
	MPrev  func(string, string) error
	// contains filtered or unexported fields
}

func NewParseHandler added in v0.3.0

func NewParseHandler() *ParseHandler

func (*ParseHandler) Length added in v0.3.0

func (ph *ParseHandler) Length() int

func (*ParseHandler) ParseAll added in v0.3.0

func (ph *ParseHandler) ParseAll(b []byte) (int, error)

ParseAll parses and verifies all instructions from bytecode.

If writer is not nil, the parsed instruction as assembly code line string is written to it.

Bytecode is consumed (and written) one instruction at a time.

It fails on any parse error encountered before the bytecode EOF is reached.

func (*ParseHandler) ToString added in v0.3.0

func (ph *ParseHandler) ToString(b []byte) (string, error)

ToString verifies all instructions in bytecode and returns an assmebly code instruction for it.

func (*ParseHandler) WithDefaultHandlers added in v0.3.0

func (ph *ParseHandler) WithDefaultHandlers() *ParseHandler

func (*ParseHandler) WithWriter added in v0.3.0

func (ph *ParseHandler) WithWriter(w io.Writer) *ParseHandler

type Vm

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

Vm holds sub-components mutated by the vm execution. TODO: Renderer should be passed to avoid proxy methods not strictly related to vm operation

func NewVm

func NewVm(st *state.State, rs resource.Resource, ca cache.Memory, sizer *render.Sizer) *Vm

NewVm creates a new Vm.

func (*Vm) Render

func (vm *Vm) Render(ctx context.Context) (string, error)

Render wraps output rendering, and handles error when attempting to browse beyond the rendered page count.

func (*Vm) Reset

func (vmi *Vm) Reset()

Reset re-initializes sub-components for output rendering.

func (*Vm) Run

func (vm *Vm) Run(ctx context.Context, b []byte) ([]byte, error)

Run extracts individual op codes and arguments and executes them.

Each step may update the state.

On error, the remaining instructions will be returned. State will not be rolled back.

func (*Vm) String added in v0.3.1

func (vmi *Vm) String() string

func (*Vm) WithMenuSeparator added in v0.2.2

func (vmi *Vm) WithMenuSeparator(sep string) *Vm

WithMenuSeparator is a chainable function that sets the separator string to use in the menu renderer.

Jump to

Keyboard shortcuts

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