-
Notifications
You must be signed in to change notification settings - Fork 1
/
GDLisp.lisp
1506 lines (1286 loc) · 48.2 KB
/
GDLisp.lisp
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; Copyright 2023 Silvio Mayolo
;;;;
;;;; This file is part of GDLisp.
;;;;
;;;; GDLisp is free software: you can redistribute it and/or modify it
;;;; under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; GDLisp is distributed in the hope that it will be useful, but
;;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with GDLisp. If not, see <https://www.gnu.org/licenses/>.
;;;;
;;;; As a special exception to the terms of the GNU General Public
;;;; License, you may use this GDLisp.lisp source file in your own
;;;; projects without restriction.
;;;; Library file for GDLisp. This file must be included as a singleton
;;;; in any project which uses GDLisp compiled files.
(sys/nostdlib)
;;; Global GDLisp constants and enums
(defconst nil ())
(defconst GODOT-VERSION (sys/special-ref godot-version))
(sys/declare superglobal GDLisp public) ; This file :)
(sys/bootstrap constants)
(sys/bootstrap constant-enums)
(sys/declare superglobal (PI PI) public)
(sys/declare superglobal (TAU TAU) public)
(sys/declare superglobal (INF INF) public)
(defenum ConnectFlags
(DEFERRED 1)
(PERSIST 2)
(ONESHOT 4)
(REFERENCE_COUNTED 8))
(defenum Notification
(POSTINITIALIZE 0)
(PREDELETE 1))
;; Note: Godot doesn't consider the below names constant for some
;; reason, since they're apparently defined *on* `Object` in some
;; weird way. So we have to hardcode the numerical values in the
;; enums. We have test cases to make sure these numbers are consistent
;; with the Godot values for the same.
(sys/declare value CONNECT_DEFERRED public)
(sys/declare value CONNECT_PERSIST public)
(sys/declare value CONNECT_ONESHOT public)
(sys/declare value CONNECT_REFERENCE_COUNTED public)
(sys/declare value NOTIFICATION_POSTINITIALIZE public)
(sys/declare value NOTIFICATION_PREDELETE public)
;;; GDScript global types
(sys/bootstrap non-singleton-types)
(sys/bootstrap singleton-types)
;;; GDLisp main class initialization
(defclass _GDLisp (Node) main
(defvar __gdlisp_Global_name_generator (sys/FreshNameGenerator:new [] 0))
(defvar __gdlisp_Global_symbol_table {})
;; Primitive types
(defvar Null (PrimitiveType:new TYPE_NIL))
(defvar Bool (PrimitiveType:new TYPE_BOOL))
(defvar Int (PrimitiveType:new TYPE_INT))
(defvar Float (PrimitiveType:new TYPE_REAL))
(defvar String (PrimitiveType:new TYPE_STRING))
(defvar Vector2 (Vector2PrimitiveType:new))
(defvar Rect2 (PrimitiveType:new TYPE_RECT2))
(defvar Vector3 (Vector3PrimitiveType:new))
(defvar Transform2D (Transform2DPrimitiveType:new))
(defvar Plane (PlanePrimitiveType:new))
(defvar Quat (QuatPrimitiveType:new))
(defvar AABB (PrimitiveType:new TYPE_AABB))
(defvar Basis (BasisPrimitiveType:new))
(defvar Transform (TransformPrimitiveType:new))
(defvar Color (ColorPrimitiveType:new))
(defvar NodePath (PrimitiveType:new TYPE_NODE_PATH))
(defvar RID (PrimitiveType:new TYPE_RID))
(defvar Object (ObjectPrimitiveType:new))
(defvar Dictionary (PrimitiveType:new TYPE_DICTIONARY))
(defvar Array (PrimitiveType:new TYPE_ARRAY))
(defvar PoolByteArray (PrimitiveType:new TYPE_RAW_ARRAY))
(defvar PoolIntArray (PrimitiveType:new TYPE_INT_ARRAY))
(defvar PoolRealArray (PrimitiveType:new TYPE_REAL_ARRAY))
(defvar PoolStringArray (PrimitiveType:new TYPE_STRING_ARRAY))
(defvar PoolVector2Array (PrimitiveType:new TYPE_VECTOR2_ARRAY))
(defvar PoolVector3Array (PrimitiveType:new TYPE_VECTOR3_ARRAY))
(defvar PoolColorArray (PrimitiveType:new TYPE_COLOR_ARRAY))
;; Synthetic types
(defvar Any (AnyType:new))
(defvar AnyRef (AnyRefType:new))
(defvar AnyVal (AnyValType:new))
(defvar Number (NumberType:new))
(defvar BaseArray (BaseArrayType:new))
(defvar Nothing (NothingType:new))
;; GDScript native types lookup table
(defvar __gdlisp_Global_native_types_lookup)
(defvar __gdlisp_Global_primitive_types_lookup)
(sys/bootstrap singleton-backing-types)
(defn _init ()
(set self:__gdlisp_Global_primitive_types_lookup
{TYPE_NIL @Null TYPE_BOOL @Bool TYPE_INT @Int TYPE_REAL @Float TYPE_STRING @String TYPE_VECTOR2 @Vector2
TYPE_RECT2 @Rect2 TYPE_VECTOR3 @Vector3 TYPE_TRANSFORM2D @Transform2D TYPE_PLANE @Plane TYPE_QUAT @Quat
TYPE_AABB @AABB TYPE_BASIS @Basis TYPE_TRANSFORM @Transform TYPE_COLOR @Color TYPE_NODE_PATH @NodePath
TYPE_RID @RID TYPE_OBJECT @Object TYPE_DICTIONARY @Dictionary TYPE_ARRAY @Array
TYPE_RAW_ARRAY @PoolByteArray TYPE_INT_ARRAY @PoolIntArray TYPE_REAL_ARRAY @PoolRealArray
TYPE_STRING_ARRAY @PoolStringArray TYPE_VECTOR2_ARRAY @PoolVector2Array
TYPE_VECTOR3_ARRAY @PoolVector3Array TYPE_COLOR_ARRAY @PoolColorArray})
(sys/bootstrap native-types-table)
(set (elt self:__gdlisp_Global_native_types_lookup "Object") @Object))
(defn typeof (value)
(let ((t ((literally typeof) value)))
(cond
((/= t TYPE_OBJECT) (elt self:__gdlisp_Global_primitive_types_lookup t))
((value:get_script))
(#t (self:__gdlisp_Global_native_types_lookup:get (value:get_class) self:Any))))))
;;; Public support classes
(defclass Cons (Reference)
(defvar car)
(defvar cdr)
(defn _init (@car @cdr)
(self:set-meta "__gdlisp_Primitive_Cons" 1))
(defn (get caar) ()
self:car:car)
(defn (get cadr) ()
self:car:cdr)
(defn (get cdar) ()
self:cdr:car)
(defn (get cddr) ()
self:cdr:cdr)
(defn (get caaar) ()
self:car:car:car)
(defn (get caadr) ()
self:car:car:cdr)
(defn (get cadar) ()
self:car:cdr:car)
(defn (get caddr) ()
self:car:cdr:cdr)
(defn (get cdaar) ()
self:cdr:car:car)
(defn (get cdadr) ()
self:cdr:car:cdr)
(defn (get cddar) ()
self:cdr:cdr:car)
(defn (get cdddr) ()
self:cdr:cdr:cdr))
(defclass Function (Reference)
(defvar __gdlisp_is_function #t)
(defvar __gdlisp_required 0)
(defvar __gdlisp_optional 0)
(defvar __gdlisp_rest 1)
;; Constants for the different types of "rest" arguments a function
;; can take. (i.e. the valid values for __gdlisp_rest)
(defconst __gdlisp_vararg_no 0)
(defconst __gdlisp_vararg_rest 1)
(defconst __gdlisp_vararg_arr 2))
(defclass Cell (Reference)
(defvar contents)
(defn _init (@contents)))
(defclass Symbol (Reference)
;; Note: This will be obsolete once we have StringName in GDScript,
;; which seems to be coming in Godot 4. For now, this manual wrapper
;; stores symbols in the least efficient way possible.
(defvar __gdlisp_contents)
(defn _init (@__gdlisp_contents)
(self:set-meta "__gdlisp_Primitive_Symbol" 1)))
(defclass sys/FreshNameGenerator (Reference)
;; This is meant to be identical to FreshNameGenerator in the Rust
;; source. We want to be able to consistently generate names in the
;; same way on both Rust and Godot and to be able to communicate
;; FreshNameGenerator state between the two (via to_json /
;; from_json)
(defconst DEFAULT_PREFIX "_G")
(defvar reserved)
(defvar index)
(defn _init (@reserved @index))
(defn generate ()
(self:generate_with sys/FreshNameGenerator:DEFAULT_PREFIX))
(defn generate_with (prefix)
(let ((name ("{}_{}":format [prefix self:index] "{}")))
(set self:index (+ self:index 1))
(cond
((member? name self:reserved) (self:generate_with prefix))
(#t name))))
(defn to_json ()
{"reserved" self:reserved "index" self:index})
(defn from_json (json) static
(sys/FreshNameGenerator:new
(elt json "reserved")
(elt json "index"))))
;;; GDLisp type constants and support types
(defclass GDLispSpecialType (Reference) private)
(defclass PrimitiveType (GDLispSpecialType) private
(defvar primitive-value)
(defn _init (@primitive-value))
(defn satisfies? (value)
(= ((literally typeof) value) self:primitive-value)))
(defclass ObjectPrimitiveType (PrimitiveType) private
(defn _init ()
(super TYPE_OBJECT)))
;; NOTE: Due to current Godot bugs, we can't actually define this,
;; since GDScript fails to parse constructor calls on Object directly.
;; See https://github.com/godotengine/godot/issues/41462. Has been
;; fixed in 4.0 and it doesn't look like they're planning to backport.
;;
;; (defn new ()
;; ((literally Object):new)))
(defclass Vector2PrimitiveType (PrimitiveType) private
(defconst AXIS_X 0)
(defconst AXIS_Y 1)
(defconst ZERO V{0 0})
(defconst ONE V{1 1})
(defconst INF V{INF INF})
(defconst LEFT V{-1 0})
(defconst RIGHT V{1 0})
(defconst UP V{0 -1})
(defconst DOWN V{0 1})
(defn _init ()
(super TYPE_VECTOR2)))
(defclass Vector3PrimitiveType (PrimitiveType) private
(defconst AXIS_X 0)
(defconst AXIS_Y 1)
(defconst AXIS_Z 2)
(defconst ZERO V{0 0 0})
(defconst ONE V{1 1 1})
(defconst INF V{INF INF INF})
(defconst LEFT V{-1 0 0})
(defconst RIGHT V{1 0 0})
(defconst UP V{0 1 0})
(defconst DOWN V{0 -1 0})
(defconst FORWARD V{0 0 -1})
(defconst BACK V{0 0 1})
(defn _init ()
(super TYPE_VECTOR3)))
(defclass Transform2DPrimitiveType (PrimitiveType) private
(defconst IDENTITY (Transform2D V{1 0} V{0 1} V{0 0}))
(defconst FLIP_X (Transform2D V{-1 0} V{0 1} V{0 0}))
(defconst FLIP_Y (Transform2D V{1 0} V{0 -1} V{0 0}))
(defn _init ()
(super TYPE_TRANSFORM2D)))
(defclass PlanePrimitiveType (PrimitiveType) private
(defconst PLANE_YZ (Plane 1 0 0 0))
(defconst PLANE_XZ (Plane 0 1 0 0))
(defconst PLANE_XY (Plane 0 0 1 0))
(defn _init ()
(super TYPE_PLANE)))
(defclass QuatPrimitiveType (PrimitiveType) private
(defconst IDENTITY (Quat 0 0 0 1))
(defn _init ()
(super TYPE_QUAT)))
(defclass BasisPrimitiveType (PrimitiveType) private
(defconst IDENTITY (Basis V{1 0 0} V{0 1 0} V{0 0 1}))
(defconst FLIP_X (Basis V{-1 0 0} V{0 1 0} V{0 0 1}))
(defconst FLIP_Y (Basis V{1 0 0} V{0 -1 0} V{0 0 -1}))
(defconst FLIP_Z (Basis V{1 0 0} V{0 -1 0} V{0 0 -1}))
(defn _init ()
(super TYPE_BASIS)))
(defclass TransformPrimitiveType (PrimitiveType) private
(defconst IDENTITY (Transform V{1 0 0} V{0 1 0} V{0 0 1} V{0 0 0}))
(defconst FLIP_X (Transform V{-1 0 0} V{0 1 0} V{0 0 1} V{0 0 0}))
(defconst FLIP_Y (Transform V{1 0 0} V{0 -1 0} V{0 0 -1} V{0 0 0}))
(defconst FLIP_Z (Transform V{1 0 0} V{0 -1 0} V{0 0 -1} V{0 0 0}))
(defn _init ()
(super TYPE_TRANSFORM)))
(defclass ColorPrimitiveType (PrimitiveType) private
(defconst aliceblue (Color 0.941176 0.972549 1 1))
(defconst antiquewhite (Color 0.980392 0.921569 0.843137 1))
(defconst aqua (Color 0 1 1 1))
(defconst aquamarine (Color 0.498039 1 0.831373 1))
(defconst azure (Color 0.941176 1 1 1))
(defconst beige (Color 0.960784 0.960784 0.862745 1))
(defconst bisque (Color 1 0.894118 0.768627 1))
(defconst black (Color 0 0 0 1))
(defconst blanchedalmond (Color 1 0.921569 0.803922 1))
(defconst blue (Color 0 0 1 1))
(defconst blueviolet (Color 0.541176 0.168627 0.886275 1))
(defconst brown (Color 0.647059 0.164706 0.164706 1))
(defconst burlywood (Color 0.870588 0.721569 0.529412 1))
(defconst cadetblue (Color 0.372549 0.619608 0.627451 1))
(defconst chartreuse (Color 0.498039 1 0 1))
(defconst chocolate (Color 0.823529 0.411765 0.117647 1))
(defconst coral (Color 1 0.498039 0.313726 1))
(defconst cornflower (Color 0.392157 0.584314 0.929412 1))
(defconst cornsilk (Color 1 0.972549 0.862745 1))
(defconst crimson (Color 0.862745 0.0784314 0.235294 1))
(defconst cyan (Color 0 1 1 1))
(defconst darkblue (Color 0 0 0.545098 1))
(defconst darkcyan (Color 0 0.545098 0.545098 1))
(defconst darkgoldenrod (Color 0.721569 0.52549 0.0431373 1))
(defconst darkgray (Color 0.662745 0.662745 0.662745 1))
(defconst darkgreen (Color 0 0.392157 0 1))
(defconst darkkhaki (Color 0.741176 0.717647 0.419608 1))
(defconst darkmagenta (Color 0.545098 0 0.545098 1))
(defconst darkolivegreen (Color 0.333333 0.419608 0.184314 1))
(defconst darkorange (Color 1 0.54902 0 1))
(defconst darkorchid (Color 0.6 0.196078 0.8 1))
(defconst darkred (Color 0.545098 0 0 1))
(defconst darksalmon (Color 0.913725 0.588235 0.478431 1))
(defconst darkseagreen (Color 0.560784 0.737255 0.560784 1))
(defconst darkslateblue (Color 0.282353 0.239216 0.545098 1))
(defconst darkslategray (Color 0.184314 0.309804 0.309804 1))
(defconst darkturquoise (Color 0 0.807843 0.819608 1))
(defconst darkviolet (Color 0.580392 0 0.827451 1))
(defconst deeppink (Color 1 0.0784314 0.576471 1))
(defconst deepskyblue (Color 0 0.74902 1 1))
(defconst dimgray (Color 0.411765 0.411765 0.411765 1))
(defconst dodgerblue (Color 0.117647 0.564706 1 1))
(defconst firebrick (Color 0.698039 0.133333 0.133333 1))
(defconst floralwhite (Color 1 0.980392 0.941176 1))
(defconst forestgreen (Color 0.133333 0.545098 0.133333 1))
(defconst fuchsia (Color 1 0 1 1))
(defconst gainsboro (Color 0.862745 0.862745 0.862745 1))
(defconst ghostwhite (Color 0.972549 0.972549 1 1))
(defconst gold (Color 1 0.843137 0 1))
(defconst goldenrod (Color 0.854902 0.647059 0.12549 1))
(defconst gray (Color 0.745098 0.745098 0.745098 1))
(defconst green (Color 0 1 0 1))
(defconst greenyellow (Color 0.678431 1 0.184314 1))
(defconst honeydew (Color 0.941176 1 0.941176 1))
(defconst hotpink (Color 1 0.411765 0.705882 1))
(defconst indianred (Color 0.803922 0.360784 0.360784 1))
(defconst indigo (Color 0.294118 0 0.509804 1))
(defconst ivory (Color 1 1 0.941176 1))
(defconst khaki (Color 0.941176 0.901961 0.54902 1))
(defconst lavender (Color 0.901961 0.901961 0.980392 1))
(defconst lavenderblush (Color 1 0.941176 0.960784 1))
(defconst lawngreen (Color 0.486275 0.988235 0 1))
(defconst lemonchiffon (Color 1 0.980392 0.803922 1))
(defconst lightblue (Color 0.678431 0.847059 0.901961 1))
(defconst lightcoral (Color 0.941176 0.501961 0.501961 1))
(defconst lightcyan (Color 0.878431 1 1 1))
(defconst lightgoldenrod (Color 0.980392 0.980392 0.823529 1))
(defconst lightgray (Color 0.827451 0.827451 0.827451 1))
(defconst lightgreen (Color 0.564706 0.933333 0.564706 1))
(defconst lightpink (Color 1 0.713726 0.756863 1))
(defconst lightsalmon (Color 1 0.627451 0.478431 1))
(defconst lightseagreen (Color 0.12549 0.698039 0.666667 1))
(defconst lightskyblue (Color 0.529412 0.807843 0.980392 1))
(defconst lightslategray (Color 0.466667 0.533333 0.6 1))
(defconst lightsteelblue (Color 0.690196 0.768627 0.870588 1))
(defconst lightyellow (Color 1 1 0.878431 1))
(defconst lime (Color 0 1 0 1))
(defconst limegreen (Color 0.196078 0.803922 0.196078 1))
(defconst linen (Color 0.980392 0.941176 0.901961 1))
(defconst magenta (Color 1 0 1 1))
(defconst maroon (Color 0.690196 0.188235 0.376471 1))
(defconst mediumaquamarine (Color 0.4 0.803922 0.666667 1))
(defconst mediumblue (Color 0 0 0.803922 1))
(defconst mediumorchid (Color 0.729412 0.333333 0.827451 1))
(defconst mediumpurple (Color 0.576471 0.439216 0.858824 1))
(defconst mediumseagreen (Color 0.235294 0.701961 0.443137 1))
(defconst mediumslateblue (Color 0.482353 0.407843 0.933333 1))
(defconst mediumspringgreen (Color 0 0.980392 0.603922 1))
(defconst mediumturquoise (Color 0.282353 0.819608 0.8 1))
(defconst mediumvioletred (Color 0.780392 0.0823529 0.521569 1))
(defconst midnightblue (Color 0.0980392 0.0980392 0.439216 1))
(defconst mintcream (Color 0.960784 1 0.980392 1))
(defconst mistyrose (Color 1 0.894118 0.882353 1))
(defconst moccasin (Color 1 0.894118 0.709804 1))
(defconst navajowhite (Color 1 0.870588 0.678431 1))
(defconst navyblue (Color 0 0 0.501961 1))
(defconst oldlace (Color 0.992157 0.960784 0.901961 1))
(defconst olive (Color 0.501961 0.501961 0 1))
(defconst olivedrab (Color 0.419608 0.556863 0.137255 1))
(defconst orange (Color 1 0.647059 0 1))
(defconst orangered (Color 1 0.270588 0 1))
(defconst orchid (Color 0.854902 0.439216 0.839216 1))
(defconst palegoldenrod (Color 0.933333 0.909804 0.666667 1))
(defconst palegreen (Color 0.596078 0.984314 0.596078 1))
(defconst paleturquoise (Color 0.686275 0.933333 0.933333 1))
(defconst palevioletred (Color 0.858824 0.439216 0.576471 1))
(defconst papayawhip (Color 1 0.937255 0.835294 1))
(defconst peachpuff (Color 1 0.854902 0.72549 1))
(defconst peru (Color 0.803922 0.521569 0.247059 1))
(defconst pink (Color 1 0.752941 0.796078 1))
(defconst plum (Color 0.866667 0.627451 0.866667 1))
(defconst powderblue (Color 0.690196 0.878431 0.901961 1))
(defconst purple (Color 0.627451 0.12549 0.941176 1))
(defconst rebeccapurple (Color 0.4 0.2 0.6 1))
(defconst red (Color 1 0 0 1))
(defconst rosybrown (Color 0.737255 0.560784 0.560784 1))
(defconst royalblue (Color 0.254902 0.411765 0.882353 1))
(defconst saddlebrown (Color 0.545098 0.270588 0.0745098 1))
(defconst salmon (Color 0.980392 0.501961 0.447059 1))
(defconst sandybrown (Color 0.956863 0.643137 0.376471 1))
(defconst seagreen (Color 0.180392 0.545098 0.341176 1))
(defconst seashell (Color 1 0.960784 0.933333 1))
(defconst sienna (Color 0.627451 0.321569 0.176471 1))
(defconst silver (Color 0.752941 0.752941 0.752941 1))
(defconst skyblue (Color 0.529412 0.807843 0.921569 1))
(defconst slateblue (Color 0.415686 0.352941 0.803922 1))
(defconst slategray (Color 0.439216 0.501961 0.564706 1))
(defconst snow (Color 1 0.980392 0.980392 1))
(defconst springgreen (Color 0 1 0.498039 1))
(defconst steelblue (Color 0.27451 0.509804 0.705882 1))
(defconst tan (Color 0.823529 0.705882 0.54902 1))
(defconst teal (Color 0 0.501961 0.501961 1))
(defconst thistle (Color 0.847059 0.74902 0.847059 1))
(defconst tomato (Color 1 0.388235 0.278431 1))
(defconst transparent (Color 1 1 1 0))
(defconst turquoise (Color 0.25098 0.878431 0.815686 1))
(defconst violet (Color 0.933333 0.509804 0.933333 1))
(defconst webgray (Color 0.501961 0.501961 0.501961 1))
(defconst webgreen (Color 0 0.501961 0 1))
(defconst webmaroon (Color 0.501961 0 0 1))
(defconst webpurple (Color 0.501961 0 0.501961 1))
(defconst wheat (Color 0.960784 0.870588 0.701961 1))
(defconst white (Color 1 1 1 1))
(defconst whitesmoke (Color 0.960784 0.960784 0.960784 1))
(defconst yellow (Color 1 1 0 1))
(defconst yellowgreen (Color 0.603922 0.803922 0.196078 1))
(defn _init ()
(super TYPE_COLOR)))
;; Named types like _Engine whose name can be returned from get_class
;; but which do not exist in the runtime namespace exposed to
;; GDScript.
(defclass NamedSyntheticType (GDLispSpecialType) private
(defvar name)
(defn _init (@name))
(defn satisfies? (value)
(= (value:get-class) self:name)))
;; Note: All of these synthetic types would theoretically be defobject
;; if we weren't writing them in the standard library. But for
;; complicated reasons, we can't use macro expansion at all in stdlib,
;; and defobject is behind three layers of macro (defobject and
;; deflazy, and the expansion includes define-symbol-macro), so it's
;; off-limits.
(defclass AnyType (GDLispSpecialType) private
(defn satisfies? (_value)
#t))
(defclass AnyRefType (GDLispSpecialType) private
(defn satisfies? (value)
(= ((literally typeof) value) TYPE_OBJECT)))
(defclass AnyValType (GDLispSpecialType) private
(defn satisfies? (value)
(/= ((literally typeof) value) TYPE_OBJECT)))
(defclass NumberType (GDLispSpecialType) private
(defn satisfies? (value)
(let ((t ((literally typeof) value)))
(cond
((= t TYPE_INT) #t)
((= t TYPE_REAL) #t)
(#t #f)))))
(defclass BaseArrayType (GDLispSpecialType) private
(defn satisfies? (value)
(<= TYPE_ARRAY ((literally typeof) value) TYPE_COLOR_ARRAY)))
(defclass NothingType (GDLispSpecialType) private
(defn satisfies? (_value)
#f))
(sys/declare value Any public)
(sys/declare value AnyRef public)
(sys/declare value AnyVal public)
(sys/declare value Number public)
(sys/declare value BaseArray public)
(sys/declare value Nothing public)
(sys/declare value Null public)
(sys/declare value Bool public)
(sys/declare value Int public)
(sys/declare value Float public)
(sys/declare value String public)
(sys/declare value Vector2 public)
(sys/declare value Rect2 public)
(sys/declare value Vector3 public)
(sys/declare value Transform2D public)
(sys/declare value Plane public)
(sys/declare value Quat public)
(sys/declare value AABB public)
(sys/declare value Basis public)
(sys/declare value Transform public)
(sys/declare value Color public)
(sys/declare value NodePath public)
(sys/declare value RID public)
(sys/declare value Object public)
(sys/declare value Dictionary public)
(sys/declare value Array public)
(sys/declare value PoolByteArray public)
(sys/declare value PoolIntArray public)
(sys/declare value PoolRealArray public)
(sys/declare value PoolStringArray public)
(sys/declare value PoolVector2Array public)
(sys/declare value PoolVector3Array public)
(sys/declare value PoolColorArray public)
;;; Polymorphic functions
(defn len (x)
(cond
((= x nil) 0)
((sys/instance-direct? x Cons)
(let ((result 0))
(while (sys/instance-direct? x Cons)
(set result (+ result 1))
(set x x:cdr))
result))
(#t ((literally len) x))))
;;; List functions
(defn list (&rest args)
(sys/call-magic LIST)
;; The argument list may be shared with some other code, but `list`
;; should always allocate a new list, so defensively copy the
;; argument list.
(list/copy args))
(defn list/copy (xs)
(list/map (lambda (x) x) xs))
(defn cons (a b)
(Cons:new a b))
(defn snoc (a b)
(append a (list b)))
(defn init (a)
(cond
((sys/instance-direct? a:cdr Cons) (cons a:car (init a:cdr)))
(#t nil)))
(defn last (a)
(cond
((sys/instance-direct? a:cdr Cons) (last a:cdr))
(#t a:car)))
(defn list->array (list)
(let ((arr []))
(while (sys/instance-direct? list Cons)
(arr:push_back list:car)
(set list list:cdr))
arr))
(defn array->list (arr)
(let ((outer (cons () ())))
(let ((curr outer))
(for elem arr
(set curr:cdr (cons elem ()))
(set curr curr:cdr))
outer:cdr)))
(defn list/elt (list n)
(list/tail list n):car)
(defn list/fold (f xs &opt x)
(cond
((= x nil)
(set x xs:car)
(set xs xs:cdr)))
(while (/= xs nil)
(set x (funcall f x xs:car))
(set xs xs:cdr))
x)
(defn list/map (f xs)
(let ((outer (cons nil nil)))
(let ((curr outer))
(while (/= xs nil)
(set curr:cdr (cons (funcall f xs:car) nil))
(set curr curr:cdr)
(set xs xs:cdr))
outer:cdr)))
(defn list/filter (p xs)
(let ((outer (cons nil nil)))
(let ((curr outer))
(while (/= xs nil)
(cond
((funcall p xs:car)
(set curr:cdr (cons xs:car nil))
(set curr curr:cdr)))
(set xs xs:cdr))
outer:cdr)))
(defn list/find (f xs &opt default)
(while (/= xs nil)
(cond
((funcall f xs:car) (return xs:car)))
(set xs xs:cdr))
default)
(defn list/reverse (arg)
(let ((rev nil))
(while (/= arg nil)
(set rev `(,arg:car . ,rev))
(set arg arg:cdr))
rev))
(defn append (&rest args)
(cond
((= args nil) nil)
(#t
(let ((outer (cons nil nil)))
(let ((curr outer))
(while (/= args:cdr nil)
(let ((inner-value args:car))
(while (/= inner-value nil)
(set curr:cdr (cons inner-value:car nil))
(set curr curr:cdr)
(set inner-value inner-value:cdr)))
(set args args:cdr))
(set curr:cdr args:car)
outer:cdr)))))
(defn list/tail (list k)
(for _i (range k)
(set list list:cdr))
list)
(defn sys/qq-smart-list (a)
(cond
((instance? a GDLisp:BaseArray) (array->list a))
(#t a)))
;;; Array functions
(defn elt (arr n)
(sys/call-magic ARRAY-SUBSCRIPT)
(elt arr n))
(defn set-elt (x arr n)
(sys/call-magic ARRAY-SUBSCRIPT-ASSIGNMENT)
(set-elt x arr n))
(defn member? (value arr)
(sys/call-magic ARRAY-MEMBER-CHECK)
(member? value arr))
(defn array/fold (f xs &opt x)
(let ((starting-index 0))
(cond
((= x nil)
(set x (elt xs 0))
(set starting-index 1)))
(for i (range starting-index (len xs))
(set x (funcall f x (elt xs i))))
x))
(defn array/map (f xs)
(let ((result []))
(for i (len xs)
(result:push_back (funcall f (elt xs i))))
result))
(defn array/filter (p xs)
(let ((result []))
(for i (len xs)
(cond
((funcall p (elt xs i))
(result:push_back (elt xs i)))))
result))
(defn array/reverse (arr)
(let ((len (len arr))
(result (arr:duplicate)))
(for i (range len)
(set (elt result i) (elt arr (- len i 1))))
result))
(defn array/find (f arr &opt default)
(for elem arr
(cond
((funcall f elem) (return elem))))
default)
;;; Higher-order functions
(defn funcall (f &rest args)
(apply f args))
;; funcall alias used in IIFEs. The user can shadow funcall (though
;; it's a bad idea), but shadowing names in sys/ is undefined behavior.
(defn sys/funcall (f &rest args)
(apply f args))
(defn apply (f &rest args)
(let ((args1 (init args))
(args2 (last args)))
(cond
((sys/instance-direct? f Function) (f:call_funcv (append args1 args2)))
(#t (push-error "Attempt to call non-function")))))
;;; Array functions
(defn array/concat (&rest arrays)
(cond
((= (len arrays) 0) [])
(#t (apply #'+ arrays))))
;;; Vector functions
(defn vector/map (f arg &rest args)
(flet ((-x (v) v:x)
(-y (v) v:y)
(-z (v) v:z))
(let ((args (cons arg args)))
(cond
((instance? arg GDLisp:Vector2) (Vector2 (apply f (list/map #'-x args))
(apply f (list/map #'-y args))))
((instance? arg GDLisp:Vector3) (Vector3 (apply f (list/map #'-x args))
(apply f (list/map #'-y args))
(apply f (list/map #'-z args))))
(#t (push-error "Attempted vector/map on non-vector"))))))
;;; Node functions
(defclass SignalAdaptor (Reference) private
(defvar function)
(defn _init (@function))
(defn invoke (a b c d e f) sys/nullargs
;; Identify the first non-null argument.
(let ((arglist (cond
((/= f ()) (list a b c d e f))
((/= e ()) (list a b c d e))
((/= d ()) (list a b c d))
((/= c ()) (list a b c))
((/= b ()) (list a b))
((/= a ()) (list a))
(#t (list)))))
(apply @function arglist))))
(defn get-signals-meta (obj) private
(cond
((obj:has-meta "__gdlisp_signals") (obj:get-meta "__gdlisp_signals"))
(#t (let ((new-meta {"__key" 0})) (obj:set-meta "__gdlisp_signals" new-meta) new-meta))))
(defn connect>> (obj signal-name function)
(let ((function (SignalAdaptor:new function)))
(obj:connect signal-name function "invoke")
(let ((meta (get-signals-meta obj)))
(let ((key (dict/elt meta "__key")))
(set (dict/elt meta key) function)
(set (dict/elt meta "__key") (+ (dict/elt meta "__key") 1))
key))))
(defn connect1>> (obj signal-name function)
(let ((key nil)
(weakref (weakref obj)))
(flet ((oneshot-function (&rest args)
(apply function args)
(let ((obj (weakref:get-ref)))
(cond
(obj (disconnect>> obj signal-name key))))))
(set key (connect>> obj signal-name #'oneshot-function)))))
(defn disconnect>> (obj signal-name index)
(let ((meta (get-signals-meta obj)))
(meta:erase index)))
;;; Miscellaneous simple functions
(defn bool (x)
;; Like the GDScript primitive but works for all types, not just
;; strings and numbers.
(cond
(x #t)
(#t #f)))
(defn vector (x y &opt z)
(sys/call-magic VECTOR)
(cond
((= z ()) V{x y})
(#t V{x y z})))
(defn array (&arr xs)
(sys/call-magic ARRAY)
xs)
(defn dict (&arr xs)
(sys/call-magic DICT)
(let ((resulting-dict {}))
(for i (range 0 (len xs) 2)
(let ((k (elt xs i))
(v (elt xs (+ i 1))))
(set (elt resulting-dict k) v)))
resulting-dict))
(defn NodePath (s)
(sys/call-magic NODEPATH-SYNTAX)
((literally NodePath) s))
(defn not (x)
(sys/call-magic BOOLEAN-NOT)
(not x))
(defn intern (a)
(cond
((member? a GDLisp:__gdlisp_Global_symbol_table) (elt GDLisp:__gdlisp_Global_symbol_table a))
(#t (set (elt GDLisp:__gdlisp_Global_symbol_table a) (Symbol:new a)))))
(defn gensym (&opt prefix)
(cond
((= prefix ()) (Symbol:new (GDLisp:__gdlisp_Global_name_generator:generate)))
(#t (Symbol:new (GDLisp:__gdlisp_Global_name_generator:generate_with prefix)))))
(defn sys/get-node (obj path)
(sys/call-magic GET-NODE-SYNTAX)
(obj:get-node path))
(defn instance? (value type)
(cond
((sys/instance-direct? type GDLispSpecialType) (type:satisfies? value))
(#t (sys/instance-direct? value type))))
(defn sys/instance-direct? (value type)
(sys/call-magic DIRECT-INSTANCE-CHECK)
(sys/instance-direct? value type))
(sys/declare function typeof (value) public)
(defn convert (what type)
(cond
((sys/instance-direct? type PrimitiveType) ((literally convert) what type:primitive-value))
(#t ((literally convert) what type))))
(defn dict/elt (dict n)
(sys/call-magic DICT-SUBSCRIPT)
(dict/elt dict n))
(defn set-dict/elt (x dict n)
(sys/call-magic DICT-SUBSCRIPT-ASSIGNMENT)
(set-dict/elt x dict n))
(defn dict/find (f dict &opt default)
(for key dict
(cond
((funcall f key (dict/elt dict key)) (return key))))
default)
;;; Math operators
(defn + (&rest args)
(sys/call-magic ADDITION)
(cond
((sys/instance-direct? args Cons)
(let ((result args:car))
(set args args:cdr)
(while (sys/instance-direct? args Cons)
(set result (+ result args:car))
(set args args:cdr))
result))
(#t 0)))
(defn * (&rest args)
(sys/call-magic MULTIPLICATION)
(let ((result 1))
(while (sys/instance-direct? args Cons)
(set result (* result args:car))
(set args args:cdr))
result))
(defn - (x &rest args)
(sys/call-magic SUBTRACTION)
(cond
((sys/instance-direct? args Cons)
(let ((result x))
(while (sys/instance-direct? args Cons)
(set result (- result args:car))
(set args args:cdr))
result))
(#t (- x))))
(defn / (x &rest args)
(sys/call-magic DIVISION)
(cond
((sys/instance-direct? args Cons)
(let ((result x))
(while (sys/instance-direct? args Cons)
(set result (/ result args:car))
(set args args:cdr))
result))
(#t (/ x))))
(defn mod (x y)
(sys/call-magic MODULO)
(mod x y))
(defn min (&rest args)
(sys/call-magic MIN-FUNCTION)
(cond
((sys/instance-direct? args Cons)
(let ((result args:car))
(set args args:cdr)
(while (sys/instance-direct? args Cons)
(set result (min result args:car))
(set args args:cdr))
result))
(#t (literally INF))))
(defn max (&rest args)
(sys/call-magic MAX-FUNCTION)
(cond
((sys/instance-direct? args Cons)
(let ((result args:car))
(set args args:cdr)
(while (sys/instance-direct? args Cons)
(set result (max result args:car))
(set args args:cdr))
result))
(#t (- (literally INF)))))
(defn gcd (&rest args)
(list/fold #'binary-gcd args 0))
(defn lcm (&rest args)
(list/fold #'binary-lcm args 1))
(defn binary-gcd (a b) private
(while (/= b 0)
(let ((tmp a))
(set a b)
(set b (mod tmp b))))
a)
(defn binary-lcm (a b) private
(/ (* a b) (binary-gcd a b)))
;;; Comparison operators