-
Notifications
You must be signed in to change notification settings - Fork 0
/
nativefunctions.go
788 lines (639 loc) · 18.9 KB
/
nativefunctions.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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package main
//
// This file contains elemental functions defined natively in Go,
// which all user-defined args and extra libraries are based on
//
import "fmt"
import "reflect"
import "mk/Apollo/events"
//
// args List is the expression list handed to the evaluator,
// the first element being the function name, all following
// elements are arguments to the denoted function
//
// Checks argument types against a variable number of allowed signatures
// Each signature is represented as a list of type names
func ValidateArgs(args List, expected ...[]string) {
valid := false
for _, checks := range expected {
partvalid := true
// if there are more checked arguments then provided ones, it cannot be a valid call
if len(checks) != args.Len() {
continue
}
// compare the required types for each argument
for i, t := range checks {
if t == "Function" {
if _, ok := args.GetElement(i).Value.(Caller); ok {
continue
}
}
argtype := reflect.TypeOf(args.GetElement(i).Value)
if argtype.Name() != t && t != "Data" {
partvalid = false
break
}
}
valid = valid || partvalid
}
if !valid {
msg := "Invalid arguments\nExpected: "
for i, checks := range expected {
if i > 0 {
msg += " or "
}
msg += "("
for j, t := range checks {
if j > 0 {
msg += " "
}
msg += t
}
msg += ")"
}
msg += "\nFound: ("
for e := args.Front(); e != nil; e = e.Next() {
if e != args.Front() {
msg += " "
}
msg += reflect.TypeOf(e.Value).Name()
}
msg += ")"
panic(msg)
}
}
// returns a function that evaluates all arguments
func __evalArgs(context *Context) func(data Data, i int) Data {
return func(data Data, i int) Data {
result, err := Evaluate(data, context)
if err != nil {
panic(err.Error())
}
return result
}
}
// (type x) - Returns the type of x as a string
func _type(args List, context *Context) Data {
args.RequireArity(1)
return args.First().GetType()
}
// (def symbol value) - Defines a new symbol and assigns the value
func _def(args List, context *Context) Data {
ValidateArgs(args, []string{"Symbol", "Data"})
// the symbol referring to the defined value
symbol := args.First().(Symbol)
// get the value that shall be associated to the symbol
value := args.Second()
value, err := Evaluate(value, context)
if err == nil {
context.Define(symbol, value)
} else {
fmt.Printf(err.Error())
return nil
}
return value
}
// (fn [name] args* stmts*)
func _fn(args List, context *Context) Data {
ValidateArgs(args, []string{"Symbol", "List", "Data"}, []string{"List", "Data"})
if args.Len() < 3 {
args.InsertBefore(Symbol{"anonymous"}, args.Front())
}
return CreateFunction(args, context)
}
// (defn name args* stmts*)
func _defn(args List, context *Context) Data {
ValidateArgs(args, []string{"Symbol", "List", "Data"})
name := args.First().(Symbol)
fn := CreateFunction(args, context)
def := context.LookUp(name)
if def == nil {
// define new function
context.Define(name, fn)
} else {
// prevent native functions from being overwritten
if _, ok := def.(NativeFunction); ok {
panic(fmt.Sprintf("%s (%s) cannot be overwritten", def.String(), def.GetType().String()))
}
// just overwrite anything else
context.Define(name, fn)
}
return fn
}
// (fn+= name args* stmts*)
func _extend_function(args List, context *Context) Data {
ValidateArgs(args, []string{"Symbol", "List", "Data"})
name := args.First().(Symbol)
fn := CreateFunction(args, context)
def := context.LookUp(name)
if def == nil {
context.Define(name, fn)
return Nothing{}
}
if basisFn, ok := def.(*Function); ok {
for _, dispatch := range fn.Dispatchers {
basisFn.AddDispatch(&dispatch)
}
} else {
panic(fmt.Sprintf("%s is not a function", def.String()))
}
return Nothing{}
}
// (list x1 x2 ...)
func _list(args List, context *Context) Data {
args.evaluated = true
return args
}
// creates a dictionary. expects even number of arguments
// (dict :key1 value1 :key2 value2 ...)
func _dict(args List, context *Context) Data {
dict := CreateDict()
if args.Len()%2 == 1 {
panic("Dictionary requires an even number of arguments")
}
for e := args.Front(); e != nil; e = e.Next() {
key, _ := e.Value.(Data)
value, _ := e.Next().Value.(Data)
e = e.Next()
dict.entries[key] = value
}
return dict
}
// (symbol name) - return a symbol with given name
func _symbol(args List, context *Context) Data {
ValidateArgs(args, []string{"String"})
return Symbol{args.First().(String).Value}
}
// (keyword name) - return a keyword with given name (prepended with a colon if not supplied)
func _keyword(args List, context *Context) Data {
ValidateArgs(args, []string{"String"})
str := args.Second().(String)
if str.Value[0] != ':' {
str.Value = ":" + str.Value
}
return Keyword{str.Value}
}
// (get dict key) - gets an entry from a dictionary
// (get list index) - gets an entry from a list
// returns Nothing if entry doesn't exist
func _get(args List, context *Context) Data {
ValidateArgs(args, []string{"Dict", "Data"}, []string{"List", "Int"})
if list, ok := args.First().(List); ok {
index := args.Second().(Int)
return list.Get(int(index.Value))
}
if dict, ok := args.First().(Dict); ok {
if value, defined := dict.entries[args.Second()]; defined {
return value
}
}
return Nothing{}
}
// (put dict key value) - sets the dictionary entry "key" to value
func _put(args List, context *Context) Data {
ValidateArgs(args, []string{"Dict", "Data", "Data"}, []string{"List", "Int", "Data"})
if dict, ok := args.First().(Dict); ok {
key := args.Second()
value := args.Third()
dict.entries[key] = value
return value
}
list := args.First().(List)
index := args.Second().(Int)
value := args.Third()
list.Set(index.Value, value)
return value
}
// (print x1 x2 ...)
func _print(args List, context *Context) Data {
args.Foreach(func(data Data, i int) {
switch t := data.(type) {
case String:
fmt.Println(t.Value)
default:
fmt.Println(data.String())
}
})
return Nothing{}
}
// (append list xs1 xs2 ...) - appends lists of items to the list and returns the modified list
func _append(args List, context *Context) Data {
args.RequireArity(2)
if list, ok := args.First().(List); ok {
args.Foreach(func(data Data, i int) {
if i > 0 {
if datas, ok := data.(List); ok {
list.PushBackList(datas.List)
} else {
list.PushBack(data)
}
}
})
return list
}
panic("First argument must be a list!")
}
// (prepend list xs1 xs2 ...) - prepends lists of items to the list and returns the modified list
func _prepend(args List, context *Context) Data {
args.RequireArity(2)
if list, ok := args.Get(1).(List); ok {
args.Foreach(func(data Data, i int) {
if i > 0 {
if datas, ok := data.(List); ok {
list.PushFrontList(datas.List)
} else {
list.PushFront(data)
}
}
})
return list
}
panic("First argument must be a list!")
}
// (slice list startInl endExcl) - get all items between startIncl and endExcl
// (slice list 0 endExcl) - get all items till end
// (slice list startIncl) - get all items from startIncl till end
func _slice(args List, context *Context) Data {
ValidateArgs(args, []string{"List", "Int", "Int"}, []string{"List", "Int"})
list := args.First().(List)
if args.Len() == 3 {
start := args.Second().(Int)
end := args.Third().(Int)
return list.Slice(start.Value, end.Value)
}
start := args.Second().(Int)
return list.Slice(start.Value, list.Len())
}
// (apply f collection) -
func _apply(args List, context *Context) Data {
ValidateArgs(args, []string{"Function", "List"}, []string{"NativeFunction", "List"})
if fn, ok := args.First().(Caller); ok {
if list, ok := args.Second().(List); ok {
return fn.Call(list, context)
}
}
return Nothing{}
}
// (foreach collection f)
func _foreach(args List, context *Context) Data {
ValidateArgs(args, []string{"List", "Function"}, []string{"List", "NativeFunction"},
[]string{"Dict", "Function"}, []string{"Dict", "NativeFunction"})
// List
if list, ok := args.First().(List); ok {
f := args.Second().(Caller)
list.Foreach(func(data Data, i int) {
fArgs := MakeList(data)
f.Call(fArgs, context)
})
} else {
// Dictionary
dict := args.First().(Dict)
for key, value := range dict.entries {
f := args.Second().(Caller)
fArgs := MakeList(key, value)
f.Call(fArgs, context)
}
}
return Nothing{}
}
// (map f collection)
func _map(args List, context *Context) Data {
ValidateArgs(args, []string{"Function", "List"}, []string{"NativeFunction", "List"},
[]string{"Function", "Dict"}, []string{"NativeFunction", "Dict"})
fn := args.First().(Caller)
if list, ok := args.Second().(List); ok {
return list.Map(func(data Data, i int) Data {
fnArgs := MakeList(data)
return fn.Call(fnArgs, context)
})
} else {
dict := args.Second().(Dict)
results := CreateList()
for key, value := range dict.entries {
fnArgs := MakeList(key, value)
results.PushBack(fn.Call(fnArgs, context))
}
return results
}
return Nothing{}
}
// (filter f list) - Returns a list of items for which f returns true
func _filter(args List, context *Context) Data {
ValidateArgs(args, []string{"Function", "List"}, []string{"NativeFunction", "List"},
[]string{"Function", "Dict"}, []string{"NativeFunction", "Dict"})
fn := args.First().(Caller)
if list, ok := args.Second().(List); ok {
return list.Filter(func(data Data, i int) bool {
fnArgs := MakeList(data)
return fn.Call(fnArgs, context).(Bool).Value
})
} else {
dict := args.Second().(Dict)
results := CreateList()
for key, value := range dict.entries {
fnArgs := MakeList(key, value)
if fn.Call(fnArgs, context).(Bool).Value {
results.PushBack(value)
}
}
return results
}
return Nothing{}
}
// (len list) - Returns number of items in list
// (len dict) - Returns number of key-value pairs in dictionary
func _len(args List, context *Context) Data {
ValidateArgs(args, []string{"Dict"}, []string{"List"}, []string{"String"})
switch t := args.First().(type) {
case List:
return Int{t.Len()}
case Dict:
return Int{len(t.entries)}
case String:
return Int{len(t.Value)}
}
return Nothing{}
}
// (str x) - returns the string representation of x
func _str(args List, context *Context) Data {
args.RequireArity(1)
if args.Len() == 1 {
return String{args.First().String()}
}
str := ""
for e := args.Front(); e != nil; e = e.Next() {
switch t := e.Value.(type) {
case String:
str += t.Value
case Data:
str += t.String()
}
}
return String{str}
}
func _first(args List, context *Context) Data {
ValidateArgs(args, []string{"List"})
switch t := args.First().(type) {
case List:
return t.Front().Value.(Data)
}
return Nothing{}
}
func _last(args List, context *Context) Data {
ValidateArgs(args, []string{"List"})
switch t := args.First().(type) {
case List:
return t.Back().Value.(Data)
}
return Nothing{}
}
// (import packagename [:as alias])
func _import(args List, context *Context) Data {
ValidateArgs(args, []string{"Symbol"}, []string{"Symbol", "Keyword", "Symbol"})
// get the module
name := args.First().(Symbol)
env := context.LookUp(Symbol{"$core"}).(*Context)
module := GetModule(name.Value, env)
// prefix (by default module name)
prefix := module.name
if args.Len() > 2 && args.Second().Equals(Keyword{":as"}) {
prefix = args.Third().(Symbol).Value
}
// import it into the current context
context.Import(module.context, prefix+".")
return Nothing{}
}
// (do (expr1) (expr2) ...)
// executes all the expressions passed in as arguments
// returns the value of the last expression
func _do(args List, context *Context) Data {
var endresult Data = nil
for e := args.Front(); e != nil; e = e.Next() {
result, err := Evaluate(e.Value.(Data), context)
if e.Next() == nil && err == nil {
endresult = result
} else if err != nil {
panic(err.Error())
}
}
return endresult
}
// (if cond ifTrue [ifFalse])
func _if(args List, context *Context) Data {
ValidateArgs(args, []string{"Data", "Data"}, []string{"Data", "Data", "Data"})
value, err := Evaluate(args.First(), context)
if err != nil {
panic("Evaluation failed")
}
boolean := value.(Bool)
if boolean.Value {
result, err := Evaluate(args.Second(), context)
if err == nil {
return result
} else {
panic(err.Error())
}
}
if args.Len() > 2 {
result, err := Evaluate(args.Third(), context)
if err == nil {
return result
} else {
panic(err.Error())
}
}
return Nothing{}
}
// (= a b)
func _equals(args List, context *Context) Data {
args.RequireArity(2)
ref := args.First()
for e := args.Front().Next(); e != nil; e = e.Next() {
if !ref.Equals(e.Value.(Data)) {
return Bool{false}
}
}
return Bool{true}
}
// (code fn) - returns the definition of the supplied user-defined function
func _code(args List, context *Context) Data {
//ValidateArgs(args, []string{"*Function"})
str := "{\n"
for _, dispatch := range args.First().(*Function).Dispatchers {
str += "\t" + dispatch.String() + "\n"
}
str += "}"
return String{str}
}
func _lambda(args List, context *Context) Data {
symbols := CreateList()
insert := func(symbol Symbol) {
// if the list is empty just append
if symbols.Len() == 0 {
symbols.PushBack(symbol)
return
}
// insert sorted
for e := symbols.Front(); e != nil; e = e.Next() {
lookat := e.Value.(Symbol)
if lookat.Equals(symbol) {
return
}
if symbol.Value == "%" {
symbols.InsertBefore(symbol, e)
}
if symbol.Value[1] < lookat.Value[1] {
symbols.InsertBefore(symbol, e)
return
}
}
// it belongs at the end
symbols.PushBack(symbol)
}
collectPlaceholders(args, insert)
params := CreateList()
symbols.Foreach(func(data Data, i int) {
if data.(Symbol).Value == "%&" {
params.PushBack(Symbol{"&"})
}
params.PushBack(data)
})
fndef := CreateList()
fndef.PushBack(Symbol{"anonymous-function-x"})
fndef.PushBack(params)
fndef.PushBack(args)
return CreateFunction(fndef, context)
}
func collectPlaceholders(data Data, callback func(Symbol)) {
switch t := data.(type) {
case List:
t.Foreach(func(item Data, index int) {
collectPlaceholders(item, callback)
})
case Symbol:
if placeholderRegex.MatchString(t.Value) {
callback(t)
}
}
}
// (let [symbol expr symbol expr] expr*)
func _let(args List, context *Context) Data {
bindings := args.First().(List)
subcontext := NewContext()
subcontext.parent = context
// when bindings is (list ...) just remove the list symbol
if symbol, ok := bindings.First().(Symbol); ok && symbol.Value == "list" {
bindings.Remove(bindings.Front())
}
// iterate over bindings pair-wise to create definitions
for e := bindings.Front(); e != nil; e = e.Next().Next() {
symbol := e.Value.(Symbol)
value, err := Evaluate(e.Next().Value.(Data), context)
if err != nil {
panic(err.Error())
}
subcontext.Define(symbol, value)
}
var result Data
args.Foreach(func(elem Data, i int) {
if i > 0 {
temp, err := Evaluate(elem, subcontext)
if err != nil {
panic(err.Error())
} else {
result = temp
}
}
})
return result
}
// (range x) - collection of numbers from 0 to x
// (range a b) - collection of numbers from a inclusive to b exclusive
// (range a b step) - collection of numbers from a inclusive to b exclusive using given step
func _range(args List, context *Context) Data {
a := 0
b := 0
step := 1
if args.Len() == 1 {
b = args.First().(Int).Value
} else if args.Len() >= 2 {
a = args.First().(Int).Value
b = args.Second().(Int).Value
}
if args.Len() > 2 {
step = args.Third().(Int).Value
}
numbers := CreateList()
for i := a; i < b; i += step {
numbers.PushBack(Int{i})
}
return numbers
}
func _entity(args List, context *Context) Data {
ent := NewEntity()
return ent
}
//-----------------------------------------------------------------------------
// Native Functions for handling evens
// (defevent name args...)
// e.g. (defevent Tick :dt), (defevent HealthChanged :old-amount :new-amount), ...
func _defevent(args List, context *Context) Data {
name := args.First().(Symbol)
def := new(UserEventDefinition)
def.Name = name.Value
def.Arguments = args.SliceFrom(1)
context.Define(name, def)
return Nothing{}
}
// (trigger source Event :key1 value1 :key2 value2 ...)
// (trigger source Event value1 value2)
func _trigger(args List, context *Context) Data {
eventDef := args.Second().(*UserEventDefinition)
eventBus := context.LookUp(Symbol{"$events"}).(NativeObject).Value.(*events.EventBus)
event := new(UserEvent)
event.Definition = eventDef
// TODO: Assign arguments
eventBus.Trigger(event, args.First().(*Entity))
return Nothing{}
}
// (on event callback)
func _on(args List, context *Context) Data {
event := args.First().(*UserEventDefinition)
callback := args.Second().(*Function)
eventBus := context.GetEventBus()
handler := NewUserEventHandler(NewEntity(), callback, context)
eventBus.Subscribe(handler, event.Name, nil)
return Nothing{}
}
// (subscribe entity :to Event [:by entity] :handler handler)
func _subscribe(args List, context *Context) Data {
def := _dict(args.SliceFrom(1), context).(Dict)
entity := args.First().(*Entity)
event := def.GetOrDefault(Keyword{":to"}, nil).(*UserEventDefinition)
fn := def.GetOrDefault(Keyword{":handler"}, nil).(*Function)
by := def.GetOrDefault(Keyword{":by"}, nil)
eventBus := context.LookUp(Symbol{"$events"}).(NativeObject).Value.(*events.EventBus)
handler := NewUserEventHandler(entity, fn, context)
if by != nil {
eventBus.Subscribe(handler, event.Name, by.(*Entity))
} else {
eventBus.Subscribe(handler, event.Name, nil)
}
return Nothing{}
}
// (unsubscribe entity :to Event [:by entity] :handler handler)
func _unsubscribe(args List, context *Context) Data {
def := _dict(args.SliceFrom(1), context).(Dict)
entity := args.First().(*Entity)
event := def.GetOrDefault(Keyword{":to"}, nil).(*UserEventDefinition)
fn := def.GetOrDefault(Keyword{":handler"}, nil).(*Function)
by := def.GetOrDefault(Keyword{":by"}, nil)
eventBus := context.LookUp(Symbol{"$events"}).(NativeObject).Value.(*events.EventBus)
handler := NewUserEventHandler(entity, fn, context)
if by != nil {
eventBus.Unsubscribe(handler, event.Name, by.(*Entity))
} else {
eventBus.Unsubscribe(handler, event.Name, nil)
}
return Nothing{}
}