vm

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatePaused     = iota
	StateRunning    = iota
	StateStepping   = iota
	StateTerminated = iota
)

The current state of the VM

View Source
const MaxStringLenght = 1024

Variables

View Source
var TerminateOnDoneVar = func(vm *VM) bool {
	value, exists := vm.GetVariable(":done")
	wantnot, _ := VariableFromType(0)
	if exists && !value.Equals(wantnot) {
		go vm.Terminate()
		return false
	}
	return true
}

TerminateOnDoneVar is a predefined LineExecutedHandlerFunc that can be used to terminate the VM once :done is set to 1

Functions

This section is empty.

Types

type BreakpointFunc

type BreakpointFunc func(vm *VM) bool

BreakpointFunc is a function that is called when a breakpoint is encountered. If true is returned the execution is resumed. Otherwise the vm remains paused

type Coordinator

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

Coordinator is responsible for coordinating the execution of multiple VMs It coordinates the line-by-line execution of the scripts and provides shared global variables

func NewCoordinator

func NewCoordinator() *Coordinator

NewCoordinator returns a new coordinator

func (*Coordinator) GetVariable

func (c *Coordinator) GetVariable(name string) (*Variable, bool)

GetVariable gets the current state of a global variable getting variables is case-insensitive

func (*Coordinator) GetVariables

func (c *Coordinator) GetVariables() map[string]Variable

GetVariables gets the current state of all global variables All returned variables have normalized (lowercased) names

func (*Coordinator) Pause added in v0.1.10

func (c *Coordinator) Pause()

Pause all coordinated vms

func (*Coordinator) Resume added in v0.1.10

func (c *Coordinator) Resume()

Resume all coordinated vms

func (*Coordinator) Run

func (c *Coordinator) Run()

Run starts the coordinated exection Once run has been called, no new VMs MUSt be added!!!

func (*Coordinator) SetVariable

func (c *Coordinator) SetVariable(name string, value *Variable) error

SetVariable sets the current state of a global variable setting variables is case-insensitive

func (*Coordinator) Terminate

func (c *Coordinator) Terminate()

Terminate all coordinated vms Once all VMs terminate the coordinator-goroutine will also shut-down

func (*Coordinator) WaitForTermination

func (c *Coordinator) WaitForTermination()

WaitForTermination blocks until all coordinated vms terminate

type ErrorHandlerFunc

type ErrorHandlerFunc func(vm *VM, err error) bool

ErrorHandlerFunc is a function that is called when a runtime-error is encountered. If true is returned the execution is resumed. Otherwise the vm remains paused

type FinishHandlerFunc

type FinishHandlerFunc func(vm *VM)

FinishHandlerFunc is a function that is called when the programm finished execution (looping is disabled)

type LineExecutedHandlerFunc added in v0.0.22

type LineExecutedHandlerFunc func(vm *VM) bool

LineExecutedHandlerFunc it the type for the handler that is called after a line has been executed

type RuntimeError

type RuntimeError struct {
	Base error
	// The Node that caused the error
	Node ast.Node
}

RuntimeError represents an error encountered during execution

func (RuntimeError) Error

func (e RuntimeError) Error() string

type VM added in v0.0.15

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

VM is a virtual machine to execute YOLOL-Code

func Create added in v0.0.15

func Create(prog *ast.Program) *VM

Create creates a new VM to run the given program in a seperate goroutine. The returned VM is paused. Configure it using the setters and then call Resume()

func CreateFromSource added in v0.0.15

func CreateFromSource(prog string) (*VM, error)

CreateFromSource creates a new VM to run the given program in a seperate goroutine. The returned VM is paused. Configure it using the setters and then call Resume()

func (*VM) AddBreakpoint added in v0.0.15

func (v *VM) AddBreakpoint(line int)

AddBreakpoint adds a breakpoint at the line. Breakpoint-lines always refer to the position recorded in the ast nodes, not the position of the Line in the Line-Slice of ast.Program.

func (*VM) CurrentAstLine added in v0.0.15

func (v *VM) CurrentAstLine() int

CurrentAstLine returns the current (=next to be executed) ast line of the program

func (*VM) CurrentSourceColoumn added in v0.0.15

func (v *VM) CurrentSourceColoumn() int

CurrentSourceColoumn returns the current (=next to be executed) source column of the program

func (*VM) CurrentSourceLine added in v0.0.15

func (v *VM) CurrentSourceLine() int

CurrentSourceLine returns the current (=next to be executed) source line of the program

func (*VM) GetExecutedLines added in v0.0.19

func (v *VM) GetExecutedLines() int

GetExecutedLines returns the number of lines executed by this VM

func (*VM) GetProgram added in v0.0.17

func (v *VM) GetProgram() *ast.Program

GetProgram returns the program that is run by the VM

func (*VM) GetVariable added in v0.0.15

func (v *VM) GetVariable(name string) (*Variable, bool)

GetVariable gets the current state of a variable

func (*VM) GetVariables added in v0.0.15

func (v *VM) GetVariables() map[string]Variable

GetVariables gets the current state of all variables

func (*VM) ListBreakpoints added in v0.0.15

