Repository navigation
Expand file tree
/
Copy pathbootstrap.pear
More file actions
3489 lines (3373 loc) · 122 KB
/
Copy pathbootstrap.pear
File metadata and controls
3489 lines (3373 loc) · 122 KB
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
(:
# "Nil is used whenever we don't actually want to provide a useful value.
Functions that only have side-effects (aka they may crash or hang) return nil.
See the assert function for an example."
nil (&)
# "Crashing stuff"
crash (\ (error) (@crash error))
assert (\ (check) (if check nil (crash "Assert failed.")))
rec (\ (input function)
(:
rec (\ (rec input) (function (\ (new) (rec rec new)) input))
(rec rec input)))
rec-2 (\ (a b function)
(:
rec (\ (rec a b) (function (\ (a b) (rec rec a b)) a b))
(rec rec a b)))
rec-3 (\ (a b c function)
(:
rec (\ (rec a b c) (function (\ (a b c) (rec rec a b c)) a b c))
(rec rec a b c)))
rec-4 (\ (a b c d function)
(:
rec (\ (rec a b c d) (function (\ (a b c d) (rec rec a b c d)) a b c d))
(rec rec a b c d)))
true (| true)
false (| false)
_ (assert true)
else true
not (\ (a) (if a false true))
and (\ (a b) (if a b false))
and-3 (\ (a b c) (and (and a b) c))
and-4 (\ (a b c d) (and (and-3 a b c) d))
and-5 (\ (a b c d e) (and (and-4 a b c d) e))
or (\ (a b) (if a true b))
or-3 (\ (a b c) (or (or a b) c))
or-4 (\ (a b c d) (or (or-3 a b c) d))
or-5 (\ (a b c d e) (or (or-4 a b c d) e))
+ (\ (a b) (@add a b))
- (\ (a b) (@subtract a b))
* (\ (a b) (@multiply a b))
/ (\ (a b) (@divide a b))
mod (\ (a b) (@modulo a b))
<< (\ (a b) (@shift-left a b))
>> (\ (a b) (@shift-right a b))
bit-and (\ (a b) (@and a b))
bit-or (\ (a b) (@or a b))
bit-xor (\ (a b) (@xor a b))
compare (\ (a b) (@compare a b))
= (\ (a b) (@equal? a b))
!= (\ (a b) (not (@equal? a b)))
< (\ (a b) (% (compare a b) less true _ false))
> (\ (a b) (% (compare a b) greater true _ false))
<= (\ (a b) (% (compare a b) greater false _ true))
>= (\ (a b) (% (compare a b) less false _ true))
max (\ (a b) (if (> a b) a b))
min (\ (a b) (if (< a b) a b))
abs (\ (n) (if (< n 0) (- 0 n) n))
f+ (\ (a b) (@f-add a b))
f- (\ (a b) (@f-subtract a b))
f* (\ (a b) (@f-multiply a b))
f/ (\ (a b) (@f-divide a b))
f-compare (\ (a b) (@f-compare a b))
f= (\ (a b) (% (f-compare a b) equal true _ false))
f!= (\ (a b) (% (f-compare a b) equal false _ true))
f< (\ (a b) (% (f-compare a b) less true _ false))
f> (\ (a b) (% (f-compare a b) greater true _ false))
f<= (\ (a b) (% (f-compare a b) greater false _ true))
f>= (\ (a b) (% (f-compare a b) less false _ true))
f-max (\ (a b) (if (f> a b) a b))
f-min (\ (a b) (if (f< a b) a b))
int->float (\ (n) (@int->float n))
float->int (\ (f) (@float->int f))
f-abs (\ (n) (if (f< n 0.) (f- 0. n) n))
even? (\ (n) (= (mod n 2) 0))
odd? (\ (n) (= (mod n 2) 1))
array-length (\ (array) (@array-length array))
array-get (\ (array index) (@array-get array index))
array->list (\ (array) (@array->list array))
list->array (\ (list) (@list->array list))
list-length (\ (list)
(rec list (\ (rec list) (% list empty 0 (more m) (+ (rec (.rest m)) 1)))))
# "Iterators. An iterator is a function that, when called, returns either
(| empty) or (| more (& item rest))."
list->iter (\ (list)
(rec list (\ (rec list)
(\ () (% list
empty (| empty)
(more m) (| more (& item (.item m) rest (rec (.rest m)))))))))
array->iter (\ (array)
(rec 0 (\ (rec i)
(\ () (if (= i (array-length array))
(| empty)
(| more (& item (array-get array i) rest (rec (+ i 1)))))))))
iter->list (\ (iter)
(rec iter (\ (rec iter)
(% (iter)
empty (| empty)
(more m) (| more (& item (.item m) rest (rec (.rest m))))))))
iter->array (\ (iter) (list->array (iter->list iter)))
map (\ (iter mapper)
(rec iter (\ (rec iter)
(\ () (% (iter)
empty (| empty)
(more m) (| more (& item (mapper (.item m)) rest (rec (.rest m)))))))))
map-indexed (\ (iter mapper)
(rec-2 iter 0 (\ (rec iter index)
(\ () (% (iter)
empty (| empty)
(more m)
(| more
(& item (mapper index (.item m)) rest (rec (.rest m) (+ index 1)))))))))
filter (\ (iter checker)
(rec iter (\ (next iter)
(\ () (rec iter (\ (seek iter)
(% (iter)
empty (| empty)
(more m)
(if (checker (.item m))
(| more (& item (.item m) rest (next (.rest m))))
(seek (.rest m))))))))))
take (\ (iter n)
(rec-2 iter n (\ (rec iter n)
(\ () (if (= n 0)
(| empty)
(% (iter)
empty (| empty)
(more m) (| more (& item (.item m) rest (rec (.rest m) (- n 1))))))))))
skip (\ (iter n)
(rec-2 iter n (\ (rec iter n)
(\ () (if (= n 0)
(iter)
(% (iter)
empty (| empty)
(more m) ((rec (.rest m) (- n 1)))))))))
take-while (\ (iter checker)
(rec iter (\ (rec iter)
(\ () (% (iter)
empty (| empty)
(more m)
(if (checker (.item m))
(| more (& item (.item m) rest (rec (.rest m))))
(| empty)))))))
skip-while (\ (iter checker)
(\ () (rec iter (\ (drop iter)
(% (iter)
empty (| empty)
(more m)
(if (checker (.item m))
(drop (.rest m))
(| more (& item (.item m) rest (.rest m)))))))))
generate (\ (length generator)
(rec 0 (\ (rec i)
(\ () (if (= i length)
(| empty)
(| more (& item (generator i) rest (rec (+ i 1)))))))))
fold (\ (iter initial reducer)
(rec-2 initial iter (\ (rec state iter)
(% (iter)
empty state
(more m) (rec (reducer state (.item m)) (.rest m))))))
reduce (\ (iter reducer)
(% (iter)
empty (crash "called reduce on empty iterator")
(more m) (fold (.rest m) (.item m) reducer)))
concat (\ (a b)
(rec a (\ (rec a)
(\ () (% (a)
empty (b)
(more m) (| more (& item (.item m) rest (rec (.rest m)))))))))
concat-3 (\ (a b c) (concat a (concat b c)))
concat-4 (\ (a b c d) (concat a (concat-3 b c d)))
concat-5 (\ (a b c d e) (concat a (concat-4 b c d e)))
concat-all (\ (iters)
(fold iters (array->iter ([])) (\ (acc it) (concat acc it))))
length (\ (iter) (fold iter 0 (\ (n it) (+ n 1))))
any (\ (iter checker)
(rec iter (\ (rec iter)
(% (iter)
empty false
(more m) (if (checker (.item m)) true (rec (.rest m)))))))
find (\ (iter checker)
(rec iter (\ (rec iter)
(% (iter)
empty (| none)
(more m) (if (checker (.item m)) (| some (.item m)) (rec (.rest m)))))))
find-index (\ (iter checker)
(rec-2 iter 0 (\ (rec iter index)
(% (iter)
empty (| none)
(more m)
(if (checker (.item m)) (| some index) (rec (.rest m) (+ index 1)))))))
all (\ (iter checker) (not (any iter (\ (x) (not (checker x))))))
_ (assert (= (iter->array (array->iter ([] 1 2 3))) ([] 1 2 3)))
_ (assert (= (iter->array (array->iter ([]))) ([])))
_ (assert (= (iter->array (take (array->iter ([] 1 2 3 4)) 2)) ([] 1 2)))
_ (assert (= (iter->array (skip (array->iter ([] 1 2 3 4)) 2)) ([] 3 4)))
_ (assert (= (iter->array (generate 3 (\ (i) (* i i)))) ([] 0 1 4)))
_ (assert (= (fold (array->iter ([] 1 2 3)) 0 +) 6))
_ (assert (= (reduce (array->iter ([] 1 2 3)) +) 6))
_ (assert (= (length (array->iter ([] 1 2 3))) 3))
_ (assert (any (array->iter ([] 1 2 3)) (\ (x) (= x 2))))
_ (assert (not (any (array->iter ([] 1 2 3)) (\ (x) (= x 9)))))
_ (assert (= (find (array->iter ([] 1 2 3)) (\ (x) (> x 1))) (| some 2)))
_ (assert (= (find-index (array->iter ([] 1 2 3)) (\ (x) (= x 3))) (| some 2)))
_ (assert (= (find-index (array->iter ([] 1 2 3)) (\ (x) (= x 9))) (| none)))
filled-list (\ (length item)
(:
_ (assert (< length 100))
rec (\ (rec i)
(if (= i length) (| empty) (| more (& item item rest (rec rec (+ i 1))))))
(rec rec 0)))
filled-array (\ (length item) (list->array (filled-list length item)))
list-skip (\ (list n) (iter->list (skip (list->iter list) n)))
array-skip (\ (array n) (iter->array (skip (array->iter array) n)))
list-take (\ (list n) (iter->list (take (list->iter list) n)))
array-take (\ (array n) (iter->array (take (array->iter array) n)))
list-slice (\ (list from to)
(iter->list (take (skip (list->iter list) from) (- to from))))
array-slice (\ (array from to)
(iter->array (take (skip (array->iter array) from) (- to from))))
list-update (\ (list index item)
(iter->list
(map-indexed (list->iter list) (\ (i it) (if (= i index) item it)))))
array-update (\ (array index item)
(iter->array
(map-indexed (array->iter array) (\ (i it) (if (= i index) item it)))))
list-remove (\ (list index)
(iter->list
(concat
(take (list->iter list) index)
(skip (list->iter list) (+ index 1)))))
array-remove (\ (array index)
(iter->array
(concat
(take (array->iter array) index)
(skip (array->iter array) (+ index 1)))))
list-insert (\ (list index item)
(iter->list
(concat-3
(take (list->iter list) index)
(list->iter (| more (& item item rest (| empty))))
(skip (list->iter list) index))))
array-insert (\ (array index item)
(iter->array
(concat-3
(take (array->iter array) index)
(list->iter (| more (& item item rest (| empty))))
(skip (array->iter array) index))))
list-append (\ (list item) (list-insert list (list-length list) item))
array-append (\ (array item) (array-insert array (array-length array) item))
list-get (\ (list index)
(% ((skip (list->iter list) index)) (more m) (.item m)))
list-swap (\ (list a b)
(iter->list
(map-indexed
(list->iter list)
(\ (i it)
(if (= i a) (list-get list b) (if (= i b) (list-get list a) it))))))
array-swap (\ (array a b)
(iter->array
(map-indexed
(array->iter array)
(\ (i it)
(if (= i a) (array-get array b) (if (= i b) (array-get array a) it))))))
list-reverse (\ (list)
(rec-2 list (| empty) (\ (rec to-reverse reversed)
(% to-reverse
empty reversed
(more m) (rec (.rest m) (| more (& item (.item m) rest reversed)))))))
array-reverse (\ (array) (list->array (list-reverse (array->list array))))
list-take-and-reverse (\ (list n)
(rec-2 (& list list n n) (| empty) (\ (rec args reversed)
(:
list (.list args)
n (.n args)
(if (= n 0)
reversed
(% list
(more m)
(rec
(& list (.rest m) n (- n 1))
(| more (& item (.item m) rest reversed)))))))))
_ (assert (= (list-get (array->list ([] 5 6 7)) 1) 6))
_ (assert (= (list->array (list-skip (array->list ([] 1 2 3 4)) 2)) ([] 3 4)))
_ (assert (= (list->array (list-take (array->list ([] 1 2 3 4)) 2)) ([] 1 2)))
_ (assert (= (list->array (list-slice (array->list ([] 1 2 3 4 5)) 1 4)) ([] 2 3 4)))
_ (assert (= (list->array (list-update (array->list ([] 1 2 3)) 1 9)) ([] 1 9 3)))
_ (assert (= (list->array (list-remove (array->list ([] 1 2 3)) 1)) ([] 1 3)))
_ (assert (= (list->array (list-insert (array->list ([] 1 2 3)) 1 9)) ([] 1 9 2 3)))
_ (assert (= (list->array (list-swap (array->list ([] 1 2 3)) 0 2)) ([] 3 2 1)))
_ (assert (= (array-update ([] 1 2 3) 1 9) ([] 1 9 3)))
_ (assert (= (array-remove ([] 1 2 3) 1) ([] 1 3)))
_ (assert (= (array-insert ([] 1 2 3) 1 9) ([] 1 9 2 3)))
_ (assert (= (array-swap ([] 1 2 3) 0 2) ([] 3 2 1)))
_ (assert (= (array-slice ([] 1 2 3 4 5) 1 4) ([] 2 3 4)))
_ (assert (= (array-reverse ([] 1 2 3)) ([] 3 2 1)))
string->bytes (\ (string) (@string->bytes string))
bytes->string (\ (bytes) (@bytes->string bytes))
string-length (\ (string) (@array-length (string->bytes string)))
string-concat (\ (a b)
(bytes->string
(iter->array
(concat
(array->iter (string->bytes a))
(array->iter (string->bytes b))))))
string-concat-3 (\ (a b c) (string-concat a (string-concat b c)))
string-concat-4 (\ (a b c d) (string-concat a (string-concat-3 b c d)))
string-concat-5 (\ (a b c d e) (string-concat a (string-concat-4 b c d e)))
string-slice (\ (string from to)
(bytes->string (array-slice (string->bytes string) from to)))
string-replace-range (\ (string range replacement)
(string-concat-3
(string-slice string 0 (.start range))
replacement
(string-slice string (.end range) (string-length string))))
string-remove-range (\ (string start end)
(string-concat
(string-slice string 0 start)
(string-slice string end (string-length string))))
string-remove (\ (string index)
(string-concat
(string-slice string 0 index)
(string-slice string (+ index 1) (string-length string))))
string-insert (\ (string index text)
(string-concat-3
(string-slice string 0 index)
text
(string-slice string index (string-length string))))
string-starts-with (\ (string prefix)
(if (> (string-length prefix) (string-length string))
false
(= (string-slice string 0 (string-length prefix)) prefix)))
ascii-char (\ (literal) (array-get (string->bytes literal) 0))
int->string (\ (int)
(if (= int 0)
"0"
(rec-2 int (| empty) (\ (rec int prev)
(if (= 0 int)
(bytes->string (list->array prev))
(rec
(/ int 10)
(| more (& item (+ (ascii-char "0") (mod int 10)) rest prev))))))))
bytes->chars (\ (rec bytes i)
(if (= i (array-length bytes))
(| empty)
(| more (&
item (@bytes->string ([] (array-get bytes i)))
rest (rec rec bytes (+ i 1))))))
bytes->chars-list (\ (bytes) (bytes->chars bytes->chars bytes 0))
string->chars-list (\ (string) (bytes->chars-list (string->bytes string)))
string->chars-array (\ (string) (list->array (string->chars-list string)))
value? (\ (node) (% node value true _ false))
bind (\ (rec mode input name value)
(% mode
expr
(% input
value input
(name n) (if (@equal? name n) value (| name n))
(let l)
(| let
(&
name (.name l)
definition (rec rec (| expr) (.definition l) name value)
body (rec rec (| expr) (.body l) name value)))
(field f)
(| field (& of (rec rec (| expr) (.of f) name value) name (.name f)))
(call c)
(| call
(&
function (rec rec (| expr) (.function c) name value)
arguments (rec rec (| array) (.arguments c) name value)))
_ (crash (| todo-bind input))
)
array (list->array (rec rec (| list) (array->list input) name value))
list
(% input
empty (| empty)
(more m)
(| more
(&
item (rec rec (| expr) (.item m) name value)
rest (rec rec (| list) (.rest m) name value))))
_ (crash (| unknown-bind-mode mode)))
)
bind (\ (input name value) (bind bind (| expr) input name value))
optimize (\ (rec mode input)
(% mode
expr
(% input
value input
(let let)
(:
name (.name let)
definition (rec rec (| expr) (.definition let))
(% definition
(value v) (rec rec (| expr) (bind (.body let) name (| value v)))
_ (:
body (rec rec (| expr) (.body let))
(| let (& name name definition definition body body))
)
)
)
(add args)
(:
left (rec rec (| expr) (.left args))
right (rec rec (| expr) (.right args))
(% left
(value l)
(% right
(value r) (| value (@add l r))
_ (| add (& left left right right))
)
_ (| add (& left left right right))))
_ input
)
fun
(@function
(@arguments input)
(rec
rec
(| expr)
(| let
(&
name "@captured"
definition (| value (@captured input))
body (@body input))))
nil)
))
optimize (\ (function) (optimize optimize (| fun) function))
##### "The Render System
When Pear code is asked to render something for the outside world, it returns
drawing instructions. These are just normal Pear values.
DrawingInstruction =
| nothing
| fill-path (& path Path color Color)
| transform (& by TransformationMatrix what DrawingInstruction)
| all ([] DrawingInstruction)
Path = ([] PathSegment)
PathSegment =
| move-to Point
| line-to Point
Point = (& x Float y Float)
Color = (& r Int g Int b Int)
TransformationMatrix = (& a Float b Float c Float d Float e Float f Float)
Initially, I tried to communicate with the outside world using pixel graphics.
However, I believe that's not the right move as pixels are more difficult to
handle in code. Having pixels (or some concept of a texture) within Pear
requires either:
- mutable values (which Pear doesn't have), or
- lots of copying on every texture modification (which Pear doesn't have), or
- a super-smart optimizing compiler (which is useful but it has the sort of
slow-by-default-and-you-have-to-trust-the-black-box-to-work feeling I don't
like), or
- inefficient storage of individual pixels in trees.
By communicating the intent on a quite high-level, we can instead make use of
GPUs when turning the high-level drawing instructions into pixels. This is
also similar to how Flutter works: The Dart VM is decently fast, but not super
fast, so it just emits drawing instructions that the lower-level C++ engine
takes and turns into pixels.
Within Pear, rendering functions usually return structs of the form
(& draw DrawingInstruction width Int height Int) rather than just raw
drawing instructions.
2026-05-10: Added this comment.
"
# "All coordinates and sizes are floats."
point (\ (x y) (& x x y y))
transform-translate (\ (x y) (& a 1. b 0. c 0. d 1. e x f y))
transform-scale (\ (factor) (& a factor b 0. c 0. d factor e 0. f 0.))
parse-hex (\ (string)
(rec-2 0 (array->list (string->bytes string)) (\ (rec value chars)
(% chars
empty value
(more m) (:
c (.item m)
digit
(cond
(and (>= c (ascii-char "0")) (<= c (ascii-char "9")))
(- c (ascii-char "0"))
(and (>= c (ascii-char "a")) (<= c (ascii-char "f")))
(+ (- c (ascii-char "a")) 10)
else (crash "invalid hex char"))
(rec (+ (* value 16) digit) (.rest m)))))))
color (\ (hex)
(:
_ (assert (= (string-length hex) 7))
_ (assert (= (string-slice hex 0 1) "#"))
(&
r (parse-hex (string-slice hex 1 3))
g (parse-hex (string-slice hex 3 5))
b (parse-hex (string-slice hex 5 7)))))
black (color "#000000")
white (color "#ffffff")
red (color "#a02020")
blue (color "#1099a0")
green (color "#50a010")
purple (color "#8e31a3")
yellow (color "#ffd900")
# "I try to name the rendering functions in a declarative way
(e.g. row instead of render-next-to-each-other)."
empty-box (\ (width height) (& draw (| nothing) width width height height))
sized-box (\ (width height child)
(& draw (.draw child) width width height height))
# "Renders the child at an offset, but still occupies the original position."
translate (\ (by child)
(&
draw
(| transform
(& by (transform-translate (.x by) (.y by)) what (.draw child)))
width (.width child)
height (.height child)))
scaled (\ (factor child)
(&
draw
(| transform
(& by (transform-scale factor) what (.draw child)))
width (f* factor (.width child))
height (f* factor (.height child))))
colored (\ (color child)
(:
width (.width child)
height (.height child)
(&
draw
(| all ([]
(| fill-path (&
path ([]
(| move-to (& x 0. y 0.))
(| line-to (& x width y 0.))
(| line-to (& x width y height))
(| line-to (& x 0. y height)))
color color))
(.draw child)))
width width
height height)))
# "padding accepts either a float (uniform on all sides) or a struct
(& left right top bottom) of floats."
padding (\ (padding child)
(if (= (@kind padding) (| float))
(&
draw
(| transform
(& by (transform-translate padding padding) what (.draw child)))
width (f+ (.width child) (f* 2. padding))
height (f+ (.height child) (f* 2. padding)))
(&
draw
(| transform (&
by (transform-translate (.left padding) (.top padding))
what (.draw child)))
width (f+ (.width child) (f+ (.left padding) (.right padding)))
height (f+ (.height child) (f+ (.top padding) (.bottom padding))))))
padded-row (\ (padding children)
(:
res (rec-2 (array->list children) 0. (\ (rec children x)
(% children
empty (& draw (| empty) width 0. height 0.)
(more m) (:
child (.item m)
rest (rec (.rest m) (f+ x (.width child)))
(&
draw
(| more (&
item
(| transform (&
by (transform-translate x 0.)
what (.draw child)))
rest (.draw rest)))
width (f+ (.width child) (f+ padding (.width rest)))
height (f-max (.height child) (.height rest)))))))
(&
draw (| all (list->array (.draw res)))
width (f- (.width res) padding)
height (.height res))))
row (\ (children) (padded-row 0. children))
column (\ (children)
(:
res (rec-2 (array->list children) 0. (\ (rec children y)
(% children
empty (& list (| empty) width 0. height 0.)
(more m) (:
child (.item m)
rest (rec (.rest m) (f+ y (.height child)))
(&
list
(| more (&
item
(| transform (&
by (transform-translate 0. y)
what (.draw child)))
rest (.list rest)))
width (f-max (.width child) (.width rest))
height (f+ (.height child) (.height rest)))))))
(&
draw (| all (list->array (.list res)))
width (.width res)
height (.height res))))
stack (\ (children)
(&
draw
(| all (iter->array (map (array->iter children) (\ (child) (.draw child)))))
width (fold (map (array->iter children) (\ (child) (.width child))) 0. f-max)
height
(fold (map (array->iter children) (\ (child) (.height child))) 0. f-max)))
flow (\ (max-width children)
(:
res
(rec-4 (array->list children) 0. 0. 0. (\ (rec items x y line-height)
(% items
empty (& draws (| empty) bottom (f+ y line-height))
(more m) (:
child (.item m)
wrap? (and (f> x 0.) (f> (f+ x (.width child)) max-width))
x (if wrap? 0. x)
y (if wrap? (f+ y line-height) y)
line-height
(if wrap? (.height child) (f-max line-height (.height child)))
rest (rec (.rest m) (f+ x (.width child)) y line-height)
(&
draws
(| more (&
item
(| transform (&
by (transform-translate x y)
what (.draw child)))
rest (.draws rest)))
bottom (.bottom rest))))))
(&
draw (| all (list->array (.draws res)))
width max-width
height (.bottom res))))
parse-pixels (\ (width string)
(:
bytes (string->bytes string)
row-rec (\ (rec i x)
(: c (array-get bytes i)
(if (= c (ascii-char "|"))
(& cursor (+ i 1) result (filled-list (- width x) 0))
(if (= x width)
(crash "Pixel line too long.")
(: rest (rec rec (+ i 1) (+ x 1))
(&
cursor (.cursor rest)
result
(| more
(& item (if (= c (ascii-char " ")) 0 1) rest (.result rest)))))))))
rec (\ (rec i)
(if (= i (array-length bytes))
(| empty)
(: row (row-rec row-rec i 0)
(| more
(& item (list->array (.result row)) rest (rec rec (.cursor row)))))))
(list->array (rec rec 0))))
pixel-glyph (\ (char width string)
(:
pixels (parse-pixels width string)
height (array-length pixels)
paths (rec-2 0 0 (\ (rec x y)
(if (= x width)
(if (= (+ y 1) height) (| empty) (rec 0 (+ y 1)))
(if (= (array-get (array-get pixels y) x) 1)
(| more (&
item ([]
(| move-to (point (int->float x) (int->float y)))
(| line-to (point (int->float (+ x 1)) (int->float y)))
(| line-to (point (int->float (+ x 1)) (int->float (+ y 1))))
(| line-to (point (int->float x) (int->float (+ y 1)))))
rest (rec (+ x 1) y)))
(rec (+ x 1) y)))))
(& char char width (int->float width) paths paths)))
line-height 13.
default-glyph
(pixel-glyph "?" 5 "M M M| M M|M M M| M M|M M M| M M|M M M| M M|M M M| M M|")
glyphs ([]
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
(pixel-glyph "\n" 2 "|")
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
default-glyph
(pixel-glyph " " 2 "|")
(pixel-glyph "!" 2 "|MM|MM|MM|MM|MM|MM||MM|MM|")
(pixel-glyph "\"" 3 "|M M|M M|M M|")
(pixel-glyph "#" 6 "|| M M|MMMMMM| M M| M M| M M|MMMMMM| M M|")
(pixel-glyph "$" 5 " M| MMM|M M M|M M|MMM| MMM| MMM| M M|M M M| MMM| M|")
(pixel-glyph "%" 9 "| MM MM|M M MM|M M MM| MM MM| MMM| MM MM| MM M M| MM M M| MM MM|")
(pixel-glyph "&" 9 "| MMMMM|MM MM|MM MM| MMMM| MMM MM|MM MM MM|MM MMM|MM MMMM| MMMM MM|")
(pixel-glyph "'" 1 "|M|M|M|")
(pixel-glyph "(" 3 " MM|MM|MM|MM|MM|MM|MM|MM|MM| MM|")
(pixel-glyph ")" 3 "MM| MM| MM| MM| MM| MM| MM| MM| MM|MM|")
(pixel-glyph "*" 7 "|||MM MM| MM MM| MMM| MM MM|MM MM|")
(pixel-glyph "+" 6 "||| MM| MM|MMMMMM| MM| MM|")
(pixel-glyph "," 2 "|||||||MM|MM| M|M|")
(pixel-glyph "-" 5 "|||||MMMMM|")
(pixel-glyph "." 2 "|||||||MM|MM|")
(pixel-glyph "/" 5 " MM| MM| MM| MM| MM| MM| MM| MM|MM|MM|")
(pixel-glyph "0" 6 "| MMMM|MM MM|MM MMM|MM MMM|MMMMMM|MMM MM|MM MM|MM MM| MMMM|")
(pixel-glyph "1" 2 "| M|MM|MM|MM|MM|MM|MM|MM|MM|")
(pixel-glyph "2" 6 "| MMMM|M MM| MM| MM| MM| MM| MM|MM|MMMMMM|")
(pixel-glyph "3" 6 "|MMMMMM| MM| MM| MMMM| MM| MM| MM|M MM| MMMM|")
(pixel-glyph "4" 7 "| MM| MMM| M MM| M MM|MM MM|MMMMMMM| MM| MM| MM|")
(pixel-glyph "5" 6 "|MMMMMM|MM|MM|MMMMM| MM| MM| MM|M MM| MMMM|")
(pixel-glyph "6" 6 "| MMM| MM|MM|MMMMM|MM MM|MM MM|MM MM|MM MM| MMMM|")
(pixel-glyph "7" 7 "|MMMMMMM| MM| MM| MM| MM| MM| MM| MM| MM|")
(pixel-glyph "8" 6 "| MMMM|MM MM|MM MM|MM MM| MMMM|MM MM|MM MM|MM MM| MMMM|")
(pixel-glyph "9" 6 "| MMMM|MM MM|MM MM|MM MM|MM MM| MMMMM| MM| MM| MMM|")
(pixel-glyph ":" 2 "|||MM|MM||||MM|MM|")
(pixel-glyph ";" 2 "|||MM|MM||||MM|MM| M|M|")
(pixel-glyph "<" 5 "|| MM| MM| MM|MM| MM| MM| MM|")
(pixel-glyph "=" 5 "||||MMMMM||MMMMM|")
(pixel-glyph ">" 5 "||MM| MM| MM| MM| MM| MM|MM|")
(pixel-glyph "?" 6 "| MMMM| MM| MM| MM| MM| MM|| MM| MM|")
(pixel-glyph "@" 9 "| MMMMM| M M|M MMM M|M M M M|M M M M|M M M M|M MMMMMM| M| MMMMM|")
(pixel-glyph "A" 6 "| MMMM|MM MM|MM MM|MM MM|MMMMMM|MM MM|MM MM|MM MM|MM MM|")
(pixel-glyph "B" 6 "|MMMMM|MM MM|MM MM|MM MM|MMMM|MM MM|MM MM|MM MM|MMMMM|")
(pixel-glyph "C" 6 "| MMMM|MM M|MM|MM|MM|MM|MM|MM M| MMMM|")
(pixel-glyph "D" 6 "|MMMMM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM|MMMMM|")
(pixel-glyph "E" 5 "|MMMMM|MM|MM|MM|MMMMM|MM|MM|MM|MMMMM|")
(pixel-glyph "F" 5 "|MMMMM|MM|MM|MM|MMMMM|MM|MM|MM|MM|")
(pixel-glyph "G" 6 "| MMMM|MM M|MM|MM|MM MM|MM MM|MM MM|MM MM| MMMM|")
(pixel-glyph "H" 6 "|MM MM|MM MM|MM MM|MM MM|MMMMMM|MM MM|MM MM|MM MM|MM MM|")
(pixel-glyph "I" 4 "|MMMM| MM| MM| MM| MM| MM| MM| MM|MMMM|")
(pixel-glyph "J" 6 "| MM| MM| MM| MM| MM|MM MM|MM MM|MM MM| MMMM|")
(pixel-glyph "K" 7 "|MM MM|MM MM|MM MM|MMMM|MMM|MMMM|MM MM|MM MM|MM MM|")
(pixel-glyph "L" 5 "|MM|MM|MM|MM|MM|MM|MM|MM|MMMMM|")
(pixel-glyph "M" 10 "|M MM|MM MMM|MMM MMMM|MMMM MMMMM|M MMMMMMMM|M MMMM MM|M MM MM|M MM|M MM|")
(pixel-glyph "N" 7 "|M M|MM M|MMM M|MMMM M|M MMM M|M MMMM|M MMM|M MM|M M|")
(pixel-glyph "O" 6 "| MMMM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMM|")
(pixel-glyph "P" 6 "|MMMMM|MM MM|MM MM|MM MM|MMMMM|MM|MM|MM|MM|")
(pixel-glyph "Q" 8 "| MMMM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMM| MM| MMM|")
(pixel-glyph "R" 6 "|MMMMM|MM MM|MM MM|MM MM|MMMMM|MM MM|MM MM|MM MM|MM MM|")
(pixel-glyph "S" 5 "| MMM|MM M|MM|MMM| MMM| MMM| MM|M MM| MMM|")
(pixel-glyph "T" 6 "|MMMMMM| MM| MM| MM| MM| MM| MM| MM| MM|")
(pixel-glyph "U" 6 "|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMM|")
(pixel-glyph "V" 7 "|MM MM|MM MM|MM MM|MM MM| MM MM| MM MM| MMM| MMM| M|")
(pixel-glyph "W" 11 "|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MMMMMMMMMM|")
(pixel-glyph "X" 6 "|MM MM|MM MM|MM MM| MMMM| MM| MMMM|MM MM|MM MM|MM MM|")
(pixel-glyph "Y" 6 "|MM MM|MM MM|MM MM|MM MM| MMMM| MM| MM| MM| MM|")
(pixel-glyph "Z" 6 "|MMMMMM| MM| MM| MM| MM| MM| MM|MM|MMMMMM|")
(pixel-glyph "[" 3 "|MMM|MM|MM|MM|MM|MM|MM|MM|MMM|")
(pixel-glyph "\\" 5 "|MM|MM| MM| MM| MM| MM| MM| MM| MM|")
(pixel-glyph "]" 3 "|MMM| MM| MM| MM| MM| MM| MM| MM|MMM|")
(pixel-glyph "^" 6 "| MM| MMMM|MM MM|")
(pixel-glyph "_" 6 "|||||||||MMMMMM|")
(pixel-glyph "`" 2 "|M| M|")
(pixel-glyph "a" 6 "||| MMMM|M MM| MM| MMMMM|MM MM|MM MM| MMMMM|")
(pixel-glyph "b" 6 "|MM|MM|MMMMM|MM MM|MM MM|MM MM|MM MM|MM MM|MMMMM|")
(pixel-glyph "c" 5 "||| MMM|MM M|MM|MM|MM|MM M| MMM|")
(pixel-glyph "d" 6 "| MM| MM| MMMMM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMMM|")
(pixel-glyph "e" 6 "||| MMMM|MM MM|MM MM|MMMMMM|MM|MM M| MMMM|")
(pixel-glyph "f" 5 "| MMM| MM|MMMM| MM| MM| MM| MM| MM| MM|")
(pixel-glyph "g" 6 "||| MMMMM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMMM| MM| M MM| MMM|")
(pixel-glyph "h" 6 "|MM|MM|MMMMM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM|")
(pixel-glyph "i" 2 "|MM||MM|MM|MM|MM|MM|MM|MM|")
(pixel-glyph "j" 4 "| MM|| MM| MM| MM| MM| MM| MM| MM| MM|MMM|")
(pixel-glyph "k" 6 "|MM|MM|MM MM|MM MM|MMMM|MM|MMMM|MM MM|MM MM|")
(pixel-glyph "l" 3 "|MM|MM|MM|MM|MM|MM|MM|MM| MM|")
(pixel-glyph "m" 10 "|||MMMMM MMM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|")
(pixel-glyph "n" 6 "|||MMMMM|MM MM|MM MM|MM MM|MM MM|MM MM|MM MM|")
(pixel-glyph "o" 6 "||| MMMM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMM|")
(pixel-glyph "p" 6 "|||MMMMM|MM MM|MM MM|MM MM|MM MM|MM MM|MMMMM|MM|MM|")
(pixel-glyph "q" 6 "||| MMMMM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMMM| MM| MM|")
(pixel-glyph "r" 5 "|||MM MM|MMMMM|MM|MM|MM|MM|MM|")
(pixel-glyph "s" 5 "||| MMM|MM M|MMM| MMM| MMM|M MM| MMM|")
(pixel-glyph "t" 5 "| MM| MM|MMMM| MM| MM| MM| MM| MM| MM|")
(pixel-glyph "u" 6 "|||MM MM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMMM|")
(pixel-glyph "v" 6 "|||MM MM|MM MM|MM MM|MM MM|MM MM| MMMM| MM|")
(pixel-glyph "w" 10 "|||MM MM MM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MM MM MM|MMMMMMMM|")
(pixel-glyph "x" 6 "|||MM MM|MM MM|MM MM| MM|MM MM|MM MM|MM MM|")
(pixel-glyph "y" 6 "|||MM MM|MM MM|MM MM|MM MM|MM MM|MM MM| MMMMM| MM| M MM| MMM|")
(pixel-glyph "z" 6 "|||MMMMMM| MM| MM| MM| MM|MM|MMMMMM|")
(pixel-glyph "{" 4 " MM| MM| MM| MM| MM|MM| MM| MM| MM| MM| MM|")
(pixel-glyph "|" 2 "MM|MM|MM|MM|MM|MM|MM|MM|MM|MM|MM|")
(pixel-glyph "{" 4 "MM| MM| MM| MM| MM| MM| MM| MM| MM| MM|MM|")
(pixel-glyph "~" 7 "|||| MM M| M MM|M M|")
default-glyph)
find-glyph (\ (rec char i)
(if (= i (array-length glyphs))
default-glyph
(: g (array-get glyphs i) (if (= char (.char g)) g (rec rec char (+ i 1))))))
find-glyph (\ (char)
(: bytes (@string->bytes char)
(if (and (= (array-length bytes) 1) (< (array-get bytes 0) 123))
(: glyph (array-get glyphs (array-get bytes 0))
_
(if (= (.char glyph) char)
nil
(crash (| bad-glyph (& char char glyph glyph))))
glyph)
(find-glyph find-glyph char 0))))
find-kerning (\ (a b) 1.)
# "Lay out text into positioned glyphs. Lines break at newline characters, and
a word is wrapped to the next line whenever it wouldn't fit within
max-width."
layout-text (\ (text max-width)
(:
# "Width of the word starting at `chars` (until the next space, newline, or
end), used to decide whether the word fits on the current line."
word-width (\ (chars)
(rec-2 chars 0. (\ (recurse chars acc)
(% chars
empty acc
(more m) (:
char (.item m)
rest (.rest m)
(if (or (= char " ") (= char "\n"))
acc
(:
glyph (find-glyph char)
kerning
(% rest
empty 0.
(more n)
(if (or (= (.item n) " ") (= (.item n) "\n"))
0.
(find-kerning char (.item n))))
(recurse rest (f+ acc (f+ (.width glyph) kerning))))))))))
(rec-4 (string->chars-list text) 0. 0. true (\ (rec chars x y at-word-start)
(% chars
empty (| empty)
(more m) (:
char (.item m)
rest (.rest m)
glyph (find-glyph char)
(if (= char "\n")
(:
next-x 0.
next-y (f+ y line-height)
(| more (&
item (& x x y y char char glyph glyph cursor-x next-x cursor-y next-y)
rest (rec rest next-x next-y true))))
(:
wrap?
(if at-word-start
(if (= char " ")
false
(if (f> x 0.) (f> (f+ x (word-width chars)) max-width) false))
false)
line-x (if wrap? 0. x)
line-y (if wrap? (f+ y line-height) y)
width (.width glyph)
kerning (% rest empty 0. (more next) (find-kerning char (.item next)))
next-x (f+ line-x (f+ width kerning))
next-cursor-x (f+ line-x (f- width (f/ kerning 2.)))
(| more (&
item
(& x line-x y line-y char char glyph glyph
cursor-x next-cursor-x cursor-y line-y)
rest (rec rest next-x line-y (= char " "))))))))))))
draw-glyph (\ (glyph color)
(| all
(iter->array
(map
(list->iter (.paths glyph))
(\ (path) (| fill-path (& path path color color)))))))
# "Render text, wrapping words to fit within max-width and breaking at
newlines. `text` passes a huge width to disable wrapping."
text-wrapped (\ (string color max-width)
(:
layouted (layout-text string max-width)
(&
draw
(| all
(iter->array
(map (list->iter layouted) (\ (glyph)
(| transform (&
by (transform-translate (.x glyph) (.y glyph))
what (draw-glyph (.glyph glyph) color)))))))
width
(fold
(map
(list->iter layouted)
(\ (glyph) (f+ (.x glyph) (.width (.glyph glyph))))) 0. f-max)
height
(fold
(map (list->iter layouted) (\ (glyph) (f+ (.y glyph) line-height)))
0.
f-max))))
text (\ (string color) (text-wrapped string color 100000.))
no-modifiers? (\ (key)
(and-3 (not (.control key)) (not (.shift key)) (not (.alt key))))
ctrl-modifier? (\ (key)
(and-3 (.control key) (not (.shift key)) (not (.alt key))))
alt-modifier? (\ (key)
(and-3 (not (.control key)) (not (.shift key)) (.alt key)))
shift-modifier? (\ (key)
(and-3 (not (.control key)) (.shift key) (not (.alt key))))
ctrl-shift-modifiers? (\ (key)
(and-3 (.control key) (.shift key) (not (.alt key))))
shift-alt-modifiers? (\ (key)
(and-3 (not (.control key)) (.shift key) (.alt key)))
key-escape? (\ (key) (and (= (.keycode key) 256) (no-modifiers? key)))
key-enter? (\ (key) (and (= (.keycode key) 257) (no-modifiers? key)))
key-backspace? (\ (key) (and (= (.keycode key) 259) (no-modifiers? key)))
key-delete? (\ (key) (and (= (.keycode key) 261) (no-modifiers? key)))
key-right? (\ (key) (and (= (.keycode key) 262) (no-modifiers? key)))
key-left? (\ (key) (and (= (.keycode key) 263) (no-modifiers? key)))
key-down? (\ (key) (and (= (.keycode key) 264) (no-modifiers? key)))
key-up? (\ (key) (and (= (.keycode key) 265) (no-modifiers? key)))
key-start? (\ (key) (and (= (.keycode key) 268) (no-modifiers? key)))
key-end? (\ (key) (and (= (.keycode key) 269) (no-modifiers? key)))
key-ctrl-enter? (\ (key) (and (= (.keycode key) 257) (ctrl-modifier? key)))
key-ctrl-backspace? (\ (key) (and (= (.keycode key) 259) (ctrl-modifier? key)))
key-ctrl-right? (\ (key) (and (= (.keycode key) 262) (ctrl-modifier? key)))
key-ctrl-left? (\ (key) (and (= (.keycode key) 263) (ctrl-modifier? key)))
key-alt-right? (\ (key) (and (= (.keycode key) 262) (alt-modifier? key)))
key-alt-left? (\ (key) (and (= (.keycode key) 263) (alt-modifier? key)))
key-alt-down? (\ (key) (and (= (.keycode key) 264) (alt-modifier? key)))
key-alt-up? (\ (key) (and (= (.keycode key) 265) (alt-modifier? key)))
key-shift-right? (\ (key) (and (= (.keycode key) 262) (shift-modifier? key)))
key-shift-left? (\ (key) (and (= (.keycode key) 263) (shift-modifier? key)))
key-shift-down? (\ (key) (and (= (.keycode key) 264) (shift-modifier? key)))
key-shift-up? (\ (key) (and (= (.keycode key) 265) (shift-modifier? key)))
key-shift-alt-down? (\ (key)
(and (= (.keycode key) 264) (shift-alt-modifiers? key)))
key-shift-alt-up? (\ (key)
(and (= (.keycode key) 265) (shift-alt-modifiers? key)))
key-shift-alt-left? (\ (key)
(and (= (.keycode key) 263) (shift-alt-modifiers? key)))
key-shift-start? (\ (key) (and (= (.keycode key) 268) (shift-modifier? key)))
key-shift-end? (\ (key) (and (= (.keycode key) 269) (shift-modifier? key)))
key-ctrl-shift-right? (\ (key)
(and (= (.keycode key) 262) (ctrl-shift-modifiers? key)))
key-ctrl-shift-left? (\ (key)
(and (= (.keycode key) 263) (ctrl-shift-modifiers? key)))
key-space? (\ (key) (and (= (.keycode key) 32) (no-modifiers? key)))
key-a? (\ (key) (and (= (.keycode key) 65) (no-modifiers? key)))
key-d? (\ (key) (and (= (.keycode key) 68) (no-modifiers? key)))
key-n? (\ (key) (and (= (.keycode key) 78) (no-modifiers? key)))
key-r? (\ (key) (and (= (.keycode key) 82) (no-modifiers? key)))
key-s? (\ (key) (and (= (.keycode key) 83) (no-modifiers? key)))
key-w? (\ (key) (and (= (.keycode key) 87) (no-modifiers? key)))
key-ctrl-n? (\ (key) (and (= (.keycode key) 78) (ctrl-modifier? key)))