forked from haifenghuang/magpie
/
tuple.go
367 lines (324 loc) · 8.17 KB
/
tuple.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package eval
import (
"bytes"
"encoding/json"
"fmt"
"magpie/ast"
"reflect"
"strings"
)
/*
Most of the code are copied from array.go with minor modifications.
*/
type Tuple struct {
// Used in function return values.
// if a function returns multiple values, they will wrap the results into a tuple,
// the flag will be set to true
IsMulti bool
Members []Object
}
func (t *Tuple) iter() bool { return true }
func (t *Tuple) Inspect() string {
var out bytes.Buffer
members := []string{}
for _, m := range t.Members {
if m.Type() == STRING_OBJ {
members = append(members, "\""+m.Inspect()+"\"")
} else {
members = append(members, m.Inspect())
}
}
out.WriteString("(")
out.WriteString(strings.Join(members, ", "))
if (len(t.Members)) == 1 {
out.WriteString(",")
}
out.WriteString(")")
return out.String()
}
func (t *Tuple) Type() ObjectType { return TUPLE_OBJ }
func (t *Tuple) CallMethod(line string, scope *Scope, method string, args ...Object) Object {
switch method {
case "count":
return t.Count(line, args...)
case "get":
return t.Get(line, args...)
case "filter", "grep":
return t.Filter(line, scope, args...)
case "index":
return t.Index(line, args...)
case "map":
return t.Map(line, scope, args...)
case "merge":
return t.Merge(line, args...)
case "reduce":
return t.Reduce(line, scope, args...)
case "empty":
return t.Empty(line, args...)
case "len":
return t.Len(line, args...)
case "first", "head":
return t.First(line, args...)
case "last":
return t.Last(line, args...)
case "tail", "rest":
return t.Tail(line, args...)
}
return NewError(line, NOMETHODERROR, method, t.Type())
}
func (t *Tuple) Len(line string, args ...Object) Object {
if len(args) != 0 {
return NewError(line, ARGUMENTERROR, "0", len(args))
}
return NewInteger(int64(len(t.Members)))
}
func (t *Tuple) Count(line string, args ...Object) Object {
if len(args) != 1 {
return NewError(line, ARGUMENTERROR, "1", len(args))
}
count := 0
for _, v := range t.Members {
switch c := args[0].(type) {
case *Integer:
if c.Int64 == v.(*Integer).Int64 {
count++
}
case *UInteger:
if c.UInt64 == v.(*UInteger).UInt64 {
count++
}
case *Float:
if c.Float64 == v.(*Float).Float64 {
count++
}
case *String:
if c.String == v.(*String).String {
count++
}
case *Boolean:
if c.Bool == v.(*Boolean).Bool {
count++
}
default:
r := reflect.DeepEqual(c, v)
if r {
count++
}
}
}
return NewInteger(int64(count))
}
func (t *Tuple) Get(line string, args ...Object) Object {
if len(args) != 1 {
return NewError(line, ARGUMENTERROR, "1", len(args))
}
idxObj, ok := args[0].(*Integer)
if !ok {
return NewError(line, PARAMTYPEERROR, "first", "get", "*Integer", args[0].Type())
}
if idxObj.Int64 < 0 || idxObj.Int64 >= int64(len(t.Members)) {
return NewError(line, INDEXERROR, idxObj.Int64)
}
return t.Members[idxObj.Int64]
}
func (t *Tuple) Filter(line string, scope *Scope, args ...Object) Object {
if len(args) != 1 {
return NewError(line, ARGUMENTERROR, "1", len(args))
}
block, ok := args[0].(*Function)
if !ok {
return NewError(line, PARAMTYPEERROR, "first", "filter", "*Function", args[0].Type())
}
tuple := &Tuple{}
tuple.Members = []Object{}
s := NewScope(scope, nil)
for _, argument := range t.Members {
s.Set(block.Literal.Parameters[0].(*ast.Identifier).Value, argument)
cond := Eval(block.Literal.Body, s)
if IsTrue(cond) {
tuple.Members = append(tuple.Members, argument)
}
}
return tuple
}
func (t *Tuple) Index(line string, args ...Object) Object {
if len(args) < 1 || len(args) > 1 {
return NewError(line, ARGUMENTERROR, "1", len(args))
}
for i, v := range t.Members {
switch c := args[0].(type) {
case *Integer:
if c.Int64 == v.(*Integer).Int64 {
return NewInteger(int64(i))
}
case *UInteger:
if c.UInt64 == v.(*UInteger).UInt64 {
return NewInteger(int64(i))
}
case *String:
if c.String == v.(*String).String {
return NewInteger(int64(i))
}
default:
r := reflect.DeepEqual(c, v)
if r {
return NewInteger(int64(i))
}
}
}
return NIL
}
func (t *Tuple) Map(line string, scope *Scope, args ...Object) Object {
if len(args) != 1 {
return NewError(line, ARGUMENTERROR, "1", len(args))
}
block, ok := args[0].(*Function)
if !ok {
return NewError(line, PARAMTYPEERROR, "first", "map", "*Function", args[0].Type())
}
tuple := &Tuple{}
s := NewScope(scope, nil)
for _, argument := range t.Members {
s.Set(block.Literal.Parameters[0].(*ast.Identifier).Value, argument)
r := Eval(block.Literal.Body, s)
if obj, ok := r.(*ReturnValue); ok {
r = obj.Value
}
tuple.Members = append(tuple.Members, r)
}
return tuple
}
func (t *Tuple) Merge(line string, args ...Object) Object {
if len(args) != 1 {
return NewError(line, ARGUMENTERROR, "1", len(args))
}
m, ok := args[0].(*Array)
if !ok {
return NewError(line, PARAMTYPEERROR, "first", "merge", "*Array", args[0].Type())
}
tuple := &Tuple{}
for _, v := range t.Members {
tuple.Members = append(tuple.Members, v)
}
for _, v := range m.Members {
tuple.Members = append(tuple.Members, v)
}
return tuple
}
func (t *Tuple) Reduce(line string, scope *Scope, args ...Object) Object {
l := len(args)
if 1 != 2 && l != 1 {
return NewError(line, ARGUMENTERROR, "1|2", l)
}
block, ok := args[0].(*Function)
if !ok {
return NewError(line, PARAMTYPEERROR, "first", "reduce", "*Function", args[0].Type())
}
s := NewScope(scope, nil)
start := 1
if l == 1 {
s.Set(block.Literal.Parameters[0].(*ast.Identifier).Value, t.Members[0])
s.Set(block.Literal.Parameters[1].(*ast.Identifier).Value, t.Members[1])
start += 1
} else {
s.Set(block.Literal.Parameters[0].(*ast.Identifier).Value, args[1])
s.Set(block.Literal.Parameters[1].(*ast.Identifier).Value, t.Members[0])
}
r := Eval(block.Literal.Body, s)
for i := start; i < len(t.Members); i++ {
s.Set(block.Literal.Parameters[0].(*ast.Identifier).Value, r)
s.Set(block.Literal.Parameters[1].(*ast.Identifier).Value, t.Members[i])
r = Eval(block.Literal.Body, s)
if obj, ok := r.(*ReturnValue); ok {
r = obj.Value
}
}
return r
}
func (t *Tuple) Empty(line string, args ...Object) Object {
l := len(args)
if l != 0 {
return NewError(line, ARGUMENTERROR, "0", l)
}
if len(t.Members) == 0 {
return TRUE
}
return FALSE
}
func (t *Tuple) First(line string, args ...Object) Object {
l := len(args)
if l != 0 {
return NewError(line, ARGUMENTERROR, "0", l)
}
if len(t.Members) == 0 {
return NIL
}
return t.Members[0]
}
func (t *Tuple) Last(line string, args ...Object) Object {
l := len(args)
if l != 0 {
return NewError(line, ARGUMENTERROR, "0", l)
}
length := len(t.Members)
if length == 0 {
return NIL
}
return t.Members[length-1]
}
func (t *Tuple) Tail(line string, args ...Object) Object {
l := len(args)
if l != 0 {
return NewError(line, ARGUMENTERROR, "0", l)
}
length := len(t.Members)
if length == 0 {
return NIL
}
newMembers := make([]Object, length+1, length+1)
copy(newMembers, t.Members)
return &Tuple{Members: newMembers}
}
func (t *Tuple) HashKey() HashKey {
// https://en.wikipedia.org/wiki/Jenkins_hash_function
var hash uint64 = 0
for _, v := range t.Members {
hashable, ok := v.(Hashable)
if !ok {
errStr := fmt.Sprintf("key error: type %s is not hashable", v.Type())
panic(errStr)
}
h := hashable.HashKey()
hash += h.Value
hash += hash << 10
hash ^= hash >> 6
}
hash += hash << 3
hash ^= hash >> 11
hash += hash << 15
return HashKey{Type: t.Type(), Value: hash}
}
//Json marshal handling
func (t *Tuple) MarshalJSON() ([]byte, error) {
if len(t.Members) == 0 {
return json.Marshal(nil)
}
var out bytes.Buffer
out.WriteString("[") //NOTE HERE: we do not use "(", because json doesn't support tuple
for idx, v := range t.Members {
if idx != 0 {
out.WriteString(",")
}
res, err := marshalJsonObject(v)
if err != nil {
return []byte{}, err
}
out.WriteString(res.String())
} //end for
out.WriteString("]") //NOTE HERE: we do not use "(", because json doesn't support tuple
return out.Bytes(), nil
}
//NO UnmarshalJSON method for Tuple, the tuple will be treated as an array.
//func (t *Tuple) UnmarshalJSON(b []byte) error {
//
//}