Browse Source

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

Simpler code, more predictable output.
pull/102/head
Virgil Dupras 4 years ago
parent
commit
7df7416e9e
2 changed files with 19 additions and 22 deletions
  1. +3
    -2
      forth/dictionary.txt
  2. +16
    -20
      forth/fmt.fs

+ 3
- 2
forth/dictionary.txt View File

@@ -193,10 +193,11 @@ core.
number parsing. By default, (parse).
(print) a -- Print string at addr a.
. 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
to (print)
are never considered negative. "-1 .X -> ffff"
C< -- c Read one char from buffered input.
EMIT c -- Spit char c to output stream
IN> -- a Address of variable containing current pos in input


+ 16
- 20
forth/fmt.fs View File

@@ -1,7 +1,6 @@
( requires core, parse )

( TODO FORGET this word )
: PUSHDGTS
: _
999 SWAP ( stop indicator )
DUP 0 = IF '0' EXIT THEN ( 0 is a special case )
BEGIN
@@ -16,7 +15,7 @@
( that "0 1 -" thing is because we don't parse negative
number correctly yet. )
DUP 0 < IF '-' EMIT 0 1 - * THEN
PUSHDGTS
_
BEGIN
DUP '9' > IF DROP EXIT THEN ( stop indicator, we're done )
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' +
ELSE '0' + THEN ( q d )
SWAP ( d q )
AGAIN
ELSE '0' + THEN
;

: .X ( n -- )
( For hex display, there are no negatives )
PUSHDGTS
BEGIN
DUP 'f' > IF DROP EXIT THEN ( stop indicator, we're done )
EMIT
AGAIN
( For hex display, there are no negatives )

: .x
256 MOD ( ensure < 0x100 )
16 /MOD ( l h )
_ EMIT ( l )
_ EMIT
;

: .X
256 /MOD ( l h )
.x .x
;

Loading…
Cancel
Save