From 192e52c1fce730381aaa5603ae7668796c26120d Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 28 May 2015 14:10:16 -0700 Subject: 1504 repl still needs to handle backspacing *back into* strings. This tries to lay the groundwork. --- repl.mu | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/repl.mu b/repl.mu index 54c014fb..67d188d7 100644 --- a/repl.mu +++ b/repl.mu @@ -38,11 +38,27 @@ scenario read-instruction1 [ ] ] +# Read characters as they're typed at the keyboard, print them to the screen, +# accumulate them in a string, return the string at the end. +# Most of the complexity is for the printing to screen, to highlight strings +# and comments specially. Especially in the presence of backspacing. recipe read-instruction [ default-space:address:array:location <- new location:type, 60:literal k:address:keyboard <- next-ingredient x:address:screen <- next-ingredient result:address:buffer <- init-buffer 10:literal # string to maybe add to + result:address:buffer, k:address:keyboard, x:address:screen <- slurp-regular-characters result:address:buffer, k:address:keyboard, x:address:screen + result2:address:array:character <- buffer-to-array result:address:buffer + reply result2:address:array:character, k:address:keyboard/same-as-ingredient:0, x:address:screen/same-as-ingredient:1 +] + +# read characters from the keyboard, print them to the screen in *white*. +# Transition to other routines for comments and strings. +recipe slurp-regular-characters [ + default-space:address:array:location <- new location:type, 60:literal + result:address:buffer <- next-ingredient + k:address:keyboard <- next-ingredient + x:address:screen <- next-ingredient { +next-character # read character @@ -51,12 +67,12 @@ recipe read-instruction [ { ctrl-d?:boolean <- equal c:character, 4:literal/ctrl-d/eof break-unless ctrl-d?:boolean - reply 0:literal, k:address:keyboard/same-as-ingredient:0, x:address:screen/same-as-ingredient:1 + reply 0:literal, k:address:keyboard/same-as-ingredient:1, x:address:screen/same-as-ingredient:2 } { null?:boolean <- equal c:character, 0:literal/null break-unless null?:boolean - reply 0:literal, k:address:keyboard/same-as-ingredient:0, x:address:screen/same-as-ingredient:1 + reply 0:literal, k:address:keyboard/same-as-ingredient:1, x:address:screen/same-as-ingredient:2 } # comment? { @@ -86,13 +102,13 @@ recipe read-instruction [ break-if done?:boolean loop } - result2:address:array:character <- buffer-to-array result:address:buffer - reply result2:address:array:character, k:address:keyboard/same-as-ingredient:0, x:address:screen/same-as-ingredient:1 + reply result:address:buffer/same-as-ingredient:0, k:address:keyboard/same-as-ingredient:1, x:address:screen/same-as-ingredient:2 ] -# Simpler version of read-instruction that prints in the comment color and -# doesn't handle comments or strings. Tracks an extra count in case we -# backspace out of it +# read characters from keyboard, print them to screen in the comment color. +# +# Simpler version of slurp-regular-characters; doesn't handle comments or +# strings. Tracks an extra count in case we backspace out of it recipe slurp-comment [ default-space:address:array:location <- new location:type, 60:literal result:address:buffer <- next-ingredient @@ -141,9 +157,13 @@ recipe slurp-comment [ reply result:address:buffer, k:address:keyboard/same-as-ingredient:1, x:address:screen/same-as-ingredient:2 ] -# Version of read-instruction that prints in the string color and doesn't -# handle comments. Does handle nested strings. Tracks an extra count in case -# we backspace out of it, which it needs to return because recursion. +# read characters from keyboard, print them to screen in the string color and +# accumulate them into a buffer. +# +# Version of slurp-regular-characters that: +# a) doesn't handle comments +# b) handles nested strings using recursive calls to itself. Tracks an extra +# count in case we backspace out of it. recipe slurp-string [ default-space:address:array:location <- new location:type, 60:literal result:address:buffer <- next-ingredient @@ -174,7 +194,7 @@ recipe slurp-string [ result:address:buffer <- buffer-append result:address:buffer, c:character # make a recursive call to handle nested strings result:address:buffer, tmp:number, k:address:keyboard, x:address:screen <- slurp-string result:address:buffer, k:address:keyboard, x:address:screen - # but if we backspace over a completed string handle it in the caller + # but if we backspace over a completed nested string, handle it in the caller characters-slurped:number <- add characters-slurped:number, tmp:number, 1:literal # for the leading '[' loop +next-character:label } @@ -491,3 +511,27 @@ scenario read-instruction-cancel-string-inside-string-on-backspace [ . . ] ] + +scenario read-instruction-backspace-back-into-string [ + assume-screen 30:literal/width, 5:literal/height + # need to escape the '[' once for 'scenario' and once for 'assume-keyboard' + assume-keyboard [\[a\]