about summary refs log tree commit diff stats
path: root/apps
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-06-15 23:53:07 -0700
committerKartik Agaram <vc@akkartik.com>2020-06-15 23:53:07 -0700
commit4e48a436603694d081826324cba2dfcaddc80499 (patch)
tree988aaf970929dc79b6290a5c9c709faec3cd4508 /apps
parent2fcdca06498bd1742b1a7f7929ee148e4442d345 (diff)
downloadmu-4e48a436603694d081826324cba2dfcaddc80499.tar.gz
6544 - arith: doc
Diffstat (limited to 'apps')
-rw-r--r--apps/arith.mu44
1 files changed, 43 insertions, 1 deletions
diff --git a/apps/arith.mu b/apps/arith.mu
index 7d77d13a..97fff9fa 100644
--- a/apps/arith.mu
+++ b/apps/arith.mu
@@ -1,12 +1,47 @@
+# Integer arithmetic using conventional precedence.
+#
+# Follows part 2 of Jack Crenshaw's "Let's build a compiler!"
+#   https://compilers.iecc.com/crenshaw
+#
+# Limitations:
+#   Reads numbers in decimal, but prints numbers in hex :(
+#   No division yet.
+#
+# To build:
+#   $ ./translate_mu apps/arith.mu
+#
+# Example session:
+#   $ ./a.elf
+#   press ctrl-c or ctrl-d to exit
+#   > 1
+#   0x00000001
+#   > 1+1
+#   0x00000002
+#   > 1 + 1
+#   0x00000002
+#   > 1+2 +3
+#   0x00000006
+#   > 1+2 *3
+#   0x00000007
+#   > (1+2) *3
+#   0x00000009
+#   > 1 + 3*4
+#   0x0000000d
+#   > ^D
+#   $
+#
+# Error handling is non-existent. This is just a prototype.
+
 fn main -> exit-status/ebx: int {
   var look/esi: byte <- copy 0  # lookahead
   var n/eax: int <- copy 0  # result of each expression
+  print-string "press ctrl-c or ctrl-d to exit\n"
   # read-eval-print loop
   {
     # print prompt
     print-string "> "
     # read and eval
-    n, look <- simplify
+    n, look <- simplify  # we explicitly thread 'look' everywhere
     # if (look == 0) break
     compare look, 0
     break-if-=
@@ -23,6 +58,7 @@ fn simplify -> result/eax: int, look/esi: byte {
   # prime the pump
   look <- get-char  # prime the pump
   look <- skip-spaces look
+  # do it
   result, look <- expression look
 }
 
@@ -139,6 +175,12 @@ $is-mul-or-div?:body: {
     result <- copy 1  # true
     break $is-mul-or-div?:body
   }
+  compare c, 0x2f  # '/'
+  {
+    break-if-!=
+    result <- copy 1  # true
+    break $is-mul-or-div?:body
+  }
   result <- copy 0  # false
 }  # $is-mul-or-div?:body
 }