exec

package
v0.0.0-...-5a4b8d9 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2019 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BreakForRange    = -1
	ContinueForRange = -2
)

Variables

View Source
var (
	// ErrStackDamaged is returned when stack is damaged.
	ErrStackDamaged = errors.New("unexpected: stack damaged")

	// ErrArityRequired is returned when calling `Call` without providing `arity`.
	ErrArityRequired = errors.New("arity required")

	// ErrArgumentsNotEnough is returned when calling a function without enough arguments.
	ErrArgumentsNotEnough = errors.New("arguments not enough")
)
View Source
var (
	// ErrNewWithoutClassName is returned when new doesn't specify a class.
	ErrNewWithoutClassName = errors.New("new object without class name")

	// ErrNewObjectWithNotType is returned when new T but T is not a type.
	ErrNewObjectWithNotType = errors.New("can't new object: not a type")

	// ErrRefWithoutObject is returned when refer member without specified an object.
	ErrRefWithoutObject = errors.New("reference without object")
)
View Source
var (
	// ErrAssignWithoutVal is returned when variable assign without value
	ErrAssignWithoutVal = errors.New("variable assign without value")

	// ErrMultiAssignExprMustBeSlice is returned when expression of multi assignment must be a slice
	ErrMultiAssignExprMustBeSlice = errors.New("expression of multi assignment must be a slice")
)
View Source
var (
	// ErrReturn is used for `panic(ErrReturn)`
	ErrReturn = errors.New("return")
)
View Source
var (
	// Exit is the instruction that executes `exit(<code>)`.
	Exit = Call(doExit)
)
View Source
var (
	OnPop func(v interface{})
)

Functions

func GetMemberVar

func GetMemberVar(m interface{}, key interface{}) interface{}

GetMemberVar implements get(object, key).

func SymbolIndex

func SymbolIndex(id, scope int) int

SymbolIndex returns symbol index value.

Types

type Class

type Class struct {
	Fns map[string]*Function
	Ctx *Context
}

A Class represents a qlang class.

func IClass

func IClass() *Class

IClass returns a Class instruction.

func (*Class) Exec

func (p *Class) Exec(stk *Stack, ctx *Context)

Exec is required by interface Instr.

func (*Class) GoType

func (p *Class) GoType() reflect.Type

GoType returns the underlying go type. required by `qlang type` spec.

func (*Class) New

func (p *Class) New(stk *Stack, args ...interface{}) *Object

New creates a new instance of this class.

func (*Class) NewInstance

func (p *Class) NewInstance(stk *Stack, args ...interface{}) interface{}

NewInstance creates a new instance of a qlang type. required by `qlang type` spec.

type Code

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

A Code represents generated instructions to execute.

func New

func New(data ...Instr) *Code

New returns a new Code object.

func (*Code) Block

func (p *Code) Block(code ...Instr) int

Block appends some instructions to code.

func (*Code) CheckConst

func (p *Code) CheckConst(ip int) (v interface{}, ok bool)

CheckConst returns the value, if code[ip] is a const instruction.

func (*Code) CodeLine

func (p *Code) CodeLine(file string, line int)

CodeLine informs current file and line.

func (*Code) Dump

func (p *Code) Dump(ranges ...int)

Dump dumps code instructions within a range.

func (*Code) Exec

func (p *Code) Exec(ip, ipEnd int, stk *Stack, ctx *Context)

Exec executes a code block from ip to ipEnd.

func (*Code) Len

func (p *Code) Len() int

Len returns code length.

func (*Code) Line

func (p *Code) Line(ip int) (file string, line int)

Line returns file line of a instruction position.

func (*Code) Reserve

func (p *Code) Reserve() ReservedInstr

Reserve reserves an instruction and returns it.

func (*Code) ToVar

func (p *Code) ToVar()

ToVar converts the last instruction from ref to var.

type Context

type Context struct {
	Stack *Stack
	Code  *Code

	Recov interface{}
	// contains filtered or unexported fields
}

A Context represents the context of an executor.

func NewContextEx

func NewContextEx(symtbl map[string]int) *Context

NewContextEx returns a new context of an executor.

func (*Context) CopyVars

func (p *Context) CopyVars() map[string]interface{}

CopyVars copies and returns all variables.

func (*Context) ExecBlock

func (p *Context) ExecBlock(ip, ipEnd int, symtbl map[string]int)

ExecBlock executes an anonym function.

func (*Context) ExecDefers

func (p *Context) ExecDefers()

ExecDefers executes defer blocks.

func (*Context) Exports

func (p *Context) Exports() map[string]interface{}

Exports returns a module exports.

func (*Context) FastGetVar

func (p *Context) FastGetVar(name int) interface{}

