core

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2018 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ConstDepth is the name of the max depth of calling functions
	ConstDepth = `DEPTH`
	// ConstCycle is the name of the max count of cycle
	ConstCycle = `CYCLE`
	// ConstIota is the name of iota for constants
	ConstIota = `IOTA`
	// ConstVersion is the version of Gentee compiler
	ConstVersion = `VERSION`

	// NotIota means that constant doesn't use IOTA
	NotIota = -1

	// Version is the current version of the compiler
	Version = `1.0.0-beta.1`
)
View Source
const (
	// RcBreak means break command
	RcBreak = 1 + iota
	// RcContinue means continue command
	RcContinue
)
View Source
const (
	// StackBlock executes function
	StackBlock = 1 + iota
	// StackReturn returns values from the function
	StackReturn
	// StackIf is the condition statement
	StackIf
	// StackWhile is the while statement
	StackWhile
	// StackAssign is an assign operator
	StackAssign
	// StackAnd is a logical AND
	StackAnd
	// StackOr is a logical OR
	StackOr
	// StackQuestion is ?(condition, exp1, exp2)
	StackQuestion
	// StackIncDec is ++ --
	StackIncDec
	// StackFor is the for statement
	StackFor
	// StackInit inits array and map variables
	StackInit
	// StackNew creates a new array or map
	StackNew
)
View Source
const (
	// DefAssignIntInt equals int = int
	DefAssignIntInt = `AssignºIntInt`
	// DefAssignStructStruct equals struct = struct
	DefAssignStructStruct = `AssignºStructStruct`
	// DefNewKeyValue returns a pair of key value
	DefNewKeyValue = `NewKeyValue`
)
View Source
const (

	// ErrRunIndex is returned when Run has been executed with wrong index
	ErrRunIndex = iota + 1
	// ErrDepth is returned when maximum depth of recursion has been reached
	ErrDepth
	// ErrDivZero is returned when there is division by zero
	ErrDivZero
	// ErrCycle is returned when maximum cycle count has been reached
	ErrCycle
	// ErrShift is returned when << or >> are used with the negative right operand
	ErrShift
	// ErrStrToInt is returned when the conversion string to integer is invalid
	ErrStrToInt
	// ErrStrToFloat is returned when the conversion string to float is invalid
	ErrStrToFloat
	// ErrEmptyCommand is returned if empty command is specified in $
	ErrEmptyCommand
	// ErrQuoteCommand is returned if there is an unclosed quotation mark in $ command
	ErrQuoteCommand
	// ErrIndexOut means that int index is out of the length of the array
	ErrIndexOut
	// ErrMapIndex is returned when there is not the key in the map
	ErrMapIndex
	// ErrAssignment is returned when there is a recursive assignment
	ErrAssignment
	// ErrUndefined means that the value of the variable is undefined
	ErrUndefined
	// ErrByteOut is returned when value for buf is greater 255
	ErrByteOut
	// ErrInvalidParam is returned when the function gets invalid parameter(s)
	ErrInvalidParam

	// ErrRuntime error. It means bug
	ErrRuntime
)
View Source
const (
	// DefName is the key name for stdlib
	DefName = `stdlib`
)
View Source
const (
	// Undefined index
	Undefined = -1
)

Variables

This section is empty.

Functions

func CopyVar

func CopyVar(ptr *interface{}, value interface{})

CopyVar copies one object to another one

func ErrorText

func ErrorText(id int) string

ErrorText returns the text of the error message

Types

type Array

type Array struct {
	Data []interface{}
}

Array is an array

func NewArray

func NewArray() *Array

NewArray creates a new array object

func (*Array) Len

func (arr *Array) Len() int

Len is part of sort.Interface.

func (*Array) Less

func (arr *Array) Less(i, j int) bool

Less is part of sort.Interface.

func (Array) String

func (arr Array) String() string

String interface for Array

func (*Array) Swap

func (arr *Array) Swap(i, j int)

Swap is part of sort.Interface.

type Buffer

type Buffer struct {
	Data []byte
}

Buffer is []byte

func NewBuffer

func NewBuffer() *Buffer

NewBuffer creates a new buffer object

func (Buffer) String

