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).

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(args ...interface{}) *Object

New creates a new instance of this class.

func (*Class) NewInstance

func (p *Class) NewInstance(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 NewContext

func NewContext() *Context

NewContext returns a new context of an executor.

func NewSimpleContext

func NewSimpleContext(vars map[string]interface{}, stk *Stack, code *Code, parent *Context) *Context

NewSimpleContext returns a new context of an executor, without module support.

func (*Context) CopyVars

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

CopyVars copies and returns all variables in executing context.

func (*Context) ExecBlock

func (p *Context) ExecBlock(ip, ipEnd 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) ResetVars

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

ResetVars resets all variables in executing context.

func (*Context) SetVar

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

SetVar sets a variable value.

func (*Context) Unset

func (p *Context) Unset(name string)

Unset deletes a variable.

func (*Context) Var

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

Var returns a variable value in executing context.

func (*Context) Vars

func (p *Context) Vars() map[string]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, args []string, variadic bool) *Function

NewFunction creates a new function object.

func (*Function) Call

func (p *Function) Call(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 AddAssign

func AddAssign(name string) Instr

AddAssign returns an instruction that means $name += $stk[top]

func And

func And(delta int) Instr

func AnonymFn

func AnonymFn(start, end int) Instr

func As

func As(name string) Instr

func Assign

func Assign(name string) Instr

Assign returns an instruction that means $name = $stk[top]

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 Dec

func Dec(name string) Instr

Dec returns an instruction that means $name--

func Defer

func Defer(start, end int) Instr

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

func Export

func Export(names ...string) Instr

func ForRange

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

func Func

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

Func returns an instruction that create a function object.

func Go

func Go(start, end int) Instr

func INew

func INew(nArgs int) Instr

INew returns a New instruction.

func Inc

func Inc(name string) Instr

Inc returns an instruction that means $name++

func Jmp

func Jmp(delta int) Instr

func JmpIfFalse

func JmpIfFalse(delta int) Instr

func Macro

func Macro(start, end 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 ModAssign

func ModAssign(name string) Instr

ModAssign returns an instruction that means $name %= $stk[top]

func Module

func Module(id string, start, end int) Instr

func MulAssign

func MulAssign(name string) Instr

MulAssign returns an instruction that means $name *= $stk[top]

func MultiAssign

func MultiAssign(names []string) Instr

MultiAssign returns an instruction that means $name1, $name2, ..., $nameN = $stk[top-N+1], ..., $stk[top]

func MultiAssignEx

func MultiAssignEx(arity int) Instr

MultiAssignEx returns a MultiAssignEx instruction.

func MultiAssignFromSlice

func MultiAssignFromSlice(names []string) Instr

MultiAssignFromSlice returns an instruction that means $name1, $name2, ..., $nameN = $stk[top]

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 OpAssign

func OpAssign(name string, op func(a, b interface{}) interface{}) Instr

OpAssign returns an instruction that means $name <op>= $stk[top]

func Or

func Or(delta int) Instr

func PopEx

func PopEx() Instr

func Push

func Push(v interface{}) Instr

func QuoAssign

func QuoAssign(name string) Instr

QuoAssign returns an instruction that means $name /= $stk[top]

func Ref

func Ref(name string) 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 SubAssign

func SubAssign(name string) Instr

SubAssign returns an instruction that means $name -= $stk[top]

func Unset

func Unset(name string) Instr

Unset returns an instruction that means unset(name)

func Var

func Var(name string) 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(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.