common

package
v0.0.0-...-84a1f60 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

borrowed from bytebufferpool

Index

Constants

View Source
const (
	UInt8 = iota
	UInt32
	UInt64
	Int32
	Int64
	Float32
	Float64
	Bool
	Bytes
	String
	Slice
	Map
	Message
	Channel
	Nil
	Enum
	Func
	ReflectType
	Any
	Illegal
)
View Source
const (
	UInt8Type   = "uint8"
	UInt32Type  = "uint32"
	UInt64Type  = "uint64"
	Int32Type   = "int32"
	Int64Type   = "int64"
	Float32Type = "float32"
	Float64Type = "float64"
	BoolType    = "bool"
	BytesType   = "bytes"
	StringType  = "string"
	SliceType   = "slice"
	MapType     = "map"
	MessageType = "message"
	AnyType     = "any"
)

Variables

View Source
var BasicTypeMap = map[string]*DataType{
	"uint8": {
		Type: "uint8",
		Kind: KindMap[UInt8],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadInt32()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[UInt8],
	},
	"int32": {
		Type: "int32",
		Kind: KindMap[Int32],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadInt32()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[Int32],
	},
	"int64": {
		Type: "int64",
		Kind: KindMap[Int64],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadInt64()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[Int64],
	},
	"uint32": {
		Type: "uint32",
		Kind: KindMap[UInt32],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadUint32()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[UInt32],
	},
	"uint64": {
		Type: "uint64",
		Kind: KindMap[UInt64],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadUint32()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[UInt64],
	},
	"float32": {
		Type: "float32",
		Kind: KindMap[Float32],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadFloat32()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[Float32],
	},
	"float64": {
		Type: "float64",
		Kind: KindMap[Float64],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadFloat64()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[Float64],
	},
	"string": {
		Type: "string",
		Kind: KindMap[String],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadString()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[String],
	},
	"bytes": {
		Type: "bytes",
		Kind: KindMap[Bytes],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return []byte(iter.ReadString())
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[Bytes],
	},
	"bool": {
		Type: "bool",
		Kind: KindMap[Bool],
		Unmarshal: func(iter *jsoniter.Iterator) interface{} {
			return iter.ReadBool()
		},
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[Bool],
	},
	"nil": {
		Type: "nil",
		Kind: KindMap[Nil],
	},
	"reflect": {
		Type:   "reflect",
		Method: map[string]*FunctionMeta{},
		Kind:   KindMap[ReflectType],
	},
	"any": {
		Type:        "any",
		Kind:        KindMap[Any],
		Method:      map[string]*FunctionMeta{},
		Constructor: ConstructorMap[Any],
	},
	"illegal": {
		Type: "illegal",
		Kind: KindMap[Illegal],
	},
}
View Source
var ConstructorMap = map[int]Constructor{
	UInt8:   NewUInt8Value,
	Int32:   NewInt32Value,
	Int64:   NewInt64Value,
	UInt32:  NewUInt32Value,
	UInt64:  NewUInt64Value,
	Float32: NewFloat32Value,
	Float64: NewFloat64Value,
	Bool:    NewBoolValue,
	String:  NewStringValue,
	Bytes:   NewBytesValue,
	Nil:     NewNilValue,
	Message: NewMessageValue,
	Slice:   NewSliceValue,
	Channel: NewChannelValue,
	Any:     NewAnyValue,
}
View Source
var KindMap = map[int]*DataKind{
	UInt8: {
		Kind: UInt8,
	},
	Int32: {
		Kind: Int32,
	},
	Int64: {
		Kind: Int64,
	},
	UInt32: {
		Kind: UInt32,
	},
	UInt64: {
		Kind: UInt64,
	},
	Float32: {
		Kind: Float32,
	},
	Float64: {
		Kind: Float64,
	},
	Bool: {
		Kind: Bool,
	},
	String: {
		Kind: String,
	},
	Bytes: {
		Kind: Bytes,
	},
	Slice: {
		Kind: Slice,
	},
	Map: {
		Kind: Map,
	},
	Message: {
		Kind: Message,
	},
	Channel: {
		Kind: Channel,
	},
	Nil: {
		Kind: Nil,
	},
	Enum: {
		Kind: Enum,
	},
	Func: {
		Kind: Func,
	},
	ReflectType: {
		Kind: ReflectType,
	},
	Any: {
		Kind: Any,
	},
	Illegal: {
		Kind: Illegal,
	},
}
View Source
var MapConstructorMap = map[int]Constructor{
	Int32:   NewMapInt32Value,
	Int64:   NewMapInt64Value,
	UInt32:  NewMapUInt32Value,
	UInt64:  NewMapUInt64Value,
	Float32: NewMapFloat32Value,
	Float64: NewMapFloat64Value,
	Bool:    NewMapBoolValue,
	String:  NewMapStringValue,
}
View Source
var RegisteredLibs = map[string]FunctionLib{}