func (buf Buffer) String() string

String interface for Buffer

type CmdAnyFunc

type CmdAnyFunc struct {
	CmdCommon
	Object   IObject
	Result   *TypeObject
	Children []ICmd
}

CmdAnyFunc calls a function with more than 2 parameters

func (*CmdAnyFunc) GetObject

func (cmd *CmdAnyFunc) GetObject() IObject

GetObject returns Object

func (*CmdAnyFunc) GetResult

func (cmd *CmdAnyFunc) GetResult() *TypeObject

GetResult returns the type of the result

func (*CmdAnyFunc) GetToken

func (cmd *CmdAnyFunc) GetToken() int

GetToken returns teh index of the token

func (*CmdAnyFunc) GetType

func (cmd *CmdAnyFunc) GetType() CmdType

GetType returns CtFunc

type CmdBinary

type CmdBinary struct {
	CmdCommon
	Object IObject
	Result *TypeObject
	Left   ICmd
	Right  ICmd
}

CmdBinary calls a binary function

func (*CmdBinary) GetObject

func (cmd *CmdBinary) GetObject() IObject

GetObject returns Object

func (*CmdBinary) GetResult

func (cmd *CmdBinary) GetResult() *TypeObject

GetResult returns the type of the result

func (*CmdBinary) GetToken

func (cmd *CmdBinary) GetToken() int

GetToken returns teh index of the token

func (*CmdBinary) GetType

func (cmd *CmdBinary) GetType() CmdType

GetType returns CtBinary

type CmdBlock

type CmdBlock struct {
	CmdCommon
	Parent   *CmdBlock
	Object   IObject
	ID       uint32 // cmdType
	Vars     []*TypeObject
	ParCount int // the count of parameters
	VarNames map[string]int
	Result   *TypeObject
	Children []ICmd
}

CmdBlock calls a stack command

func (*CmdBlock) GetObject

func (cmd *CmdBlock) GetObject() IObject

GetObject returns nil

func (*CmdBlock) GetResult

func (cmd *CmdBlock) GetResult() *TypeObject

GetResult returns result

func (*CmdBlock) GetToken

func (cmd *CmdBlock) GetToken() int

GetToken returns teh index of the token

func (*CmdBlock) GetType

func (cmd *CmdBlock) GetType() CmdType

GetType returns CtStack

type CmdCommand

type CmdCommand struct {
	CmdCommon
	ID uint32 // id of the command
}

CmdCommand is a runtime command

func (*CmdCommand) GetObject

func (cmd *CmdCommand) GetObject() IObject

GetObject returns nil

func (*CmdCommand) GetResult

func (cmd *CmdCommand) GetResult() *TypeObject

GetResult returns result

func (*CmdCommand) GetToken

func (cmd *CmdCommand) GetToken() int

GetToken returns the index of the token

func (*CmdCommand) GetType

func (cmd *CmdCommand) GetType() CmdType

GetType returns CtCommand

type CmdCommon

type CmdCommon struct {
	TokenID uint32 // the index of the token in lexeme
}

CmdCommon is a common structure for all commands

type CmdConst

type CmdConst struct {
	CmdCommon
	Object IObject
}

CmdConst pushes a value of the constant into stack

func (*CmdConst) GetObject

func (cmd *CmdConst) GetObject() IObject

GetObject returns nil

func (*CmdConst) GetResult

func (cmd *CmdConst) GetResult() *TypeObject

GetResult returns result

func (*CmdConst) GetToken

func (cmd *CmdConst) GetToken() int

GetToken returns the index of the token

func (*CmdConst) GetType

func (cmd *CmdConst) GetType() CmdType

GetType returns CtConst

type CmdRet

type CmdRet struct {
	Cmd  ICmd        // the value of the index
	Type *TypeObject // the type of the result
}

CmdRet is the command for getting index values

type CmdType

type CmdType uint32

CmdType is used for types of commands

const (
	// CtValue pushes value into stack
	CtValue CmdType = iota + 1
	// CtVar pushes the value of the variable into stack
	CtVar
	// CtConst pushes the value of the constant into stack
	CtConst
	// CtStack is a stack command
	CtStack
	// CtUnary is an unary function
	CtUnary
	// CtBinary is binary function
	CtBinary
	// CtFunc is other functions
	CtFunc
	// CtCommand is a command of vm like break continue
	CtCommand
)

