about summary refs log tree commit diff stats
path: root/apps/tile
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-11-29 09:50:15 -0800
committerKartik Agaram <vc@akkartik.com>2020-11-29 12:17:23 -0800
commit6b1cde4761711934211465c3e4ade12d344662ff (patch)
tree1738cf4b1e521c6e31b1fede23504ab0b2e3d3d9 /apps/tile
parent4a280280cd6c0d24c4719727b86ed86aef1d74f2 (diff)
downloadmu-6b1cde4761711934211465c3e4ade12d344662ff.tar.gz
7297 - tile: use floats everywhere
Diffstat (limited to 'apps/tile')
-rw-r--r--apps/tile/data.mu2
-rw-r--r--apps/tile/float-stack.mu63
-rw-r--r--apps/tile/int-stack.mu63
-rw-r--r--apps/tile/main.mu6
-rw-r--r--apps/tile/rpn.mu77
-rw-r--r--apps/tile/table.mu6
-rw-r--r--apps/tile/value-stack.mu14
-rw-r--r--apps/tile/value.mu24
8 files changed, 131 insertions, 124 deletions
diff --git a/apps/tile/data.mu b/apps/tile/data.mu
index ead9948e..6b6d4775 100644
--- a/apps/tile/data.mu
+++ b/apps/tile/data.mu
@@ -36,7 +36,7 @@ type word {
 # todo: turn this into a sum type
 type value {
   type: int
-  int-data: int  # if type = 0
+  number-data: float  # if type = 0
   text-data: (handle array byte)  # if type = 1
   array-data: (handle array value)  # if type = 2
   file-data: (handle buffered-file)  # if type = 3
diff --git a/apps/tile/float-stack.mu b/apps/tile/float-stack.mu
new file mode 100644
index 00000000..b6585192
--- /dev/null
+++ b/apps/tile/float-stack.mu
@@ -0,0 +1,63 @@
+type float-stack {
+  data: (handle array float)
+  top: int
+}
+
+fn initialize-float-stack _self: (addr float-stack), n: int {
+  var self/esi: (addr float-stack) <- copy _self
+  var d/edi: (addr handle array float) <- get self, data
+  populate d, n
+  var top/eax: (addr int) <- get self, top
+  copy-to *top, 0
+}
+
+fn clear-float-stack _self: (addr float-stack) {
+  var self/esi: (addr float-stack) <- copy _self
+  var top/eax: (addr int) <- get self, top
+  copy-to *top, 0
+}
+
+fn push-float-stack _self: (addr float-stack), _val: float {
+  var self/esi: (addr float-stack) <- copy _self
+  var top-addr/ecx: (addr int) <- get self, top
+  var data-ah/edx: (addr handle array float) <- get self, data
+  var data/eax: (addr array float) <- lookup *data-ah
+  var top/edx: int <- copy *top-addr
+  var dest-addr/edx: (addr float) <- index data, top
+  var val/xmm0: float <- copy _val
+  copy-to *dest-addr, val
+  add-to *top-addr, 1
+}
+
+fn pop-float-stack _self: (addr float-stack) -> _/xmm0: float {
+  var self/esi: (addr float-stack) <- copy _self
+  var top-addr/ecx: (addr int) <- get self, top
+  {
+    compare *top-addr, 0
+    break-if->
+    return 0
+  }
+  subtract-from *top-addr, 1
+  var data-ah/edx: (addr handle array float) <- get self, data
+  var data/eax: (addr array float) <- lookup *data-ah
+  var top/edx: int <- copy *top-addr
+  var result-addr/eax: (addr float) <- index data, top
+  return *result-addr
+}
+
+fn float-stack-empty? _self: (addr float-stack) -> _/eax: boolean {
+  var self/esi: (addr float-stack) <- copy _self
+  var top-addr/eax: (addr int) <- get self, top
+  compare *top-addr, 0
+  {
+    break-if-!=
+    return 1  # true
+  }
+  return 0  # false
+}
+
+fn float-stack-length _self: (addr float-stack) -> _/eax: int {
+  var self/esi: (addr float-stack) <- copy _self
+  var top-addr/eax: (addr int) <- get self, top
+  return *top-addr
+}
diff --git a/apps/tile/int-stack.mu b/apps/tile/int-stack.mu
deleted file mode 100644
index 726684bd..00000000
--- a/apps/tile/int-stack.mu
+++ /dev/null
@@ -1,63 +0,0 @@
-type int-stack {
-  data: (handle array int)
-  top: int
-}
-
-fn initialize-int-stack _self: (addr int-stack), n: int {
-  var self/esi: (addr int-stack) <- copy _self
-  var d/edi: (addr handle array int) <- get self, data
-  populate d, n
-  var top/eax: (addr int) <- get self, top
-  copy-to *top, 0
-}
-
-fn clear-int-stack _self: (addr int-stack) {
-  var self/esi: (addr int-stack) <- copy _self
-  var top/eax: (addr int) <- get self, top
-  copy-to *top, 0
-}
-
-fn push-int-stack _self: (addr int-stack), _val: int {
-  var self/esi: (addr int-stack) <- copy _self
-  var top-addr/ecx: (addr int) <- get self, top
-  var data-ah/edx: (addr handle array int) <- get self, data
-  var data/eax: (addr array int) <- lookup *data-ah
-  var top/edx: int <- copy *top-addr
-  var dest-addr/edx: (addr int) <- index data, top
-  var val/eax: int <- copy _val
-  copy-to *dest-addr, val
-  add-to *top-addr, 1
-}
-
-fn pop-int-stack _self: (addr int-stack) -> _/eax: int {
-  var self/esi: (addr int-stack) <- copy _self
-  var top-addr/ecx: (addr int) <- get self, top
-  {
-    compare *top-addr, 0
-    break-if->
-    return 0
-  }
-  subtract-from *top-addr, 1
-  var data-ah/edx: (addr handle array int) <- get self, data
-  var data/eax: (addr array int) <- lookup *data-ah
-  var top/edx: int <- copy *top-addr
-  var result-addr/eax: (addr int) <- index data, top
-  return *result-addr
-}
-
-fn int-stack-empty? _self: (addr int-stack) -> _/eax: boolean {
-  var self/esi: (addr int-stack) <- copy _self
-  var top-addr/eax: (addr int) <- get self, top
-  compare *top-addr, 0
-  {
-    break-if-!=
-    return 1  # true
-  }
-  return 0  # false
-}
-
-fn int-stack-length _self: (addr int-stack) -> _/eax: int {
-  var self/esi: (addr int-stack) <- copy _self
-  var top-addr/eax: (addr int) <- get self, top
-  return *top-addr
-}
diff --git a/apps/tile/main.mu b/apps/tile/main.mu
index c47bbe3d..d81588ce 100644
--- a/apps/tile/main.mu
+++ b/apps/tile/main.mu
@@ -130,9 +130,9 @@ fn repl {
     {
       compare empty?, 0  # false
       break-if-!=
-      var result/eax: int <- pop-int-from-value-stack stack
-      print-int32-decimal-to-real-screen result
-      print-string-to-real-screen "\n"
+      var result/xmm0: float <- pop-number-from-value-stack stack
+      print-float-decimal-approximate 0, result, 3
+      print-string 0, "\n"
     }
     #
     loop
diff --git a/apps/tile/rpn.mu b/apps/tile/rpn.mu
index 1b288fa5..193bd2c9 100644
--- a/apps/tile/rpn.mu
+++ b/apps/tile/rpn.mu
@@ -21,33 +21,33 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         var is-add?/eax: boolean <- stream-data-equal? curr-stream, "+"
         compare is-add?, 0
         break-if-=
-        var _b/eax: int <- pop-int-from-value-stack out
-        var b/edx: int <- copy _b
-        var a/eax: int <- pop-int-from-value-stack out
+        var _b/xmm0: float <- pop-number-from-value-stack out
+        var b/xmm1: float <- copy _b
+        var a/xmm0: float <- pop-number-from-value-stack out
         a <- add b
-        push-int-to-value-stack out, a
+        push-number-to-value-stack out, a
         break $evaluate:process-word
       }
       {
         var is-sub?/eax: boolean <- stream-data-equal? curr-stream, "-"
         compare is-sub?, 0
         break-if-=
-        var _b/eax: int <- pop-int-from-value-stack out
-        var b/edx: int <- copy _b
-        var a/eax: int <- pop-int-from-value-stack out
+        var _b/xmm0: float <- pop-number-from-value-stack out
+        var b/xmm1: float <- copy _b
+        var a/xmm0: float <- pop-number-from-value-stack out
         a <- subtract b
-        push-int-to-value-stack out, a
+        push-number-to-value-stack out, a
         break $evaluate:process-word
       }
       {
         var is-mul?/eax: boolean <- stream-data-equal? curr-stream, "*"
         compare is-mul?, 0
         break-if-=
-        var _b/eax: int <- pop-int-from-value-stack out
-        var b/edx: int <- copy _b
-        var a/eax: int <- pop-int-from-value-stack out
+        var _b/xmm0: float <- pop-number-from-value-stack out
+        var b/xmm1: float <- copy _b
+        var a/xmm0: float <- pop-number-from-value-stack out
         a <- multiply b
-        push-int-to-value-stack out, a
+        push-number-to-value-stack out, a
         break $evaluate:process-word
       }
       ## strings/arrays
@@ -77,13 +77,14 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
           var src-ah/eax: (addr handle array byte) <- get target-val, text-data
           var src/eax: (addr array byte) <- lookup *src-ah
           var result/ebx: int <- length src
+          var result-f/xmm0: float <- convert result
           # save result into target-val
           var type-addr/eax: (addr int) <- get target-val, type
           copy-to *type-addr, 0  # int
           var target-string-ah/eax: (addr handle array byte) <- get target-val, text-data
           clear-object target-string-ah
-          var target/eax: (addr int) <- get target-val, int-data
-          copy-to *target, result
+          var target/eax: (addr float) <- get target-val, number-data
+          copy-to *target, result-f
           break $evaluate:process-word
         }
         compare *target-type-addr, 2  # array of ints
@@ -93,13 +94,14 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
           var src-ah/eax: (addr handle array value) <- get target-val, array-data
           var src/eax: (addr array value) <- lookup *src-ah
           var result/ebx: int <- length src
+          var result-f/xmm0: float <- convert result
           # save result into target-val
           var type-addr/eax: (addr int) <- get target-val, type
           copy-to *type-addr, 0  # int
           var target-array-ah/eax: (addr handle array value) <- get target-val, array-data
           clear-object target-array-ah
-          var target/eax: (addr int) <- get target-val, int-data
-          copy-to *target, result
+          var target/eax: (addr float) <- get target-val, number-data
+          copy-to *target, result-f
           break $evaluate:process-word
         }
       }
