quasigo

package module
v0.0.0-...-5e90b7b Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2023 License: MIT Imports: 7 Imported by: 0

README

quasigo

quasigo is a Go subset interpreter written in Go.

Rationale

I like working on compilers and interpreters. This is a good enough rationale for me to work on this project.

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.

func Call

func Call(env *EvalEnv, fn Func) CallResult

Call invokes a given function. Before calling this function, be sure to bind arguments to the env using BindArgs.

func (CallResult) BoolValue

func (res CallResult) BoolValue() bool

func (CallResult) IntValue

func (res CallResult) IntValue() int

func (CallResult) StringValue

func (res CallResult) StringValue() string

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

	Package *types.Package
	Types   *types.Info
	Sizes   types.Sizes
	Fset    *token.FileSet

	Optimize bool
	Static   bool

	TestingContext interface{}
}

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(qnative.CallContext))

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(qnative.CallContext))

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(stackSize int) *EvalEnv

GetEvalEnv creates a new goroutine-local handle of env. Stack size is amount of bytes we allocate for all stack frames of this 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.

func (*EvalEnv) BindArgs

func (env *EvalEnv) BindArgs(args ...interface{})

BindArgs prepares the arguments for the call. Bound args can be used many times if you don't need to change the call arguments.

If BindArgs+Call is not convenient for you, consider using the simple wrapper that does this combination for you. Note, however, that reusing bound arguments, whether possible, if more efficient.

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) (Func, error)

Compile prepares an executable version of fn.

func (Func) IsNil

func (fn Func) IsNil() bool

func (Func) ScalarConstants

func (fn Func) ScalarConstants() []uint64

TODO: remove this. Only needed for testing.

func (Func) StringConstants

func (fn Func) StringConstants() []string

TODO: remove this. Only needed for testing.

Directories

Path Synopsis
internal
ir
stdlib

Jump to

Keyboard shortcuts

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