vm

package
v0.0.0-...-485ca13 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

The vm package implements the Romualdo Virtual Machine, which executes the bytecode generated by the Romualdo Language Compiler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSWFromPath

CSWFromPath loads the CompiledStoryworld and DebugInfo from the given path, which can be either a compiled Storyworld (*.ras) file or a directory with the Storyworld source code (*.ral).

func DeserializeCallFrame

func DeserializeCallFrame(r io.Reader, stack *Stack) (*callFrame, errs.Error)

DeserializeCallFrame deserializes a callFrame from the given io.Reader. The stack parameter will be used for callFrame.stack.stack; you can technically pass a nil here and fill the field later, but things will look much tidier if you pass the right stack here.

func LoadCompiledStoryworldBinaries

func LoadCompiledStoryworldBinaries(cswPath string, diRequired bool) (*bytecode.CompiledStoryworld, *bytecode.DebugInfo, errs.Error)

LoadCompiledStoryworldBinaries loads the CompiledStoryworld from cwPath. It also looks for the corresponding DebugInfo file and loads it if found. If the DebugInfo file is not found, it returns an error only if diRequired is true.

func RunCSW

func RunCSW(csw *bytecode.CompiledStoryworld, di *bytecode.DebugInfo, trace bool) (err errs.Error)

RunCSW interprets the given CompiledStoryworld and (potentially nil) DebugInfo. If trace is true, it prints a trace/disassembly of the execution to stdout as it goes.

Types

type Stack

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

Stack implements the VM runtime stack, which is a stack of bytecode.Values.

func DeserializeStack

func DeserializeStack(r io.Reader) (*Stack, errs.Error)

DeserializeStack deserializes a Stack from the given io.Reader.

func (*Stack) Serialize

func (s *Stack) Serialize(w io.Writer) errs.Error

Serialize serializes the Stack to the given io.Writer.

type StackView

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

StackView provides a read-write view into a Stack. It looks just like a Stack, but uses data owned by a real Stack, and behaves as if its base was at some arbitrary point within that backing Stack.

It's assumed that all accesses made to this view are done while it is the topmost view created on the backing stack.

This behavior matches the use case of call frames.

type State

type State int

State represents the state of a VM.

const (
	// StateNew is the state of a VM that has just been created. From here,
	// you'd typically call VM.Start() or VM.LoadState().
	StateNew State = iota

	// StateWaitingForInput is the state of a VM that has started executing the
	// Storyworld and is waiting for input from the user. The typical next
	// action is to call either VM.Step() or VM.SaveState(). VM.LoadState() is
	// also valid.
	StateWaitingForInput

	// StateEndOfStory is the state of a VM that has finished executing the
	// Storyworld. From here, could simply exit, but calling VM.LoadState() or
	// VM.Start() are also valid. VM.SaveState() is also possible, but a bit
	// odd.
	StateEndOfStory
)

type VM

type VM struct {

	// Swid is the Storyworld ID. It must match the corresponding field in csw.
	Swid string

	// Swov is the Storyworld version. When loading a saved state, the
	// serialized state must be equal to or less than the corresponding field in
	// csw. In other words, we can deal with saved states from older versions of
	// the Storyworld, but not from newer versions.
	Swov int32

	// State is the current state of the VM.
	State State

	// Options contains the options available to the Player. It is only valid
	// when VM.State == StateWaitingForInput.
	Options string

	// Set DebugTraceExecution to true to make the VM disassemble the code as it
	// runs through it.
	DebugTraceExecution bool
	// contains filtered or unexported fields
}

VM is a Romualdo Virtual Machine.

func New

New returns a new Virtual Machine capable of executing the given Storyworld csw. If not nil, the VM will use the given DebugInfo di to provide better error messages.

func (*VM) Deserialize

func (vm *VM) Deserialize(r io.Reader) errs.Error

Deserialize deserializes a VM state from the given io.Reader.

func (*VM) Serialize

func (vm *VM) Serialize(w io.Writer) errs.Error

Serialize serializes the VM state to the given io.Writer.

func (*VM) Start

func (vm *VM) Start() string

Start starts the execution of the Storyworld, running until the first Listen instruction or the end of the Story (whatever comes first). Returns the first output generated by the Storyworld.

Must be called when vm.Started is false, otherwise it panics.

func (*VM) Step

func (vm *VM) Step(choice string) string

Step executes of the Storyworld until the next Listen instruction or the end of the Story (whatever comes first). Returns the output generated by the Storyworld.

Must be called when VM.WaitingForInput is true, otherwise it panics.

Jump to

Keyboard shortcuts

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