Get rid of cutesy justify thresholds. They didn't actually save me any
trouble, and they won't generalize to other literals besides ints.
This commit is contained in:
Kartik Agaram 2020-09-25 09:47:55 -07:00
parent 5914ed31a9
commit d061cbff87
3 changed files with 49 additions and 50 deletions

View File

@ -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"
}

View File

@ -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
}

View File

@ -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