1
2
3
4
5
6 def! main [
7 local-scope
8 open-console
9 env:&:environment <- new-programming-environment 0/filesystem, 0/screen
10 render-all 0/screen, env, render
11 event-loop 0/screen, 0/console, env, 0/filesystem
12
13 ]
14
15 container environment [
16 recipes:&:editor
17 current-sandbox:&:editor
18 sandbox-in-focus?:bool
19 ]
20
21 def new-programming-environment resources:&:resources, screen:&:screen, test-sandbox-editor-contents:text -> result:&:environment [
22 local-scope
23 load-ingredients
24 width:num <- screen-width screen
25 result <- new environment:type
26
27 initial-recipe-contents:text <- slurp resources, [lesson/recipes.mu]
28 divider:num, _ <- divide-with-remainder width, 2
29 recipes:&:editor <- new-editor initial-recipe-contents, 0/left, divider/right
30
31 sandbox-left:num <- add divider, 1
32 current-sandbox:&:editor <- new-editor test-sandbox-editor-contents, sandbox-left, width/right
33 *result <- put *result, recipes:offset, recipes
34 *result <- put *result, current-sandbox:offset, current-sandbox
35 *result <- put *result, sandbox-in-focus?:offset, 0/false
36 <programming-environment-initialization>
37 ]
38
39 def event-loop screen:&:screen, console:&:console, env:&:environment, resources:&:resources -> screen:&:screen, console:&:console, env:&:environment, resources:&:resources [
40 local-scope
41 load-ingredients
42 recipes:&:editor <- get *env, recipes:offset
43 current-sandbox:&:editor <- get *env, current-sandbox:offset
44 sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
45
46
47
48 render-all-on-no-more-events?:bool <- copy 0/false
49 {
50 ¦
51 ¦ +next-event
52 ¦ e:event, found?:bool, quit?:bool, console <- read-event console
53 ¦ loop-unless found?
54 ¦ break-if quit?
55 ¦ trace 10, [app], [next-event]
56 ¦ <handle-event>
57 ¦
58 ¦ {
59 ¦ ¦ k:num, is-keycode?:bool <- maybe-convert e:event, keycode:variant
60 ¦ ¦ break-unless is-keycode?
61 ¦ ¦ <global-keypress>
62 ¦ }
63 ¦ {
64 ¦ ¦ c:char, is-unicode?:bool <- maybe-convert e:event, text:variant
65 ¦ ¦ break-unless is-unicode?
66 ¦ ¦ <global-type>
67 ¦ }
68 ¦
69 ¦ {
70 ¦ ¦ t:touch-event, is-touch?:bool <- maybe-convert e:event, touch:variant
71 ¦ ¦ break-unless is-touch?
72 ¦ ¦
73 ¦ ¦
74 ¦ ¦ touch-type:num <- get t, type:offset
75 ¦ ¦ is-left-click?:bool <- equal touch-type, 65513/mouse-left
76 ¦ ¦ loop-unless is-left-click?, +next-event
77 ¦ ¦ click-row:num <- get t, row:offset
78 ¦ ¦ click-column:num <- get t, column:offset
79 ¦ ¦
80 ¦ ¦ <global-touch>
81 ¦ ¦
82 ¦ ¦ _ <- move-cursor-in-editor screen, recipes, t
83 ¦ ¦ sandbox-in-focus?:bool <- move-cursor-in-editor screen, current-sandbox, t
84 ¦ ¦ *env <- put *env, sandbox-in-focus?:offset, sandbox-in-focus?
85 ¦ ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
86 ¦ ¦ loop +next-event
87 ¦ }
88 ¦
89 ¦
90 ¦ {
91 ¦ ¦ r:resize-event, is-resize?:bool <- maybe-convert e:event, resize:variant
92 ¦ ¦ break-unless is-resize?
93 ¦ ¦ env, screen <- resize screen, env
94 ¦ ¦ screen <- render-all screen, env, render-without-moving-cursor
95 ¦ ¦ loop +next-event
96 ¦ }
97 ¦
98 ¦ {
99 ¦ ¦ hide-screen screen
100 ¦ ¦ sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
101 ¦ ¦ {
102 ¦ ¦ ¦ break-if sandbox-in-focus?
103 ¦ ¦ ¦ render?:bool <- handle-keyboard-event screen, recipes, e:event
104 ¦ ¦ ¦
105 ¦ ¦ ¦
106 ¦ ¦ ¦ more-events?:bool <- has-more-events? console
107 ¦ ¦ ¦ {
108 ¦ ¦ ¦ ¦ break-unless more-events?
109 ¦ ¦ ¦ ¦ render-all-on-no-more-events? <- copy 1/true
110 ¦ ¦ ¦ ¦ jump +finish-event
111 ¦ ¦ ¦ }
112 ¦ ¦ ¦ {
113 ¦ ¦ ¦ ¦ break-if more-events?
114 ¦ ¦ ¦ ¦ {
115 ¦ ¦ ¦ ¦ ¦ break-unless render-all-on-no-more-events?
116 ¦ ¦ ¦ ¦ ¦
117 ¦ ¦ ¦ ¦ ¦ screen <- render-all screen, env, render
118 ¦ ¦ ¦ ¦ ¦ render-all-on-no-more-events? <- copy 0/false
119 ¦ ¦ ¦ ¦ ¦ jump +finish-event
120 ¦ ¦ ¦ ¦ }
121 ¦ ¦ ¦ ¦
122 ¦ ¦ ¦ ¦ {
123 ¦ ¦ ¦ ¦ ¦ break-unless render?
124 ¦ ¦ ¦ ¦ ¦ screen <- render-recipes screen, env, render
125 ¦ ¦ ¦ ¦ ¦ jump +finish-event
126 ¦ ¦ ¦ ¦ }
127 ¦ ¦ ¦ }
128 ¦ ¦ }
129 ¦ ¦ {
130 ¦ ¦ ¦ break-unless sandbox-in-focus?
131 ¦ ¦ ¦ render?:bool <- handle-keyboard-event screen, current-sandbox, e:event
132 ¦ ¦ ¦
133 ¦ ¦ ¦
134 ¦ ¦ ¦ more-events?:bool <- has-more-events? console
135 ¦ ¦ ¦ {
136 ¦ ¦ ¦ ¦ break-unless more-events?
137 ¦ ¦ ¦ ¦ render-all-on-no-more-events? <- copy 1/true
138 ¦ ¦ ¦ ¦ jump +finish-event
139 ¦ ¦ ¦ }
140 ¦ ¦ ¦ {
141 ¦ ¦ ¦ ¦ break-if more-events?
142 ¦ ¦ ¦ ¦ {
143 ¦ ¦ ¦ ¦ ¦ break-unless render-all-on-no-more-events?
144 ¦ ¦ ¦ ¦ ¦
145 ¦ ¦ ¦ ¦ ¦ screen <- render-all screen, env, render
146 ¦ ¦ ¦ ¦ ¦ render-all-on-no-more-events? <- copy 0/false
147 ¦ ¦ ¦ ¦ ¦ jump +finish-event
148 ¦ ¦ ¦ ¦ }
149 ¦ ¦ ¦ ¦
150 ¦ ¦ ¦ ¦ {
151 ¦ ¦ ¦ ¦ ¦ break-unless render?
152 ¦ ¦ ¦ ¦ ¦ screen <- render-sandbox-side screen, env, render
153 ¦ ¦ ¦ ¦ ¦ jump +finish-event
154 ¦ ¦ ¦ ¦ }
155 ¦ ¦ ¦ }
156 ¦ ¦ }
157 ¦ ¦ +finish-event
158 ¦ ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
159 ¦ ¦ show-screen screen
160 ¦ }
161 ¦ loop
162 }
163 ]
164
165 def resize screen:&:screen, env:&:environment -> env:&:environment, screen:&:screen [
166 local-scope
167 load-ingredients
168 clear-screen screen
169 width:num <- screen-width screen
170 divider:num, _ <- divide-with-remainder width, 2
171
172 recipes:&:editor <- get *env, recipes:offset
173 right:num <- subtract divider, 1
174 *recipes <- put *recipes, right:offset, right
175
176 *recipes <- put *recipes, cursor-row:offset, 1
177 *recipes <- put *recipes, cursor-column:offset, 0
178
179 current-sandbox:&:editor <- get *env, current-sandbox:offset
180 left:num <- add divider, 1
181 *current-sandbox <- put *current-sandbox, left:offset, left
182 right:num <- subtract width, 1
183 *current-sandbox <- put *current-sandbox, right:offset, right
184
185 *current-sandbox <- put *current-sandbox, cursor-row:offset, 1
186 *current-sandbox <- put *current-sandbox, cursor-column:offset, left
187 ]
188
189
190
191
192 def render-without-moving-cursor screen:&:screen, editor:&:editor -> last-row:num, last-column:num, screen:&:screen, editor:&:editor [
193 local-scope
194 load-ingredients
195 return-unless editor, 1/top, 0/left
196 left:num <- get *editor, left:offset
197 screen-height:num <- screen-height screen
198 right:num <- get *editor, right:offset
199 curr:&:duplex-list:char <- get *editor, top-of-screen:offset
200 prev:&:duplex-list:char <- copy curr
201 curr <- next curr
202 color:num <- copy 7/white
203 row:num <- copy 1/top
204 column:num <- copy left
205
206 old-before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset
207
208
209 *editor <- put *editor, cursor-row:offset, row
210 *editor <- put *editor, cursor-column:offset, column
211 top-of-screen:&:duplex-list:char <- get *editor, top-of-screen:offset
212 *editor <- put *editor, before-cursor:offset, top-of-screen
213 screen <- move-cursor screen, row, column
214 {
215 ¦ +next-character
216 ¦ break-unless curr
217 ¦ off-screen?:bool <- greater-or-equal row, screen-height
218 ¦ break-if off-screen?
219 ¦
220 ¦
221 ¦
222 ¦ {
223 ¦ ¦ at-cursor?:bool <- equal old-before-cursor, prev
224 ¦ ¦ break-unless at-cursor?
225 ¦ ¦ *editor <- put *editor, cursor-row:offset, row
226 ¦ ¦ *editor <- put *editor, cursor-column:offset, column
227 ¦ ¦ *editor <- put *editor, before-cursor:offset, old-before-cursor
228 ¦ }
229 ¦ c:char <- get *curr, value:offset
230 ¦ <character-c-received>
231 ¦ {
232 ¦ ¦
233 ¦ ¦ newline?:bool <- equal c, 10/newline
234 ¦ ¦ break-unless newline?
235 ¦ ¦
236 ¦ ¦ clear-line-until screen, right
237 ¦ ¦
238 ¦ ¦ row <- add row, 1
239 ¦ ¦ column <- copy left
240 ¦ ¦ screen <- move-cursor screen, row, column
241 ¦ ¦ curr <- next curr
242 ¦ ¦ prev <- next prev
243 ¦ ¦ loop +next-character
244 ¦ }
245 ¦ {
246 ¦ ¦
247 ¦ ¦
248 ¦ ¦ at-right?:bool <- equal column, right
249 ¦ ¦ break-unless at-right?
250 ¦ ¦
251 ¦ ¦ wrap-icon:char <- copy 8617/loop-back-to-left
252 ¦ ¦ print screen, wrap-icon, 245/grey
253 ¦ ¦ column <- copy left
254 ¦ ¦ row <- add row, 1
255 ¦ ¦ screen <- move-cursor screen, row, column
256 ¦ ¦
257 ¦ ¦ loop +next-character
258 ¦ }
259 ¦ print screen, c, color
260 ¦ curr <- next curr
261 ¦ prev <- next prev
262 ¦ column <- add column, 1
263 ¦ loop
264 }
265
266 *editor <- put *editor, bottom-of-screen:offset, curr
267 *editor <- put *editor, bottom:offset, row
268 return row, column
269 ]
270
271 scenario point-at-multiple-editors [
272 local-scope
273 trace-until 100/app
274 assume-screen 30/width, 5/height
275
276 assume-resources [
277 ¦ [lesson/recipes.mu] <- [
278 ¦ ¦ |abc|
279 ¦ ]
280 ]
281 env:&:environment <- new-programming-environment resources, screen, [def]
282
283 assume-console [
284 ¦ left-click 1, 1
285 ¦ left-click 1, 17
286 ]
287
288 run [
289 ¦ event-loop screen, console, env, resources
290 ¦ recipes:&:editor <- get *env, recipes:offset
291 ¦ 5:num/raw <- get *recipes, cursor-column:offset
292 ¦ sandbox:&:editor <- get *env, current-sandbox:offset
293 ¦ 7:num/raw <- get *sandbox, cursor-column:offset
294 ]
295 memory-should-contain [
296 ¦ 5 <- 1
297 ¦ 7 <- 17
298 ]
299 ]
300
301 scenario edit-multiple-editors [
302 local-scope
303 trace-until 100/app
304 assume-screen 30/width, 5/height
305
306 assume-resources [
307 ¦ [lesson/recipes.mu] <- [
308 ¦ ¦ |abc|
309 ¦ ]
310 ]
311 env:&:environment <- new-programming-environment resources, screen, [def]
312 render-all screen, env, render
313
314 assume-console [
315 ¦ left-click 1, 1
316 ¦ type [0]
317 ¦ left-click 1, 17
318 ¦ type [1]
319 ]
320 run [
321 ¦ event-loop screen, console, env, resources
322 ¦ recipes:&:editor <- get *env, recipes:offset
323 ¦ 5:num/raw <- get *recipes, cursor-column:offset
324 ¦ sandbox:&:editor <- get *env, current-sandbox:offset
325 ¦ 7:num/raw <- get *sandbox, cursor-column:offset
326 ]
327 screen-should-contain [
328 ¦ . run (F4) . # this line has a different background, but we don't test that yet
329 ¦ .a0bc ╎d1ef .
330 ¦ . ╎──────────────.
331 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
332 ¦ . ╎ .
333 ]
334 memory-should-contain [
335 ¦ 5 <- 2
336 ¦ 7 <- 18
337 ]
338
339 run [
340 ¦ cursor:char <- copy 9251/␣
341 ¦ print screen, cursor
342 ]
343 screen-should-contain [
344 ¦ . run (F4) .
345 ¦ .a0bc ╎d1␣f .
346 ¦ . ╎──────────────.
347 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
348 ¦ . ╎ .
349 ]
350 ]
351
352 scenario editor-in-focus-keeps-cursor [
353 local-scope
354 trace-until 100/app
355 assume-screen 30/width, 5/height
356 assume-resources [
357 ¦ [lesson/recipes.mu] <- [
358 ¦ ¦ |abc|
359 ¦ ]
360 ]
361 env:&:environment <- new-programming-environment resources, screen, [def]
362 render-all screen, env, render
363
364 assume-console []
365 run [
366 ¦ event-loop screen, console, env, resources
367 ¦ cursor:char <- copy 9251/␣
368 ¦ print screen, cursor
369 ]
370
371 screen-should-contain [
372 ¦ . run (F4) .
373 ¦ .␣bc ╎def .
374 ¦ . ╎──────────────.
375 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
376 ¦ . ╎ .
377 ]
378
379 assume-console [
380 ¦ type [z]
381 ]
382 run [
383 ¦ event-loop screen, console, env, resources
384 ¦ cursor:char <- copy 9251/␣
385 ¦ print screen, cursor
386 ]
387
388 screen-should-contain [
389 ¦ . run (F4) .
390 ¦ .z␣bc ╎def .
391 ¦ . ╎──────────────.
392 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎ .
393 ¦ . ╎ .
394 ]
395 ]
396
397 scenario backspace-in-sandbox-editor-joins-lines [
398 local-scope
399 trace-until 100/app
400 assume-screen 30/width, 5/height
401 assume-resources [
402 ]
403
404 test-sandbox-editor-contents:text <- new [abc
405 def]
406 env:&:environment <- new-programming-environment resources, screen, test-sandbox-editor-contents
407 render-all screen, env, render
408 screen-should-contain [
409 ¦ . run (F4) .
410 ¦ . ╎abc .
411 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎def .
412 ¦ . ╎──────────────.
413 ¦ . ╎ .
414 ]
415
416 assume-console [
417 ¦ left-click 2, 16
418 ¦ press backspace
419 ]
420 run [
421 ¦ event-loop screen, console, env, resources
422 ¦ cursor:char <- copy 9251/␣
423 ¦ print screen, cursor
424 ]
425
426 screen-should-contain [
427 ¦ . run (F4) .
428 ¦ . ╎abc␣ef .
429 ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎──────────────.
430 ¦ . ╎ .
431 ]
432 ]
433
434 def render-all screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
435 local-scope
436 load-ingredients
437 trace 10, [app], [render all]
438 hide-screen screen
439
440 trace 11, [app], [render top menu]
441 width:num <- screen-width screen
442 draw-horizontal screen, 0, 0/left, width, 32/space, 0/black, 238/grey
443 button-start:num <- subtract width, 20
444 button-on-screen?:bool <- greater-or-equal button-start, 0
445 assert button-on-screen?, [screen too narrow for menu]
446 screen <- move-cursor screen, 0/row, button-start
447 print screen, [ run (F4) ], 255/white, 161/reddish
448
449 trace 11, [app], [render divider]
450 divider:num, _ <- divide-with-remainder width, 2
451 height:num <- screen-height screen
452 draw-vertical screen, divider, 1/top, height, 9482/vertical-dotted
453
454 screen <- render-recipes screen, env, render-editor
455 screen <- render-sandbox-side screen, env, render-editor
456 <render-components-end>
457
458 recipes:&:editor <- get *env, recipes:offset
459 current-sandbox:&:editor <- get *env, current-sandbox:offset
460 sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
461 screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
462
463 show-screen screen
464 ]
465
466 def render-recipes screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
467 local-scope
468 load-ingredients
469 trace 11, [app], [render recipes]
470 recipes:&:editor <- get *env, recipes:offset
471
472 left:num <- get *recipes, left:offset
473 right:num <- get *recipes, right:offset
474 row:num, column:num, screen <- call render-editor, screen, recipes
475 clear-line-until screen, right
476 row <- add row, 1
477 <render-recipe-components-end>
478
479 draw-horizontal screen, row, left, right, 9480/horizontal-dotted
480 row <- add row, 1
481 clear-screen-from screen, row, left, left, right
482 ]
483
484
485 def render-sandbox-side screen:&:screen, env:&:environment, {render-editor: (recipe (address screen) (address editor) -> number number (address screen) (address editor))} -> screen:&:screen, env:&:environment [
486 local-scope
487 load-ingredients
488 current-sandbox:&:editor <- get *env, current-sandbox:offset
489 left:num <- get *current-sandbox, left:offset
490 right:num <- get *current-sandbox, right:offset
491 row:num, column:num, screen, current-sandbox <- call render-editor, screen, current-sandbox
492 clear-line-until screen, right
493 row <- add row, 1
494
495 draw-horizontal screen, row, left, right
496 row <- add row, 1
497 clear-screen-from screen, row, left, left, right
498 ]
499
500 def update-cursor screen:&:screen, recipes:&:editor, current-sandbox:&:editor, sandbox-in-focus?:bool, env:&:environment -> screen:&:screen [
501 local-scope
502 load-ingredients
503 <update-cursor-special-cases>
504 {
505 ¦ break-if sandbox-in-focus?
506 ¦ cursor-row:num <- get *recipes, cursor-row:offset
507 ¦ cursor-column:num <- get *recipes, cursor-column:offset
508 }
509 {
510 ¦ break-unless sandbox-in-focus?
511 ¦ cursor-row:num <- get *current-sandbox, cursor-row:offset
512 ¦ cursor-column:num <- get *current-sandbox, cursor-column:offset
513 }
514 screen <- move-cursor screen, cursor-row, cursor-column
515 ]
516
517
518
519 after <global-type> [
520 {
521 ¦ redraw-screen?:bool <- equal c, 12/ctrl-l
522 ¦ break-unless redraw-screen?
523 ¦ screen <- render-all screen, env:&:environment, render
524 ¦ sync-screen screen
525 ¦ loop +next-event
526 }
527 ]
528
529
530
531
532 after <global-type> [
533 {
534 ¦ switch-side?:bool <- equal c, 14/ctrl-n
535 ¦ break-unless switch-side?
536 ¦ sandbox-in-focus?:bool <- get *env, sandbox-in-focus?:offset
537 ¦ sandbox-in-focus? <- not sandbox-in-focus?
538 ¦ *env <- put *env, sandbox-in-focus?:offset, sandbox-in-focus?
539 ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
540 ¦ loop +next-event
541 }
542 ]
543
544
545
546 def draw-vertical screen:&:screen, col:num, y:num, bottom:num -> screen:&:screen [
547 local-scope
548 load-ingredients
549 style:char, style-found?:bool <- next-ingredient
550 {
551 ¦ break-if style-found?
552 ¦ style <- copy 9474/vertical
553 }
554 color:num, color-found?:bool <- next-ingredient
555 {
556 ¦
557 ¦ break-if color-found?
558 ¦ color <- copy 245/grey
559 }
560 {
561 ¦ continue?:bool <- lesser-than y, bottom
562 ¦ break-unless continue?
563 ¦ screen <- move-cursor screen, y, col
564 ¦ print screen, style, color
565 ¦ y <- add y, 1
566 ¦ loop
567 }
568 ]