-
Notifications
You must be signed in to change notification settings - Fork 15
/
value_set_native_func.go
135 lines (108 loc) · 3.19 KB
/
value_set_native_func.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
package rel
import (
"reflect"
"unsafe"
"github.com/arr-ai/hash"
"github.com/arr-ai/wbnf/parser"
)
// NativeFunction represents a binary relation uniquely mapping inputs to outputs.
type NativeFunction struct {
name string
fn func(Value) (Value, error)
}
// NewNativeFunction returns a new function.
func NewNativeFunction(name string, fn func(Value) (Value, error)) Value {
return &NativeFunction{"⦑" + name + "⦒", fn}
}
// NewNativeLambda returns a nameless function.
func NewNativeLambda(fn func(Value) (Value, error)) Value {
return NewNativeFunction("", fn)
}
// NewNativeFunctionAttr returns a new Attr with a named key and NativeFunction value.
func NewNativeFunctionAttr(name string, fn func(Value) (Value, error)) Attr {
return NewAttr(name, NewNativeFunction(name, fn))
}
// Name returns a native function's name.
func (f *NativeFunction) Name() string {
return f.name
}
// Hash computes a hash for a NativeFunction.
func (f *NativeFunction) Hash(seed uintptr) uintptr {
return hash.String(f.String(), hash.Uintptr(9714745597188477233>>(64-8*unsafe.Sizeof(uintptr(0))), seed))
}
// Equal tests two Values for equality. Any other type returns false.
func (f *NativeFunction) Equal(i interface{}) bool {
if g, ok := i.(*NativeFunction); ok {
return f == g
}
return false
}
// String returns a string representation of the expression.
func (f *NativeFunction) String() string {
return f.name
}
// Eval returns the Value
func (f *NativeFunction) Eval(local Scope) (Value, error) {
return f, nil
}
// Source returns an empty scanner since NativeFunction doesn't have associated
// source code.
func (f *NativeFunction) Source() parser.Scanner {
return *parser.NewScanner("")
}
var nativeFunctionKind = registerKind(203, reflect.TypeOf(NativeFunction{}))
// Kind returns a number that is unique for each major kind of Value.
func (f *NativeFunction) Kind() int {
return nativeFunctionKind
}
// Bool always returns true.
func (f *NativeFunction) IsTrue() bool {
return true
}
// Less returns true iff g is not a number or f.number < g.number.
func (f *NativeFunction) Less(g Value) bool {
if f.Kind() != g.Kind() {
return f.Kind() < g.Kind()
}
return f.String() < g.String()
}
// Negate returns {(negateTag): f}.
func (f *NativeFunction) Negate() Value {
return NewTuple(NewAttr(negateTag, f))
}
// Export exports a NativeFunction.
func (f *NativeFunction) Export() interface{} {
return f.fn
}
func (*NativeFunction) Count() int {
return 1
}
func (*NativeFunction) Has(Value) bool {
panic("unimplemented")
}
func (*NativeFunction) Enumerator() ValueEnumerator {
panic("unimplemented")
}
func (*NativeFunction) With(Value) Set {
panic("unimplemented")
}
func (*NativeFunction) Without(Value) Set {
panic("unimplemented")
}
func (*NativeFunction) Map(func(Value) Value) Set {
panic("unimplemented")
}
func (*NativeFunction) Where(p func(v Value) (bool, error)) (Set, error) {
panic("unimplemented")
}
// Call calls the NativeFunction with the given parameter.
func (f *NativeFunction) CallAll(arg Value) (Set, error) {
v, err := f.fn(arg)
if err != nil {
return nil, err
}
return NewSet(v), nil
}
func (*NativeFunction) ArrayEnumerator() (OffsetValueEnumerator, bool) {
panic("unimplemented")
}