fmt: add word ".x" and fix .X to 4 chars output

Simpler code, more predictable output.
This commit is contained in:
Virgil Dupras 2020-04-05 12:11:59 -04:00
parent 7390cb18ed
commit 7df7416e9e
2 changed files with 19 additions and 22 deletions

View File

@ -193,10 +193,11 @@ core.
number parsing. By default, (parse). number parsing. By default, (parse).
(print) a -- Print string at addr a. (print) a -- Print string at addr a.
. n -- Print n in its decimal form . n -- Print n in its decimal form
.X n -- Print n in its hexadecimal form. In hex, numbers .x n -- Print n's LSB in hex form. Always 2 characters.
.X n -- Print n in hex form. Always 4 characters. Numbers
are never considered negative. "-1 .X" --> ffff
." xxx" -- *I* Compiles string literal xxx followed by a call ." xxx" -- *I* Compiles string literal xxx followed by a call
to (print) to (print)
are never considered negative. "-1 .X -> ffff"
C< -- c Read one char from buffered input. C< -- c Read one char from buffered input.
EMIT c -- Spit char c to output stream EMIT c -- Spit char c to output stream
IN> -- a Address of variable containing current pos in input IN> -- a Address of variable containing current pos in input

View File

@ -1,7 +1,6 @@
( requires core, parse ) ( requires core, parse )
( TODO FORGET this word ) : _
: PUSHDGTS
999 SWAP ( stop indicator ) 999 SWAP ( stop indicator )
DUP 0 = IF '0' EXIT THEN ( 0 is a special case ) DUP 0 = IF '0' EXIT THEN ( 0 is a special case )
BEGIN BEGIN
@ -16,7 +15,7 @@
( that "0 1 -" thing is because we don't parse negative ( that "0 1 -" thing is because we don't parse negative
number correctly yet. ) number correctly yet. )
DUP 0 < IF '-' EMIT 0 1 - * THEN DUP 0 < IF '-' EMIT 0 1 - * THEN
PUSHDGTS _
BEGIN BEGIN
DUP '9' > IF DROP EXIT THEN ( stop indicator, we're done ) DUP '9' > IF DROP EXIT THEN ( stop indicator, we're done )
EMIT EMIT
@ -25,24 +24,21 @@
: ? @ . ; : ? @ . ;
: PUSHDGTS : _
999 SWAP ( stop indicator )
DUP 0 = IF '0' EXIT THEN ( 0 is a special case )
BEGIN
DUP 0 = IF DROP EXIT THEN
16 /MOD ( r q )
SWAP ( r q )
DUP 9 > IF 10 - 'a' + DUP 9 > IF 10 - 'a' +
ELSE '0' + THEN ( q d ) ELSE '0' + THEN
SWAP ( d q )
AGAIN
; ;
: .X ( n -- ) ( For hex display, there are no negatives )
( For hex display, there are no negatives )
PUSHDGTS : .x
BEGIN 256 MOD ( ensure < 0x100 )
DUP 'f' > IF DROP EXIT THEN ( stop indicator, we're done ) 16 /MOD ( l h )
EMIT _ EMIT ( l )
AGAIN _ EMIT
;
: .X
256 /MOD ( l h )
.x .x
; ;