Functions

func EvalConstInstructions

func EvalConstInstructions(instructions []Instruction) interface{}

func FormatError

func FormatError(ctx antlr.ParserRuleContext, message string) string

func IsBuiltIn

func IsBuiltIn(name string) bool

func IsError

func IsError(i Instruction) bool

func IsFloat

func IsFloat(d *DataType) bool

func IsInteger

func IsInteger(d *DataType) bool

func IsNumber

func IsNumber(d *DataType) bool

func NewAnyValue

func NewAnyValue(params ...interface{}) interface{}

func NewBoolValue

func NewBoolValue(params ...interface{}) interface{}

func NewBytesValue

func NewBytesValue(params ...interface{}) interface{}

func NewChannelValue

func NewChannelValue(params ...interface{}) interface{}

func NewFloat32Value

func NewFloat32Value(params ...interface{}) interface{}

func NewFloat64Value

func NewFloat64Value(params ...interface{}) interface{}

func NewInt32Value

func NewInt32Value(params ...interface{}) interface{}

func NewInt64Value

func NewInt64Value(params ...interface{}) interface{}

func NewMapBoolValue

func NewMapBoolValue(params ...interface{}) interface{}

func NewMapFloat32Value

func NewMapFloat32Value(params ...interface{}) interface{}

func NewMapFloat64Value

func NewMapFloat64Value(params ...interface{}) interface{}

func NewMapInt32Value

func NewMapInt32Value(params ...interface{}) interface{}

func NewMapInt64Value

func NewMapInt64Value(params ...interface{}) interface{}

func NewMapStringValue

func NewMapStringValue(params ...interface{}) interface{}

func NewMapUInt32Value

func NewMapUInt32Value(params ...interface{}) interface{}

func NewMapUInt64Value

func NewMapUInt64Value(params ...interface{}) interface{}

func NewMapValue

func NewMapValue(params ...interface{}) interface{}

func NewMessageValue

func NewMessageValue(params ...interface{}) interface{}

func NewNilValue

func NewNilValue(params ...interface{}) interface{}

func NewSliceValue

func NewSliceValue(params ...interface{}) interface{}

func NewStringValue

func NewStringValue(params ...interface{}) interface{}

func NewUInt32Value

func NewUInt32Value(params ...interface{}) interface{}

func NewUInt64Value

func NewUInt64Value(params ...interface{}) interface{}

func NewUInt8Value

func NewUInt8Value(params ...interface{}) interface{}

constructor

func Put

func Put(b *Stack)

Put returns byte buffer to the pool.

ByteBuffer.B mustn't be touched after returning it to the pool. Otherwise data races will occur.

func RegisterLib

func RegisterLib(name string, lib FunctionLib)

Types

type CompileErr

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

func (*CompileErr) Error

func (t *CompileErr) Error() string

func (*CompileErr) Type

func (t *CompileErr) Type() ScriptErrorType

type Constant

type Constant struct {
	Symbol string
	Type   *DataType
	Data   interface{}
}

type Constructor

type Constructor func(...interface{}) interface{}

type DataKind

type DataKind struct {
	// maybe this struct will be useful, so I just keep it here
	Kind int // int/float/message/slice/map/...
}

type DataType

type DataType struct {
	Type       string
	Kind       *DataKind
	FieldType  map[string]*DataType // for complex
	OneOf      map[string]string
	OneOfGroup map[string][]string
	ItemType   *DataType // for sequence item type
	KeyType    *DataType // map key
	ValueType  *DataType // map val
	Method     map[string]*FunctionMeta

	LambdaMeta  *FunctionMeta // for closure
	Unmarshal   func(iterator *jsoniter.Iterator) interface{}
	Constructor Constructor
}

func CanAddSubMulPow

func CanAddSubMulPow(d *DataType, b *DataType) *DataType

func CanAndOr

func CanAndOr(d *DataType, b *DataType) *DataType

func CanCompare

func CanCompare(d *DataType, b *DataType) *DataType

func CanDiv

func CanDiv(d *DataType, b *DataType) *DataType

func CanEqual

func CanEqual(d *DataType, b *DataType) *DataType

func CanMod

func CanMod(d *DataType, b *DataType) *DataType

func (*DataType) CanConvertTo

func (d *DataType) CanConvertTo(b *DataType) bool

func (*DataType) CanNegate

func (d *DataType) CanNegate() bool

type DataTypeBuilder

type DataTypeBuilder struct {
	T *DataType
}