@@ -266,10 +268,10 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         compare *top-addr, 0
         break-if-<=
         # pop width and height from out
-        var _nrows/eax: int <- pop-int-from-value-stack out2
-        var nrows/edx: int <- copy _nrows
-        var _ncols/eax: int <- pop-int-from-value-stack out2
-        var ncols/ebx: int <- copy _ncols
+        var nrows-f/xmm0: float <- pop-number-from-value-stack out2
+        var nrows/edx: int <- convert nrows-f
+        var ncols-f/xmm0: float <- pop-number-from-value-stack out2
+        var ncols/ebx: int <- convert ncols-f
         # define a new screen with those dimensions
         var screen-h: (handle screen)
         var screen-ah/eax: (addr handle screen) <- address screen-h
@@ -332,10 +334,10 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         break-if-=
         var out2/esi: (addr value-stack) <- copy out
         # pop args
-        var _r/eax: int <- pop-int-from-value-stack out2
-        var r/ecx: int <- copy _r
-        var _c/eax: int <- pop-int-from-value-stack out2
-        var c/edx: int <- copy _c
+        var r-f/xmm0: float <- pop-number-from-value-stack out2
+        var r/ecx: int <- convert r-f
+        var c-f/xmm0: float <- pop-number-from-value-stack out2
+        var c/edx: int <- convert c-f
         # select screen from top of out (but don't pop it)
         var top-addr/ebx: (addr int) <- get out2, top
         compare *top-addr, 0
