opruntime

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package opruntime is glue: a registry mapping an opcodes.Op to the Go function that implements it, plus dispatch.

It is not a virtual machine. It owns no registers, no operand stack, and no execution state; all state lives in closures the caller supplies as Handlers.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNilHandler       = errors.New("nil handler")
	ErrDuplicateHandler = errors.New("duplicate handler")
	ErrNoHandler        = errors.New("no handler registered")
)

Functions

func Run

func Run(program []opcodes.Instruction, table *Table) error

Run dispatches every instruction in program, in order, stopping at the first error. It is a thin convenience loop around Table.Dispatch.

Types

type DispatchError

type DispatchError struct {
	Index int
	Op    opcodes.Op
	Cause error
}

DispatchError identifies an instruction that failed to dispatch or whose handler returned an error. Index is -1 when produced directly by Table.Dispatch outside of Run.

func (*DispatchError) Error

func (e *DispatchError) Error() string

func (*DispatchError) Unwrap

func (e *DispatchError) Unwrap() error

type Handler

type Handler func(opcodes.Instruction) error

Handler executes a single decoded instruction. A Handler must return normally; it must not panic or call runtime.Goexit.

type RegisterError

type RegisterError struct {
	Op    opcodes.Op
	Cause error
}

RegisterError identifies a Table.Register call that could not be applied.

func (*RegisterError) Error

func (e *RegisterError) Error() string

func (*RegisterError) Unwrap

func (e *RegisterError) Unwrap() error

type Table

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

Table maps an opcodes.Op to the Handler that implements it. opcodes.Op is a uint8, so lookup is a direct array index rather than a hash lookup.

func NewTable

func NewTable() *Table

NewTable creates an empty Table.

func (*Table) Dispatch

func (t *Table) Dispatch(inst opcodes.Instruction) error

Dispatch looks up the handler registered for inst.Op and calls it. It returns a DispatchError if no handler is registered or if the handler itself returns an error.

Example
package main

import (
	"fmt"

	"github.com/apsis-io/velocity/opcodes"
	"github.com/apsis-io/velocity/opruntime"
)

func main() {
	const opPrint opcodes.Op = 1

	table := opruntime.NewTable()
	_ = table.Register(opPrint, func(inst opcodes.Instruction) error {
		fmt.Println("printed", inst.A)
		return nil
	})

	_ = table.Dispatch(opcodes.Instruction{Op: opPrint, A: 42})
}
Output:
printed 42

func (*Table) Handler

func (t *Table) Handler(op opcodes.Op) (Handler, bool)

Handler returns the handler registered for op, if any.

func (*Table) Register

func (t *Table) Register(op opcodes.Op, h Handler) error

Register adds h as the handler for op. Registering a nil handler or a second handler for the same op is an error.

Jump to

Keyboard shortcuts

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