-
Notifications
You must be signed in to change notification settings - Fork 31
/
funcs.go
159 lines (144 loc) · 3.57 KB
/
funcs.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
package stdlib
import (
"encoding/json"
"strconv"
"github.com/wavesplatform/gowaves/pkg/ride/ast"
)
var funcsByVersion = mustLoadFuncs()
func FuncsByVersion() map[ast.LibraryVersion]FunctionsSignatures {
return funcsByVersion
}
type FunctionsSignatures struct {
Funcs map[string][]FunctionParams
}
type FunctionParams struct {
ID ast.Function
Arguments []Type
ReturnType Type
}
func (sig *FunctionsSignatures) Get(name string, args []Type) (FunctionParams, bool) {
overloaded, ok := sig.Funcs[name]
if !ok {
return FunctionParams{}, ok
}
var res FunctionParams
for _, o := range overloaded {
if len(o.Arguments) != len(args) {
continue
}
isMatched := true
for i := range o.Arguments {
if !o.Arguments[i].EqualWithEntry(args[i]) {
isMatched = false
break
}
}
if isMatched {
res = o
break
}
}
if res.ID != nil {
return getGenericFuncsSign(name, args, res), true
}
return FunctionParams{}, false
}
func (sig *FunctionsSignatures) Check(name string) bool {
_, ok := sig.Funcs[name]
return ok
}
type FunctionsSignaturesJson struct {
Versions []FunctionsInVersions `json:"versions"`
}
type FunctionsInVersions struct {
New map[string][]FunctionParamsJson `json:"new"`
Remove []string `json:"remove"`
}
type FunctionParamsJson struct {
ID string `json:"id"`
Arguments []string `json:"arguments"`
ReturnType string `json:"return_type"`
}
func mustLoadFuncs() map[ast.LibraryVersion]FunctionsSignatures {
f, err := embedFS.ReadFile("funcs.json")
if err != nil {
panic(err)
}
s := &FunctionsSignaturesJson{}
if err = json.Unmarshal(f, s); err != nil {
panic(err)
}
res := map[ast.LibraryVersion]FunctionsSignatures{}
for v, funcs := range s.Versions {
funcsInVersion := FunctionsSignatures{
Funcs: map[string][]FunctionParams{},
}
if v > 0 {
// copy prev version
for name, over := range res[ast.LibraryVersion(byte(v))].Funcs {
funcsInVersion.Funcs[name] = over
}
}
for _, name := range funcs.Remove {
delete(funcsInVersion.Funcs, name)
}
for name, over := range funcs.New {
var funcsParams []FunctionParams
for _, o := range over {
var args []Type
for _, a := range o.Arguments {
args = append(args, ParseType(a))
}
var funcId ast.Function
if _, err := strconv.ParseInt(o.ID, 10, 64); err != nil {
funcId = ast.UserFunction(o.ID)
} else {
funcId = ast.NativeFunction(o.ID)
}
funcsParams = append(funcsParams, FunctionParams{
ID: funcId,
Arguments: args,
ReturnType: ParseType(o.ReturnType),
})
}
funcsInVersion.Funcs[name] = funcsParams
}
res[ast.LibraryVersion(byte(v+1))] = funcsInVersion
}
return res
}
// handleTemplateFuncs
func getGenericFuncsSign(name string, args []Type, findFuncPar FunctionParams) FunctionParams {
switch name {
case "extract", "value", "valueOrErrorMessage":
var resType Type
if u, ok := args[0].(UnionType); ok {
uResType := UnionType{Types: []Type{}}
for _, uT := range u.Types {
if !uT.Equal(SimpleType{Type: "Unit"}) {
uResType.AppendType(uT)
}
}
if len(uResType.Types) != 1 {
resType = uResType
} else {
resType = uResType.Types[0]
}
} else {
resType = args[0]
}
findFuncPar.ReturnType = resType
case "getElement":
l := args[0].(ListType)
findFuncPar.ReturnType = l.Type
case "removeByIndex":
findFuncPar.ReturnType = args[0]
case "cons":
l := args[1].(ListType)
l.AppendType(args[0])
findFuncPar.ReturnType = l
case "valueOrElse":
findFuncPar.ReturnType = args[1]
}
return findFuncPar
}