about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-05-14 12:06:16 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-05-14 12:27:31 -0700
commitaf336c444f4fe9d8a1677a200f40748ce5f24a26 (patch)
treeb06b1c2323a5033194724171af7681e760bd546b
parentb98d3876b67a35f1d913ba374749bc97103b7790 (diff)
downloadmu-af336c444f4fe9d8a1677a200f40748ce5f24a26.tar.gz
1368 - alias carriage-return and newline
CRLF still shows as two newlines, though. Cross that bridge when we get
to it.

The new chessboard test is still hanging, though.
-rw-r--r--061channel.mu10
-rw-r--r--070display.cc5
-rw-r--r--071print.mu4
-rw-r--r--074keyboard.mu2
-rw-r--r--chessboard.mu67
-rw-r--r--termbox/termbox.h3
6 files changed, 79 insertions, 12 deletions
diff --git a/061channel.mu b/061channel.mu
index bb18f931..5445447d 100644
--- a/061channel.mu
+++ b/061channel.mu
@@ -280,6 +280,8 @@ scenario channel-read-not-full [
 # out:address:channel <- buffer-lines in:address:channel, out:address:channel
 recipe buffer-lines [
   default-space:address:address:array:location <- new location:type, 30:literal
+#?   $print [buffer-lines: aaa
+#? ]
   in:address:channel <- next-ingredient
   out:address:channel <- next-ingredient
   # repeat forever
@@ -313,13 +315,17 @@ recipe buffer-lines [
         loop +next-character:label
       }
       # append anything else
+#?       $print [buffer-lines: appending ], c:character, [ 
+#? ]
       line:address:buffer <- buffer-append line:address:buffer, c:character
-      line-done?:boolean <- equal c:character, 13:literal/newline
+      line-done?:boolean <- equal c:character, 10:literal/newline
       break-if line-done?:boolean
       loop
     }
 #?     return-to-console #? 1
     # copy line into 'out'
+#?     $print [buffer-lines: emitting
+#? ]
     i:number <- copy 0:literal
     line-contents:address:array:character <- get line:address:buffer/deref, data:offset
     max:number <- get line:address:buffer/deref, length:offset
@@ -368,7 +374,7 @@ F buffer-lines-blocks-until-newline: channel should be empty after writing 'a']
     assert 7:boolean, [
 F buffer-lines-blocks-until-newline: channel should be empty after writing 'b']
     # write newline
-    1:address:channel <- write 1:address:channel, 13:literal/newline
+    1:address:channel <- write 1:address:channel, 10:literal/newline
     restart 4:number/buffer-routine
     wait-for-routine 4:number/buffer-routine
     8:boolean <- channel-empty? 2:address:channel/buffered-stdin