FastGetVar returns a variable value.

func (*Context) FastRefVar

func (p *Context) FastRefVar(name int) *interface{}

FastRefVar returns a variable address.

func (*Context) FastSetVar

func (p *Context) FastSetVar(name int, v interface{})

FastSetVar sets a variable value.

func (*Context) GetVar

func (p *Context) GetVar(name string) (v interface{}, ok bool)

GetVar returns a variable value.

func (*Context) ResetVars

func (p *Context) ResetVars(vars map[string]interface{})

ResetVars resets all variables.

func (*Context) ResizeVars

func (p *Context) ResizeVars()

ResizeVars is reserved for internal use.

func (*Context) SetVar

func (p *Context) SetVar(name string, v interface{})

SetVar sets a variable value.

func (*Context) UnsetVar

func (p *Context) UnsetVar(name string)

UnsetVar deletes a variable.

func (*Context) Var

func (p *Context) Var(name string) interface{}

Var returns a variable value.

func (*Context) Vars

func (p *Context) Vars() []interface{}

Vars is deprecated. please use `CopyVars` method.

type Error

type Error struct {
	Err   error
	File  string
	Line  int
	Stack []byte
}

A Error represents a qlang runtime error.

func (*Error) Error

func (p *Error) Error() string

type Function

type Function struct {
	Cls    *Class
	Parent *Context

	Args     []string
	Variadic bool
	// contains filtered or unexported fields
}

A Function represents a function object.

func NewFunction

func NewFunction(cls *Class, start, end int, symtbl map[string]int, args []string, variadic bool) *Function

NewFunction creates a new function object.

func (*Function) Call

func (p *Function) Call(stk *Stack, args ...interface{}) (ret interface{})

Call calls this function with default context.

func (*Function) ExtCall

func (p *Function) ExtCall(ctx *Context, args ...interface{}) (ret interface{})

ExtCall calls this function with a specified context.

type Instr

type Instr interface {
	Exec(stk *Stack, ctx *Context)
}

A Instr represents a instruction of the executor.

var (
	Nil Instr = Push(nil)
	Pop Instr = iPop(0)
)
var (
	ChanIn  Instr = iChanIn(0)
	ChanOut Instr = iChanOut(0)
)
var (
	AddAssignEx    Instr = iAddAssignEx(0)
	SubAssignEx    Instr = iSubAssignEx(0)
	MulAssignEx    Instr = iMulAssignEx(0)
	QuoAssignEx    Instr = iQuoAssignEx(0)
	ModAssignEx    Instr = iModAssignEx(0)
	XorAssignEx    Instr = iXorAssignEx(0)
	BitAndAssignEx Instr = iBitAndAssignEx(0)
	BitOrAssignEx  Instr = iBitOrAssignEx(0)
	AndNotAssignEx Instr = iAndNotAssignEx(0)
	LshrAssignEx   Instr = iLshrAssignEx(0)
	RshrAssignEx   Instr = iRshrAssignEx(0)
	IncEx          Instr = iIncEx(0)
	DecEx          Instr = iDecEx(0)
)
var AssignEx Instr = iAssignEx(0)

AssignEx is qlang AssignEx instruction.

var Chan Instr = tchan(0)

Chan is an instruction that returns chan T.

var (
	Clear Instr = iClear(0)
)
var (
	Default Instr = Pop
)
var Get Instr = iGet(0)

Get is the Get instruction.

var GetVar Instr = iGetVar(0)

GetVar is the Get instruction.

var Map Instr = tmap(0)

Map is an instruction that returns `map[key]elem`.

var (
	// Recover is the instruction that executes `recover()`.
	Recover Instr = iRecover(0)
)
var Slice Instr = slice(0)

Slice is an instruction that returns []T.

func And

func And(delta int) Instr

func AnonymFn

func AnonymFn(start, end int, symtbl map[string]int) Instr

AnonymFn returns an instruction that creates an anonymous function.

func As

func As(name int) Instr

As returns an instruction that specifies an alias name of a module.

func Call

func Call(fn interface{}, varity ...int) Instr

Call returns a function call instruction.

func CallFn

func CallFn(arity int) Instr

CallFn returns a function call instruction.

func CallFnv

func CallFnv(arity int) Instr

CallFnv returns a function call instruction.

func Case

func Case(delta int) Instr

func Defer

func Defer(start, end int) Instr

Defer returns an instruction that executes `defer <expr>`.

func Export

func Export(names ...string) Instr

Export returns an instruction that exports some module symbols.

func ForRange

func ForRange(args []int, start, end int) Instr

ForRange returns an instruction that represents for..range

func Func

func Func(cls *Class, start, end int, symtbl map[string]int, args []string, variadic bool) Instr

