about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-05-27 14:56:01 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-05-27 14:56:01 -0700
commit7199af30e8fdcdc0848a2e2b90014b98f97998b3 (patch)
tree056cf4c6499071133a5010e3f869c6044ba8096f
parent70763af34945192d04605029339b58b3f3434371 (diff)
downloadmu-7199af30e8fdcdc0848a2e2b90014b98f97998b3.tar.gz
1485 - start of a repl tool
Doesn't actually work interactively yet; for some reason it prints in
color, enter doesn't work, etc.

It'll be interesting to try to add color and history as separate
'layers' using before/after.

I'll also likely have to delete traces for its tests at some point as
they inevitably explode in size.
-rw-r--r--060string.mu28
-rw-r--r--074keyboard.mu4
-rw-r--r--repl.mu72
3 files changed, 102 insertions, 2 deletions
diff --git a/060string.mu b/060string.mu
index 480f7db5..227115ad 100644
--- a/060string.mu
+++ b/060string.mu
@@ -279,6 +279,34 @@ recipe integer-to-decimal-string [
   reply result:address:array:character
 ]
 
+recipe buffer-to-array [
+  default-space:address:array:character <- new location:type, 30:literal
+  in:address:buffer <- next-ingredient
+  len:number <- get in:address:buffer/deref, length:offset
+#?   $print [size ], len:number, [ 
+#? ] #? 1
+  s:address:array:character <- get in:address:buffer/deref, data:offset
+  {
+    # propagate null buffer
+    break-if s:address:array:character
+    reply 0:literal
+  }
+  # we can't just return s because it is usually the wrong length
+  result:address:array:character <- new character:type, len:number
+  i:number <- copy 0:literal
+  {
+#?     $print i:number #? 1
+    done?:boolean <- greater-or-equal i:number, len:number
+    break-if done?:boolean
+    src:character <- index s:address:array:character/deref, i:number
+    dest:address:character <- index-address result:address:array:character/deref, i:number
+    dest:address:character/deref <- copy src:character
+    i:number <- add i:number, 1:literal
+    loop
+  }
+  reply result:address:array:character
+]
+
 scenario integer-to-decimal-digit-zero [
   run [
     1:address:array:character/raw <- integer-to-decimal-string 0:literal
diff --git a/074keyboard.mu b/074keyboard.mu
index a991a71b..91fdb501 100644
--- a/074keyboard.mu
+++ b/074keyboard.mu
@@ -47,7 +47,7 @@ recipe wait-for-key [
   {
     break-unless x:address:keyboard
     # on fake keyboards 'wait-for-key' behaves just like 'read-key'
-    c:character, x:address:keyboard <- read-key x:address:keyboard
+    c:character, found?:boolean, x:address:keyboard <- read-key x:address:keyboard
     reply c:character, x:address:keyboard/same-as-ingredient:0
   }
   c:character <- wait-for-key-from-keyboard
@@ -60,7 +60,7 @@ recipe send-keys-to-channel [
   chan:address:channel <- next-ingredient
   screen:address <- next-ingredient
   {
-    c:character, found?:boolean <- read-key keyboard:address
+    c:character, found?:boolean, keyboard:address <- read-key keyboard:address
     loop-unless found?:boolean
 #?     print-integer screen:address, c:character #? 1
     print-character screen:address, c:character
diff --git a/repl.mu b/repl.mu
new file mode 100644
index 00000000..0099d2cc
--- /dev/null
+++ b/repl.mu
@@ -0,0 +1,72 @@
+# interactive prompt for mu
+
+recipe main [
+  default-space:address:array:location <- new location:type, 30:literal
+  switch-to-display
+  msg:address:array:character <- new [ready! type in an instruction, then hit enter. ctrl-d exits.
+]
+  0:literal/real-screen <- print-string 0:literal/real-screen, msg:address:array:character
+  {
+    inst:address:array:character, 0:literal/real-keyboard, 0:literal/real-screen <- read-instruction 0:literal/real-keyboard, 0:literal/real-screen
+    break-unless inst:address:array:character
+    0:literal/real-screen <- print-string 0:literal/real-screen, inst:address:array:character
+    loop
+  }
+  wait-for-key-from-keyboard
+  return-to-console
+]
+
+# basic keyboard input; just text and enter
+scenario read-instruction1 [
+  assume-screen 30:literal/width, 5:literal/height
+  assume-keyboard [x <- copy y
+]
+  run [
+    1:address:array:character <- read-instruction keyboard:address, screen:address
+    2:address:array:character <- new [=> ]
+    print-string screen:address, 2:address:array:character
+    print-string screen:address, 1:address:array:character
+  ]
+  screen-should-contain [
+    .x <- copy y                   .
+    .=> x <- copy y                .
+    .                              .
+  ]
+  screen-should-contain-in-color 7:literal/white, [
+    .x <- copy y                   .
+    .=> x <- copy y                .
+    .                              .
+  ]
+]
+
+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
+  {
+    c:character, k:address:keyboard <- wait-for-key k:address:keyboard
+#?     $print c:character, [ 
+#? ] #? 1
+    print-character x:address:screen, c:character
+    # quit?
+    {
+      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
+    }
+    {
+      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
+    }
+    # append
+    result:address:buffer <- buffer-append result:address:buffer, c:character
+    # done with this instruction?
+    done?:boolean <- equal c:character, 10:literal/newline
+    break-if done?:boolean
+    loop
+  }
+  result2:address:array:character <- buffer-to-array result:address:buffer
+  reply result2:address:array:character
+]