about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-09-22 00:27:56 -0700
committerKartik Agaram <vc@akkartik.com>2020-09-22 00:27:56 -0700
commit8152b0d109a3ee01b7a357926aa01b1a8de3f366 (patch)
tree00e0717e85cafccd60879d8008bf2ade7cd8e2a7
parent04d06dfe537703885ae30230940e5756fecdcb19 (diff)
downloadmu-8152b0d109a3ee01b7a357926aa01b1a8de3f366.tar.gz
6832 - tile: right-justify numbers
Fails noisily for negative integers so far.
-rw-r--r--407print-int32-decimal-right-justified.mu52
-rw-r--r--apps/tile/environment.mu10
-rw-r--r--apps/tile/int-stack.mu22
3 files changed, 80 insertions, 4 deletions
diff --git a/407print-int32-decimal-right-justified.mu b/407print-int32-decimal-right-justified.mu
new file mode 100644
index 00000000..d9b7e5b6
--- /dev/null
+++ b/407print-int32-decimal-right-justified.mu
@@ -0,0 +1,52 @@
+# print n with enough leading spaces to be right-justified with max
+# only works for positive ints for now
+fn print-int32-decimal-right-justified screen: (addr screen), n: int, max: int {
+  var threshold/eax: int <- right-justify-threshold max
+  {
+#?     print-int32-decimal screen, threshold
+    compare n, threshold
+    break-if->=
+#?     print-string screen, "!"
+    print-grapheme screen, 0x20  # space
+    threshold <- try-divide threshold, 0xa
+    loop
+  }
+  print-int32-decimal screen, n
+}
+
+fn right-justify-threshold n: int -> result/eax: int {
+  var curr/eax: int <- copy n
+  var out/esi: int <- copy 1
+  var ten/ecx: int <- copy 0xa  # constant
+  {
+    compare curr, 0xa
+    break-if-<
+    curr <- try-divide curr, 0xa
+    out <- multiply ten
+    loop
+  }
+  result <- copy out
+}
+
+fn test-right-justify-threshold {
+  var x/eax: int <- right-justify-threshold 0
+  check-ints-equal x, 1, "F - test-right-justify-threshold: 0"
+  x <- right-justify-threshold 1
+  check-ints-equal x, 1, "F - test-right-justify-threshold: 1"
+  x <- right-justify-threshold 4
+  check-ints-equal x, 1, "F - test-right-justify-threshold: 4"
+  x <- right-justify-threshold 9
+  check-ints-equal x, 1, "F - test-right-justify-threshold: 9"
+  x <- right-justify-threshold 0xa
+  check-ints-equal x, 0xa, "F - test-right-justify-threshold: 10"
+  x <- right-justify-threshold 0xb
+  check-ints-equal x, 0xa, "F - test-right-justify-threshold: 11"
+  x <- right-justify-threshold 0x4f  # 79
+  check-ints-equal x, 0xa, "F - test-right-justify-threshold: 79"
+  x <- right-justify-threshold 0x64  # 100
+  check-ints-equal x, 0x64, "F - test-right-justify-threshold: 100"
+  x <- right-justify-threshold 0x3e7  # 999
+  check-ints-equal x, 0x64, "F - test-right-justify-threshold: 999"
+  x <- right-justify-threshold 0x3e8  # 1000
+  check-ints-equal x, 0x3e8, "F - test-right-justify-threshold: 1000"
+}
diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu
index 14a8490b..8b7cfc1e 100644
--- a/apps/tile/environment.mu
+++ b/apps/tile/environment.mu
@@ -203,7 +203,7 @@ fn render-column screen: (addr screen), first-word: (addr word), final-word: (ad
     break-if-=
     # indent stack
     var indented-col/ebx: int <- copy left-col
-    indented-col <- add 1
+    indented-col <- add 1  # margin-right - 2 for padding spaces
     # compute stack
     var stack: int-stack
     var stack-addr/edi: (addr int-stack) <- address stack
@@ -211,6 +211,8 @@ fn render-column screen: (addr screen), first-word: (addr word), final-word: (ad
     evaluate first-word, final-word, stack-addr
     # render stack
     var curr-row/edx: int <- copy 6  # input-row 3 + stack-margin-top 3
+    var _max-val/eax: int <- max-stack-value stack-addr
+    var max-val/esi: int <- copy _max-val
     var i/eax: int <- int-stack-length stack-addr
     {
       compare i, 0
@@ -218,7 +220,7 @@ fn render-column screen: (addr screen), first-word: (addr word), final-word: (ad
       move-cursor screen, curr-row, indented-col
       {
         var val/eax: int <- pop-int-stack stack-addr
-        render-integer screen, val
+        render-integer screen, val, max-val
         var size/eax: int <- decimal-size val
         compare size, max-width
         break-if-<=
@@ -259,7 +261,7 @@ fn render-column screen: (addr screen), first-word: (addr word), final-word: (ad
 }
 
 # synaesthesia
-fn render-integer screen: (addr screen), val: int {
+fn render-integer screen: (addr screen), val: int, max-val: int {
   var bg/eax: int <- hash-color val
   var fg/ecx: int <- copy 7
   {
@@ -279,7 +281,7 @@ fn render-integer screen: (addr screen), val: int {
   }
   start-color screen, fg, bg
   print-grapheme screen, 0x20  # space
-  print-int32-decimal screen, val
+  print-int32-decimal-right-justified screen, val, max-val
   print-grapheme screen, 0x20  # space
 }
 
diff --git a/apps/tile/int-stack.mu b/apps/tile/int-stack.mu
index 3bb9336f..74aa16fd 100644
--- a/apps/tile/int-stack.mu
+++ b/apps/tile/int-stack.mu
@@ -67,3 +67,25 @@ fn int-stack-length _self: (addr int-stack) -> result/eax: int {
   var top-addr/eax: (addr int) <- get self, top
   result <- copy *top-addr
 }
+
+fn max-stack-value _self: (addr int-stack) -> result/eax: int {
+  var self/esi: (addr int-stack) <- copy _self
+  var data-ah/edi: (addr handle array int) <- get self, data
+  var _data/eax: (addr array int) <- lookup *data-ah
+  var data/edi: (addr array int) <- copy _data
+  var top-addr/ecx: (addr int) <- get self, top
+  var i/ebx: int <- copy 0
+  result <- copy 0
+  {
+    compare i, *top-addr
+    break-if->=
+    var g/edx: (addr int) <- index data, i
+    compare *g, result
+    {
+      break-if-<=
+      result <- copy *g
+    }
+    i <- increment
+    loop
+  }
+}