quasigo

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2021 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package quasigo implements a Go subset compiler and interpreter.

The implementation details are not part of the contract of this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Disasm

func Disasm(env *Env, fn *Func) string

Disasm returns the compiled function disassembly text. This output is not guaranteed to be stable between versions and should be used only for debugging purposes.

Types

type CallResult

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

CallResult is a return value of Call function. For most functions, Value() should be called to get the actual result. For int-typed functions, IntValue() should be used instead.

func Call

func Call(env *EvalEnv, fn *Func, args ...interface{}) CallResult

Call invokes a given function with provided arguments.

func (CallResult) IntValue

func (res CallResult) IntValue() int

IntValue unboxes an actual call return value.

func (CallResult) Value

func (res CallResult) Value() interface{}

Value unboxes an actual call return value. For int results, use IntValue().

type CompileContext

type CompileContext struct {
	// Env is shared environment that should be used for all functions
	// being compiled; then it should be used to execute these functions.
	Env *Env

	Types *types.Info
	Fset  *token.FileSet
}

CompileContext is used to provide necessary data to the compiler.

type Env

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

Env is used to hold both compilation and evaluation data.

func NewEnv

func NewEnv() *Env

NewEnv creates a new empty environment.

func (*Env) AddFunc

func (env *Env) AddFunc(pkgPath, funcName string, f *Func)

AddFunc binds `$pkgPath.$funcName` symbol with f.

func (*Env) AddNativeFunc

func (env *Env) AddNativeFunc(pkgPath, funcName string, f func(*ValueStack))

AddNativeFunc binds `$pkgPath.$funcName` symbol with f. A pkgPath should be a full package path in which funcName is defined.

func (*Env) AddNativeMethod

func (env *Env) AddNativeMethod(typeName, methodName string, f func(*ValueStack))

AddNativeMethod binds `$typeName.$methodName` symbol with f. A typeName should be fully qualified, like `github.com/user/pkgname.TypeName`. It method is defined only on pointer type, the typeName should start with `*`.

func (*Env) GetEvalEnv

func (env *Env) GetEvalEnv() *EvalEnv

GetEvalEnv creates a new goroutine-local handle of env.

func (*Env) GetFunc

func (env *Env) GetFunc(pkgPath, funcName string) *Func

GetFunc finds previously bound function searching for the `$pkgPath.$funcName` symbol.

type EvalEnv

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

EvalEnv is a goroutine-local handle for Env. To get one, use Env.GetEvalEnv() method.

type Func

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

Func is a compiled function that is ready to be executed.

func Compile

func Compile(ctx *CompileContext, fn *ast.FuncDecl) (compiled *Func, err error)

Compile prepares an executable version of fn.

type ValueStack

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

ValueStack is used to manipulate runtime values during the evaluation. Function arguments are pushed to the stack. Function results are returned via stack as well.

For the sake of efficiency, it stores different types separately. If int was pushed with PushInt(), it should be retrieved by PopInt(). It's a bad idea to do a Push() and then PopInt() and vice-versa.

func (*ValueStack) Pop

func (s *ValueStack) Pop() interface{}

Pop removes the top stack element and returns it. Important: for int-typed values, use PopInt.

func (*ValueStack) PopInt

func (s *ValueStack) PopInt() int

PopInt removes the top stack element and returns it.

func (*ValueStack) Push

func (s *ValueStack) Push(x interface{})

Push adds x to the stack. Important: for int-typed values, use PushInt.

func (*ValueStack) PushInt

func (s *ValueStack) PushInt(x int)

PushInt adds x to the stack.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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