-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspiker.go
91 lines (74 loc) · 1.76 KB
/
spiker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package spiker
import (
"crypto/sha1"
"strings"
"sync"
)
// EnableAstCache enable ast cache, default is false
var EnableAstCache = false
// cachedAst cached ast node map
var cachedAst = sync.Map{}
// Execute return the computed result of a string expression
func Execute(code string) (val interface{}, err error) {
ast, err := ParseAst(code)
if err != nil {
return
}
return Evaluator(ast)
}
// ExecuteWithScope return the execute result with scope
func ExecuteWithScope(code string, scope *VariableScope) (val interface{}, err error) {
ast, err := ParseAst(code)
if err != nil {
return
}
return EvaluateWithScope(ast, scope)
}
// Format return the formatted expression
func Format(code string) (s string, err error) {
ast, err := ParseAst(code)
if err != nil {
return
}
return FormatAst(ast)
}
// ParseAst lexer, statements, transform, and return the ast nodes
// if EnableAstCache is true, it will cache the ast nodes
func ParseAst(code string) (ast []AstNode, err error) {
// get cached ast nodes
hashKey := sha1.Sum([]byte(code))
if EnableAstCache {
if ast, ok := cachedAst.Load(hashKey); ok {
return ast.([]AstNode), nil
}
}
// padding semicolon
code = padSemicolon(code)
// lexer, parser, transform
lexer := NewLexer(code)
p := Parser{Lexer: lexer}
// parse statements
stmts, err := p.Statements()
if err != nil {
return
}
// transform to ast nodes
ast, err = Transform(stmts)
if err != nil {
return
}
// cache ast nodes
if EnableAstCache {
cachedAst.Store(hashKey, ast)
}
return
}
// padding semicolon
func padSemicolon(code string) string {
code = strings.TrimSpace(code)
last := code[len(code)-1:]
if last != SymbolSemicolon.String() && last != SymbolRbrace.String() {
code += SymbolSemicolon.String()
}
return code
}