Func returns an instruction that create a function object.

func GfnRef

func GfnRef(v interface{}, toVar func() Instr) Instr

GfnRef returns an instruction that refers a fntable item.

func Go

func Go(start, end int) Instr

func INew

func INew(nArgs int) Instr

INew returns a New instruction.

func Jmp

func Jmp(delta int) Instr

func JmpIfFalse

func JmpIfFalse(delta int) Instr

func MapInit

func MapInit(arity int) Instr

MapInit returns a MapInit instruction that means `map[key]elem{key1: expr1, key2: expr2, ...}`.

func MemberRef

func MemberRef(name string) Instr

MemberRef returns a MemberRef instruction.

func MemberVar

func MemberVar(name string) Instr

MemberVar returns a MemberVar instruction.

func Module

func Module(id string, start, end int, symtbl map[string]int) Instr

Module returns an instruction that creates a new module.

func MultiAssignEx

func MultiAssignEx(arity int) Instr

MultiAssignEx returns a MultiAssignEx instruction.

func MultiAssignFromSliceEx

func MultiAssignFromSliceEx(arity int) Instr

MultiAssignFromSliceEx returns a MultiAssignFromSliceEx instruction.

func Op3

func Op3(op func(v, a, b interface{}) interface{}, hasA, hasB bool) Instr

func Or

func Or(delta int) Instr

func PopEx

func PopEx() Instr

func Push

func Push(v interface{}) Instr

func Ref

func Ref(name int) Instr

Ref returns an instruction that refers a variable.

func Rem

func Rem(file string, line int, code string) Instr

func Return

func Return(arity int) Instr

Return returns an instruction that means `return <expr1>, ..., <exprN>`.

func SliceFrom

func SliceFrom(arity int) Instr

SliceFrom is an instruction that creates slice in [a1, a2, ...] form.

func SliceFromTy

func SliceFromTy(arity int) Instr

SliceFromTy is an instruction that creates slice in []T{a1, a2, ...} form.

func StructInit

func StructInit(arity int) Instr

StructInit returns a StructInit instruction that means `&StructType{name1: expr1, name2: expr2, ...}`.

func Var

func Var(name int) Instr

Var returns a Var instruction.

type Method

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

A Method represents a method of an Object.

func (*Method) Call

func (p *Method) Call(stk *Stack, a ...interface{}) interface{}

Call calls this method with arguments.

type Object

type Object struct {
	Cls *Class
	// contains filtered or unexported fields
}

A Object represents a qlang object.

func (*Object) Member

func (p *Object) Member(name string) interface{}

Member returns a member of this object.

func (*Object) SetVar

func (p *Object) SetVar(name string, val interface{})

SetVar sets the value of a qlang object's member.

func (*Object) Vars

func (p *Object) Vars() map[string]interface{}

Vars returns all variables (not including methods) of this object.

type RefToVar

type RefToVar interface {
	ToVar() Instr
}

RefToVar converts a value reference instruction into a assignable variable instruction.

type ReservedInstr

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

A ReservedInstr represents a reserved instruction to be assigned.

func (ReservedInstr) Delta

func (p ReservedInstr) Delta(b ReservedInstr) int

Delta returns distance from b to p.

func (ReservedInstr) Next

func (p ReservedInstr) Next() int

Next returns next instruction position.

func (ReservedInstr) Set

func (p ReservedInstr) Set(code Instr)

Set sets a reserved instruction.

type Stack

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

A Stack represents a FILO container.

func NewStack

func NewStack() *Stack

NewStack returns a new Stack.

func (*Stack) BaseFrame

func (p *Stack) BaseFrame() int

BaseFrame returns current stack size.

func (*Stack) Pop

func (p *Stack) Pop() (v interface{}, ok bool)

Pop pops a value from this stack.

func (*Stack) PopArgs

func (p *Stack) PopArgs(arity int) (args []reflect.Value, ok bool)

PopArgs pops arguments of a function call.

func (*Stack) PopFnArgs

func (p *Stack) PopFnArgs(arity int) []string

PopFnArgs pops argument names of a function call.

func (*Stack) PopNArgs

func (p *Stack) PopNArgs(arity int) []interface{}

PopNArgs pops arguments of a function call.

func (*Stack) Push

func (p *Stack) Push(v interface{})

Push pushs a value into this stack.

func (*Stack) PushRet

func (p *Stack) PushRet(ret []reflect.Value) error

PushRet pushs a function call result.

func (*Stack) SetFrame

func (p *Stack) SetFrame(n int)

SetFrame sets stack to new size.

func (*Stack) Top

func (p *Stack) Top() (v interface{}, ok bool)

Top returns the last pushed value, if it exists.

Jump to

Keyboard shortcuts

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