@@ -364,8 +366,8 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         compare *top-addr, 0
         break-if-<=
         # pop args
-        var _d/eax: int <- pop-int-from-value-stack out2
-        var d/ecx: int <- copy _d
+        var d-f/xmm0: float <- pop-number-from-value-stack out2
+        var d/ecx: int <- convert d-f
         # select screen from top of out (but don't pop it)
         compare *top-addr, 0
         break-if-<=
@@ -407,8 +409,8 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         compare *top-addr, 0
         break-if-<=
         # pop args
-        var _d/eax: int <- pop-int-from-value-stack out2
-        var d/ecx: int <- copy _d
+        var d-f/xmm0: float <- pop-number-from-value-stack out2
+        var d/ecx: int <- convert d-f
         # select screen from top of out (but don't pop it)
         compare *top-addr, 0
         break-if-<=
@@ -452,8 +454,8 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         compare *top-addr, 0
         break-if-<=
         # pop args
-        var _d/eax: int <- pop-int-from-value-stack out2
-        var d/ecx: int <- copy _d
+        var d-f/xmm0: float <- pop-number-from-value-stack out2
+        var d/ecx: int <- convert d-f
         # select screen from top of out (but don't pop it)
         compare *top-addr, 0
         break-if-<=
@@ -496,8 +498,8 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         compare *top-addr, 0
         break-if-<=
         # pop args
