about summary refs log tree commit diff stats
path: root/apps/tile
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-10-26 21:01:42 -0700
committerKartik Agaram <vc@akkartik.com>2020-10-26 21:01:42 -0700
commit14a41f4a27dee7f4b149090ae93e04184c39393b (patch)
tree996517d2177261aab597a8b3e7e5d94970cd827c /apps/tile
parent16a31669e3ef87e518935c2f122a22beffd2bd2d (diff)
downloadmu-14a41f4a27dee7f4b149090ae93e04184c39393b.tar.gz
7112 - tile: arrays of non-integers
Diffstat (limited to 'apps/tile')
-rw-r--r--apps/tile/data.mu2
-rw-r--r--apps/tile/environment.mu33
-rw-r--r--apps/tile/rpn.mu49
-rw-r--r--apps/tile/value-stack.mu114
4 files changed, 125 insertions, 73 deletions
diff --git a/apps/tile/data.mu b/apps/tile/data.mu
index 8a23fea9..b1ed4083 100644
--- a/apps/tile/data.mu
+++ b/apps/tile/data.mu
@@ -37,7 +37,7 @@ type value {
   type: int
   int-data: int  # if type = 0
   text-data: (handle array byte)  # if type = 1
-  array-data: (handle array int)  # if type = 2
+  array-data: (handle array value)  # if type = 2
   file-data: (handle buffered-file)  # if type = 3
 }
 
diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu
index 69de9c71..09a0b434 100644
--- a/apps/tile/environment.mu
+++ b/apps/tile/environment.mu
@@ -1407,8 +1407,8 @@ $render-value:body: {
   compare *val-type, 2  # array
   {
     break-if-!=
-    var val-ah/eax: (addr handle array int) <- get val, array-data
-    var val-array/eax: (addr array int) <- lookup *val-ah
+    var val-ah/eax: (addr handle array value) <- get val, array-data
+    var val-array/eax: (addr array value) <- lookup *val-ah
     render-array screen, val-array
     break $render-value:body
   }
@@ -1430,6 +1430,14 @@ $render-value:body: {
 
 # synaesthesia
 fn render-integer screen: (addr screen), val: int, max-width: int {
+$render-integer:body: {
+  # if max-width is 0, we're inside an array. No coloring.
+  compare max-width, 0
+  {
+    break-if-!=
+    print-int32-decimal screen, val
+    break $render-integer:body
+  }
   var bg/eax: int <- hash-color val
   var fg/ecx: int <- copy 7
   {
@@ -1452,12 +1460,29 @@ fn render-integer screen: (addr screen), val: int, max-width: int {
   print-int32-decimal-right-justified screen, val, max-width
   print-grapheme screen, 0x20  # space
 }
+}
 
-fn render-array screen: (addr screen), val: (addr array int) {
+fn render-array screen: (addr screen), _a: (addr array value) {
   start-color screen, 0, 7
   # don't surround in spaces
   print-grapheme screen, 0x5b  # '['
-  print-array-of-ints-in-decimal screen, val
+  var a/esi: (addr array value) <- copy _a
+  var max/ecx: int <- length a
+  var i/eax: int <- copy 0
+  {
+    compare i, max
+    break-if->=
+    {
+      compare i, 0
+      break-if-=
+      print-string screen, " "
+    }
+    var off/ecx: (offset value) <- compute-offset a, i
+    var x/ecx: (addr value) <- index a, off
+    render-value screen, x, 0
+    i <- increment
+    loop
+  }
   print-grapheme screen, 0x5d  # ']'
 }
 
diff --git a/apps/tile/rpn.mu b/apps/tile/rpn.mu
index 4f180b38..fcfb25be 100644
--- a/apps/tile/rpn.mu
+++ b/apps/tile/rpn.mu
@@ -88,14 +88,14 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         {
           break-if-!=
           # compute length
-          var src-ah/eax: (addr handle array int) <- get target-val, array-data
-          var src/eax: (addr array int) <- lookup *src-ah
+          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
           # 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 int) <- get target-val, array-data
-          var empty: (handle array int)
+          var target-array-ah/eax: (addr handle array value) <- get target-val, array-data
+          var empty: (handle array value)
           copy-handle empty, target-array-ah
           var target/eax: (addr int) <- get target-val, int-data
           copy-to *target, result
@@ -308,15 +308,40 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
         var end/eax: byte <- stream-final curr-stream
         compare end, 0x5d  # ']'
         break-if-!=
-        # wastefully create a new string to strip quotes
-        var h: (handle array int)
-        var tmp-ah/eax: (addr handle array byte) <- address h
-        unquote-stream-to-string curr-stream, tmp-ah  # leak
-        var tmp/eax: (addr array byte) <- lookup *tmp-ah
+        # wastefully create a new input string to strip quotes
+        var h: (handle array value)
+        var input-ah/eax: (addr handle array byte) <- address h
+        unquote-stream-to-string curr-stream, input-ah  # leak
+        # wastefully parse input into int-array
+        # TODO: support parsing arrays of other types
+        var input/eax: (addr array byte) <- lookup *input-ah
         var h2: (handle array int)
-        var array-ah/ecx: (addr handle array int) <- address h2
-        parse-array-of-ints tmp, array-ah  # leak
-        push-array-to-value-stack out, *array-ah
+        var int-array-ah/esi: (addr handle array int) <- address h2
+        parse-array-of-ints input, int-array-ah  # leak
+        var _int-array/eax: (addr array int) <- lookup *int-array-ah
+        var int-array/esi: (addr array int) <- copy _int-array
+        var len/ebx: int <- length int-array
+        # push value-array of same size as int-array
+        var h3: (handle array value)
+        var value-array-ah/eax: (addr handle array value) <- address h3
+        populate value-array-ah, len
+        push-array-to-value-stack out, *value-array-ah
+        # copy int-array into value-array
+        var _value-array/eax: (addr array value) <- lookup *value-array-ah
+        var value-array/edi: (addr array value) <- copy _value-array
+        var i/eax: int <- copy 0
+        {
+          compare i, len
+          break-if->=
+          var src-addr/ecx: (addr int) <- index int-array, i
+          var src/ecx: int <- copy *src-addr
+          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
+          i <- increment
+          loop
+        }
         break $evaluate:process-word
       }
       # otherwise assume it's a literal int and push it
diff --git a/apps/tile/value-stack.mu b/apps/tile/value-stack.mu
index 2eae94d3..788a39b7 100644
--- a/apps/tile/value-stack.mu
+++ b/apps/tile/value-stack.mu
@@ -55,7 +55,7 @@ fn push-string-to-value-stack _self: (addr value-stack), val: (handle array byte
   increment *top-addr
 }
 
-fn push-array-to-value-stack _self: (addr value-stack), val: (handle array int) {
+fn push-array-to-value-stack _self: (addr value-stack), val: (handle array value) {
   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
@@ -63,15 +63,10 @@ fn push-array-to-value-stack _self: (addr value-stack), val: (handle array 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 handle array int) <- get dest-addr, array-data
+  var dest-addr2/eax: (addr handle array value) <- get dest-addr, array-data
   copy-handle val, dest-addr2
+  # update type
   var dest-addr3/eax: (addr int) <- get dest-addr, type
-#?   print-string 0, "setting type to 1: "
-#?   {
-#?     var foo/eax: int <- copy dest-addr3
-#?     print-int32-hex 0, foo
-#?   }
-#?   print-string 0, "\n"
   copy-to *dest-addr3, 2  # type array
   increment *top-addr
 }
@@ -141,50 +136,10 @@ fn value-stack-max-width _self: (addr value-stack) -> result/eax: int {
     compare i, *top-addr
     break-if->=
     var o/edx: (offset value) <- compute-offset data, i
-    var g/edx: (addr value) <- index data, o
-    var type/eax: (addr int) <- get g, type
-    {
-      compare *type, 0  # int
-      break-if-!=
-      var g2/edx: (addr int) <- get g, int-data
-      var w/eax: int <- decimal-size *g2
-      compare w, out
-      break-if-<=
-      copy-to out, w
-    }
-    {
-      compare *type, 1  # string
-      break-if-!=
-      var s-ah/eax: (addr handle array byte) <- get g, text-data
-      var s/eax: (addr array byte) <- lookup *s-ah
-      compare s, 0
-      break-if-=
-      var w/eax: int <- length s
-      compare w, out
-      break-if-<=
-      copy-to out, w
-    }
+    var v/edx: (addr value) <- index data, o
+    var w/eax: int <- value-width v
+    # if (w > out) w = out
     {
-      compare *type, 2  # array
-      break-if-!=
-      var a-ah/eax: (addr handle array int) <- get g, array-data
-      var a/eax: (addr array int) <- lookup *a-ah
-      compare a, 0
-      break-if-=
-      var w/eax: int <- array-decimal-size a
-      compare w, out
-      break-if-<=
-      copy-to out, w
-    }
-    {
-      compare *type, 3  # file handle
-      break-if-!=
-      var f-ah/eax: (addr handle buffered-file) <- get g, file-data
-      var f/eax: (addr buffered-file) <- lookup *f-ah
-      compare f, 0
-      break-if-=
-      # TODO
-      var w/eax: int <- copy 4
       compare w, out
       break-if-<=
       copy-to out, w
@@ -195,9 +150,55 @@ fn value-stack-max-width _self: (addr value-stack) -> result/eax: int {
   result <- copy out
 }
 
-# keep sync'd with print-array-of-ints
-fn array-decimal-size _a: (addr array int) -> result/eax: int {
-  var a/esi: (addr array int) <- copy _a
+fn value-width _v: (addr value) -> result/eax: int {
+$value-width:body: {
+  var v/esi: (addr value) <- copy _v
+  var type/eax: (addr int) <- get v, type
+  {
+    compare *type, 0  # int
+    break-if-!=
+    var v-int/edx: (addr int) <- get v, int-data
+    result <- decimal-size *v-int
+    break $value-width:body
+  }
+  {
+    compare *type, 1  # string
+    break-if-!=
+    var s-ah/eax: (addr handle array byte) <- get v, text-data
+    var s/eax: (addr array byte) <- lookup *s-ah
+    compare s, 0
+    break-if-=
+    result <- length s
+    break $value-width:body
+  }
+  {
+    compare *type, 2  # array
+    break-if-!=
+    var a-ah/eax: (addr handle array value) <- get v, array-data
+    var a/eax: (addr array value) <- lookup *a-ah
+    compare a, 0
+    break-if-=
+    result <- array-width a
+    break $value-width:body
+  }
+  {
+    compare *type, 3  # file handle
+    break-if-!=
+    var f-ah/eax: (addr handle buffered-file) <- get v, file-data
+    var f/eax: (addr buffered-file) <- lookup *f-ah
+    compare f, 0
+    break-if-=
+    # TODO
+    result <- copy 4
+    break $value-width:body
+    var w/eax: int <- copy 4
+  }
+}
+}
+
+# keep sync'd with render-array
+fn array-width _a: (addr array value) -> result/eax: int {
+  var a/esi: (addr array value) <- copy _a
   var max/ecx: int <- length a
   var i/eax: int <- copy 0
   var out/edi: int <- copy 0
@@ -209,9 +210,10 @@ fn array-decimal-size _a: (addr array int) -> result/eax: int {
       break-if-=
       out <- increment  # for space
     }
-    var x/ecx: (addr int) <- index a, i
+    var off/ecx: (offset value) <- compute-offset a, i
+    var x/ecx: (addr value) <- index a, off
     {
-      var w/eax: int <- decimal-size *x
+      var w/eax: int <- value-width x
       out <- add w
     }
     i <- increment