-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.mar
1425 lines (1322 loc) · 44 KB
/
editor.mar
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
| Whirling
| How Rendering Works
| We create an SDL texture and render everything to that. This corresponds to
| the pixely design. This texture is then rendered to the window, scaled up to
| fill it.
| Colors
struct Color { r: U8, g: U8, b: U8 }
fun color(hex: U64): Color {
Color {
r = {hex / 16#10000}.and(16#ff).to_U8(),
g = {hex / 16#100}.and(16#ff).to_U8(),
b = {hex / 16#1}.and(16#ff).to_U8(),
}
}
fun write[W](writer: W, color: Color) {
writer."#
'{if color.r >= 16:U8 then "" else "0"}{color.r.radix(16)}
'{if color.g >= 16:U8 then "" else "0"}{color.g.radix(16)}
'{if color.b >= 16:U8 then "" else "0"}{color.b.radix(16)}"
}
| Thank you to @antoniusnaumann for the color theme!
var background = color(16#1c1c1e)
var foreground = color(16#f2f2f7)
var grey = color(16#999999)
var dark_grey = color(16#555555)
var blue = color(16#1a94ff)
var purple = color(16#d189f5)
var green = color(16#41d496)
var lime = color(16#7dd70f)
var yellow = color(16#ffd60a)
var light_yellow = color(16#ffea80)
var orange = color(16#ff9f0a)
var red = color(16#ff5959)
var dark_red = color(16#992222)
| Offset
struct Offset { x: I32, y: I32 }
fun @(x: I32, y: I32): Offset { Offset { x, y } }
var origin = 0:I32 @ 0:I32
fun +(a: Offset, b: Offset): Offset { {a.x + b.x} @ {a.y + b.y} }
fun -(a: Offset, b: Offset): Offset { {a.x - b.x} @ {a.y - b.y} }
fun *(a: Offset, b: I32): Offset { b * a }
fun *(a: I32, b: Offset): Offset { {a * b.x} @ {a * b.y} }
| Rectangle
struct Rect { offset: Offset, size: Offset }
fun @(offset: Offset, size: Offset): Rect { Rect { offset, size } }
| SDL2 Bindings
| SDL2 has to be linked together with this program. Here are some wrappers that
| call the linked SDL functions. All SDL functions follow the system V calling
| convention, which means that the stack needs to be aligned to 16 bytes.
fun raw_sdl_init(flags: U64): U64 asm {
mov rdi, [rsp + 16] ; flags
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_Init
add rsp, 8
pop rsp
; return
mov r8, [rsp + 8]
mov [r8], rax
ret
}
fun raw_sdl_quit() asm {
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_Quit
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_get_error(): U64 asm {
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_GetError
add rsp, 8
pop rsp
; return
mov r8, [rsp + 8]
mov [r8], rax
ret
}
fun raw_sdl_delay(millis: U64) asm {
mov rdi, [rsp + 16]
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_Delay
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_create_window(
title: OsStr, x: U64, y: U64, width: U64, height: U64
): Address asm {
mov rdi, [rsp + 16] ; title
mov rsi, [rsp + 24] ; x
mov rdx, [rsp + 32] ; y
mov rcx, [rsp + 40] ; width
mov r8, [rsp + 48] ; height
mov r9, 4
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_CreateWindow
add rsp, 8
pop rsp
; return
mov r8, [rsp + 8]
mov [r8], rax
ret
}
fun raw_sdl_destroy_window(window: Address) asm {
mov rdi, [rsp + 16]
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_DestroyWindow
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_create_renderer(window: Address): Address asm {
mov rdi, [rsp + 16]
mov rsi, -1
mov rdx, 6
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_CreateRenderer
add rsp, 8
pop rsp
; return
mov r8, [rsp + 8]
mov [r8], rax
ret
}
fun raw_sdl_destroy_renderer(renderer: Address) asm {
mov rdi, [rsp + 16]
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_DestroyRenderer
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_create_texture(
renderer: Address, format: U32, access: I32, width: I32, height: I32
): Address asm {
mov rdi, [rsp + 16] ; renderer
mov rsi, [rsp + 24] ; format
mov rdx, [rsp + 28] ; access
mov rcx, [rsp + 32] ; width
mov r8, [rsp + 36] ; height
mov r9, 4
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_CreateTexture
add rsp, 8
pop rsp
; return
mov r8, [rsp + 8]
mov [r8], rax
ret
}
fun raw_sdl_destroy_texture(texture: Address) asm {
mov rdi, [rsp + 16]
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_DestroyTexture
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_set_render_target(renderer: Address, texture: Address) asm {
mov rdi, [rsp + 16] ; renderer
mov rsi, [rsp + 24] ; texture
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_SetRenderTarget
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_set_render_draw_color(
renderer: Address, r: U64, g: U64, b: U64, a: U64
) asm {
mov rdi, [rsp + 16]
mov rsi, [rsp + 24]
mov rdx, [rsp + 32]
mov rcx, [rsp + 40]
mov r8, [rsp + 48]
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_SetRenderDrawColor
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_render_clear(renderer: Address) asm {
mov rdi, [rsp + 16]
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_RenderClear
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_render_copy(
renderer: Address, texture: Address, src: Address, dest: Address
) asm {
mov rdi, [rsp + 16] ; renderer
mov rsi, [rsp + 24] ; texture
mov rdx, [rsp + 32] ; src
mov rcx, [rsp + 40] ; dst
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_RenderCopy
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_render_draw_point(renderer: Address, x: I32, y: I32) asm {
mov rdi, [rsp + 16] ; renderer
mov rsi, [rsp + 24] ; x
mov rdx, [rsp + 28] ; y
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_RenderDrawPoint
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_render_fill_rect(renderer: Address, rect: Address) asm {
mov rdi, [rsp + 16] ; renderer
mov rsi, [rsp + 24] ; rect
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_RenderFillRect
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_render_present(renderer: Address) asm {
mov rdi, [rsp + 16]
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_RenderPresent
add rsp, 8
pop rsp
; return
ret
}
fun raw_sdl_poll_event(event: Address): U64 asm {
mov rdi, [rsp + 16]
; make the call with a 16-bytes-aligned stack
mov rbp, rsp
and rsp, 0xfffffffffffffff0
push rbp
sub rsp, 8
call SDL_PollEvent
add rsp, 8
pop rsp
; return
mov r8, [rsp + 8]
mov [r8], rax
ret
}
| SDL wrappers
| These are some type-safe wrappers around the SDL functions.
fun init_sdl(): Result[Nothing, U64] {
var result = raw_sdl_init(62001)
if result == 0 then ok[Nothing, U64]({}) else error[Nothing, U64](result)
}
fun quit_sdl() { raw_sdl_quit() }
fun delay(millis: U64) { raw_sdl_delay(millis) }
| A handle to an actual window that is opened.
struct Window { handle: Address }
fun create_window(title: Str, position: Offset, size: Offset): Window {
var handle = raw_sdl_create_window(
title.to_os_str(),
position.x.cast[I32, U32]().to_U64(),
position.y.cast[I32, U32]().to_U64(),
size.x.cast[I32, U32]().to_U64(),
size.y.cast[I32, U32]().to_U64(),
)
Window { handle }
}
fun destroy(window: Window) { window.handle.raw_sdl_destroy_window() }
| A renderer is a stateful entity linked to a window. It is double-buffered, so
| all operations are hidden until you call renderer.present().
struct Renderer { handle: Address }
fun create_renderer(window: Window): Renderer {
var handle = window.handle.raw_sdl_create_renderer()
Renderer { handle }
}
fun destroy(renderer: Renderer) { renderer.handle.raw_sdl_destroy_renderer() }
| A texture is a buffer for pixel data.
struct Texture { handle: Address }
enum TextureAccess { static, streaming, target }
fun create_texture(
renderer: Renderer, access: TextureAccess, size: Offset
): Texture {
var handle = renderer.handle.raw_sdl_create_texture(
372645892:U32, | SDL_PIXELFORMAT_ARGB8888
switch access
case static 0:I32
case streaming 1:I32
case target 2:I32,
size.x,
size.y,
)
Texture { handle }
}
fun destroy(texture: Texture) { texture.handle.raw_sdl_destroy_texture() }
fun set_target(renderer: Renderer, texture: Texture) {
renderer.handle.raw_sdl_set_render_target(texture.handle)
}
fun reset_target_to_window(renderer: Renderer) {
renderer.handle.raw_sdl_set_render_target(null)
}
fun set_color(renderer: Renderer, color: Color) {
renderer.handle.raw_sdl_set_render_draw_color(
color.r.to_U64(), color.g.to_U64(), color.b.to_U64(), 16#ff)
}
fun clear(renderer: Renderer) { renderer.handle.raw_sdl_render_clear() }
fun draw(renderer: Renderer, point: Offset) {
renderer.handle.raw_sdl_render_draw_point(point.x, point.y)
}
fun draw(renderer: Renderer, rect: Rect) {
renderer.handle.raw_sdl_render_fill_rect(rect.&.to_address())
}
fun draw(renderer: Renderer, texture: Texture) {
renderer.handle.raw_sdl_render_copy(texture.handle, null, null)
}
fun present(renderer: Renderer) { renderer.handle.raw_sdl_render_present() }
| Event handling.
enum Event {
quit,
text_input: Str,
key_down: KeyDown,
mouse_wheel: I32,
}
struct KeyDown { keycode: U8, modifiers: Modifiers }
struct Modifiers { value: U16 }
fun ctrl(mod: Modifiers): Bool {
{mod.value.to_U64().and(64) != 0} / {mod.value.to_U64().and(128) != 0}
}
fun shift(mod: Modifiers): Bool {
{mod.value.to_U64().and(1) != 0} / {mod.value.to_U64().and(2) != 0}
}
var event_buffer = uninitialized_slice[U8](2048).data.to_address()
fun poll_event(): Maybe[Event] {
loop {
if raw_sdl_poll_event(event_buffer) == 0 then break
var type = event_buffer.cast[Address, &U32]().*
| Events: https://wiki.libsdl.org/SDL2/SDL_Event
| Event types: https://github.com/libsdl-org/SDL/blob/cacac6cc341d5856d1857bdcf7390551eed54865/include/SDL3/SDL_events.h
if type == 16#100:U32 then return some(Event.quit)
else if type == 16#303:U32 then {
| offset | TextInput {
| 0 | type: U32
| 4 | timestamp: U32
| 8 | windowId: U32
| 12 | text: char[32] (null terminated)
| | }
var text = {event_buffer + 12}.cast[Address, OsStr]().to_str()
return some(Event.text_input(text))
}
else if type == 16#300:U32 then {
| offset | KeyDown {
| 0 | type: U32
| 4 | timestamp: U32
| 8 | windowId: U32
| 12 | state: U8
| 13 | repeat: U8
| 16 | keysym: Keysym {
| 16 | scancode: Scancode: U32
| 20 | sym: Keycode: U8
| 24 | mod: U16
| 28 | _unused: U32
| | }
| | }
var keycode = {event_buffer + 20}.cast[Address, &U8]().*
var mod = {event_buffer + 24}.cast[Address, &U16]().*
return some(Event.key_down(KeyDown {
keycode, modifiers = Modifiers { value = mod }
}))
}
else if type == 16#403:U32 then {
| offset | MouseWheel {
| 0 | type: U32
| 4 | timestamp: U32
| 8 | windowId: U32
| 12 | which: U32
| 16 | x: I32
| 20 | y: I32
| 24 | direction: U32 (normal or flipped)
| 28 | preciseX: Float
| | preciseY: Float
| | }
var y = {event_buffer + 20}.cast[Address, &I32]().*
return some(Event.mouse_wheel(y))
}
else {
stderr."unhandled event type 0x{type.radix(16)}{newline}"
continue
}
}
return none[Event]()
}
| Tokenizer
| Instead of a full parser, syntax highlighting works with a simple tokenizer,
| making it more robust.
enum Token {
comment,
comment_title,
name,
declaration,
control,
type,
function,
literal,
literal_support,
operator,
punctuation,
default_,
}
var declaration_keywords = vec("fun", "var", "struct", "enum", "opaque")
var control_keywords = vec("if", "then", "else", "switch", "case", "default",
"orelse", "loop", "for", "in", "do", "break", "continue", "return")
var operator_chars = "%!~@^\/`.&*+$-<>="
fun tokenize(input: Slice[Char], out: &Vec[Token]) {
out.len = 0
var cursor = 0
loop {
if cursor >= input.len then break
var char = input.get(cursor)
if char == #| then {
var end = cursor
loop {
var c = input.get_maybe(end) orelse break
if c == newline then break else end = end + 1
}
var comment = input.subslice(cursor..end).to_str()
var token =
if comment.ends_with(" ") then Token.comment_title else Token.comment
for i in cursor..end do out.push(token)
cursor = end
continue
}
if {#A..=#Z}.contains(char) / {#a..=#z}.contains(char) / {char == #_} then {
var is_type = {#A..=#Z}.contains(char) / {char == #_}
var end = cursor
loop {
var c = input.get_maybe(end) orelse break
if {#A..=#Z}.contains(c) / {#a..=#z}.contains(c) / {c == #_}
/ {#0..=#9}.contains(c)
then end = end + 1
else break
}
var word = input.subslice(cursor..end).to_str()
var char_after_end = input.get_maybe(end) orelse #.
var token =
if is_type then Token.type else
if {char_after_end == #(} / {char_after_end == #[}
then Token.function
else if declaration_keywords.iter().&.contains(word)
then Token.declaration
else if control_keywords.iter().&.contains(word)
then Token.control
else Token.name
for i in cursor..end do out.push(token)
cursor = end
continue
}
if {#0..=#9}.contains(char) then {
var end = cursor
loop {
var c = input.get_maybe(end) orelse break
if {#0..=#9}.contains(c) / {c == #_} then end = end + 1 else break
}
var is_radix =
switch input.get_maybe(end)
case none false
case some(char) char == ##
if is_radix then {
end = end + 1
for i in cursor..end do out.push(Token.literal_support)
cursor = end
loop {
var c = input.get_maybe(end) orelse break
if {#0..=#9}.contains(c) / {c == #_}
/ {#a..=#z}.contains(c) / {#A..=#Z}.contains(c)
then end = end + 1
else break
}
}
for i in cursor..end do out.push(Token.literal)
cursor = end
continue
}
if operator_chars.iter().&.contains(char) then {
var end = cursor
loop {
var c = input.get_maybe(end) orelse break
if operator_chars.iter().&.contains(c) then end = end + 1 else break
}
var name = input.subslice(cursor..end).to_str()
var token =
if {name == "."} / {name == "="}
then Token.punctuation
else Token.operator
for i in cursor..end do out.push(token)
cursor = end
continue
}
if {char == #(} / {char == #)} / {char == #[} / {char == #]} / {char == #{}
/ {char == #}} / {char == #,} / {char == #.} / {char == #:}
then {
out.push(Token.punctuation)
cursor = cursor + 1
continue
}
if char == ## then {
out.push(Token.literal_support)
out.push(Token.literal)
cursor = cursor + 2
continue
}
var metaness = 0
loop
if {input.get_maybe(cursor) orelse break} == #'
then {
metaness = metaness + 1
cursor = cursor + 1
out.push(Token.literal)
}
else break
if input.get_maybe(cursor) == some(#") then {
cursor = cursor + 1
out.push(Token.literal)
loop {
var string_ends = {input.get_maybe(cursor) orelse break} == #"
for i in 0..metaness do {
var char = input.get_maybe(cursor + i + 1)
orelse {string_ends = false break }
if char != #' then string_ends = false
}
if string_ends then {
cursor = cursor + metaness + 1
for i in 0..{metaness + 1} do out.push(Token.literal)
break
}
out.push(Token.literal)
cursor = cursor + 1
}
continue
}
out.push(Token.default_)
cursor = cursor + 1
}
}
| The Actual Editor
var editor_size = 513:I32 @ 400:I32
var pixel_size = 2:I32
var window_size = editor_size * pixel_size
| Whirling
| This is the name of the text area itself.
struct Whirling {
text: Vec[Char],
lines: Vec[U64],
tokens: Vec[Token],
needs_tokenization: Bool,
scroll: I32,
cursor: U64,
}
fun whirling(text: Str): Whirling {
var whirling = Whirling {
text = vec[Char](),
lines = vec[U64](),
tokens = vec[Token](),
needs_tokenization = false,
scroll = 0:I32,
cursor = 0,
}
for char in text do whirling.text.&.push(char)
whirling.&.text_updated()
whirling
}
| Calculates lines and tokens based on the text. Is pretty expensive.
fun text_updated(whirling: &Whirling) {
whirling.lines.len = 0
whirling.lines.&.push(0)
for char in whirling.text.iter().enumerate() do
if char.item == newline then whirling.lines.&.push(char.index)
tokenize(whirling.text.to_slice(), whirling.tokens.&)
whirling.needs_tokenization = false
}
| Given a cursor between 0 and text.len (inclusive), returns the start of the
| line that the cursor is in. This position is either 0 or right after a newline
| character.
fun line_start(whirling: Whirling, cursor: U64): U64 {
loop {
if cursor == 0 then break
cursor = cursor - 1
var is_at_end = false
if cursor == whirling.text.len then is_at_end = true
else if whirling.text.get(cursor) == newline then is_at_end = true
if is_at_end then { cursor = cursor + 1 break }
}
cursor
}
| Given a cursor between 0 and text.len (inclusive), returns the end of the line
| that the cursor is in. This position is either text.len or it contains a
| newline character.
fun line_end(whirling: Whirling, cursor: U64): U64 {
loop {
if cursor == whirling.text.len then break
if whirling.text.get(cursor) == newline then break
cursor = cursor + 1
}
cursor
}
fun input(whirling: &Whirling, str: Str) {
whirling.text.&.make_space_at(whirling.cursor, str.len, 0:U8.to_char())
str.chars().copy_to(
whirling.text.to_slice().subslice(whirling.cursor ..+ str.len))
if not(str.chars().iter().&.contains(newline)) then {
whirling.tokens.&.make_space_at(whirling.cursor, str.len, Token.default_)
for line in whirling.lines.iter().enumerate() do
whirling.lines.get_ref(line.index).* =
if line.item >= whirling.cursor
then line.item + str.len
else line.item
whirling.needs_tokenization = true
} else whirling.text_updated()
whirling.cursor = whirling.cursor + str.len
whirling.scroll_cursor_into_view()
}
fun enter(whirling: &Whirling) { whirling.input("{newline}") }
fun backspace(whirling: &Whirling) {
if whirling.cursor == 0 then return {}
var index = whirling.cursor - 1
var removed = whirling.text.get(index)
whirling.text.&.remove(index..+1)
if removed == newline then whirling.text_updated() else {
whirling.tokens.&.remove(index..+1)
for line in whirling.lines.iter().enumerate() do
whirling.lines.get_ref(line.index).* =
if line.item >= whirling.cursor
then line.item - 1
else line.item
whirling.needs_tokenization = true
}
whirling.cursor = whirling.cursor - 1
whirling.scroll_cursor_into_view()
}
fun delete(whirling: &Whirling) {
if whirling.cursor == whirling.text.len then return {}
whirling.cursor = whirling.cursor + 1
whirling.backspace()
}
fun move_left(whirling: &Whirling) {
if whirling.cursor == 0 then return {}
whirling.cursor = whirling.cursor - 1
whirling.scroll_cursor_into_view()
}
fun move_right(whirling: &Whirling) {
if whirling.cursor == whirling.text.len then return {}
whirling.cursor = whirling.cursor + 1
whirling.scroll_cursor_into_view()
}
fun move_up(whirling: &Whirling) {
var start_of_line = whirling.line_start(whirling.cursor)
if start_of_line == 0 then return {}
var chars_before = whirling.cursor - start_of_line
var end_of_previous_line = start_of_line - 1
var start_of_previous_line = whirling.line_start(end_of_previous_line)
whirling.cursor = {start_of_previous_line + chars_before}
.clamp(0, end_of_previous_line)
whirling.scroll_cursor_into_view()
}
fun move_down(whirling: &Whirling) {
var end_of_line = whirling.line_end(whirling.cursor)
if end_of_line == whirling.text.len then return {}
var start_of_line = whirling.line_start(whirling.cursor)
var chars_before = whirling.cursor - start_of_line
var start_of_next_line = end_of_line + 1
var end_of_next_line = whirling.line_end(start_of_next_line)
whirling.cursor = {start_of_next_line + chars_before}.clamp(0, end_of_next_line)
whirling.scroll_cursor_into_view()
}
fun move_start(whirling: &Whirling) {
whirling.cursor = whirling.line_start(whirling.cursor)
whirling.scroll_cursor_into_view()
}
fun move_end(whirling: &Whirling) {
whirling.cursor = whirling.line_end(whirling.cursor)
whirling.scroll_cursor_into_view()
}
| Control + arrow keys move one word.
| Concretely:
| 1. Skip spaces
| 2. How many characters of punctuation follow?
| 0: Skip all letters
| 1: Skip the punctuation, then all letters after it
| 2: Skip all punctutation
fun is_word(char: Char): Bool {
{#a..=#z}.contains(char) / {#A..=#Z}.contains(char) / {char == #_}
/ {#0..=#9}.contains(char)
}
fun is_punctuation(char: Char): Bool { {char != space} & not(char.is_word()) }
fun find_word_boundary[I](iter: &Iter[Char, I]): U64 {
var consumed = 0
var char = loop {
var char = iter.next() orelse return consumed
if char != space then break(char)
consumed = consumed + 1
}
consumed = consumed + 1
if char.is_punctuation()
then {
var char = iter.next() orelse return consumed
if char == space then return consumed
consumed = consumed + 1
if char.is_word()
then loop {
if {iter.next() orelse return consumed}.is_word().not()
then return consumed
consumed = consumed + 1
}
else loop {
if {iter.next() orelse return consumed}.is_punctuation().not()
then return consumed
consumed = consumed + 1
}
}
else loop {
if {iter.next() orelse return consumed}.is_word().not()
then return consumed
consumed = consumed + 1
}
}
fun move_ctrl_left(whirling: &Whirling) {
var amount_to_move = whirling.text.to_slice()
.subslice(0..whirling.cursor).rev_iter().&.find_word_boundary()
whirling.cursor = whirling.cursor - amount_to_move
whirling.scroll_cursor_into_view()
}
fun move_ctrl_right(whirling: &Whirling) {
var amount_to_move = whirling.text.to_slice()
.subslice(whirling.cursor..whirling.text.len).iter().&.find_word_boundary()
whirling.cursor = whirling.cursor + amount_to_move
whirling.scroll_cursor_into_view()
}
fun scroll_to(whirling: &Whirling, y: I32) {
var viewport_height = 9:I32 * whirling.lines.len.to_I32() + 3:I32
whirling.scroll = y.clamp(
0:I32,
max(viewport_height - editor_size.y, 0:I32),
)
}
fun scroll_by(whirling: &Whirling, amount: I32) {
whirling.scroll_to(whirling.scroll + amount)
}
fun scroll_line_to_center(whirling: &Whirling, line: U64) {
var scroll_so_line_is_at_center = 2:I32 | top padding
+ {9:I32 * line.to_I32()}
- 4:I32 | about 9/2 to center the line
- {editor_size.y.to_U64() / 2}.to_I32()
whirling.scroll_to(scroll_so_line_is_at_center)
}
fun scroll_line_into_view(whirling: &Whirling, line: U64) {
var scroll_so_line_is_at_center = 2:I32 | top padding
+ {9:I32 * line.to_I32()}
- 4:I32 | about 9/2 to center the line
- {editor_size.y.to_U64() / 2}.to_I32()
var leeway = {editor_size.y.to_U64() / 4}.to_I32()
whirling.scroll_to(whirling.scroll.clamp(
scroll_so_line_is_at_center - leeway,
scroll_so_line_is_at_center + leeway,
))
}
fun scroll_cursor_into_view(whirling: &Whirling) {
var line = 0
for l in whirling.lines.iter().enumerate() do
if l.item > whirling.cursor then { line = l.index break }
whirling.scroll_line_into_view(line)
}
fun handle_event(whirling: &Whirling, event: Event) {
switch event
case text_input(text) whirling.&.input(text)
case key_down(keydown) {
var keycode = keydown.keycode
var modifiers = keydown.modifiers
if keycode == 8:U8 then whirling.&.backspace()
else if keycode == 13:U8 then whirling.&.enter()
else if keycode == 74:U8 then whirling.&.move_start()
else if keycode == 77:U8 then whirling.&.move_end()
else if keycode == 79:U8 then {
if modifiers.ctrl()
then whirling.&.move_ctrl_right()
else whirling.&.move_right()
}
else if keycode == 80:U8 then {
if modifiers.ctrl()
then whirling.&.move_ctrl_left()
else whirling.&.move_left()
}
else if keycode == 81:U8 then whirling.&.move_down()
else if keycode == 82:U8 then whirling.&.move_up()
else if keycode == 103:U8 then {
|if modifiers.ctrl() then whirling.&.go_to()
}
else if keycode == 127:U8 then whirling.&.delete()
else stderr."Key down {keycode}{newline}"
}
case mouse_wheel(amount) whirling.&.scroll_by({0:I32 - 10:I32} * amount)
default {}
}
| All letters are sized 5x8 pixels.
struct Glyph { a: U8, b: U8, c: U8, d: U8, e: U8 }
fun glyph(a: U64, b: U64, c: U64, d: U64, e: U64): Glyph {
Glyph { a = a.to_U8(), b = b.to_U8(), c = c.to_U8(), d = d.to_U8(), e = e.to_U8() }
}
var font = {
var map = map[Char, Glyph]().&
map.put(newline,
glyph(2#00100000, 2#01110000, 2#00100000, 2#00111000, 2#00000000))
map.put(# , glyph(2#00000000, 2#00000000, 2#00000000, 2#00000000, 2#00000000))
map.put(#!, glyph(2#00000000, 2#00000000, 2#01011111, 2#00000000, 2#00000000))
map.put(#", glyph(2#00000000, 2#00000111, 2#00000000, 2#00000111, 2#00000000))
map.put(##, glyph(2#00010100, 2#01111111, 2#00010100, 2#01111111, 2#00010100))
map.put(#$, glyph(2#00100100, 2#00101010, 2#01101011, 2#00101010, 2#00010010))
map.put(#%, glyph(2#01000011, 2#00110000, 2#00001000, 2#00000110, 2#01100001))
map.put(#&, glyph(2#00110000, 2#01001010, 2#01011101, 2#00110010, 2#01001000))
map.put(#', glyph(2#00000000, 2#00000000, 2#00000111, 2#00000000, 2#00000000))
map.put(#(, glyph(2#00000000, 2#00111110, 2#01000001, 2#01000001, 2#00000000))
map.put(#), glyph(2#00000000, 2#01000001, 2#01000001, 2#00111110, 2#00000000))
map.put(#*, glyph(2#00000000, 2#00010100, 2#00001000, 2#00010100, 2#00000000))
map.put(#+, glyph(2#00001000, 2#00001000, 2#00111110, 2#00001000, 2#00001000))
map.put(#,, glyph(2#00000000, 2#10000000, 2#01100000, 2#00000000, 2#00000000))
map.put(#-, glyph(2#00001000, 2#00001000, 2#00001000, 2#00001000, 2#00001000))
map.put(#., glyph(2#00000000, 2#00000000, 2#01100000, 2#00000000, 2#00000000))
map.put(#/, glyph(2#00100000, 2#00010000, 2#00001000, 2#00000100, 2#00000010))
map.put(#0, glyph(2#00111110, 2#01010001, 2#01001001, 2#01000101, 2#00111110))
map.put(#1, glyph(2#01000000, 2#01000010, 2#01111111, 2#01000000, 2#01000000))
map.put(#2, glyph(2#01100010, 2#01010001, 2#01001001, 2#01001001, 2#01000110))
map.put(#3, glyph(2#00100010, 2#01000001, 2#01001001, 2#01001001, 2#00110110))
map.put(#4, glyph(2#00011000, 2#00010100, 2#00010010, 2#00010001, 2#01111111))
map.put(#5, glyph(2#00100111, 2#01000101, 2#01000101, 2#01000101, 2#00111001))
map.put(#6, glyph(2#00111100, 2#01001010, 2#01001001, 2#01001001, 2#00110000))
map.put(#7, glyph(2#00000011, 2#00000001, 2#01110001, 2#00001001, 2#00000111))
map.put(#8, glyph(2#00110110, 2#01001001, 2#01001001, 2#01001001, 2#00110110))
map.put(#9, glyph(2#00000110, 2#01001001, 2#01001001, 2#00101001, 2#00011110))
map.put(#:, glyph(2#00000000, 2#00000000, 2#01100110, 2#00000000, 2#00000000))
map.put(#;, glyph(2#00000000, 2#10000000, 2#01100110, 2#00000000, 2#00000000))
map.put(#<, glyph(2#00001000, 2#00010100, 2#00100010, 2#01000001, 2#00000000))
map.put(#=, glyph(2#00100100, 2#00100100, 2#00100100, 2#00100100, 2#00100100))
map.put(#>, glyph(2#00000000, 2#01000001, 2#00100010, 2#00010100, 2#00001000))
map.put(#?, glyph(2#00000010, 2#00000001, 2#01010001, 2#00001001, 2#00000110))
map.put(#@, glyph(2#00111110, 2#01000001, 2#01011101, 2#01010001, 2#00011110))
map.put(#A, glyph(2#01111110, 2#00000101, 2#00000101, 2#00000101, 2#01111110))
map.put(#B, glyph(2#01111111, 2#01000101, 2#01000101, 2#01000101, 2#00111010))
map.put(#C, glyph(2#00111110, 2#01000001, 2#01000001, 2#01000001, 2#00100010))
map.put(#D, glyph(2#01111111, 2#01000001, 2#01000001, 2#01000001, 2#00111110))
map.put(#E, glyph(2#01111111, 2#01000101, 2#01000101, 2#01000001, 2#01000001))
map.put(#F, glyph(2#01111111, 2#00000101, 2#00000101, 2#00000001, 2#00000001))
map.put(#G, glyph(2#00111110, 2#01000001, 2#01000001, 2#01000101, 2#00111101))
map.put(#H, glyph(2#01111111, 2#00000100, 2#00000100, 2#00000100, 2#01111111))
map.put(#I, glyph(2#00000000, 2#01000001, 2#01111111, 2#01000001, 2#00000000))
map.put(#J, glyph(2#00100000, 2#01000000, 2#01000000, 2#01000000, 2#00111111))
map.put(#K, glyph(2#01111111, 2#00000100, 2#00000100, 2#00001010, 2#01110001))
map.put(#L, glyph(2#01111111, 2#01000000, 2#01000000, 2#01000000, 2#01000000))
map.put(#M, glyph(2#01111111, 2#00000010, 2#00000100, 2#00000010, 2#01111111))
map.put(#N, glyph(2#01111111, 2#00000010, 2#00000100, 2#00001000, 2#01111111))
map.put(#O, glyph(2#00111110, 2#01000001, 2#01000001, 2#01000001, 2#00111110))
map.put(#P, glyph(2#01111111, 2#00000101, 2#00000101, 2#00000101, 2#00000010))
map.put(#Q, glyph(2#00111110, 2#01000001, 2#01000001, 2#00100001, 2#01011110))
map.put(#R, glyph(2#01111111, 2#00000101, 2#00000101, 2#00000101, 2#01111010))
map.put(#S, glyph(2#00100010, 2#01000101, 2#01000101, 2#01000101, 2#00111001))
map.put(#T, glyph(2#00000001, 2#00000001, 2#01111111, 2#00000001, 2#00000001))
map.put(#U, glyph(2#00111111, 2#01000000, 2#01000000, 2#01000000, 2#00111111))
map.put(#V, glyph(2#00001111, 2#00110000, 2#01000000, 2#00110000, 2#00001111))
map.put(#W, glyph(2#01111111, 2#00100000, 2#00010000, 2#00100000, 2#01111111))
map.put(#X, glyph(2#01110001, 2#00001010, 2#00000100, 2#00001010, 2#01110001))