From d061cbff87aa313e6204781b0569caa2d54c3d11 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Fri, 25 Sep 2020 09:47:55 -0700 Subject: 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. --- 407print-int32-decimal-right-justified.mu | 81 +++++++++++++++---------------- 1 file changed, 40 insertions(+), 41 deletions(-) (limited to '407print-int32-decimal-right-justified.mu') 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" } -- cgit 1.4.1-2-gfad0