Documentation
¶
Overview ¶
Package spiker - real-time computing package
Index ¶
- Variables
- func EvalExpr(node AstNode, scope *VariableScope) interface{}
- func EvaluateWithScope(nodeList []AstNode, scope *VariableScope) (res interface{}, err error)
- func Evaluator(nodeList []AstNode) (res interface{}, err error)
- func Execute(code string) (val interface{}, err error)
- func ExecuteWithScope(code string, scope *VariableScope) (val interface{}, err error)
- func Format(code string) (s string, err error)
- func FormatAst(nodeList []AstNode) (f string, err error)
- func Interface2Float64(inter interface{}) float64
- func Interface2String(inter interface{}) string
- func IsNumber(value string) bool
- func IsTrue(value interface{}) bool
- func ParseNumber(value string) (float64, error)
- func RegisterFunc(name string, fn Func)
- type Ast
- type AstNode
- type Func
- type Lexer
- type NodeAssignOp
- type NodeBinaryOp
- type NodeBool
- type NodeBreak
- type NodeContinue
- type NodeFuncCallOp
- type NodeFuncDef
- type NodeIf
- type NodeList
- type NodeMap
- type NodeNumber
- type NodeParam
- type NodeReturn
- type NodeString
- type NodeUnaryOp
- type NodeVarIndex
- type NodeVariable
- type NodeWhile
- type Parser
- type Symbol
- type Token
- type ValueList
- type ValueMap
- type VariableScope
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var EnableAstCache = false
EnableAstCache enable ast cache, default is false
Functions ¶
func EvalExpr ¶ added in v0.0.3
func EvalExpr(node AstNode, scope *VariableScope) interface{}
EvalExpr returns the value of the expression
func EvaluateWithScope ¶
func EvaluateWithScope(nodeList []AstNode, scope *VariableScope) (res interface{}, err error)
EvaluateWithScope same as Evaluator, evaluate with scope
func Execute ¶
Execute return the computed result of a string expression
Example ¶
ExampleExecute
package main
import (
"fmt"
"github.com/shockerli/spiker"
)
func main() {
src := `
a = 100;
a = len("abc");
if (a > 1) {
b = 10;
}
export(b + "10");
`
fmt.Println(spiker.Execute(src))
}
Output: 20 <nil>
func ExecuteWithScope ¶
func ExecuteWithScope(code string, scope *VariableScope) (val interface{}, err error)
ExecuteWithScope return the execute result with scope
func Interface2Float64 ¶
func Interface2Float64(inter interface{}) float64
Interface2Float64 convert interface{} to float64
func Interface2String ¶
func Interface2String(inter interface{}) string
Interface2String convert interface{} to string
func ParseNumber ¶
ParseNumber parses the number in the string
func RegisterFunc ¶ added in v0.0.3
RegisterFunc register builtin function
Types ¶
type Ast ¶
type Ast struct {
// contains filtered or unexported fields
}
Ast syntax parsing infrastructure
type AstNode ¶
AstNode AST node interface
type Func ¶ added in v0.0.3
type Func func(fnc *NodeFuncCallOp, scope *VariableScope) interface{}
Func builtin function type
type NodeAssignOp ¶
type NodeAssignOp struct {
Ast
Var NodeVariable
Op Symbol
Expr AstNode
}
NodeAssignOp assignment operator node
type NodeBinaryOp ¶
NodeBinaryOp binary operator node
type NodeFuncCallOp ¶
type NodeFuncCallOp struct {
Ast
Name NodeVariable
Params []AstNode
}
NodeFuncCallOp function call node
type NodeFuncDef ¶
type NodeFuncDef struct {
Ast
Name NodeVariable
Params []NodeParam
Body []AstNode
SingleStmt bool
}
NodeFuncDef function define
type NodeNumber ¶
NodeNumber number node
type NodeParam ¶
type NodeParam struct {
Ast
Default interface{}
Name NodeVariable
}
NodeParam function param
type NodeReturn ¶
NodeReturn return node
type NodeString ¶
NodeString string node
type NodeUnaryOp ¶
NodeUnaryOp unary operator node
type NodeVarIndex ¶
NodeVarIndex return the value of the specified index(list, string)
type NodeVariable ¶
NodeVariable variable node
type Parser ¶
type Parser struct {
Lexer *Lexer
}
Parser Lexer parser
func (*Parser) Statements ¶
Statements parse statements
type Symbol ¶
type Symbol string
Symbol lexical symbol
const ( SymbolIdent Symbol = "(IDENT)" SymbolNumber Symbol = "(NUMBER)" SymbolString Symbol = "(STRING)" SymbolEOF Symbol = "(EOF)" SymbolTuple Symbol = "()" SymbolArray Symbol = "[]" SymbolMap Symbol = "{}" SymbolPound Symbol = "#" SymbolTrue Symbol = "true" SymbolFalse Symbol = "false" SymbolNone Symbol = "none" SymbolIf Symbol = "if" SymbolElse Symbol = "else" SymbolReturn Symbol = "return" SymbolContinue Symbol = "continue" SymbolBreak Symbol = "break" SymbolWhile Symbol = "while" SymbolColon Symbol = ":" SymbolSemicolon Symbol = ";" SymbolLparen Symbol = "(" SymbolRparen Symbol = ")" SymbolLbrack Symbol = "[" SymbolRbrack Symbol = "]" SymbolLbrace Symbol = "{" SymbolRbrace Symbol = "}" SymbolComma Symbol = "," SymbolFuncDeclare Symbol = "->" // mathematical SymbolAdd Symbol = "+" SymbolSub Symbol = "-" SymbolMul Symbol = "*" SymbolDiv Symbol = "/" SymbolMod Symbol = "%" SymbolPow Symbol = "**" SymbolIn Symbol = "in" // bit arithmetic SymbolAnd Symbol = "&" SymbolOr Symbol = "|" SymbolXor Symbol = "^" SymbolNot Symbol = "~" SymbolSHL Symbol = "<<" SymbolSHR Symbol = ">>" // logic SymbolLogicNot Symbol = "!" SymbolLogicAnd Symbol = "&&" SymbolLogicOr Symbol = "||" // assignment SymbolAssign Symbol = "=" SymbolAssignAdd Symbol = "+=" SymbolAssignSub Symbol = "-=" SymbolAssignMul Symbol = "*=" SymbolAssignDiv Symbol = "/=" SymbolAssignMod Symbol = "%=" // comparison SymbolEQL Symbol = "==" SymbolNEQ Symbol = "!=" SymbolGTR Symbol = ">" SymbolGTE Symbol = ">=" SymbolLSS Symbol = "<" SymbolLTE Symbol = "<=" )
Supported symbol
type VariableScope ¶
type VariableScope struct {
// contains filtered or unexported fields
}
VariableScope variable scope
func NewScopeTable ¶
func NewScopeTable(scopeName string, scopeLevel int, scope *VariableScope) *VariableScope
NewScopeTable return a new VariableScope
func (*VariableScope) Get ¶
func (scope *VariableScope) Get(variable string) (interface{}, bool)
Get fetch variable values
func (*VariableScope) Set ¶
func (scope *VariableScope) Set(variable string, val interface{})
Set store variable values