func (v *VM) ListBreakpoints() []int

ListBreakpoints returns the list of active breakpoints

func (*VM) Pause added in v0.0.15

func (v *VM) Pause()

Pause pauses the execution Blocks until the VM reacts on the request

func (*VM) PrintVariables added in v0.0.15

func (v *VM) PrintVariables() string

PrintVariables gets an overview over the current variable state

func (*VM) RemoveBreakpoint added in v0.0.15

func (v *VM) RemoveBreakpoint(line int)

RemoveBreakpoint removes the breakpoint at the line

func (*VM) Resume added in v0.0.15

func (v *VM) Resume()

Resume resumes execution after a breakpoint or pause() or the initial stopped state Blocks until the VM reacts on the request

func (*VM) SetBreakpointHandler added in v0.0.15

func (v *VM) SetBreakpointHandler(f BreakpointFunc)

SetBreakpointHandler sets the function to be called when hitting a breakpoint

func (*VM) SetCoordinator added in v0.0.15

func (v *VM) SetCoordinator(c *Coordinator)

SetCoordinator sets the coordinator that is used to coordinate execution with other vms

func (*VM) SetErrorHandler added in v0.0.15

func (v *VM) SetErrorHandler(f ErrorHandlerFunc)

SetErrorHandler sets the function to be called when encountering an error

func (*VM) SetFinishHandler added in v0.0.15

func (v *VM) SetFinishHandler(f FinishHandlerFunc)

SetFinishHandler sets the function to be called when execution finishes

func (*VM) SetLineExecutedHandler added in v0.0.22

func (v *VM) SetLineExecutedHandler(handler LineExecutedHandlerFunc)

SetLineExecutedHandler registers a callback that is executed when a line has been completely executed When running coordinated, this handler is called before passing execution to the next VM

func (*VM) SetMaxExecutedLines added in v0.0.15

func (v *VM) SetMaxExecutedLines(lines int)

SetMaxExecutedLines sets the maximum number of lines to run if the amount is reached the VM terminates Can be used to prevent blocking by endless loops <= 0 disables this. Default is 0

func (*VM) SetStepHandler added in v0.0.15

func (v *VM) SetStepHandler(f FinishHandlerFunc)

SetStepHandler sets the function to be called when a step completes

func (*VM) SetVariable added in v0.0.15

func (v *VM) SetVariable(name string, value *Variable) error

SetVariable sets the current state of a variable

func (*VM) State added in v0.0.15

func (v *VM) State() int

State returns the current vm state

func (*VM) Step added in v0.0.15

func (v *VM) Step()

Step executes the next line and paused the execution Blocks until the VM reacts on the request

func (*VM) Terminate added in v0.0.15

func (v *VM) Terminate()

Terminate the vm goroutine (if running)

func (*VM) WaitForTermination added in v0.0.15

func (v *VM) WaitForTermination()

WaitForTermination blocks until the VM terminates

type Variable

type Variable struct {
	Value interface{}
}

Variable represents a yolol-variable during the execution

func RunBinaryOperation

func RunBinaryOperation(arg1 *Variable, arg2 *Variable, operator string) (*Variable, error)

RunBinaryOperation executes the given operation with the given arguments and returns the result

func RunUnaryOperation

func RunUnaryOperation(arg *Variable, operator string) (*Variable, error)

RunUnaryOperation executes the given operation with the given argument and returns the result

func VariableFromString added in v0.0.17

func VariableFromString(str string) *Variable

VariableFromString tries to create a variable of the correct type from the given string. If the string is enclosed in quotes, the string between the quotes is used as string-value for the variable. Else, it tries to parse the given string into a number. If that also fails, the plain given string is used as value.

func VariableFromType added in v0.0.17

func VariableFromType(inp interface{}) (*Variable, error)

VariableFromType creates a new variable from the given input. The type of the variable is decided by the input-type.

func (*Variable) Bool added in v0.1.0

func (v *Variable) Bool() *Variable

Bool returns the truth-value of a variable

func (*Variable) Equals added in v0.0.17

func (v *Variable) Equals(other *Variable) bool

Equals checks if this variable equals another variable

func (*Variable) IsNumber

func (v *Variable) IsNumber() bool

IsNumber returns true if the variable represents a number

func (*Variable) IsString

func (v *Variable) IsString() bool

IsString returns true if the variable represents a string

func (*Variable) Itoa

func (v *Variable) Itoa() string

Itoa returns the string-representation of the number stored in the variable

func (*Variable) Number

func (v *Variable) Number() number.Number

Number returns the value of the variable as number

func (*Variable) Repr added in v0.0.17

func (v *Variable) Repr() string

Repr returns the string-representation of the variable. If the variable is of type string, its value is enclosed in quotes.

func (*Variable) SameType

func (v *Variable) SameType(other *Variable) bool

SameType returns true if the variable has the same type as the given variable

func (*Variable) String

func (v *Variable) String() string

func (*Variable) TypeName added in v0.0.17

func (v *Variable) TypeName() string

TypeName returns the name of the type this variable has

Jump to

Keyboard shortcuts

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