about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-06-15 18:12:45 -0700
committerKartik Agaram <vc@akkartik.com>2020-06-15 18:19:17 -0700
commitdce8ad424dac64b8221d12d5850a8212ba118948 (patch)
treee8f60ab0f1fea3ce36a9f13d36e9cbb76c26b243
parent5fe0bc16884c0f16f173db55a7f59a9738e67f02 (diff)
downloadmu-dce8ad424dac64b8221d12d5850a8212ba118948.tar.gz
6532 - arith: REPL converting ints to hex
I'm using one character of lookahead, inspired by Crenshaw's "let's build
a compiler".
-rw-r--r--apps/arith.mu24
1 files changed, 18 insertions, 6 deletions
diff --git a/apps/arith.mu b/apps/arith.mu
index 6d0307ee..43aa404c 100644
--- a/apps/arith.mu
+++ b/apps/arith.mu
@@ -1,16 +1,28 @@
 fn main -> exit-status/ebx: int {
-  var val/eax: int <- num
-  print-int32-to-screen val
-  print-string "\n"
+  {
+    var c/eax: byte <- read-key
+    # if (c == 0) break
+    compare c, 0
+    break-if-=
+    # parse an int from screen and print it out
+    var n/eax: int <- num c
+    print-int32-to-screen n
+    print-string "\n"
+    loop
+  }
   exit-status <- copy 0
 }
 
-fn num -> result/eax: int {
+fn num firstc: byte -> result/eax: int {
   var out/edi: int <- copy 0
   {
+    var first-digit/eax: int <- to-decimal-digit firstc
+    out <- copy first-digit
+  }
+  {
     var c/eax: byte <- read-key
-    # if (c == EOF) break
-    compare c, 0xffffffff  # EOF marker
+    # if (c == 0) break
+    compare c, 0
     break-if-=
     # if (c == ' ') break
     compare c, 0x20  # space