type CmdUnary

type CmdUnary struct {
	CmdCommon
	Object  IObject
	Result  *TypeObject
	Operand ICmd
}

CmdUnary calls an unary function

func (*CmdUnary) GetObject

func (cmd *CmdUnary) GetObject() IObject

GetObject returns Object

func (*CmdUnary) GetResult

func (cmd *CmdUnary) GetResult() *TypeObject

GetResult returns the type of the result

func (*CmdUnary) GetToken

func (cmd *CmdUnary) GetToken() int

GetToken returns teh index of the token

func (*CmdUnary) GetType

func (cmd *CmdUnary) GetType() CmdType

GetType returns CtUnary

type CmdValue

type CmdValue struct {
	CmdCommon
	Value  interface{}
	Result *TypeObject
}

CmdValue pushes a value into stack

func (*CmdValue) GetObject

func (cmd *CmdValue) GetObject() IObject

GetObject returns nil

func (*CmdValue) GetResult

func (cmd *CmdValue) GetResult() *TypeObject

GetResult returns result

func (*CmdValue) GetToken

func (cmd *CmdValue) GetToken() int

GetToken returns the index of the token

func (*CmdValue) GetType

func (cmd *CmdValue) GetType() CmdType

GetType returns CtValue

type CmdVar

type CmdVar struct {
	CmdCommon
	Block   *CmdBlock // pointer to the block of the variable
	Index   int       // the index of the variable in the block
	Indexes []CmdRet  // the indexes list of the variable
}

CmdVar pushes the value of the variable into stack

func (*CmdVar) GetObject

func (cmd *CmdVar) GetObject() IObject

GetObject returns nil

func (*CmdVar) GetResult

func (cmd *CmdVar) GetResult() *TypeObject

GetResult returns result

func (*CmdVar) GetToken

func (cmd *CmdVar) GetToken() int

GetToken returns teh index of the token

func (*CmdVar) GetType

func (cmd *CmdVar) GetType() CmdType

GetType returns CtValue

type ConstObject

type ConstObject struct {
	Object
	Redefined bool
	Exp       ICmd        // expression
	Return    *TypeObject // the type of the constant
	Iota      int64       // iota
}

ConstObject contains information about the constant

func (*ConstObject) GetLex

func (constObj *ConstObject) GetLex() *Lex

GetLex returns the lex structure of the object

func (*ConstObject) GetName

func (constObj *ConstObject) GetName() string

GetName returns the name of the object

func (*ConstObject) GetNext

func (constObj *ConstObject) GetNext() IObject

GetNext returns the next object with the same name

func (*ConstObject) GetParams

func (constObj *ConstObject) GetParams() []*TypeObject

GetParams returns the slice of parameters

func (*ConstObject) GetType

func (constObj *ConstObject) GetType() ObjectType

GetType returns ObjType

func (*ConstObject) Result

func (constObj *ConstObject) Result() *TypeObject

Result returns the type of the result

func (*ConstObject) SetNext

func (constObj *ConstObject) SetNext(next IObject)

SetNext sets the next with the same name

type EmbedObject

type EmbedObject struct {
	Object
	Func   interface{}   // golang function
	Return *TypeObject   // the type of the result
	Params []*TypeObject // the types of parameters
}

EmbedObject contains information about the golang function

func (*EmbedObject) GetLex

func (embedObj *EmbedObject) GetLex() *Lex

GetLex returns the lex structure of the object

func (*EmbedObject) GetName

func (embedObj *EmbedObject) GetName() string

GetName returns the name of the object

func (*EmbedObject) GetNext

func (embedObj *EmbedObject) GetNext() IObject

GetNext returns the next object with the same name

func (*EmbedObject) GetParams

func (embedObj *EmbedObject) GetParams() []*TypeObject

GetParams returns the slice of parameters

func (*EmbedObject) GetType

func (embedObj *EmbedObject) GetType() ObjectType

GetType returns ObjEmbedded

func (*EmbedObject) Result

