-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
652 lines (535 loc) · 11.7 KB
/
data.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package main
import "container/list"
import "fmt"
import "bytes"
import "mk/Apollo/events"
type Data interface {
String() string
Equals(other Data) bool
GetType() DataType
}
type DataType struct {
TypeName string
}
type List struct {
*list.List
evaluated bool
}
type Dict struct {
entries map[Data]Data
}
type String struct {
Value string
}
type Symbol struct {
Value string
}
type Keyword struct {
Value string
}
type Bool struct {
Value bool
}
type Nothing struct {
}
type NativeObject struct {
Value interface{}
}
func (t NativeObject) String() string {
return "NativeObject"
}
func (t DataType) String() string {
return t.TypeName
}
func (s String) String() string {
return fmt.Sprintf("\"%s\"", s.Value)
}
func (s Symbol) String() string {
return s.Value
}
func (k Keyword) String() string {
return k.Value
}
func (n Nothing) String() string {
return "Nothing"
}
func (l List) String() string {
var buffer bytes.Buffer
if l.evaluated {
buffer.WriteString("[")
} else {
buffer.WriteString("(")
}
for e := l.Front(); e != nil; e = e.Next() {
if buffer.Len() > 1 {
buffer.WriteString(" ")
}
if data, ok := e.Value.(Data); ok {
buffer.WriteString(data.String())
}
}
if l.evaluated {
buffer.WriteString("]")
} else {
buffer.WriteString(")")
}
return buffer.String()
}
func (d Dict) String() string {
var buffer bytes.Buffer
buffer.WriteString("{")
i := 0
for key, value := range d.entries {
if i > 0 {
buffer.WriteString(" ")
}
buffer.WriteString(key.String())
buffer.WriteString(" ")
buffer.WriteString(value.String())
i++
}
buffer.WriteString("}")
return buffer.String()
}
func (b Bool) String() string {
if b.Value {
return "true"
}
return "false"
}
func CreateList() List {
return List{
List: list.New(),
}
}
func MakeList(args ...Data) List {
list := CreateList()
for _, arg := range args {
list.PushBack(arg)
}
return list
}
func CreateDict() Dict {
return Dict{
entries: make(map[Data]Data),
}
}
func (d Dict) GetOrDefault(key Data, defaultValue Data) Data {
if value, ok := d.entries[key]; ok {
return value
} else {
return defaultValue
}
}
func (ls List) Plus(a Data) Data {
switch t := a.(type) {
case List:
copy := CreateList()
copy.PushBackList(ls.List)
copy.PushBackList(t.List)
return copy
default:
copy := CreateList()
copy.PushBackList(ls.List)
copy.PushBack(t)
return copy
}
return Nothing{}
}
func (ls List) Set(n int, value Data) Data {
// positive indices = offset from front
if n >= 0 {
i := 0
for e := ls.Front(); e != nil; e = e.Next() {
if i == n {
e.Value = value
return value
}
i++
}
}
// negative indices = offset from back
i := -1
for e := ls.Back(); e != nil; e = e.Prev() {
if i == n {
e.Value = value
return value
}
i--
}
return Nothing{}
}
func (ls List) GetElement(n int) *list.Element {
// positive indices = offset from front
if n >= 0 {
i := 0
for e := ls.Front(); e != nil; e = e.Next() {
if i == n {
return e
}
i++
}
}
// negative indices = offset from back
i := -1
for e := ls.Back(); e != nil; e = e.Prev() {
if i == n {
return e
}
i--
}
return nil
}
func (ls List) Get(n int) Data {
// positive indices = offset from front
if n >= 0 {
i := 0
for e := ls.Front(); e != nil; e = e.Next() {
if i == n {
if data, ok := e.Value.(Data); ok {
return data
}
}
i++
}
}
// negative indices = offset from back
i := -1
for e := ls.Back(); e != nil; e = e.Prev() {
if i == n {
if data, ok := e.Value.(Data); ok {
return data
}
}
i--
}
return Nothing{}
}
func (ls List) Last() Data {
last := ls.Back()
if last == nil {
return Nothing{}
} else {
if data, ok := last.Value.(Data); ok {
return data
} else {
panic("List contains invalid data type")
}
}
}
func (ls List) First() Data {
return ls.Get(0)
}
func (ls List) Second() Data {
return ls.Get(1)
}
func (ls List) Third() Data {
return ls.Get(2)
}
func (ls List) RequireArity(n int) {
if ls.Len() < n {
panic(fmt.Sprintf("%d elements expected, only %d provided", n, ls.Len()))
}
}
func (ls List) Foreach(f func(a Data, i int)) {
i := 0
for e := ls.Front(); e != nil; e = e.Next() {
switch t := e.Value.(type) {
case Data:
f(t, i)
}
i++
}
}
func (ls List) Slice(startIncl int, endExcl int) List {
if startIncl < 0 {
startIncl += ls.Len()
}
if endExcl < 0 {
endExcl += ls.Len()
}
return ls.Filter(func(a Data, i int) bool {
return i >= startIncl && i < endExcl
})
}
func (ls List) SliceFrom(startIncl int) List {
if startIncl < 0 {
startIncl += ls.Len()
}
return ls.Filter(func(a Data, i int) bool {
return i >= startIncl
})
}
func (ls List) Filter(f func(a Data, i int) bool) List {
list := CreateList()
i := 0
for e := ls.Front(); e != nil; e = e.Next() {
switch t := e.Value.(type) {
case Data:
if f(t, i) {
list.PushBack(t)
}
}
i++
}
return list
}
func (ls List) Map(f func(a Data, i int) Data) List {
list := CreateList()
i := 0
for e := ls.Front(); e != nil; e = e.Next() {
switch t := e.Value.(type) {
case Data:
list.PushBack(f(t, i))
}
i++
}
return list
}
func (x NativeObject) GetType() DataType {
return NativeObjectType
}
func (x String) GetType() DataType {
return StringType
}
func (x Int) GetType() DataType {
return IntType
}
func (x Float) GetType() DataType {
return FloatType
}
func (x Bool) GetType() DataType {
return BoolType
}
func (x Symbol) GetType() DataType {
return SymbolType
}
func (x Keyword) GetType() DataType {
return KeywordType
}
func (x List) GetType() DataType {
return ListType
}
func (x Dict) GetType() DataType {
return DictType
}
func (x Nothing) GetType() DataType {
return NothingType
}
func (x NativeFunction) GetType() DataType {
return NativeFunctionType
}
func (x NativeFunctionB) GetType() DataType {
return NativeFunctionBType
}
func (x DataType) GetType() DataType {
return DataTypeType
}
func (x Function) GetType() DataType {
return FunctionType
}
//=============================================================================
// Equality
//=============================================================================
func (x NativeObject) Equals(other Data) bool {
switch t := other.(type) {
case NativeObject:
return t.Value == x.Value
}
return false
}
func (x Int) Equals(other Data) bool {
switch t := other.(type) {
case Int:
return t.Value == x.Value
case Float:
return t.Value == float64(x.Value)
}
return false
}
func (x Float) Equals(other Data) bool {
switch t := other.(type) {
case Float:
return t.Value == x.Value
case Int:
return float64(t.Value) == x.Value
}
return false
}
func (x Bool) Equals(other Data) bool {
switch t := other.(type) {
case Bool:
return t.Value == x.Value
}
return false
}
func (x String) Equals(other Data) bool {
switch t := other.(type) {
case String:
return t.Value == x.Value
}
return false
}
// two lists are defined as equal when their contents are equal
func (x List) Equals(other Data) bool {
switch t := other.(type) {
case List:
if t.Len() != x.Len() {
return false
}
a := x.Front()
b := t.Front()
for a != nil && b != nil {
dataA := a.Value.(Data)
dataB := b.Value.(Data)
if !dataA.Equals(dataB) {
return false
}
a = a.Next()
b = b.Next()
}
}
return false
}
func (x Nothing) Equals(other Data) bool {
switch other.(type) {
case Nothing:
return true
}
return false
}
func (x Symbol) Equals(other Data) bool {
switch t := other.(type) {
case Symbol:
return t.Value == x.Value
}
return false
}
func (x Keyword) Equals(other Data) bool {
switch t := other.(type) {
case Keyword:
return t.Value == x.Value
}
return false
}
func (x DataType) Equals(other Data) bool {
switch t := other.(type) {
case DataType:
return t.TypeName == x.TypeName
}
return false
}
func (x Dict) Equals(other Data) bool {
otherDict, ok := other.(Dict)
if !ok {
return false
}
for key, value := range x.entries {
otherValue, ok := otherDict.entries[key]
if !ok {
return false
}
if !otherValue.Equals(value) {
return false
}
}
return true
}
func (x NativeFunction) Equals(other Data) bool {
panic("native functions cannot be compared")
}
func (x NativeFunctionB) Equals(other Data) bool {
panic("native functions cannot be compared")
}
func (x Function) Equals(other Data) bool {
switch t := other.(type) {
case Function:
return t.String() == x.String()
}
return false
}
//-----------------------------------------------------------------------------
// Event related data types
type UserEventDefinition struct {
Name string
Arguments List
}
func (def *UserEventDefinition) String() string {
return "Event<" + def.Name + ">"
}
func (def *UserEventDefinition) Equals(other Data) bool {
if otherDef, ok := other.(*UserEventDefinition); ok {
return otherDef.Name == def.Name && otherDef.Arguments.Equals(def.Arguments)
}
return false
}
func (def *UserEventDefinition) GetType() DataType {
return EventType
}
//---
type UserEvent struct {
Definition *UserEventDefinition
Arguments Dict
}
func (e *UserEvent) EventName() string {
return e.Definition.Name
}
type UserEventHandler struct {
Owner *Entity
Handler *Function
eventChannel events.EventChannel
closure *Context
}
func NewUserEventHandler(owner *Entity, handlerFunction *Function, context *Context) *UserEventHandler {
handler := new(UserEventHandler)
handler.Owner = owner
handler.Handler = handlerFunction
handler.eventChannel = make(events.EventChannel, 100)
handler.closure = context
go handler.handleEvents()
return handler
}
func (handler *UserEventHandler) EventChannel() events.EventChannel {
return handler.eventChannel
}
func (handler *UserEventHandler) EventSourceID() uint64 {
return handler.Owner.id
}
func (handler *UserEventHandler) handleEvents() {
for event := range handler.eventChannel {
if event == nil {
close(handler.eventChannel)
return
}
switch t := event.(type) {
case *UserEvent:
args := MakeList(handler.Owner, t.Arguments)
handler.Handler.Call(args, handler.closure)
case events.EventMessage:
e := t.Content.(*UserEvent)
args := MakeList(handler.Owner, e.Arguments)
handler.Handler.Call(args, handler.closure)
}
}
}
//=============================================================================
// Global Variables
//=============================================================================
// built-in intrinsic types
var BoolType = DataType{"Bool"}
var DictType = DataType{"Dict"}
var FloatType = DataType{"Float"}
var IntType = DataType{"Int"}
var KeywordType = DataType{"Keyword"}
var ListType = DataType{"List"}
var FunctionType = DataType{"Function"}
var NativeFunctionBType = DataType{"NativeFunctionB"}
var NativeFunctionType = DataType{"NativeFunction"}
var NothingType = DataType{"Nothing"}
var StringType = DataType{"String"}
var SymbolType = DataType{"Symbol"}
var DataTypeType = DataType{"DataType"}
var ContextType = DataType{"Context"}
var EntityType = DataType{"Entity"}
var NativeObjectType = DataType{"NativeObject"}
var EventType = DataType{"Event"}