func NewDataTypeBuilder

func NewDataTypeBuilder(name string) *DataTypeBuilder

func (*DataTypeBuilder) SetField

func (d *DataTypeBuilder) SetField(fieldName string, fieldType *DataType)

func (*DataTypeBuilder) SetItem

func (d *DataTypeBuilder) SetItem(itemType *DataType)

func (*DataTypeBuilder) SetKey

func (d *DataTypeBuilder) SetKey(keyType *DataType)

func (*DataTypeBuilder) SetKind

func (d *DataTypeBuilder) SetKind(k int)

func (*DataTypeBuilder) SetOneOf

func (d *DataTypeBuilder) SetOneOf(oneOfName string, fieldName string)

func (*DataTypeBuilder) SetValue

func (d *DataTypeBuilder) SetValue(valType *DataType)

type Function

type Function struct {
	Type *DataType
	F    *Instruction
}

type FunctionLib

type FunctionLib interface {
	Init(tr *TypeRegistry) map[string]*Function
}

type FunctionMeta

type FunctionMeta struct {
	In        []*DataType
	Out       []*DataType
	TailArray bool
	ConstExpr bool // for future optimization
}

func (*FunctionMeta) GenerateTypeName

func (f *FunctionMeta) GenerateTypeName() string

type IndexErr

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

func (*IndexErr) Error

func (t *IndexErr) Error() string

func (*IndexErr) Type

func (t *IndexErr) Type() ScriptErrorType

type InitializationErr

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

func (*InitializationErr) Error

func (t *InitializationErr) Error() string

func (*InitializationErr) Type

type Instruction

type Instruction func(m *Memory, stk *Stack)
var ErrorInstruction Instruction = func(m *Memory, stk *Stack) {}

type MathErr

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

func (*MathErr) Error

func (t *MathErr) Error() string

func (*MathErr) Type

func (t *MathErr) Type() ScriptErrorType

type Memory

type Memory struct {
	Data []interface{}
}

func (*Memory) Copy

func (m *Memory) Copy() *Memory

func (*Memory) Get

func (m *Memory) Get(v *Symbol) interface{}

func (*Memory) MustGet

func (m *Memory) MustGet(v *Symbol) *interface{}

func (*Memory) Set

func (m *Memory) Set(v *Symbol, data interface{})

type MismatchErr

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

func (*MismatchErr) Error

func (t *MismatchErr) Error() string

func (*MismatchErr) Type

func (t *MismatchErr) Type() ScriptErrorType

type Pool

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

Pool represents byte buffer pool.

Distinct pools may be used for distinct types of byte buffers. Properly determined byte buffer types with their own pools may help reducing memory waste.

func (*Pool) Get

func (p *Pool) Get() *Stack

Get returns new byte buffer with zero length.

The byte buffer may be returned to the pool via Put after the use in order to minimize GC overhead.

func (*Pool) Put

func (p *Pool) Put(b *Stack)

Put releases byte buffer obtained via Get to the pool.

The buffer mustn't be accessed after returning to the pool.

type Scope

type Scope struct {
	Outer       *Scope
	VarIndex    *int
	LocalIndex  int
	ReturnIndex int
	Variables   map[string]*Symbol
	Parameters  map[string]*Symbol
	Constants   map[string]*Symbol

	EnableCapture bool
	CaptureOffset int
	Capture       []*Symbol
	CapturedSet   map[string]*Symbol
}

func NewScope

func NewScope(outer *Scope) *Scope

func (*Scope) AddConstant

func (s *Scope) AddConstant(name string, constVal *Symbol)

func (*Scope) AddGlobalVariable

func (s *Scope) AddGlobalVariable(v *Symbol)

func (*Scope) AddLocalVariable

func (s *Scope) AddLocalVariable(v *Symbol)

func (*Scope) AddReturnVariable

func (s *Scope) AddReturnVariable(v *Symbol)

func (*Scope) GetSymbol

func (s *Scope) GetSymbol(name string) *Symbol

func (*Scope) SetCaptureMode

func (s *Scope) SetCaptureMode()

type ScriptError

type ScriptError interface {
	Error() string
	Type() ScriptErrorType
}

func NewCompileErr

func NewCompileErr(message string) ScriptError

func NewIndexErr

func NewIndexErr(message string) ScriptError

func NewInitializationErr

func NewInitializationErr(message string) ScriptError

func NewMathErr

func NewMathErr(message string) ScriptError

func NewMismatchErr

func NewMismatchErr(message string) ScriptError

func NewSymbolErr

func NewSymbolErr(message string) ScriptError

func NewTypeErr

func NewTypeErr(message string) ScriptError

func NewTypeNotFoundErr

