/
register.go
262 lines (237 loc) · 6.27 KB
/
register.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
// Copyright 2013 Walter Schulze
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package funcs
import (
"fmt"
"reflect"
"strings"
"github.com/katydid/katydid/relapse/types"
)
var errTyp = reflect.TypeOf((*error)(nil)).Elem()
var funcTyp = reflect.TypeOf((*Func)(nil)).Elem()
// Register registers a function as function that can composed.
func Register(name string, fnc interface{}) {
typ := reflect.TypeOf(fnc)
if typ.Kind() != reflect.Func {
panic(fmt.Sprintf("expecting constructor function for %s, but got %T", name, fnc))
}
if typ.NumOut() == 2 {
if !typ.Out(1).Implements(errTyp) {
panic(fmt.Sprintf("the second return type of the constructor for %s is not an error", name))
}
}
if typ.NumOut() > 2 {
panic(fmt.Sprintf("the constructor for %s has more than 2 return values", name))
}
if typ.NumOut() == 0 {
panic(fmt.Sprintf("the constructor for %s has no return values", name))
}
eval, ok := typ.Out(0).MethodByName("Eval")
if !ok {
panic(fmt.Sprintf("the constructor for %s returns a type without an Eval method", name))
}
if !typ.Out(0).Implements(funcTyp) {
panic(fmt.Sprintf("the constructor for %s returns a type that does not implement funcs.Func", name))
}
returnType := eval.Type
ins := typ.NumIn()
fMaker := &Maker{
Name: name,
Out: types.FromGo(returnType.Out(0)),
newfnc: fnc,
}
for i := 0; i < ins; i++ {
meth, ok := typ.In(i).MethodByName("Eval")
if !ok {
continue
}
if !typ.In(i).Implements(funcTyp) {
panic(fmt.Sprintf("the constructor for %s has an input parameter (number %d) that does not implement funcs.Func", name, i))
}
fMaker.InConst = append(fMaker.InConst, IsConst(typ.In(i)))
inType := types.FromGo(meth.Type.Out(0))
fMaker.In = append(fMaker.In, inType)
}
globalFactory.register(fMaker)
}
// IsConst returns whether a reflected type is a function that is actually a constant value.
func IsConst(typ reflect.Type) bool {
switch typ {
case typConstDouble:
case typConstInt:
case typConstUint:
case typConstBool:
case typConstString:
case typConstBytes:
case typConstDoubles:
case typConstInts:
case typConstUints:
case typConstBools:
case typConstStrings:
case typConstListOfBytes:
default:
return false
}
return true
}
// Which returns the Funk (function creator) of the function given the function name and parameter types.
func GetMaker(name string, ins ...types.Type) (*Maker, error) {
return globalFactory.getMaker(name, ins...)
}
type errUnknownFunction struct {
f string
ins []string
}
func newErrUnknownFunction(name string, ins []types.Type) error {
inss := make([]string, len(ins))
for i, in := range ins {
inss[i] = in.String()
}
return &errUnknownFunction{name, inss}
}
func (this *errUnknownFunction) Error() string {
return "relapse/funcs: unknown function: " + this.f + "(" + strings.Join(this.ins, ", ") + ")"
}
var globalFactory = newFactory()
type Factory map[string][]*Maker
func newFactory() Factory {
return make(map[string][]*Maker)
}
func (this Factory) register(f *Maker) {
if _, ok := this[f.Name]; !ok {
this[f.Name] = []*Maker{}
}
this[f.Name] = append(this[f.Name], f)
}
func (this Factory) getMaker(name string, ins ...types.Type) (*Maker, error) {
funks, ok := this[name]
if !ok {
return nil, newErrUnknownFunction(name, ins)
}
for _, f := range funks {
if len(f.In) != len(ins) {
continue
}
eq := true
for i := range f.In {
if f.In[i] != ins[i] {
eq = false
break
}
}
if !eq {
continue
}
return f, nil
}
return nil, newErrUnknownFunction(name, ins)
}
type Maker struct {
Name string
In []types.Type
InConst []bool
Out types.Type
newfnc interface{}
}
func (this *Maker) String() string {
ins := make([]string, len(this.In))
for i, in := range this.In {
ins[i] = in.String()
}
return fmt.Sprintf("func %v(%v) %v", this.Name, strings.Join(ins, ","), this.Out.String())
}
func (f *Maker) New(values ...interface{}) (interface{}, error) {
newf := reflect.ValueOf(f.newfnc)
rvalues := make([]reflect.Value, len(values))
for i := range rvalues {
rvalues[i] = reflect.ValueOf(values[i])
}
res := newf.Call(rvalues)
if len(res) == 2 {
if !res[1].IsNil() {
return res[0].Interface(), res[1].Interface().(error)
}
}
return res[0].Interface(), nil
}
// IsFalse returns whether a function is a false constant.
func IsFalse(fn Bool) bool {
v, ok := fn.(*constBool)
if !ok {
return false
}
return v.v == false
}
// IsTrue returns whether a function is a true constant.
func IsTrue(fn Bool) bool {
v, ok := fn.(*constBool)
if !ok {
return false
}
return v.v == true
}
// Equal returns whether two functions are equal.
func Equal(l, r Comparable) bool {
hl := l.Hash()
hr := r.Hash()
if hl != hr {
return false
}
return l.Compare(r) == 0
}
// IsSimpleEqual returns whether the input function is a simple equal expression,
// where one argument is a constant and the other is a variable.
func IsSimpleEqual(f Bool) bool {
switch eq := f.(type) {
case *stringEq:
_, v := isVarConst(eq.V1, eq.V2)
return v
case *intEq:
_, v := isVarConst(eq.V1, eq.V2)
return v
case *uintEq:
_, v := isVarConst(eq.V1, eq.V2)
return v
}
return false
}
func isVarConst(a, b interface{}) (aConst, bool) {
if c, aok := a.(aConst); aok {
if _, bok := b.(aVariable); bok {
return c, true
}
} else if c, bok := b.(aConst); bok {
if _, aok := a.(aVariable); aok {
return c, true
}
}
return nil, false
}
// Hash calculates a hash for a function, given a name and its parameters.
func Hash(name string, hs ...Hashable) uint64 {
h := uint64(17)
h = 31*h + deriveHashString(name)
for _, hashable := range hs {
h = 31*h + hashable.Hash()
}
return h
}
func hashWithId(id uint64, hs ...Hashable) uint64 {
h := uint64(17)
h = 31*h + id
for _, hashable := range hs {
h = 31*h + hashable.Hash()
}
return h
}