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