-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.go
265 lines (239 loc) · 6.25 KB
/
operator.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
package gmnlisp
import (
"context"
"math"
)
func notNullToTrue(v Node, err error) (Node, error) {
if v == nil || err != nil {
return Null, err
}
if _, ok := v.(_NullType); ok {
return Null, err
}
return True, err
}
type canPlus interface {
Node
Add(Node) (Node, error)
}
func cmdAdd(ctx context.Context, w *World, param Node) (Node, error) {
return w.inject(ctx, param, func(left, right Node) (Node, error) {
if _left, ok := left.(canPlus); ok {
return _left.Add(right)
}
return nil, MakeError(ErrNotSupportType, left)
})
}
type canMinus interface {
Node
Sub(Node) (Node, error)
}
func cmdSub(ctx context.Context, w *World, param Node) (Node, error) {
return w.inject(ctx, param, func(left, right Node) (Node, error) {
if _left, ok := left.(canMinus); ok {
return _left.Sub(right)
}
return nil, MakeError(ErrNotSupportType, left)
})
}
func cmdMulti(ctx context.Context, w *World, param Node) (Node, error) {
type CanMulti interface {
Node
Multi(Node) (Node, error)
}
return w.inject(ctx, param, func(left, right Node) (Node, error) {
if _left, ok := left.(CanMulti); ok {
return _left.Multi(right)
}
return nil, MakeError(ErrNotSupportType, left)
})
}
func cmdDevide(ctx context.Context, w *World, param Node) (Node, error) {
type CanDevide interface {
Node
Divide(Node) (Node, error)
}
return w.inject(ctx, param, func(left, right Node) (Node, error) {
if _left, ok := left.(CanDevide); ok {
return _left.Divide(right)
}
return nil, MakeError(ErrNotSupportType, left)
})
}
type canLessThan interface {
LessThan(Node) (bool, error)
}
func cmdLessThan(ctx context.Context, w *World, param Node) (Node, error) {
return notNullToTrue(w.inject(ctx, param, func(left, right Node) (Node, error) {
if _left, ok := left.(canLessThan); ok {
result, err := _left.LessThan(right)
if err != nil {
return Null, err
}
if result {
return right, nil
}
return Null, nil
}
return nil, MakeError(ErrNotSupportType, left)
}))
}
func cmdGreaterThan(ctx context.Context, w *World, param Node) (Node, error) {
return notNullToTrue(w.inject(ctx, param, func(left, right Node) (Node, error) {
if _right, ok := right.(canLessThan); ok {
result, err := _right.LessThan(left)
if err != nil {
return Null, err
}
if result {
return right, nil
}
return Null, nil
}
return nil, MakeError(ErrNotSupportType, right)
}))
}
func cmdEqualOp(ctx context.Context, w *World, param Node) (Node, error) {
return notNullToTrue(w.inject(ctx, param, func(left, right Node) (Node, error) {
if left.Equals(right, EQUALP) {
return right, nil
}
return Null, nil
}))
}
func cmdGreaterOrEqual(ctx context.Context, w *World, param Node) (Node, error) {
return notNullToTrue(w.inject(ctx, param, func(left, right Node) (Node, error) {
// left >= right
// <=> not (left < right )
if _left, ok := left.(canLessThan); ok {
result, err := _left.LessThan(right)
if err != nil {
return Null, err
}
if result {
return Null, nil
}
return right, nil
}
return nil, MakeError(ErrNotSupportType, right)
}))
}
func cmdLessOrEqual(ctx context.Context, w *World, param Node) (Node, error) {
return notNullToTrue(w.inject(ctx, param, func(left, right Node) (Node, error) {
// left <= right
// <=> not (right < left)
if _right, ok := right.(canLessThan); ok {
result, err := _right.LessThan(left)
if err != nil {
return Null, err
}
if result {
return Null, nil
}
return right, nil
}
return nil, MakeError(ErrNotSupportType, right)
}))
}
func cmdAnd(ctx context.Context, w *World, param Node) (Node, error) {
for {
var value Node
var err error
value, param, err = w.ShiftAndEvalCar(ctx, param)
if err != nil {
return nil, err
}
if IsNone(value) {
return Null, nil
}
if IsNone(param) {
return value, nil
}
}
}
func cmdOr(ctx context.Context, w *World, param Node) (Node, error) {
for {
var value Node
var err error
value, param, err = w.ShiftAndEvalCar(ctx, param)
if err != nil {
return nil, err
}
if IsSome(value) {
return value, nil
}
if IsNone(param) {
return Null, nil
}
}
}
func floatToInteger(argv []Node, f func(float64) float64) (Node, error) {
if value, ok := argv[0].(Integer); ok {
return value, nil
}
if value, ok := argv[0].(Float); ok {
return Integer(int(f(float64(value)))), nil
}
return nil, MakeError(ErrNotSupportType, argv[0])
}
// funTruncate implements (truncte X). It returns the integer value of X.
func funTruncate(ctx context.Context, w *World, argv []Node) (Node, error) {
return floatToInteger(argv, math.Trunc)
}
// funFloor implements (truncte X). It returns the greatest integer value less than or equal to x.
func funFloor(ctx context.Context, w *World, argv []Node) (Node, error) {
return floatToInteger(argv, math.Floor)
}
// funCeiling implements (ceiling X). It returns the least integer value greater than or equal to x.
func funCeiling(ctx context.Context, w *World, argv []Node) (Node, error) {
return floatToInteger(argv, math.Ceil)
}
func funRound(ctx context.Context, w *World, argv []Node) (Node, error) {
return floatToInteger(argv, math.Round)
}
func funMod(ctx context.Context, w *World, list []Node) (Node, error) {
var left float64
var right float64
bits := 0
if _left, ok := list[0].(Float); ok {
left = float64(_left)
} else {
_left, ok := list[0].(Integer)
if !ok {
return nil, ErrNotSupportType
}
left = float64(int(_left))
bits = 1
}
if _right, ok := list[1].(Float); ok {
right = float64(_right)
} else {
_right, ok := list[1].(Integer)
if !ok {
return nil, ErrNotSupportType
}
right = float64(int(_right))
bits |= 2
}
value := math.Remainder(float64(left), float64(right))
if bits == 3 {
return Integer(int(value)), nil
}
return Float(value), nil
}
func funRem(ctx context.Context, w *World, list []Node) (Node, error) {
if left, ok := list[0].(Integer); ok {
if right, ok := list[1].(Integer); ok {
return Integer(left % right), nil
}
}
if left, ok := list[0].(Float); ok {
if right, ok := list[1].(Float); ok {
return Float(math.Mod(float64(left), float64(right))), nil
}
if right, ok := list[1].(Integer); ok {
return Float(math.Mod(float64(left), float64(int(right)))), nil
}
}
return nil, ErrNotSupportType
}