Documentation ¶
Overview ¶
Package quasigo implements a Go subset compiler and interpreter.
The implementation details are not part of the contract of this package.
Index ¶
- func Disasm(env *Env, fn *Func) string
- type CallResult
- type CompileContext
- type Env
- func (env *Env) AddFunc(pkgPath, funcName string, f *Func)
- func (env *Env) AddNativeFunc(pkgPath, funcName string, f func(*ValueStack))
- func (env *Env) AddNativeMethod(typeName, methodName string, f func(*ValueStack))
- func (env *Env) GetEvalEnv() *EvalEnv
- func (env *Env) GetFunc(pkgPath, funcName string) *Func
- type EvalEnv
- type Func
- type ValueStack
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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) CallResult
Call invokes a given function. All arguments should be pushed to env.Stack prior to this call.
Note that arguments are not popped off the stack, so you can bind the args once and use Call multiple times. If you want to reset arguments, do env.Stack.Reset().
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 Package *types.Package 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 (*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 ¶
GetEvalEnv creates a new goroutine-local handle of env.
type EvalEnv ¶
type EvalEnv struct { Stack ValueStack // 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.
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) PopVariadic ¶ added in v0.3.14
func (s *ValueStack) PopVariadic() []interface{}
PopVariadic removes the `...` argument and returns it as a slice.
Slice elements are in the order they were passed to the function, for example, a call Sprintf("%s:%d", filename, line) returns the slice []interface{filename, line}.
func (*ValueStack) Push ¶
func (s *ValueStack) Push(x interface{})
Push adds x to the stack. Important: for int-typed values, use PushInt.