Documentation
¶
Index ¶
- Constants
- Variables
- func AddGlobalValue(k string, v interface{}, doc ...string)
- func NewCloser(v Value) io.Closer
- func NewReader(v Value) io.Reader
- func NewWriter(v Value) io.Writer
- func RemoveGlobalValue(k string)
- func Stringify(v interface{}) string
- func WebREPLHandler(opt *CompileOptions, cb func(*Program)) func(w http.ResponseWriter, r *http.Request)
- type CompileOptions
- type Env
- func (env *Env) B(index int) Value
- func (env *Env) Clear()
- func (env *Env) CopyStack() []Value
- func (env *Env) Get(index int) Value
- func (env *Env) Prepend(v Value)
- func (env *Env) Push(v Value)
- func (env *Env) PushVararg(v []Value)
- func (env *Env) Set(index int, value Value)
- func (env *Env) Size() int
- func (env *Env) Stack() []Value
- func (env *Env) String() string
- type ExecError
- type FuncBody
- type Function
- func (c *Function) Call(args ...Value) (v1 Value, err error)
- func (c *Function) CallVal(args ...interface{}) (v1 interface{}, err error)
- func (f *Function) Copy() *Function
- func (c *Function) EmergStop()
- func (c *Function) PrettyCode() string
- func (f *Function) Pure() *Function
- func (c *Function) String() string
- func (c *Function) Value() Value
- type Program
- type Table
- func (m *Table) ArrayLen() int
- func (m *Table) ArrayPart() []Value
- func (m *Table) Clear()
- func (m *Table) ClearArray()
- func (m *Table) ClearMap()
- func (m *Table) Contains(k Value) bool
- func (m *Table) Copy() *Table
- func (m *Table) Foreach(f func(k, v Value) bool)
- func (m *Table) Get(k Value) (v Value)
- func (m *Table) GetString(k string) (v Value)
- func (m *Table) Len() int
- func (m *Table) MapLen() int
- func (m *Table) MapPart() map[Value]Value
- func (m *Table) Merge(src *Table, kvs ...Value) *Table
- func (m *Table) Name() string
- func (m *Table) New() *Table
- func (m *Table) Next(k Value) (Value, Value)
- func (m *Table) Parent() *Table
- func (m *Table) RawGet(k Value) (v Value)
- func (m *Table) RawSet(k, v Value) (prev Value)
- func (m *Table) Set(k, v Value) (prev Value)
- func (m *Table) SetFirstParent(m2 *Table)
- func (m *Table) SetParent(m2 *Table)
- func (m *Table) SetString(k string, v Value) (prev Value)
- func (m *Table) Size() int
- func (m *Table) String() string
- func (m *Table) Value() Value
- type Value
- func Array(m ...Value) Value
- func Bool(v bool) Value
- func Byte(s byte) Value
- func Bytes(b []byte) Value
- func Float(f float64) Value
- func Func(name string, f func(env *Env), doc ...string) Value
- func Func1(name string, f func(Value) Value, doc ...string) Value
- func Func2(name string, f func(Value, Value) Value, doc ...string) Value
- func Func3(name string, f func(Value, Value, Value) Value, doc ...string) Value
- func Int(i int64) Value
- func Map(kvs ...Value) Value
- func MustRun(p *Program, err error) Value
- func Run(p *Program, err error) (Value, error)
- func Rune(r rune) Value
- func Str(s string) Value
- func TableMerge(dst Value, src Value, kvs ...Value) Value
- func TableProto(p *Table, kvs ...Value) Value
- func Val(i interface{}) Value
- func ValRec(v interface{}) Value
- func (v Value) Bool() bool
- func (v Value) Equal(r Value) bool
- func (v Value) Float() float64
- func (v Value) Func() *Function
- func (v Value) HashCode() uint64
- func (v Value) Int() int64
- func (v Value) Interface() interface{}
- func (v Value) IsFalse() bool
- func (v Value) IsInt() bool
- func (v Value) IsValue(parser.Node)
- func (v Value) JSONString() string
- func (v Value) MarshalJSON() ([]byte, error)
- func (v Value) MaybeFloat(d float64) float64
- func (v Value) MaybeInt(d int64) int64
- func (v Value) MaybeStr(d string) string
- func (v Value) MaybeTableGetString(key string) Value
- func (v Value) MustBool(msg string) bool
- func (v Value) MustFloat(msg string) float64
- func (v Value) MustFunc(msg string) *Function
- func (v Value) MustInt(msg string) int64
- func (v Value) MustNum(msg string) Value
- func (v Value) MustStr(msg string) string
- func (v Value) MustTable(msg string) *Table
- func (v Value) ReflectValue(t reflect.Type) reflect.Value
- func (v Value) Str() string
- func (v Value) StrLen() int
- func (v Value) String() string
- func (v Value) Table() *Table
- func (v Value) Type() typ.ValueType
- type ValueIO
Constants ¶
const (
ValueSize = unsafe.Sizeof(Value{})
)
const Version int64 = 327
Variables ¶
var ( ReaderProto = Map(Str("__name"), Str("reader"), Str("read"), Func2("read", func(rx, n Value) Value { f := rx.Table().GetString("_f").Interface().(io.Reader) switch n.Type() { case typ.Number: p := make([]byte, n.MaybeInt(0)) rn, err := f.Read(p) if err == nil || rn > 0 { return Bytes(p[:rn]) } if err == io.EOF { return Nil } panic(err) default: buf, err := ioutil.ReadAll(f) panicErr(err) return Bytes(buf) } }, "read() string", "\tread all bytes, return nil if EOF reached", "read(n: int) string", "\tread n bytes"), Str("readbuf"), Func2("readbuf", func(rx, n Value) Value { rn, err := rx.Table().GetString("_f").Interface().(io.Reader).Read(n.Interface().([]byte)) return Array(Int(int64(rn)), Val(err)) }, "$f(buf: bytes) array", "\tread into buf and return { bytes_read, error } in Go style"), Str("readlines"), Func2("readlines", func(rx, cb Value) Value { f := rx.Table().GetString("_f").Interface().(io.Reader) delim := rx.Table().GetString("delim").MaybeStr("\n") if cb == Nil { buf, err := ioutil.ReadAll(f) if err != nil { panic(err) } parts := bytes.Split(buf, []byte(delim)) var res []Value for i, line := range parts { if i < len(parts)-1 { line = append(line, delim...) } res = append(res, Bytes(line)) } return Array(res...) } for rd := bufio.NewReader(f); ; { line, err := rd.ReadString(delim[0]) if len(line) > 0 { if v, err := cb.MustFunc("callback").Call(Str(line)); err != nil { panic(err) } else if v != Nil { return v } } if err != nil { if err != io.EOF { panic(err) } break } } return Nil }, "readlines() array", "\tread the whole file and return lines as a table array", "readlines(f: function)", "\tfor every line read, f(line) will be called", "\tto exit the reading, return anything other than nil in f", ), ).Table() WriterProto = Map(Str("__name"), Str("writer"), Str("write"), Func2("write", func(rx, buf Value) Value { f := rx.Table().GetString("_f").Interface().(io.Writer) wn, err := f.Write([]byte(buf.MustStr(""))) panicErr(err) return Int(int64(wn)) }, "$f({w}: value, buf: string) int", "\twrite buf to w"), Str("pipe"), Func3("pipe", func(dest, src, n Value) Value { var wn int64 var err error if n := n.MaybeInt(0); n > 0 { wn, err = io.CopyN(NewWriter(dest), NewReader(src), n) } else { wn, err = io.Copy(NewWriter(dest), NewReader(src)) } panicErr(err) return Int(wn) }, "$f({w}: value, r: value) int", "\tcopy bytes from r to w, return number of bytes copied", "$f({w}: value, r: value, n: int) int", "\tcopy at most n bytes from r to w"), ).Table() SeekerProto = Map(Str("__name"), Str("seeker"), Str("seek"), Func3("seek", func(rx, off, where Value) Value { f := rx.Table().GetString("_f").Interface().(io.Seeker) wn, err := f.Seek(off.MustInt("offset"), int(where.MustInt("where"))) panicErr(err) return Int(int64(wn)) }, "")).Table() CloserProto = Map(Str("__name"), Str("closer"), Str("close"), Func1("close", func(rx Value) Value { panicErr(rx.Table().GetString("_f").Interface().(io.Closer).Close()) return Nil }, "")).Table() ReadWriterProto = ReaderProto.Copy().Merge(WriterProto, Str("__name"), Str("readwriter")) ReadCloserProto = ReaderProto.Copy().Merge(CloserProto, Str("__name"), Str("readcloser")) WriteCloserProto = WriterProto.Copy().Merge(CloserProto, Str("__name"), Str("writecloser")) ReadWriteCloserProto = ReadWriterProto.Copy().Merge(CloserProto, Str("__name"), Str("readwritecloser")) ReadWriteSeekCloserProto = ReadWriteCloserProto.Copy().Merge(SeekerProto, Str("__name"), Str("readwriteseekcloser")) )
Functions ¶
func AddGlobalValue ¶
func RemoveGlobalValue ¶
func RemoveGlobalValue(k string)
func WebREPLHandler ¶
func WebREPLHandler(opt *CompileOptions, cb func(*Program)) func(w http.ResponseWriter, r *http.Request)
Types ¶
type CompileOptions ¶
type CompileOptions struct {
GlobalKeyValues map[string]interface{}
}
type Env ¶
type Env struct { Global *Program A Value // Debug info for native functions to read IP uint32 CS *Function Stacktrace []stacktrace // contains filtered or unexported fields }
Env is the environment for a function to run within. stack contains arguments used by the execution and is a global shared value, local can only use stack[stackOffset:] A stores the result of the execution
func (*Env) PushVararg ¶
type ExecError ¶
type ExecError struct {
// contains filtered or unexported fields
}
ExecError represents the runtime error
func (*ExecError) GetRootPanic ¶
func (e *ExecError) GetRootPanic() interface{}
type Function ¶
func (*Function) EmergStop ¶
func (c *Function) EmergStop()
EmergStop terminates the execution of Func After calling, Func will become unavailable for any further operations
func (*Function) PrettyCode ¶
type Program ¶
type Program struct { Top *Function Symbols map[string]*symbol MaxStackSize int64 Stack *[]Value Functions []*Function Stdout io.Writer Stderr io.Writer Stdin io.Reader }
func LoadString ¶
func LoadString(code string, opt *CompileOptions) (*Program, error)
func (*Program) EmergStop ¶
func (p *Program) EmergStop()
EmergStop terminates the execution of program After calling, program will become unavailable for any further operations
func (*Program) PrettyCode ¶
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
func (*Table) Clear ¶
func (m *Table) Clear()
Clear clears the table, where already allocated memory will be reused.
func (*Table) ClearArray ¶
func (m *Table) ClearArray()
func (*Table) Set ¶
Set inserts or updates a key/val pair into the Map. If val == Nil, then key will get deleted
func (*Table) SetFirstParent ¶
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is the basic data type used by the intepreter, an empty Value naturally represent nil
func Map ¶
Map creates a map from `kvs`, which should be laid out as: key1, value1, key2, value2, ...
func TableMerge ¶
TableMerge merges key-value pairs from `src` and `kvs` into `dst`
func TableProto ¶
TableProto creates a table whose parent will be set to `p`
func Val ¶
func Val(i interface{}) Value
Val creates a `Value` from golang `interface{}` `slice`, `array` and `map` will be left as is (except []Value), to convert them recursively, use ValRec instead
func (Value) Interface ¶
func (v Value) Interface() interface{}
Interface returns value as an interface{}
func (Value) JSONString ¶
func (Value) MarshalJSON ¶
func (Value) MaybeFloat ¶
func (Value) MaybeTableGetString ¶
func (Value) ReflectValue ¶
ReflectValue returns value as a reflect.Value based on reflect.Type