-        var _d/eax: int <- pop-int-from-value-stack out2
-        var d/ecx: int <- copy _d
+        var _d/xmm0: float <- pop-number-from-value-stack out2
+        var d/ecx: int <- convert _d
         # select screen from top of out (but don't pop it)
         compare *top-addr, 0
         break-if-<=
@@ -689,10 +691,11 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
           break-if->=
           var src-addr/ecx: (addr int) <- index int-array, i
           var src/ecx: int <- copy *src-addr
+          var src-f/xmm0: float <- convert src
           var dest-offset/edx: (offset value) <- compute-offset value-array, i
           var dest-val/edx: (addr value) <- index value-array, dest-offset
-          var dest/edx: (addr int) <- get dest-val, int-data
-          copy-to *dest, src
+          var dest/edx: (addr float) <- get dest-val, number-data
+          copy-to *dest, src-f
           i <- increment
           loop
         }
@@ -701,7 +704,8 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
       ### otherwise assume it's a literal number and push it
       {
         var n/eax: int <- parse-decimal-int-from-stream curr-stream
-        push-int-to-value-stack out, n
+        var n-f/xmm0: float <- convert n
+        push-number-to-value-stack out, n-f
       }
     }
     # termination check
@@ -742,7 +746,8 @@ fn test-evaluate {
   var stack/edi: (addr value-stack) <- address stack-storage
   initialize-value-stack stack, 0x10
   evaluate functions, table, line, 0, stack
-  var x/eax: int <- pop-int-from-value-stack stack
+  var x-f/xmm0: float <- pop-number-from-value-stack stack
+  var x/eax: int <- convert x-f
   check-ints-equal x, 3, "F - test-evaluate"
 }
 
diff --git a/apps/tile/table.mu b/apps/tile/table.mu
index b0fdb648..899408e1 100644
--- a/apps/tile/table.mu
+++ b/apps/tile/table.mu
@@ -83,15 +83,15 @@ fn next-empty-slot _data: (addr array bind), key: (addr handle array byte) -> _/
   return result
 }
 
