about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-09-25 09:47:55 -0700
committerKartik Agaram <vc@akkartik.com>2020-09-25 09:47:55 -0700
commitd061cbff87aa313e6204781b0569caa2d54c3d11 (patch)
treea898165a5a2f98b03dbb5fe8dad7726a88bb7a18
parent5914ed31a9d5fb097f249cb681bc109eb3ed2a92 (diff)
downloadmu-d061cbff87aa313e6204781b0569caa2d54c3d11.tar.gz
6855
Get rid of cutesy justify thresholds. They didn't actually save me any
trouble, and they won't generalize to other literals besides ints.
-rw-r--r--407print-int32-decimal-right-justified.mu81
-rw-r--r--apps/tile/environment.mu10
-rw-r--r--apps/tile/int-stack.mu8
3 files changed, 49 insertions, 50 deletions
diff --git a/407print-int32-decimal-right-justified.mu b/407print-int32-decimal-right-justified.mu
index 7d83217a..47725b96 100644
--- a/407print-int32-decimal-right-justified.mu
+++ b/407print-int32-decimal-right-justified.mu
@@ -1,66 +1,65 @@
-# print 'n' with enough leading spaces to be right-justified with 'threshold'
-# 'threshold' should be the minimum positive number for some width
-fn print-int32-decimal-right-justified screen: (addr screen), n: int, _threshold: int {
+# print 'n' with enough leading spaces to be right-justified in 'width'
+fn print-int32-decimal-right-justified screen: (addr screen), n: int, _width: int {
   # tweak things for negative numbers
-  var n2/ecx: int <- right-justify-threshold-decimal n
-  var threshold/eax: int <- copy _threshold
+  var n-width/ecx: int <- int-width-decimal n
+  var width/eax: int <- copy _width
   {
-    compare n2, threshold
+    compare n-width, width
     break-if->=
     print-grapheme screen, 0x20  # space
-    threshold <- try-divide threshold, 0xa
+    width <- decrement
     loop
   }
   print-int32-decimal screen, n
 }
 
-# return the minimum positive number with the same width in decimal as 'n'
-fn right-justify-threshold-decimal n: int -> result/ecx: int {
-  var ten/edx: int <- copy 0xa  # constant
-  # replace '-' at the start with '0' at the end
+fn int-width-decimal n: int -> result/ecx: int {
+  result <- copy 1
   var curr/eax: int <- copy n
+  # account for '-'
   compare curr, 0
   {
     break-if->=
     curr <- negate
-    curr <- multiply ten
+    result <- increment
   }
   # now we're dealing with a positive number
-  result <- copy 1
   {
     compare curr, 0xa
     break-if-<
     curr <- try-divide curr, 0xa
-    result <- multiply ten
+    result <- increment
     loop
   }
 }
 
-fn test-right-justify-threshold {
-  var x/ecx: int <- right-justify-threshold-decimal 0
-  check-ints-equal x, 1, "F - test-right-justify-threshold-decimal: 0"
-  x <- right-justify-threshold-decimal 1
-  check-ints-equal x, 1, "F - test-right-justify-threshold-decimal: 1"
-  x <- right-justify-threshold-decimal 4
-  check-ints-equal x, 1, "F - test-right-justify-threshold-decimal: 4"
-  x <- right-justify-threshold-decimal 9
-  check-ints-equal x, 1, "F - test-right-justify-threshold-decimal: 9"
-  x <- right-justify-threshold-decimal 0xa
-  check-ints-equal x, 0xa, "F - test-right-justify-threshold-decimal: 10"
-  x <- right-justify-threshold-decimal 0xb
-  check-ints-equal x, 0xa, "F - test-right-justify-threshold-decimal: 11"
-  x <- right-justify-threshold-decimal 0x4f  # 79
-  check-ints-equal x, 0xa, "F - test-right-justify-threshold-decimal: 79"
-  x <- right-justify-threshold-decimal 0x64  # 100
-  check-ints-equal x, 0x64, "F - test-right-justify-threshold-decimal: 100"
-  x <- right-justify-threshold-decimal 0x65  # 101
-  check-ints-equal x, 0x64, "F - test-right-justify-threshold-decimal: 101"
-  x <- right-justify-threshold-decimal 0x3e7  # 999
-  check-ints-equal x, 0x64, "F - test-right-justify-threshold-decimal: 999"
-  x <- right-justify-threshold-decimal 0x3e8  # 1000
-  check-ints-equal x, 0x3e8, "F - test-right-justify-threshold-decimal: 1000"
-  x <- right-justify-threshold-decimal -1
-  check-ints-equal x, 0xa, "F - test-right-justify-threshold-decimal: -1"
-  x <- right-justify-threshold-decimal -0xb  # -11
-  check-ints-equal x, 0x64, "F - test-right-justify-threshold-decimal: -11"
+fn test-int-width-decimal {
+  var x/ecx: int <- int-width-decimal 0
+  check-ints-equal x, 1, "F - test-int-width-decimal: 0"
+  x <- int-width-decimal 1
+  check-ints-equal x, 1, "F - test-int-width-decimal: 1"
+  x <- int-width-decimal 4
+  check-ints-equal x, 1, "F - test-int-width-decimal: 4"
+  x <- int-width-decimal 9
+  check-ints-equal x, 1, "F - test-int-width-decimal: 9"
+  x <- int-width-decimal 0xa
+  check-ints-equal x, 2, "F - test-int-width-decimal: 10"
+  x <- int-width-decimal 0xb
+  check-ints-equal x, 2, "F - test-int-width-decimal: 11"
+  x <- int-width-decimal 0x4f  # 79
+  check-ints-equal x, 2, "F - test-int-width-decimal: 79"
+  x <- int-width-decimal 0x63  # 99
+  check-ints-equal x, 2, "F - test-int-width-decimal: 100"
+  x <- int-width-decimal 0x64  # 100
+  check-ints-equal x, 3, "F - test-int-width-decimal: 100"
+  x <- int-width-decimal 0x65  # 101
+  check-ints-equal x, 3, "F - test-int-width-decimal: 101"
+  x <- int-width-decimal 0x3e7  # 999
+  check-ints-equal x, 3, "F - test-int-width-decimal: 999"
+  x <- int-width-decimal 0x3e8  # 1000
+  check-ints-equal x, 4, "F - test-int-width-decimal: 1000"
+  x <- int-width-decimal -1
+  check-ints-equal x, 2, "F - test-int-width-decimal: -1"
+  x <- int-width-decimal -0xb  # -11
+  check-ints-equal x, 3, "F - test-int-width-decimal: -11"
 }
diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu
index a52db8b8..abe92275 100644
--- a/apps/tile/environment.mu
+++ b/apps/tile/environment.mu
@@ -242,8 +242,8 @@ fn render-column screen: (addr screen), defs: (addr function), scratch: (addr li
     # render stack
     var curr-row/edx: int <- copy top-row
     curr-row <- add 3  # stack-margin-top
-    var _justify-threshold/eax: int <- max-stack-justify-threshold stack-addr
-    var justify-threshold/esi: int <- copy _justify-threshold
+    var _max-width/eax: int <- int-stack-max-width stack-addr
+    var max-width/esi: int <- copy _max-width
     var i/eax: int <- int-stack-length stack-addr
     {
       compare i, 0
@@ -251,7 +251,7 @@ fn render-column screen: (addr screen), defs: (addr function), scratch: (addr li
       move-cursor screen, curr-row, indented-col
       {
         var val/eax: int <- pop-int-stack stack-addr
-        render-integer screen, val, justify-threshold
+        render-integer screen, val, max-width
         var size/eax: int <- decimal-size val
         compare size, max-width
         break-if-<=
@@ -292,7 +292,7 @@ fn render-column screen: (addr screen), defs: (addr function), scratch: (addr li
 }
 
 # synaesthesia
-fn render-integer screen: (addr screen), val: int, justify-threshold: int {
+fn render-integer screen: (addr screen), val: int, max-width: int {
   var bg/eax: int <- hash-color val
   var fg/ecx: int <- copy 7
   {
@@ -312,7 +312,7 @@ fn render-integer screen: (addr screen), val: int, justify-threshold: int {
   }
   start-color screen, fg, bg
   print-grapheme screen, 0x20  # space
-  print-int32-decimal-right-justified screen, val, justify-threshold
+  print-int32-decimal-right-justified screen, val, max-width
   print-grapheme screen, 0x20  # space
 }
 
diff --git a/apps/tile/int-stack.mu b/apps/tile/int-stack.mu
index 65b9107b..8e58f456 100644
--- a/apps/tile/int-stack.mu
+++ b/apps/tile/int-stack.mu
@@ -68,7 +68,7 @@ fn int-stack-length _self: (addr int-stack) -> result/eax: int {
   result <- copy *top-addr
 }
 
-fn max-stack-justify-threshold _self: (addr int-stack) -> result/eax: int {
+fn int-stack-max-width _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
@@ -80,11 +80,11 @@ fn max-stack-justify-threshold _self: (addr int-stack) -> result/eax: int {
     compare i, *top-addr
     break-if->=
     var g/edx: (addr int) <- index data, i
-    var threshold/ecx: int <- right-justify-threshold-decimal *g
-    compare threshold, result
+    var w/ecx: int <- int-width-decimal *g
+    compare w, result
     {
       break-if-<=
-      result <- copy threshold
+      result <- copy w
     }
     i <- increment
     loop