func (embedObj *EmbedObject) Result() *TypeObject

Result returns the type of the result

func (*EmbedObject) SetNext

func (embedObj *EmbedObject) SetNext(next IObject)

SetNext sets the next with the same name

type FuncObject

type FuncObject struct {
	Object
	Block CmdBlock
}

FuncObject contains information about the function

func (*FuncObject) GetLex

func (funcObj *FuncObject) GetLex() *Lex

GetLex returns the lex structure of the object

func (*FuncObject) GetName

func (funcObj *FuncObject) GetName() string

GetName returns the name of the object

func (*FuncObject) GetNext

func (funcObj *FuncObject) GetNext() IObject

GetNext returns the next object with the same name

func (*FuncObject) GetParams

func (funcObj *FuncObject) GetParams() []*TypeObject

GetParams returns the slice of parameters

func (*FuncObject) GetType

func (funcObj *FuncObject) GetType() ObjectType

GetType returns ObjFunc

func (*FuncObject) Result

func (funcObj *FuncObject) Result() *TypeObject

Result returns the type of the result

func (*FuncObject) SetNext

func (funcObj *FuncObject) SetNext(next IObject)

SetNext sets the next with the same name

type ICmd

type ICmd interface {
	GetType() CmdType
	GetResult() *TypeObject
	GetObject() IObject
	GetToken() int
}

ICmd is an interface for stack commands

type IObject

type IObject interface {
	GetName() string
	GetNext() IObject
	Result() *TypeObject
	GetLex() *Lex
	GetParams() []*TypeObject
	GetType() ObjectType
	SetNext(IObject)
}

IObject describes interface for all objects

type KeyValue

type KeyValue struct {
	Key   interface{}
	Value interface{}
}

KeyValue is the type for key value :

type Lex

type Lex struct {
	Source  []rune
	Tokens  []Token
	Lines   []int    // offsets of lines
	Strings []string // array of constant strings
	Header  string   // # header
	Path    string   // full path to the source
}

Lex contains the result of the lexical parsing

func (Lex) LineColumn

func (lp Lex) LineColumn(ind int) (line int, column int)

LineColumn return the line and the column of the ind-th token

func (*Lex) NewToken

func (lp *Lex) NewToken(token, offset, length int)

NewToken appends a new token to lexems

func (*Lex) NewTokens

func (lp *Lex) NewTokens(offset int, tokens ...int)

NewTokens appends one-byte new tokens to lexems

type Map

type Map struct {
	Keys []string // it is required for 'for' statement and String interface
	Data map[string]interface{}
}

Map is a map

func NewMap

func NewMap() *Map

NewMap creates a new map object

func (Map) String

func (pmap Map) String() string

String interface for Map

type Object

type Object struct {
	//	Type  ObjectType
	Name  string
	Next  IObject // Next object with the same name
	LexID int     // the identifier of source code in Lexeme of Unit
	Unit  *Unit
}

Object contains infromation about any compiled object of the virtual machine

type ObjectType

type ObjectType int

ObjectType is used for types of objects

const (
	// ObjType is a type
	ObjType ObjectType = iota + 1
	// ObjEmbedded is a embedded golang function
	ObjEmbedded
	// ObjFunc is a gentee function
	ObjFunc
	// ObjConst is a constant
	ObjConst
)

type Range

type Range struct {
	From int64
	To   int64
}

Range is the type for operator ..

type RunTime

type RunTime struct {
	VM      *VirtualMachine
	Stack   []interface{} // the stack of values
	Calls   []ICmd        // the stack of calling functions
	Blocks  []RunTimeBlock
	Result  interface{} // result value
	Command uint32
	Consts  map[string]interface{}
}

RunTime is the structure for running compiled functions

type RunTimeBlock

type RunTimeBlock struct {
	Vars  []interface{}
	Block *CmdBlock
}

RunTimeBlock is a structure for storing variables

type Struct

type Struct struct {
	Type   *TypeObject
	Values []interface{} // Values of fields
}

Struct is used for custom struct types

func NewStruct

func NewStruct(ptype *TypeObject) *Struct

NewStruct creates a new struct object

func (Struct) String

func (pstruct Struct) String() string

