Documentation ¶
Index ¶
- Constants
- Variables
- func ContractsList(value string) ([]string, error)
- func ConvertMap(in any) any
- func FunctionsList(value string) ([]string, error)
- func GetFieldDefaultValue(fieldType Token) any
- func StateName(state uint32, name string) string
- type ByteCode
- func (b *ByteCode) CodeBlock() *CodeBlock
- func (b *ByteCode) FuncTailCmd() *FuncTailCmd
- func (b *ByteCode) IndexInfo() *IndexInfo
- func (b *ByteCode) Map() *Map
- func (b *ByteCode) MapItemList() *MapItemList
- func (b *ByteCode) Object() *Object
- func (b *ByteCode) SliceItem() *SliceItem
- func (b *ByteCode) VarInfo() *VarInfo
- func (b *ByteCode) VarInfos() []*VarInfo
- type ByteCodes
- type Cmd
- type CodeBlock
- func (bc *CodeBlock) AssertVar(name string) bool
- func (bc *CodeBlock) CheckLoop() bool
- func (bc *CodeBlock) CodeToString() string
- func (bc *CodeBlock) GetContractInfo() *ContractInfo
- func (bc *CodeBlock) GetFunctionInfo() *FunctionInfo
- func (bc *CodeBlock) GetInfo() isCodeBlockInfo
- func (bc *CodeBlock) GetName() string
- func (bc *CodeBlock) GetObjByName(name string) (ret *Object)
- func (bc *CodeBlock) GetOwnerInfo() *OwnerInfo
- func (bc *CodeBlock) GetType() CodeBlockType
- func (bc *CodeBlock) IsParentContract() bool
- func (bc *CodeBlock) SetExtendFunc(ext []ExtendFunc)
- func (bc *CodeBlock) SetInfo(info isCodeBlockInfo)
- type CodeBlockElseInfo
- type CodeBlockIfInfo
- type CodeBlockType
- type CodeBlockWhileInfo
- type CodeBlocks
- type CompConfig
- type ContractInfo
- type ExtFuncInfo
- type ExtendFunc
- type FieldInfo
- type FuncTail
- type FuncTailCmd
- type FunctionInfo
- type IgnoreLevel
- type IndexInfo
- type Lexeme
- func (l *Lexeme) GetBoolean() bool
- func (l *Lexeme) GetFloat64() float64
- func (l *Lexeme) GetInt64() int64
- func (l *Lexeme) GetLogger() *log.Entry
- func (l *Lexeme) GetString() string
- func (l *Lexeme) GetToken() Token
- func (l *Lexeme) Interface() any
- func (l *Lexeme) IsInteger() bool
- func (l *Lexeme) Position() string
- func (l *Lexeme) String() string
- type LexemeBoolean
- type LexemeNil
- type LexemeNumber
- type LexemeString
- type LexemeToken
- type Lexemes
- type Link
- type Map
- func (m *Map) Clear()
- func (m *Map) Get(key string) (value any, found bool)
- func (m *Map) IsEmpty() bool
- func (m *Map) Keys() []string
- func (m *Map) MarshalJSON() ([]byte, error)
- func (m *Map) Remove(key string)
- func (m *Map) Set(key string, value any)
- func (m *Map) Size() int
- func (m *Map) String() string
- func (m *Map) Values() []any
- type MapItem
- type MapItemList
- type ObjInfoExtendVariable
- type ObjInfoVariable
- type Object
- func (obj *Object) GetCodeBlock() *CodeBlock
- func (obj *Object) GetContractInfo() *ContractInfo
- func (obj *Object) GetExtFuncInfo() *ExtFuncInfo
- func (obj *Object) GetExtendVariable() *ObjInfoExtendVariable
- func (obj *Object) GetFunctionInfo() *FunctionInfo
- func (obj *Object) GetName() string
- func (obj *Object) GetParamsLen() int
- func (obj *Object) GetResultsLen() int
- func (obj *Object) GetValue() isObjValue
- func (obj *Object) GetVariable() *ObjInfoVariable
- func (obj *Object) GetVariadic() bool
- func (obj *Object) IsCodeBlockContract() bool
- func (obj *Object) IsCodeBlockFunction() bool
- type ObjectType
- type OwnerInfo
- type Parser
- type SliceItem
- type Token
- type VarInfo
Constants ¶
const ( // CmdSys is the command for delimiters CmdSys cmd = 0xff CmdUnary = 50 )
const ( // MapConst represents a constant value. MapConst = iota // MapVar represents a variable. MapVar // MapMap represents a map. MapMap // MapExtend represents an extended variable. MapExtend // MapArray represents an array. MapArray )
const ( // MustKey represents a key. MustKey = iota // MustColon represents a colon. MustColon // MustComma represents a comma. MustComma // MustValue represents a value. MustValue )
const ( SliceLow = iota SliceLowNum SliceHigh SliceHighNum )
Variables ¶
var CodeBlockTypeName = map[int32]string{
1: "CodeBlockDefault",
2: "CodeBlockOwner",
3: "CodeBlockContract",
4: "CodeBlockFunction",
5: "CodeBlockIf",
6: "CodeBlockElse",
7: "CodeBlockWhile",
}
var KeywordValue = map[string]Token{ "contract": CONTRACT, "func": FUNC, "return": RETURN, "if": IF, "elif": ELIF, "else": ELSE, "while": WHILE, "true": TRUE, "false": FALSE, "var": VAR, "data": FIELD, "settings": SETTINGS, "break": BREAK, "continue": CONTINUE, "warning": ERRWARNING, "info": ERRINFO, "nil": NIL, "action": ACTION, "conditions": CONDITIONS, "...": TAIL, "error": ERROR, }
KeywordValue is a map of keywords to tokens
var ObjectTypeName = map[int32]string{
0: "Default",
1: "CodeBlock",
2: "ExtFunc",
3: "Variable",
4: "ExtVar",
}
ObjectTypeName maps the integer values of ObjectType to their string representations.
var TypeNameReflect = map[Token]reflect.Type{ BOOL: reflect.TypeOf(true), BYTES: reflect.TypeOf([]byte{}), INT: reflect.TypeOf(int64(0)), ADDRESS: reflect.TypeOf(int64(0)), ARRAY: reflect.TypeOf([]any{}), MAP: reflect.TypeOf(&Map{}), MONEY: reflect.TypeOf(decimal.Zero), FLOAT: reflect.TypeOf(0.0), STRING: reflect.TypeOf(""), FILE: reflect.TypeOf(&Map{}), }
TypeNameReflect is a map of types to reflect.Type
var TypeNameValue = map[string]Token{ "bool": BOOL, "bytes": BYTES, "int": INT, "address": ADDRESS, "array": ARRAY, "map": MAP, "money": MONEY, "float": FLOAT, "string": STRING, "file": FILE, }
TypeNameValue is a map of types to tokens
Functions ¶
func ContractsList ¶
ContractsList parses the given value string and returns a list of contract names found.
func FunctionsList ¶
FunctionsList parses the given value string and returns a list of function names found.
func GetFieldDefaultValue ¶
GetFieldDefaultValue returns default value for field type
func StateName ¶
StateName checks the name of the contract and modifies it to @[state]name if it is necessary. If the name does not start with '@', it returns the name prefixed with the state number. If the name starts with '@' and the second character is not a digit, it returns the name prefixed with the state number. Otherwise, it returns the name as is.
Types ¶
type ByteCode ¶
type ByteCode struct { Cmd Cmd Lexeme *Lexeme // Types that are assignable to Value: // // *FuncTailCmd, assigned to CmdFuncTail. // *IndexInfo, assigned to CmdGetIndex, CmdSetIndex. // *VarInfo, assigned to CmdVar. // *CodeBlock, assigned to CmdIf, CmdElse, CmdWhile // *Object, assigned to CmdCall, CmdCallVariadic both for CodeBlockFunction, ObjExtFunc and CodeBlockContract // *SliceItem, assigned to CmdSliceColon // *Map, assigned to CmdMapInit // *[]*VarInfo, assigned to CmdAssignVar // *MapItemList, assigned to CmdArrayInit // *Lexeme.Value, assigned to CmdPush, CmdError // *map[string][]any, assigned to CmdPush for function tail // *string, assigned to CmdPush, CmdCallExtend, CmdExtend // *uint32, assigned to CmdPush for OwnerInfo.StateId // *uint16, assigned to CmdSys, op.Cmd is operatorPriority // *int, assigned to CmdReturn, CmdBreak, CmdContinue, CmdWhile, CmdPush, CmdAssign, CmdUnwrapArr, CmdLabel Value any }
ByteCode stores a command and an additional parameter.
func (*ByteCode) FuncTailCmd ¶
func (b *ByteCode) FuncTailCmd() *FuncTailCmd
func (*ByteCode) MapItemList ¶
func (b *ByteCode) MapItemList() *MapItemList
type Cmd ¶
type Cmd uint16
const ( CmdPush Cmd = iota + 1 // Push value to stack CmdVar // Push variable to stack CmdExtend // Push extend variable to stack CmdCallExtend // Call extend function CmdPushStr // Push identifier as string CmdCall // call a function CmdCallVariadic // call a variadic function CmdReturn // return from function CmdIf // run block if Value is true CmdElse // run block if Value is false CmdAssignVar // list of assigned var CmdAssign // assign CmdLabel // label for continue CmdContinue // continue from label CmdWhile // while CmdBreak // break CmdGetIndex // get index [] CmdSetIndex // set index [] CmdFuncTail // set func tail Func(...).tail(...) CmdUnwrapArr // unwrap array to stack CmdMapInit // map initialization CmdArrayInit // array initialization CmdError // error command CmdSliceColon // slice [:] CmdNot // ! CmdSign // unary sign )
here are described the commands of bytecode
const ( CmdInc Cmd = iota | 0x0100 // ++ CmdDec // -- CmdAssignAdd // += CmdAssignSub // -= CmdAssignMul // *= CmdAssignDiv // /= CmdAssignMod // %= CmdAssignAnd // &= CmdAssignOr // |= CmdAssignXor // ^= CmdAssignLShift // <<= CmdAssignRShift // >>= )
the commands for operations in expressions are listed below
type CodeBlock ¶
type CodeBlock struct { Objects map[string]*Object Type CodeBlockType // Types that are assignable to Info: // // *FunctionInfo // *ContractInfo // *OwnerInfo // *CodeBlockIfInfo // *CodeBlockElseInfo // *CodeBlockWhileInfo Info isCodeBlockInfo Parent *CodeBlock Vars []Token Code ByteCodes // PredeclaredVar is a list of variables that are declared in the block PredeclaredVar []string Children CodeBlocks }
CodeBlock contains all information about compiled block {...} and its children
func CompileBlock ¶
func CompileBlock(input []rune, conf *CompConfig) (*CodeBlock, error)
CompileBlock compiles a block of input runes into a CodeBlock using the provided CompConfig.
func NewCodeBlock ¶
func NewCodeBlock(conf *CompConfig) *CodeBlock
func (*CodeBlock) CheckLoop ¶
CheckLoop checks if the current block or any of its parent blocks is of type 'ObjWhile', which represents a loop in the code. If a loop is found, it returns true, otherwise false.
func (*CodeBlock) CodeToString ¶
func (*CodeBlock) GetContractInfo ¶
func (bc *CodeBlock) GetContractInfo() *ContractInfo
func (*CodeBlock) GetFunctionInfo ¶
func (bc *CodeBlock) GetFunctionInfo() *FunctionInfo
func (*CodeBlock) GetObjByName ¶
func (*CodeBlock) GetOwnerInfo ¶
func (*CodeBlock) GetType ¶
func (bc *CodeBlock) GetType() CodeBlockType
GetType returns the type of the block.
func (*CodeBlock) IsParentContract ¶
func (*CodeBlock) SetExtendFunc ¶
func (bc *CodeBlock) SetExtendFunc(ext []ExtendFunc)
type CodeBlockElseInfo ¶
type CodeBlockElseInfo struct{}
type CodeBlockIfInfo ¶
type CodeBlockIfInfo struct{}
type CodeBlockType ¶
type CodeBlockType int32
const ( CodeBlockDefault CodeBlockType = iota + 1 CodeBlockOwner CodeBlockContract CodeBlockFunction CodeBlockIf CodeBlockElse CodeBlockWhile )
func (CodeBlockType) String ¶
func (c CodeBlockType) String() string
type CodeBlockWhileInfo ¶
type CodeBlockWhileInfo struct{}
type CodeBlocks ¶
type CodeBlocks []*CodeBlock
CodeBlocks is a slice of blocks
func (*CodeBlocks) ParentOwner ¶
func (bs *CodeBlocks) ParentOwner() *OwnerInfo
type CompConfig ¶
type CompConfig struct { Owner *OwnerInfo Func []ExtendFunc PreVar []string Objects map[string]*Object IgnoreObj IgnoreLevel }
CompConfig is used for the definition of the extended functions and variables
func (*CompConfig) MakeConfig ¶
func (cfg *CompConfig) MakeConfig() *CompConfig
MakeConfig sets default values and returns the CompConfig.
func (*CompConfig) MakeExtFunc ¶
func (cfg *CompConfig) MakeExtFunc() map[string]*Object
MakeExtFunc returns a map of the object of the extended functions
type ContractInfo ¶
type ContractInfo struct { Id uint32 Name string Owner *OwnerInfo Used map[string]bool // Called contracts Field *[]*FieldInfo // contract fields Settings map[string]any CanWrite bool // true if the function can update DB }
ContractInfo contains the contract information.
func (*ContractInfo) TxMap ¶
func (c *ContractInfo) TxMap() map[string]*FieldInfo
TxMap returns the map of the contract fields.
type ExtFuncInfo ¶
type ExtFuncInfo struct { Name string Params []reflect.Type Results []reflect.Type Auto []string Variadic bool Func any CanWrite bool // If the function can update DB }
ExtFuncInfo is the structure for the extended golang function.
func (*ExtFuncInfo) AutoParamsCount ¶
func (e *ExtFuncInfo) AutoParamsCount() int
AutoParamsCount returns the number of auto params.
func (*ExtFuncInfo) Call ¶
func (e *ExtFuncInfo) Call(params []any) (ret []any)
Call executes the function with the provided parameters. It takes a slice of any type as an argument which represents the parameters to be passed to the function. It returns a slice of any type which represents the return values of the function.
type ExtendFunc ¶
type ExtendFunc struct { Name string // Func is the function to be called, it must be a function type Func any CanWrite bool AutoPars map[string]string }
ExtendFunc is used for the definition of the extended functions
func (*ExtendFunc) ExtFuncInfo ¶
func (item *ExtendFunc) ExtFuncInfo() *ExtFuncInfo
ExtFuncInfo returns an ExtFuncInfo if the Func is a function type.
func (*ExtendFunc) MakeObject ¶
func (item *ExtendFunc) MakeObject() *Object
MakeObject returns an Object if the ExtendFuncInfo is not nil.
type FieldInfo ¶
FieldInfo describes the field of the data structure.
func (*FieldInfo) ContainsTag ¶
ContainsTag returns whether the tag is contained in this field.
type FuncTail ¶
FuncTail contains the tail function information
func (FuncTail) IsParamEmpty ¶
IsParamEmpty checks if the parameter at the given index is empty (nil).
type FuncTailCmd ¶
FuncTailCmd contains the function tail and the count of parameters.
type FunctionInfo ¶
type FunctionInfo struct { Id uint32 Name string Params []Token Results []Token // tail function Tails map[string]FuncTail Variadic bool CanWrite bool // If the function can update DB }
FunctionInfo contains the function information.
func (*FunctionInfo) HasTails ¶
func (e *FunctionInfo) HasTails() bool
HasTails returns whether the function has tails function.
func (*FunctionInfo) ParamsCount ¶
func (e *FunctionInfo) ParamsCount() int
ParamsCount returns the number of parameters of the function.
type IgnoreLevel ¶
type IgnoreLevel int
const ( IgnoreNone IgnoreLevel = iota // IgnoreIdent ignore not found identifiers object. // If it is not found, it will be treated as a contract object. IgnoreIdent )
type Lexeme ¶
type Lexeme struct { Type Token // Types that are assignable to Value: // // *LexemeString // *LexemeNumber // *LexemeToken // *LexemeBoolean // *LexemeNil Value isLexemeValue Line int Column int }
Lexeme is a lexical token of the program.
func (*Lexeme) GetBoolean ¶
GetBoolean returns the boolean value of the lexeme.
func (*Lexeme) GetFloat64 ¶
GetFloat64 returns the float64 value of the lexeme.
type LexemeBoolean ¶
type LexemeBoolean struct {
Value bool
}
LexemeBoolean is a boolean lexeme.
func NewLexemeBoolean ¶
func NewLexemeBoolean(value bool) *LexemeBoolean
func (*LexemeBoolean) String ¶
func (b *LexemeBoolean) String() string
type LexemeNumber ¶
LexemeNumber is a number lexeme.
func NewLexemeNumber ¶
func NewLexemeNumber(value any) *LexemeNumber
func (*LexemeNumber) String ¶
func (n *LexemeNumber) String() string
type LexemeString ¶
type LexemeString struct {
Value string
}
LexemeString is a string lexeme.
func NewLexemeString ¶
func NewLexemeString(value string) *LexemeString
func (*LexemeString) String ¶
func (s *LexemeString) String() string
type LexemeToken ¶
type LexemeToken struct {
Value Token
}
LexemeToken is a token lexeme.
func NewLexemeToken ¶
func NewLexemeToken(value Token) *LexemeToken
func (*LexemeToken) String ¶
func (t *LexemeToken) String() string
type Link ¶
type Link struct {
// contains filtered or unexported fields
}
Link represents a node of doubly linked list.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map holds the elements in go's native map, also maintains the head and tail link to keep the elements in insertion order.
func NewFileFromMap ¶
NewFileFromMap creates a new file from a map.
func (*Map) Get ¶
Get searches the element in the map by key and returns its value or nil if key doesn't exists. Second return parameter is true if key was found, otherwise false.
func (*Map) MarshalJSON ¶
MarshalJSON marshals the Map into JSON.
type MapItem ¶
MapItem represents a map item with a type and value.
func NewMapItem ¶
func NewMapItem(v isMapItemValue) *MapItem
func (*MapItem) GetMapItemList ¶
func (m *MapItem) GetMapItemList() *MapItemList
func (*MapItem) GetVarInfo ¶
type MapItemList ¶
type MapItemList []*MapItem
type ObjInfoExtendVariable ¶
type ObjInfoExtendVariable struct { // Name is the name of the extended variable. Name string }
ObjInfoExtendVariable is the structure for the extended variable.
type ObjInfoVariable ¶
type ObjInfoVariable struct { // Name is the name of the local variable. Name string // Index is the position of the variable in the current block. Index int }
ObjInfoVariable is the structure for the local variable.
type Object ¶
type Object struct { Type ObjectType // Types that are assignable to Value: // // *CodeBlock // *ExtFuncInfo // *ObjInfoVariable // *ObjInfoExtendVariable Value isObjValue }
Object is the common object type that can be compiled.
func NewObject ¶
func NewObject(v isObjValue) *Object
NewObject creates a new Object with the given value and determines its ObjectType.
func (*Object) GetCodeBlock ¶
GetCodeBlock returns the CodeBlock of the object if it exists.
func (*Object) GetContractInfo ¶
func (obj *Object) GetContractInfo() *ContractInfo
GetContractInfo returns the ContractInfo of the object if it exists.
func (*Object) GetExtFuncInfo ¶
func (obj *Object) GetExtFuncInfo() *ExtFuncInfo
GetExtFuncInfo returns the ExtFuncInfo of the object if it exists.
func (*Object) GetExtendVariable ¶
func (obj *Object) GetExtendVariable() *ObjInfoExtendVariable
GetExtendVariable returns the ObjInfoExtendVariable of the object if it exists.
func (*Object) GetFunctionInfo ¶
func (obj *Object) GetFunctionInfo() *FunctionInfo
GetFunctionInfo returns the FunctionInfo of the object if it exists.
func (*Object) GetParamsLen ¶
GetParamsLen returns the number of parameters of the object if it is a function or extended function.
func (*Object) GetResultsLen ¶
GetResultsLen returns the number of results of the object if it is a function or extended function.
func (*Object) GetValue ¶
func (obj *Object) GetValue() isObjValue
GetValue returns the value of the object.
func (*Object) GetVariable ¶
func (obj *Object) GetVariable() *ObjInfoVariable
GetVariable returns the ObjInfoVariable of the object if it exists.
func (*Object) GetVariadic ¶
GetVariadic returns whether the object is a variadic function or extended function.
func (*Object) IsCodeBlockContract ¶
func (*Object) IsCodeBlockFunction ¶
type ObjectType ¶
type ObjectType int32
ObjectType Types of the compiled objects
const ( // ObjDefault is an default object. ObjDefault ObjectType = iota // ObjCodeBlock is a code block object. ObjCodeBlock // ObjExtFunc is an extended build in function object. ObjExtFunc // ObjVariable is a variable object. ObjVariable // ObjExtVar is an extended build in variable. ObjExtVar )
func (ObjectType) String ¶
func (x ObjectType) String() string
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a parser for the compiler.
func NewParser ¶
func NewParser(lexemes Lexemes, conf *CompConfig) *Parser
NewParser returns a new parser for the given lexemes and configuration.
type SliceItem ¶
type SliceItem struct {
Index [2]int
}
SliceItem is a slice item, Index[0] is low, Index[1] is high.
type Token ¶
type Token uint
Token is the incoming program is implemented in this file. It is the first phase of compilation where the incoming text is divided into a sequence of lexemes.
const ( UNKNOWN Token = iota DELIMITER // Delimiters OPERATOR // Operators NUMBER // integer or float IDENTIFIER // IDENTIFIER, including KEYWORD, TYPENAME, EXTEND NEWLINE // Line translation LITERAL // string or char COMMENT // Comment KEYWORD // keyword of IDENTIFIER TYPENAME // name of the type of IDENTIFIER EXTEND // Referring to an external variable of IDENTIFIER )
represents the lexical token type of the program.
The list of delimiters for DELIMITER.
const ( Not Token = OPERATOR<<8 | iota Mul Add Sub Quo MOD Less Great Assign NotEq And LessEq EqEq GrEq Or BitAnd BitOr BitXor LSHIFT RSHIFT AddEq SubEq MulEq DivEq ModEq LshEq RshEq AndEq OrEq XorEq Inc Dec )
The list of operators for OPERATOR.
const ( CONTRACT Token = KEYWORD<<8 | iota FUNC RETURN IF ELIF ELSE WHILE TRUE FALSE VAR FIELD SETTINGS BREAK CONTINUE ERRWARNING ERRINFO NIL ACTION CONDITIONS TAIL ERROR )
The list of keyword identifiers for IDENTIFIER.
The list of data types for parameters and variables for TYPENAME.
func (Token) EqualsType ¶
func (Token) ReflectType ¶
Source Files ¶
- alphabet.go
- byte_code.go
- cmd_list.go
- cmd_list_string.go
- code_block.go
- code_block_contract.go
- code_block_function.go
- code_block_if.go
- code_block_owner.go
- code_block_while.go
- compiler.go
- config.go
- errors.go
- handle.go
- item.go
- lex_table.go
- lexeme.go
- map.go
- object.go
- parser.go
- priority.go
- scanner.go
- state_table.go
- token.go
- token_parser.go
- token_string.go