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