func NewTypeNotFoundErr(message string) ScriptError

type ScriptErrorType

type ScriptErrorType int
const (
	TypeNotFoundError ScriptErrorType = iota
	SymbolError
	IndexError
	MismatchError
	TypeError
	MathError
	InitializationError
	CompileError
)

type Stack

type Stack struct {
	Bp   int
	Sp   int
	Pc   int
	Data []interface{}
}

func Get

func Get() *Stack

Get returns an empty byte buffer from the pool.

Got byte buffer may be returned to the pool via Put call. This reduces the number of memory allocations required for byte buffer management.

func (*Stack) Call

func (s *Stack) Call(inst Instruction, mem *Memory, stk *Stack, params int)

func (*Stack) Get

func (s *Stack) Get(v *Symbol) interface{}

func (*Stack) MustGet

func (s *Stack) MustGet(v *Symbol) *interface{}

func (*Stack) Pop

func (s *Stack) Pop()

func (*Stack) PopN

func (s *Stack) PopN(i int)

func (*Stack) Push

func (s *Stack) Push(i interface{})

func (*Stack) Reset

func (s *Stack) Reset() *Stack

func (*Stack) ReturnValue

func (s *Stack) ReturnValue(val interface{})

func (*Stack) ReturnValues

func (s *Stack) ReturnValues(values ...interface{})

func (*Stack) Set

func (s *Stack) Set(spOffset int, i interface{})

func (*Stack) Top

func (s *Stack) Top() interface{}

func (*Stack) TopIndex

func (s *Stack) TopIndex(i int) interface{}

type Symbol

type Symbol struct {
	Offset     int // for slice mem
	Symbol     string
	SymbolType SymbolType
	DataType   *DataType
	Scope      *Scope
	Data       interface{}
}

type SymbolErr

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

func (*SymbolErr) Error

func (t *SymbolErr) Error() string

func (*SymbolErr) Type

func (t *SymbolErr) Type() ScriptErrorType

type SymbolType

type SymbolType int
const (
	Global SymbolType = iota
	Const
	Local
	Captured
)

type TypeErr

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

func (*TypeErr) Error

func (t *TypeErr) Error() string

func (*TypeErr) Type

func (t *TypeErr) Type() ScriptErrorType

type TypeNotFoundErr

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

func (*TypeNotFoundErr) Error

func (t *TypeNotFoundErr) Error() string

func (*TypeNotFoundErr) Type

func (t *TypeNotFoundErr) Type() ScriptErrorType

type TypeRegistry

type TypeRegistry struct {
	Types        map[string]*DataType
	Enums        map[string]map[string]int32
	REnums       map[string]map[int32]string
	TypesInBuild map[string]*DataType
}

func NewTypeRegistry

func NewTypeRegistry() *TypeRegistry

func (*TypeRegistry) AddBuiltType

func (t *TypeRegistry) AddBuiltType(dtb *DataTypeBuilder)

func (*TypeRegistry) AddEnum

func (t *TypeRegistry) AddEnum(name string, e map[string]int32)

func (*TypeRegistry) AddType

func (t *TypeRegistry) AddType(name string, dType *DataType)

not suggested to use, use DataTypeBuilder instead

func (*TypeRegistry) AddTypePlaceHolderInBuild

func (t *TypeRegistry) AddTypePlaceHolderInBuild(name string) *DataType

func (*TypeRegistry) FindChanType

func (t *TypeRegistry) FindChanType(name string) *DataType

func (*TypeRegistry) FindFuncType

func (t *TypeRegistry) FindFuncType(meta *FunctionMeta) *DataType

func (*TypeRegistry) FindMapType

func (t *TypeRegistry) FindMapType(key, val string) *DataType

func (*TypeRegistry) FindSliceType

func (t *TypeRegistry) FindSliceType(name string) *DataType

func (*TypeRegistry) FindType

func (t *TypeRegistry) FindType(name string) *DataType

func (*TypeRegistry) GetEnums

func (t *TypeRegistry) GetEnums(name string) interface{}

func (*TypeRegistry) GetREnums

func (t *TypeRegistry) GetREnums(name string) map[int32]string

func (*TypeRegistry) MakeChanType

func (t *TypeRegistry) MakeChanType(name string) *DataType

func (*TypeRegistry) MakeFuncType

func (t *TypeRegistry) MakeFuncType(meta *FunctionMeta) *DataType

func (*TypeRegistry) MakeMapType

func (t *TypeRegistry) MakeMapType(key, val string) *DataType

func (*TypeRegistry) MakeSliceType

func (t *TypeRegistry) MakeSliceType(name string) *DataType

Jump to

Keyboard shortcuts

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