diff --git a/070display.cc b/070display.cc
index 8a782f87..b93731ab 100644
--- a/070display.cc
+++ b/070display.cc
@@ -214,9 +214,10 @@ case READ_KEY_FROM_KEYBOARD: {
   long long int found = false;
 //?   cerr << event_type << '\n'; //? 1
   if (event_type == TB_EVENT_KEY) {
-    if (event.key == TB_KEY_CTRL_C) tb_shutdown(), exit(1);
-    if (event.key == TB_KEY_BACKSPACE2) event.key = TB_KEY_BACKSPACE;
     result = event.key ? event.key : event.ch;
+    if (result == TB_KEY_CTRL_C) tb_shutdown(), exit(1);
+    if (result == TB_KEY_BACKSPACE2) result = TB_KEY_BACKSPACE;
+    if (result == TB_KEY_CARRIAGE_RETURN) result = TB_KEY_NEWLINE;
     found = true;
   }
   products.resize(2);
diff --git a/071print.mu b/071print.mu
index 411c8319..ac8ae57c 100644
--- a/071print.mu
+++ b/071print.mu
@@ -67,7 +67,7 @@ recipe print-character [
     max-row:number <- subtract height:number, 1:literal
     # special-case: newline
     {
-      newline?:boolean <- equal c:character, 13:literal
+      newline?:boolean <- equal c:character, 10:literal/newlin
 #?       $print c:character, [ ], newline?:boolean, [ #? 1
 #? ] #? 1
       break-unless newline?:boolean
@@ -153,7 +153,7 @@ scenario print-newline-character [
 #?     $start-tracing #? 3
     1:address:screen <- init-fake-screen 3:literal/width, 2:literal/height
     1:address:screen <- print-character 1:address:screen, 97:literal  # 'a'
-    1:address:screen <- print-character 1:address:screen, 13:literal/newline
+    1:address:screen <- print-character 1:address:screen, 10:literal/newline
     2:number <- get 1:address:screen/deref, cursor-row:offset
     3:number <- get 1:address:screen/deref, cursor-column:offset
     4:address:array:character <- get 1:address:screen/deref, data:offset
diff --git a/074keyboard.mu b/074keyboard.mu
index fbbaad48..34f6789c 100644
--- a/074keyboard.mu
+++ b/074keyboard.mu
@@ -29,7 +29,7 @@ recipe read-key [
     {
       done?:boolean <- greater-or-equal idx:address:number/deref, max:number
       break-unless done?:boolean
-      reply 0:literal, 0:literal/done, x:address:keyboard/same-as-ingredient:0
+      reply 0:literal, 0:literal/found, x:address:keyboard/same-as-ingredient:0
     }
     c:character <- index buf:address:array:character/deref, idx:address:number/deref
     idx:address:number/deref <- add idx:address:number/deref, 1:literal
diff --git a/chessboard.mu b/chessboard.mu
index e6e28611..ca722405 100644
--- a/chessboard.mu
+++ b/chessboard.mu
@@ -22,10 +22,57 @@ recipe main [
   return-to-console  # cleanup screen and keyboard
 ]
 
+# But enough about mu. Here's what it looks like to run the chessboard
+# program.
+
+scenario print-board-and-read-move [
+  assume-screen 30:literal/width, 12:literal/height
+  assume-keyboard [a2-a4
+]  # newline is important
+  run [
+    screen:address, keyboard:address <- chessboard screen:address, keyboard:address
+  ]
+  screen-should-contain [
+  #  012345678901234567890123456789
+    .8 | r n b q k b n r           .
+    .7 | p p p p p p p p           .
+    .6 |                           .
+    .5 |                           .
+    .4 |                           .
+    .3 |                           .
+    .2 | P P P P P P P P           .
+    .1 | R N B Q K B N R           .
+    .  +----------------           .
+    .    a b c d e f g h           .
+    .                              .
+    .                              .
+  ]
+#?   assume-keyboard [a2-a4]
+#?   screen-should-contain [
+#?   #  012345678901234567890123456789
+#?     .8 | r n b q k b n r           .
+#?     .7 | p p p p p p p p           .
+#?     .6 |                           .
+#?     .5 |                           .
+#?     .4 |                           .
+#?     .3 |                           .
+#?     .2 | P P P P P P P P           .
+#?     .1 | R N B Q K B N R           .
+#?     .  +----------------           .
+#?     .    a b c d e f g h           .
+#?     .                              .
+#?     .                              .
+#?   ]
+]
+
 recipe chessboard [
+#?   $start-tracing [schedule] #? 1
+#?   $start-tracing #? 1
   default-space:address:array:location <- new location:type, 30:literal
   screen:address <- next-ingredient
   keyboard:address <- next-ingredient
+  $print [screen: ], screen:address, [, keyboard: ], keyboard:address, [ 
+]
   board:address:array:address:array:character <- initial-position
   # hook up stdin
   stdin:address:channel <- init-channel 10:literal/capacity
@@ -37,21 +84,31 @@ recipe chessboard [
     msg:address:array:character <- new [Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves.
 ]
     print-string screen:address, msg:address:array:character
+    $print [aaa
+]
     cursor-to-next-line screen:address
     print-board screen:address, board:address:array:address:array:character
     cursor-to-next-line screen:address
     msg:address:array:character <- new [Type in your move as <from square>-<to square>. For example: 'a2-a4'. Then press <enter>.
 ]
     print-string screen:address, msg:address:array:character
+    $print [bbb
+]
     cursor-to-next-line screen:address
     msg:address:array:character <- new [Hit 'q' to exit.
 ]
     print-string screen:address, msg:address:array:character
+    $print [ccc
+]
     {
       cursor-to-next-line screen:address
       msg:address:array:character <- new [move: ]
       print-string screen:address, msg:address:array:character
+    $print [ddd
+]
       m:address:move, quit:boolean, error:boolean <- read-move buffered-stdin:address:channel, screen:address
+    $print [eee
+]
       break-if quit:boolean, +quit:offset
       buffered-stdin:address:channel <- clear-channel buffered-stdin:address:channel  # cleanup after error. todo: test this?
       loop-if error:boolean
@@ -234,7 +291,7 @@ recipe read-move [
   reply-if quit?:boolean, 0:literal/dummy, quit?:boolean, error?:boolean
   reply-if error?:boolean, 0:literal/dummy, quit?:boolean, error?:boolean
 #?   $exit #? 1
-  error?:boolean <- expect-from-channel stdin:address:channel, 13:literal/newline, screen:address
+  error?:boolean <- expect-from-channel stdin:address:channel, 10:literal/newline, screen:address
   reply-if error?:boolean, 0:literal/dummy, 0:literal/quit, error?:boolean
   reply result:address:move, quit?:boolean, error?:boolean
 ]
@@ -257,7 +314,7 @@ recipe read-file [
     reply 0:literal/dummy, 1:literal/quit, 0:literal/error
   }
   {
-    newline?:boolean <- equal c:character, 13:literal/newline
+    newline?:boolean <- equal c:character, 10:literal/newline
     break-unless newline?:boolean
     error-message:address:array:character <- new [that's not enough]
     print-string screen:address, error-message:address:array:character
@@ -305,7 +362,7 @@ recipe read-rank [
     reply 0:literal/dummy, 1:literal/quit, 0:literal/error
   }
   {
-    newline?:boolean <- equal c:character, 13:literal/newline
+    newline?:boolean <- equal c:character, 10:literal  # newline
     break-unless newline?:boolean
     error-message:address:array:character <- new [that's not enough]
     print-string screen:address, error-message:address:array:character
@@ -429,7 +486,7 @@ F read-move-blocking: routine failed to pause after rank 'a2-a']
     assert 4:boolean/waiting?, [
 F read-move-blocking: routine failed to pause after file 'a2-a4']
     # press 'newline'
-    1:address:channel <- write 1:address:channel, 13:literal  # newline
+    1:address:channel <- write 1:address:channel, 10:literal  # newline
     restart 2:number/routine
     # 'read-move' now completes
     wait-for-routine 2:number
@@ -527,7 +584,7 @@ scenario read-move-empty [
     4:boolean/waiting? <- equal 3:number/routine-state, 2:literal/waiting
     assert 4:boolean/waiting?, [
 F read-move-file: routine failed to pause after coming up (before any keys were pressed)]
-    1:address:channel <- write 1:address:channel, 13:literal/newline
+    1:address:channel <- write 1:address:channel, 10:literal/newline
     1:address:channel <- write 1:address:channel, 97:literal  # 'a'
     restart 2:number/routine
     wait-for-routine 2:number
diff --git a/termbox/termbox.h b/termbox/termbox.h
index b1b75ac8..0bd55be9 100644
--- a/termbox/termbox.h
+++ b/termbox/termbox.h
@@ -184,6 +184,9 @@ struct tb_event {
  * #define TB_KEY_CTRL_9 clash with '9'
  * #define TB_KEY_CTRL_0 clash with '0'
  */
+/* Some aliases */
+#define TB_KEY_NEWLINE TB_KEY_CTRL_J
+#define TB_KEY_CARRIAGE_RETURN TB_KEY_CTRL_M
 
 /* Wait for an event up to 'timeout' milliseconds and fill the 'event'
  * structure with it, when the event is available. Returns the type of the