-
Notifications
You must be signed in to change notification settings - Fork 43
/
helper.go
266 lines (231 loc) · 6.38 KB
/
helper.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package yak
import (
"bytes"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/olekukonko/tablewriter"
"github.com/yaklang/yaklang/common/yak/antlr4yak"
"reflect"
"sort"
"strings"
)
type PalmScriptEngineHelper struct {
Libs map[string]*PalmScriptLib
BuildInFunctions map[string]*PalmScriptLibFunc
UserFunctions map[string]*PalmScriptLibFunc
Instances map[string]*PalmScriptLibInstance
}
func (p *PalmScriptEngineHelper) GetAllLibs() []string {
var k []string
for name := range p.Libs {
k = append(k, name)
}
sort.Strings(k)
return k
}
func (p *PalmScriptEngineHelper) HelpInfo() string {
buffer := bytes.NewBuffer(nil)
// 内置用户函数
buffer.WriteString("### 内置用户函数定义\n")
var items []string
for _, i := range p.UserFunctions {
items = append(items, i.String())
}
sort.Strings(items)
for _, i := range items {
buffer.WriteString(fmt.Sprintf(" %v\n", i))
}
buffer.WriteString(fmt.Sprintf("\n%v\n", strings.Repeat("-", 48)))
t := tablewriter.NewWriter(buffer)
t.SetHeader([]string{"内置值", "值的类型", "值"})
for name, item := range p.Instances {
if item.Value == nil {
t.Append([]string{name, item.Type, "-"})
} else {
t.Append([]string{name, item.Type, spew.Sdump(item.Value)})
}
}
t.Render()
buffer.WriteString(fmt.Sprintf("\n%v\n", strings.Repeat("-", 48)))
t = tablewriter.NewWriter(buffer)
t.SetHeader([]string{"可用依赖库", "依赖库可用元素(值/函数)"})
for libName, libs := range p.Libs {
t.Append([]string{libName, fmt.Sprint(len(libs.ElementDocs))})
}
t.Render()
return buffer.String()
}
func (p *PalmScriptEngineHelper) ShowHelpInfo() {
fmt.Println(p.HelpInfo())
}
func (p *PalmScriptEngineHelper) LibHelpInfo(name string) string {
lib, ok := p.Libs[name]
if !ok {
return ""
}
return lib.String()
}
func (p *PalmScriptEngineHelper) ShowLibHelpInfo(name string) {
fmt.Println(p.LibHelpInfo(name))
}
type PalmScriptLib struct {
Name string
Values map[string]interface{}
ElementDocs []string
FuncElements []*PalmScriptLibFunc
InstanceElements []*PalmScriptLibInstance
}
func (p *PalmScriptLib) String() string {
buff := bytes.NewBuffer(nil)
buff.WriteString(fmt.Sprintf("### Palm ExtLib: [%v] - %v elements\n\n", p.Name, len(p.Values)))
sort.Strings(p.ElementDocs)
for _, i := range p.ElementDocs {
buff.WriteString(fmt.Sprintf(" %v\n", i))
}
return buff.String()
}
type PalmScriptLibFunc struct {
LibName string
MethodName string
Params []string
Returns []string
}
func (p *PalmScriptLibFunc) String() string {
if p == nil {
return ""
}
var end string
if len(p.Returns) > 1 {
end = fmt.Sprintf(": (%v)", strings.Join(p.Returns, ", "))
} else if len(p.Returns) == 1 {
end = fmt.Sprintf(": %v", strings.Join(p.Returns, ", "))
}
if p.LibName == "" || p.LibName == "__GLOBAL__" {
return fmt.Sprintf(
"fn %v(%v)%v",
p.MethodName,
strings.Join(p.Params, ", "),
end,
)
}
return fmt.Sprintf(
"fn %v.%v(%v)%v",
p.LibName, p.MethodName,
strings.Join(p.Params, ", "),
end,
)
}
type PalmScriptLibInstance struct {
LibName string
InstanceName string
Type string
Value interface{}
}
func (p *PalmScriptLibInstance) String() string {
if p == nil {
return ""
}
return fmt.Sprintf("%v.%v: %v = %v", p.LibName, p.InstanceName, p.Type, p.Value)
}
func funcTypeToPalmScriptLibFunc(libName, method string, methodType reflect.Type) *PalmScriptLibFunc {
if methodType.Kind() != reflect.Func {
return nil
}
methodItem := &PalmScriptLibFunc{
LibName: libName,
MethodName: method,
}
// 遍历参数
for i := range make([]int, methodType.NumIn()) {
paramType := methodType.In(i)
text := fmt.Sprintf("var_%v: %v", i+1, paramType.String())
if i+1 == methodType.NumIn() && methodType.IsVariadic() {
methodItem.Params = append(
methodItem.Params,
fmt.Sprintf("vars: ...%v", paramType.Elem()),
)
} else {
methodItem.Params = append(
methodItem.Params,
text,
)
}
}
// 遍历返回值
for i := range make([]int, methodType.NumOut()) {
paramType := methodType.Out(i)
methodItem.Returns = append(
methodItem.Returns,
fmt.Sprintf("%v", paramType.String()),
)
}
return methodItem
}
func anyTypeToPalmScriptLibInstance(libName, name string, methodType reflect.Type) *PalmScriptLibInstance {
return &PalmScriptLibInstance{
LibName: libName,
InstanceName: name,
Type: methodType.String(),
}
}
func EngineToHelper(engine *antlr4yak.Engine) *PalmScriptEngineHelper {
helper := &PalmScriptEngineHelper{
Libs: make(map[string]*PalmScriptLib),
BuildInFunctions: make(map[string]*PalmScriptLibFunc),
UserFunctions: make(map[string]*PalmScriptLibFunc),
Instances: make(map[string]*PalmScriptLibInstance),
}
var extLibs []*PalmScriptLib
for name, item := range engine.GetFntable() {
iTy := reflect.TypeOf(item)
iVl := reflect.ValueOf(item)
_, _ = iTy, iVl
switch iTy {
case reflect.TypeOf(make(map[string]interface{})):
res := item.(map[string]interface{})
if res == nil && len(res) <= 0 {
continue
}
extLib := &PalmScriptLib{
Name: name,
Values: res,
}
extLibs = append(extLibs, extLib)
helper.Libs[extLib.Name] = extLib
for elementName, value := range res {
switch methodType := reflect.TypeOf(value); methodType.Kind() {
case reflect.Func:
methodItem := funcTypeToPalmScriptLibFunc(name, elementName, methodType)
if methodItem == nil {
continue
}
extLib.ElementDocs = append(extLib.ElementDocs, methodItem.String())
extLib.FuncElements = append(extLib.FuncElements, methodItem)
default:
item := anyTypeToPalmScriptLibInstance(
extLib.Name, elementName,
methodType,
)
extLib.ElementDocs = append(extLib.ElementDocs, item.String())
extLib.InstanceElements = append(extLib.InstanceElements, item)
}
}
default:
if iTy == nil {
continue
}
globalBanner := "__GLOBAL__"
switch iTy.Kind() {
case reflect.Func:
if strings.HasPrefix(name, "$") || strings.HasPrefix(name, "_") {
helper.BuildInFunctions[name] = funcTypeToPalmScriptLibFunc(globalBanner, name, iTy)
} else {
helper.UserFunctions[name] = funcTypeToPalmScriptLibFunc(globalBanner, name, iTy)
}
default:
helper.Instances[name] = anyTypeToPalmScriptLibInstance(globalBanner, name, iTy)
}
}
}
return helper
}