String interface for Struct

type StructType

type StructType struct {
	Fields map[string]int64 // Names of fields with indexes of the order
	Types  []*TypeObject    // Types of fields
}

StructType is used for custom struct types

type Token

type Token struct {
	Type   int32
	Index  int32 // index in Lex.Strings if Type is tkStr
	Offset int
	Length int
}

Token is a lexical token.

type TypeObject

type TypeObject struct {
	Object
	Original reflect.Type // Original golang type
	IndexOf  *TypeObject  // consists of elements
	Custom   *StructType  // for custom struct type
}

TypeObject contains information about the type

func (*TypeObject) GetLex

func (typeObj *TypeObject) GetLex() *Lex

GetLex returns the lex structure of the object

func (*TypeObject) GetName

func (typeObj *TypeObject) GetName() (ret string)

GetName returns the name of the object

func (*TypeObject) GetNext

func (typeObj *TypeObject) GetNext() IObject

GetNext returns the next object with the same name

func (*TypeObject) GetParams

func (typeObj *TypeObject) GetParams() []*TypeObject

GetParams returns the slice of parameters

func (*TypeObject) GetType

func (typeObj *TypeObject) GetType() ObjectType

GetType returns ObjType

func (*TypeObject) Result

func (typeObj *TypeObject) Result() *TypeObject

Result returns the type of the result

func (*TypeObject) SetNext

func (typeObj *TypeObject) SetNext(next IObject)

SetNext sets the next with the same name

type Unit

type Unit struct {
	Type    UnitType
	Objects []IObject
	Names   map[string]IObject
	Lexeme  []*Lex // The array of source code
	RunID   int    // The index of run function. Undefined (-1) - run has not yet been defined
	Name    string // The name of the unit
}

Unit is a common structure for Library and Run packages

func InitUnit

func InitUnit(unitType UnitType) *Unit

InitUnit initialize a unit structure

func (*Unit) NameToType

func (unit *Unit) NameToType(name string) IObject

NameToType searches the type by its name. It accepts names like name.name.name. It creates a new type if it absents.

func (*Unit) NewConst

func (unit *Unit) NewConst(name string, value interface{}, redefined bool)

NewConst adds a new ConstObject to Unit

func (*Unit) NewEmbed

func (unit *Unit) NewEmbed(Func interface{})

NewEmbed adds a new EmbedObject to Unit

func (*Unit) NewEmbedExt

func (unit *Unit) NewEmbedExt(Func interface{}, in string, out string)

NewEmbedExt adds a new EmbedObject to Unit with string types

func (*Unit) NewEmbedTypes

func (unit *Unit) NewEmbedTypes(Func interface{}, inTypes []*TypeObject, outType *TypeObject)

NewEmbedTypes adds a new EmbedObject to Unit with types

func (*Unit) NewObject

func (unit *Unit) NewObject(obj IObject) IObject

NewObject adds a new IObject to Unit

func (*Unit) NewType

func (unit *Unit) NewType(name string, original reflect.Type, indexOf IObject) IObject

NewType adds a new type to Unit

func (*Unit) TypeByGoType

func (unit *Unit) TypeByGoType(goType reflect.Type) *TypeObject

TypeByGoType returns the type by the go type name

type UnitType

type UnitType int

UnitType is used for types of runs or packages

const (
	// UnitPackage is a package
	UnitPackage UnitType = iota + 1
	// UnitRun is an executing module
	UnitRun
)

type VirtualMachine

type VirtualMachine struct {
	Units    []*Unit
	Names    map[string]int
	Compiled int // the index of the latest compiled unit
}

VirtualMachine contains information of compiled source code

func NewVM

func NewVM() *VirtualMachine

NewVM returns a new virtual machine

func (*VirtualMachine) Run

func (vm *VirtualMachine) Run(name string) (interface{}, error)

Run executes run block

func (*VirtualMachine) StdLib

func (vm *VirtualMachine) StdLib() *Unit

StdLib returns the pointer to Standard Library Unit

func (*VirtualMachine) Unit

func (vm *VirtualMachine) Unit(name string) *Unit

Unit returns the pointer to Unit by its name

Jump to

Keyboard shortcuts

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