-fn make-int-binding _self: (addr bind), key: (addr handle array byte), _val: int {
+fn make-number-binding _self: (addr bind), key: (addr handle array byte), _val: float {
   var self/esi: (addr bind) <- copy _self
   var dest/eax: (addr handle array byte) <- get self, key
   copy-object key, dest
   var dest2/eax: (addr handle value) <- get self, value
   allocate dest2
   var dest3/eax: (addr value) <- lookup *dest2
-  var dest4/eax: (addr int) <- get dest3, int-data
-  var val/ecx: int <- copy _val
+  var dest4/eax: (addr float) <- get dest3, number-data
+  var val/xmm0: float <- copy _val
   copy-to *dest4, val
 }
 
diff --git a/apps/tile/value-stack.mu b/apps/tile/value-stack.mu
index a3d05322..64f33fa2 100644
--- a/apps/tile/value-stack.mu
+++ b/apps/tile/value-stack.mu
@@ -19,7 +19,7 @@ fn clear-value-stack _self: (addr value-stack) {
   copy-to *top, 0
 }
 
-fn push-int-to-value-stack _self: (addr value-stack), _val: int {
+fn push-number-to-value-stack _self: (addr value-stack), _val: float {
   var self/esi: (addr value-stack) <- copy _self
   var top-addr/ecx: (addr int) <- get self, top
   var data-ah/edx: (addr handle array value) <- get self, data
@@ -27,13 +27,13 @@ fn push-int-to-value-stack _self: (addr value-stack), _val: int {
   var top/edx: int <- copy *top-addr
   var dest-offset/edx: (offset value) <- compute-offset data, top
   var dest-addr/edx: (addr value) <- index data, dest-offset
-  var dest-addr2/eax: (addr int) <- get dest-addr, int-data
-  var val/esi: int <- copy _val
+  var dest-addr2/eax: (addr float) <- get dest-addr, number-data
+  var val/xmm0: float <- copy _val
 #?   print-int32-hex-to-real-screen val
   copy-to *dest-addr2, val
   increment *top-addr
-  dest-addr2 <- get dest-addr, type
-  copy-to *dest-addr2, 0  # int
+  var type-addr/eax: (addr int) <- get dest-addr, type
+  copy-to *type-addr, 0  # number
 }
 
 fn push-string-to-value-stack _self: (addr value-stack), val: (handle array byte) {
@@ -85,7 +85,7 @@ fn push-value-stack _self: (addr value-stack), val: (addr value) {
   increment *top-addr
 }
 
-fn pop-int-from-value-stack _self: (addr value-stack) -> _/eax: int {
+fn pop-number-from-value-stack _self: (addr value-stack) -> _/xmm0: float {
   var self/esi: (addr value-stack) <- copy _self
   var top-addr/ecx: (addr int) <- get self, top
   {
@@ -99,7 +99,7 @@ fn pop-int-from-value-stack _self: (addr value-stack) -> _/eax: int {
   var top/edx: int <- copy *top-addr
   var dest-offset/edx: (offset value) <- compute-offset data, top
   var result-addr/eax: (addr value) <- index data, dest-offset
-  var result-addr2/eax: (addr int) <- get result-addr, int-data
+  var result-addr2/eax: (addr float) <- get result-addr, number-data
   return *result-addr2
 }
 
diff --git a/apps/tile/value.mu b/apps/tile/value.mu
index 790b2de2..3cb93a1d 100644
--- a/apps/tile/value.mu
+++ b/apps/tile/value.mu
@@ -57,20 +57,22 @@ fn render-value-at screen: (addr screen), row: int, col: int, _val: (addr value)
     return
   }
   # render ints by default for now
-  var val-int/eax: (addr int) <- get val, int-data
-  render-integer screen, *val-int, max-width
+  var val-num/eax: (addr float) <- get val, number-data
+  render-number screen, *val-num, max-width
 }
 
 # synaesthesia
-fn render-integer screen: (addr screen), val: int, max-width: int {
+# TODO: right-justify
+fn render-number screen: (addr screen), val: float, max-width: int {
   # if max-width is 0, we're inside an array. No coloring.
   compare max-width, 0
   {
     break-if-!=
-    print-int32-decimal screen, val
+    print-float-decimal-approximate screen, val, 3
     return
   }
-  var bg/eax: int <- hash-color val
+  var val-int/eax: int <- convert val
+  var bg/eax: int <- hash-color val-int
   var fg/ecx: int <- copy 7
   {
     compare bg, 2
@@ -89,7 +91,7 @@ fn render-integer screen: (addr screen), val: int, max-width: int {
   }
   start-color screen, fg, bg
   print-grapheme screen, 0x20  # space
-  print-int32-decimal-right-justified screen, val, max-width
+  print-float-decimal-approximate screen, val, 3
   print-grapheme screen, 0x20  # space
 }
 
@@ -219,8 +221,8 @@ fn value-width _v: (addr value), top-level: boolean -> _/eax: int {
   {
     compare *type, 0  # int
     break-if-!=
-    var v-int/edx: (addr int) <- get v, int-data
-    var result/eax: int <- decimal-size *v-int
+    var v-num/edx: (addr float) <- get v, number-data
+    var result/eax: int <- float-size *v-num, 3
     return result
   }
   {
@@ -344,9 +346,9 @@ fn deep-copy-value _src: (addr value), _dest: (addr value) {
   {
     break-if-!=
 #?     print-string 0, "int value\n"
-    var x/eax: (addr int) <- get src, int-data
-    y <- get dest, int-data
-    copy-object x, y
+    var src-n/eax: (addr float) <- get src, number-data
+    var dest-n/ecx: (addr float) <- get dest, number-data
+    copy-object src-n, dest-n
     return
   }
   compare *type, 1  # string