-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
377 lines (351 loc) · 8.8 KB
/
struct.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
368
369
370
371
372
373
374
375
376
377
package vesper
import (
"strings"
)
// IsValidStructKey - return true of the object is a valid <struct> key.
func IsValidStructKey(o *Object) bool {
switch o.Type {
case StringType, SymbolType, KeywordType, TypeType:
return true
}
return false
}
// EmptyStruct - a <struct> with no bindings
var EmptyStruct = MakeStruct(0)
// MakeStruct - create an empty <struct> object with the specified capacity
func MakeStruct(capacity int) *Object {
return &Object{
Type: StructType,
bindings: make(map[*Object]*Object, capacity),
}
}
// Struct - create a new <struct> object from the arguments, which can be other structs, or key/value pairs
func Struct(fieldvals []*Object) (*Object, error) {
strct := &Object{
Type: StructType,
bindings: make(map[*Object]*Object),
}
count := len(fieldvals)
i := 0
var bindings map[*Object]*Object
for i < count {
o := Value(fieldvals[i])
i++
switch o.Type {
case StructType: // not a valid key, just copy bindings from it
if bindings == nil {
bindings = make(map[*Object]*Object, len(o.bindings))
}
for k, v := range o.bindings {
bindings[k] = v
}
case StringType, SymbolType, KeywordType, TypeType:
if i == count {
return nil, Error(ArgumentErrorKey, "Mismatched keyword/value in arglist: ", o)
}
if bindings == nil {
bindings = make(map[*Object]*Object)
}
bindings[o] = fieldvals[i]
i++
default:
return nil, Error(ArgumentErrorKey, "Bad struct key: ", o)
}
}
if bindings == nil {
strct.bindings = make(map[*Object]*Object)
} else {
strct.bindings = bindings
}
return strct, nil
}
// StructLength - return the length (field count) of the <struct> object
func StructLength(strct *Object) int {
return len(strct.bindings)
}
// Get - return the value for the key of the object. The Value() function is first called to
// handle typed instances of <struct>.
// This is called by the VM, when a keyword is used as a function.
func Get(obj *Object, key *Object) (*Object, error) {
s := Value(obj)
if s.Type != StructType {
return nil, Error(ArgumentErrorKey, "get expected a <struct> argument, got a ", obj.Type)
}
return structGet(s, key), nil
}
func structGet(s *Object, key *Object) *Object {
switch key.Type {
case KeywordType, SymbolType, TypeType, StringType:
result, ok := s.bindings[key]
if ok {
return result
}
}
return Null
}
// Has returns whether the struct has the given key.
// Returns an error if the object is not a struct.
func Has(obj *Object, key *Object) (bool, error) {
tmp, err := Get(obj, key)
if err != nil || IsNull(tmp) {
return false, err
}
return true, nil
}
// Put adds the given object to the struct with the given key
func Put(obj *Object, key *Object, val *Object) {
obj.bindings[key] = val
}
// Unput deletes the given key from the struct
func Unput(obj *Object, key *Object) {
delete(obj.bindings, key)
}
func sliceContains(slice []*Object, obj *Object) bool {
for _, o := range slice {
if o == obj {
return true
}
}
return false
}
func slicePut(bindings []*Object, key *Object, val *Object) []*Object {
size := len(bindings)
for i := 0; i < size; i += 2 {
if key == bindings[i] {
bindings[i+1] = val
return bindings
}
}
return append(bindings, key, val)
}
func (vm *VM) validateKeywordArgList(args *Object, keys []*Object) (*Object, error) {
tmp, err := vm.validateKeywordArgBindings(args, keys)
if err != nil {
return nil, err
}
return ListFromValues(tmp), nil
}
func (vm *VM) validateKeywordArgBindings(args *Object, keys []*Object) ([]*Object, error) {
count := ListLength(args)
bindings := make([]*Object, 0, count)
for args != EmptyList {
key := Car(args)
switch key.Type {
case SymbolType:
key = vm.Intern(key.text + ":")
fallthrough
case KeywordType:
if !sliceContains(keys, key) {
return nil, Error(ArgumentErrorKey, key, " bad keyword parameter. Allowed keys: ", keys)
}
args = args.cdr
if args == EmptyList {
return nil, Error(ArgumentErrorKey, key, " mismatched keyword/value pair in parameter")
}
bindings = slicePut(bindings, key, Car(args))
case StructType:
for sym, v := range key.bindings {
if sliceContains(keys, sym) {
bindings = slicePut(bindings, sym, v)
}
}
default:
return nil, Error(ArgumentErrorKey, "Not a keyword: ", key)
}
args = args.cdr
}
return bindings, nil
}
// StructEqual returns true if the object is equal to the argument
func StructEqual(s1 *Object, s2 *Object) bool {
bindings1 := s1.bindings
size := len(bindings1)
bindings2 := s2.bindings
if size == len(bindings2) {
for k, v := range bindings1 {
v2, ok := bindings2[k]
if !ok {
return false
}
if !Equal(v, v2) {
return false
}
}
return true
}
return false
}
func structToString(s *Object) string {
var buf strings.Builder
buf.WriteString("{")
first := true
for k, v := range s.bindings {
if first {
first = false
} else {
buf.WriteString(" ")
}
buf.WriteString(k.String())
buf.WriteString(" ")
buf.WriteString(v.String())
}
buf.WriteString("}")
return buf.String()
}
func structToList(s *Object) (*Object, error) {
result := EmptyList
tail := EmptyList
for k, v := range s.bindings {
tmp := List(k, v)
if result == EmptyList {
result = List(tmp)
tail = result
} else {
tail.cdr = List(tmp)
tail = tail.cdr
}
}
return result, nil
}
func structToArray(s *Object) *Object {
size := len(s.bindings)
el := make([]*Object, size)
j := 0
for k, v := range s.bindings {
el[j] = Array(k, v)
j++
}
return ArrayFromElements(el, size)
}
// StructKeys returns the keys of the struct
func StructKeys(s *Object) *Object {
return structKeyList(s)
}
// StructValues returns a list of the values of the struct
func StructValues(s *Object) *Object {
return structValueList(s)
}
func structKeyList(s *Object) *Object {
result := EmptyList
tail := EmptyList
for key := range s.bindings {
if result == EmptyList {
result = List(key)
tail = result
} else {
tail.cdr = List(key)
tail = tail.cdr
}
}
return result
}
func structValueList(s *Object) *Object {
result := EmptyList
tail := EmptyList
for _, v := range s.bindings {
if result == EmptyList {
result = List(v)
tail = result
} else {
tail.cdr = List(v)
tail = tail.cdr
}
}
return result
}
func listToStruct(lst *Object) (*Object, error) {
strct := &Object{
Type: StructType,
bindings: make(map[*Object]*Object),
}
for lst != EmptyList {
k := lst.car
lst = lst.cdr
switch k.Type {
case ListType:
if EmptyList == k || EmptyList == k.cdr || EmptyList != k.cdr.cdr {
return nil, Error(ArgumentErrorKey, "Bad struct binding: ", k)
}
if !IsValidStructKey(k.car) {
return nil, Error(ArgumentErrorKey, "Bad struct key: ", k.car)
}
Put(strct, k.car, k.cdr.car)
case ArrayType:
elements := k.elements
n := len(elements)
if n != 2 {
return nil, Error(ArgumentErrorKey, "Bad struct binding: ", k)
}
if !IsValidStructKey(elements[0]) {
return nil, Error(ArgumentErrorKey, "Bad struct key: ", elements[0])
}
Put(strct, elements[0], elements[1])
default:
if !IsValidStructKey(k) {
return nil, Error(ArgumentErrorKey, "Bad struct key: ", k)
}
if lst == EmptyList {
return nil, Error(ArgumentErrorKey, "Mismatched keyword/value in list: ", k)
}
Put(strct, k, lst.car)
lst = lst.cdr
}
}
return strct, nil
}
func arrayToStruct(a *Object) (*Object, error) {
count := len(a.elements)
strct := &Object{
Type: StructType,
bindings: make(map[*Object]*Object, count),
}
i := 0
for i < count {
k := a.elements[i]
i++
switch k.Type {
case ListType:
if EmptyList == k || EmptyList == k.cdr || EmptyList != k.cdr.cdr {
return nil, Error(ArgumentErrorKey, "Bad struct binding: ", k)
}
if !IsValidStructKey(k.car) {
return nil, Error(ArgumentErrorKey, "Bad struct key: ", k.car)
}
Put(strct, k.car, k.cdr.car)
case ArrayType:
elements := k.elements
n := len(elements)
if n != 2 {
return nil, Error(ArgumentErrorKey, "Bad struct binding: ", k)
}
if !IsValidStructKey(elements[0]) {
return nil, Error(ArgumentErrorKey, "Bad struct key: ", k.elements[0])
}
Put(strct, elements[0], elements[1])
case StringType, SymbolType, KeywordType, TypeType:
default:
if !IsValidStructKey(k) {
return nil, Error(ArgumentErrorKey, "Bad struct key: ", k)
}
if i == count {
return nil, Error(ArgumentErrorKey, "Mismatched keyword/value in array: ", k)
}
Put(strct, k, a.elements[i])
i++
}
}
return strct, nil
}
// ToStruct converts an object to a struct
func ToStruct(obj *Object) (*Object, error) {
val := Value(obj)
switch val.Type {
case StructType:
return val, nil
case ListType:
return listToStruct(val)
case ArrayType:
return arrayToStruct(val)
}
return nil, Error(ArgumentErrorKey, "to-struct cannot accept argument of type ", obj.Type)
}