runtime

package
v0.0.0-...-e334538 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

View Source
const (

	// RuntimeError is used for an error of unknown/generic nature.
	RuntimeError = "Runtime"

	// TypeError is used for an error where types are incompatible or wrong.
	TypeError = "Type"

	// InternalError is used for internal errors, and when something is wrong with the bytecode.
	InternalError = "Internal"

	// NameError is used for undefined variables, functions, and other names.
	NameError = "Name"

	// ArgumentError is used for invalid arguments to a function or model.
	ArgumentError = "Argument"

	// StructureError is used for a structure error, such as a break outside a loop.
	StructureError = "Structure"

	// IndexError is used when an invalid index/key is used.
	IndexError = "Index"
)

Variables

View Source
var (
	// MaxDataStackSize is the maximum amount of objects which are allowed to
	// be inside the data stack.
	MaxDataStackSize = 100000

	// DefaultDataStackCapacity is the capacity initially allocated to the stack. A
	// higher value will possibly increase performance, but use more RAM.
	DefaultDataStackCapacity = 32

	// ErrDataStackOverflow tells the user that too many objects were pushed to
	// the data stack.
	ErrDataStackOverflow = makeError(InternalError, "too many objects on the data stack")

	// ErrDataStackUnderflow tells the user that a pop was attempted but no objects
	// were on the stack.
	ErrDataStackUnderflow = makeError(InternalError, "no objects to pop from the data stack")
)
View Source
var Effectors [256]Effector

Effectors stores the effector function for each instruction. The effector for an instruction n can be accessed via Effectors[n].

View Source
var (
	// InterruptQueueSize specifies the buffer size of the interrupt queue.
	InterruptQueueSize = 64
)

Functions

This section is empty.

Types

type Effector

type Effector func(v *VM, f *Frame, arg rune) error

An Effector is the function which is called for a particular instruction.

type Error

type Error struct {
	Type    ErrorType
	Message string
}

An Error represents any type of runtime error (not just RuntimeError), and implements the error interface.

func (*Error) Error

func (e *Error) Error() string

type ErrorType

type ErrorType string

ErrorType specifies the type of a runtime error.

type Frame

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

A Frame is created for each function call, and also one for the main program. It contains the bytecode to execute, along with the frame's constants and names, and other data.

type Interrupt

type Interrupt int

Interrupt defines the type of an interrupt which, when sent to the virtual machine through the Interrupts chan, stops the fetch-decode-execute cycle and does something.

const (

	// Stop stops the execution
	Stop Interrupt

	// Pause pauses the execution until Resume is sent. All other interrupts are ignored until
	// Resume is sent.
	Pause

	// Resume resumes a paused virtual machine.
	Resume
)

type Stack

type Stack struct {
	Objects []object.Object
}

A Stack is created for each frame in the virtual machine, and stores a stack of objects which can be popped or pushed.

func NewStack

func NewStack() *Stack

NewStack makes a new empty Stack, with capacity equal to DefaultStackCapacity.

func (*Stack) Len

func (s *Stack) Len() int

Len gets the length of the stack.

func (*Stack) Pop

func (s *Stack) Pop() (object.Object, error)

Pop pops an Object from the Stack, returning it. If it returns an error, it will be ErrDataStackUnderflow.

func (*Stack) Push

func (s *Stack) Push(obj object.Object) error

Push pushes an Object to the top of a Stack. If it returns an error, it will be ErrDataStackOverflow.

func (*Stack) Top

func (s *Stack) Top() (object.Object, error)

Top returns the top item in the Stack. If there are no items, it will return an ErrDataStackUnderflow. It is equivalent to popping, but without removing the item from the stack.

type Store

type Store struct {
	Data      map[string]*Variable
	Enclosing *Store
}

A Store contains all the variables defined in a particular scope. A Store also has a pointer to the enclosing scope.

func NewStore

func NewStore(enclosing *Store) *Store

NewStore creates a new empty store with the given enclosing scope (can be nil).

func (*Store) Get

func (s *Store) Get(name string) (*Variable, bool)

Get gets a variable from the store. If it isn't found, it checks the enclosing scope, and so on.

func (*Store) Set

func (s *Store) Set(name string, val object.Object, declare bool)

Set sets a variable in the store. If declare is false, enclosing scopes will be assigned to instead of this one, if the variable is already defined there.

type StorePool

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

A StorePool contains a number of Stores, which can be released quickly. This avoids creating stores, which is slower than just reusing existing ones.

func NewStorePool

func NewStorePool() *StorePool

NewStorePool makes a new store pool with initialStorePoolSize stores.

func (*StorePool) Add

func (s *StorePool) Add(sto *Store)

Add adds a store back into the pool.

func (*StorePool) IsEmpty

func (s *StorePool) IsEmpty() bool

IsEmpty checks whether or not the pool is empty.

func (*StorePool) Release

func (s *StorePool) Release(enclosing *Store) *Store

Release releases a store from the pool, allowing for use elsewhere.

type VM

type VM struct {

	// Interrupts is a queue (in the form of a channel) which, when an interrupt is added,
	// will stop the virtual machine and do something based on the nature of the interrupt.
	Interrupts chan Interrupt

	// Out is the io.Writer to which the virtual machine outputs to. This includes functions
	// like print, but also errors and various messages.
	Out io.Writer
	// contains filtered or unexported fields
}

A VM (for Virtual Machine), interprets bytecode. It's a stack machine, so operates by pushing to and popping from data stacks, instead of using registers like a computer.

func New

func New() *VM

New creates a new virtual machine.

func (*VM) Error

func (v *VM) Error() error

Error returns the VM's error, if one exists. nil if there is no error.

func (*VM) ExtractValue

func (v *VM) ExtractValue() object.Object

ExtractValue returns the top value from the top frame, if both those things exist.

func (*VM) MakeFrame

func (v *VM) MakeFrame(code bytecode.Code, args, store *Store, constants []object.Object, names []string, jumps []int) *Frame

MakeFrame makes a Frame instance with the given parameters. offset is set to 0 and a new data stack is created. The variables in args, if it's non-nil, are declared in the Frame's store (note: declared, not assigned).

func (*VM) PopFrame

func (v *VM) PopFrame() *Frame

PopFrame pops the frame from the top of the call stack.

func (*VM) PushFrame

func (v *VM) PushFrame(frame *Frame)

PushFrame pushes a frame to the top of the call stack.

func (*VM) Run

func (v *VM) Run() (object.Object, error)

Run executes a virtual machine, starting from the most recently pushed frame. If, after execution, any values are left in the top frame, the top one will be returned. It will also return, if any, a runtime error.

type Variable

type Variable struct {
	Name  string
	Value object.Object
}

A Variable represents a Radon variable. A Variable includes a Name and Value.

Jump to

Keyboard shortcuts

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