From 017a469d9c19e2f8fd705a56d562e7abb1574409 Mon Sep 17 00:00:00 2001
From: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 17 Mar 2020 12:26:28 -0400
Subject: [PATCH] forth: Forth-ify "."

---
 apps/forth/core.fs   | 24 ++++++++++++++++++++++++
 apps/forth/dict.asm  | 18 +-----------------
 apps/forth/glue.asm  |  1 -
 apps/forth/util.asm  |  4 ++--
 emul/forth/glue0.asm |  1 -
 emul/forth/glue1.asm |  1 -
 6 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/apps/forth/core.fs b/apps/forth/core.fs
index a021a8e..8578171 100644
--- a/apps/forth/core.fs
+++ b/apps/forth/core.fs
@@ -49,3 +49,27 @@
 : > CMP 1 = ;
 : / /MOD SWAP DROP ;
 : MOD /MOD DROP ;
+
+( Format decimals )
+( TODO FORGET this word )
+: PUSHDGTS
+    999 SWAP        ( stop indicator )
+    DUP 0 = IF '0' EXIT THEN    ( 0 is a special case )
+    BEGIN
+    DUP 0 = IF DROP EXIT THEN
+    10 /MOD         ( r q )
+    SWAP '0' + SWAP ( d q )
+    AGAIN
+;
+
+: .               ( n -- )
+    ( handle negative )
+    ( 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
+    AGAIN
+;
diff --git a/apps/forth/dict.asm b/apps/forth/dict.asm
index 3ac6dbc..1c3d53b 100644
--- a/apps/forth/dict.asm
+++ b/apps/forth/dict.asm
@@ -476,26 +476,10 @@ INP:
 	.dw	sysvarWord
 	.dw	INPUTPOS
 
-; ( n -- )
-	.db "."
-	.fill 6
-	.dw INP
-	.db 0
-DOT:
-	.dw nativeWord
-	pop	de
-	; We check PS explicitly because it doesn't look nice to spew gibberish
-	; before aborting the stack underflow.
-	call	chkPSRS
-	call	pad
-	call	fmtDecimalS
-	call	printstr
-	jp	next
-
 ; ( n a -- )
 	.db "!"
 	.fill 6
-	.dw DOT
+	.dw INP
 	.db 0
 STORE:
 	.dw nativeWord
diff --git a/apps/forth/glue.asm b/apps/forth/glue.asm
index c89d689..a6af811 100644
--- a/apps/forth/glue.asm
+++ b/apps/forth/glue.asm
@@ -5,7 +5,6 @@ jp	forthMain
 .inc "lib/util.asm"
 .inc "lib/parse.asm"
 .inc "lib/ari.asm"
-.inc "lib/fmt.asm"
 .equ FORTH_RAMSTART RAMSTART
 .inc "forth/main.asm"
 .inc "forth/util.asm"
diff --git a/apps/forth/util.asm b/apps/forth/util.asm
index 1ab7eac..740d7f5 100644
--- a/apps/forth/util.asm
+++ b/apps/forth/util.asm
@@ -215,12 +215,12 @@ HLPointsUNWORD:
 	pop	hl
 	ret
 
-; Checks flags Z and C and sets BC to 0 if Z, 1 if C and -1 otherwise
+; Checks flags Z and S and sets BC to 0 if Z, 1 if C and -1 otherwise
 flagsToBC:
 	ld	bc, 0
 	ret	z	; equal
 	inc	bc
-	ret	c	; >
+	ret	m	; >
 	; <
 	dec	bc
 	dec	bc
diff --git a/emul/forth/glue0.asm b/emul/forth/glue0.asm
index f7d9591..7502015 100644
--- a/emul/forth/glue0.asm
+++ b/emul/forth/glue0.asm
@@ -26,7 +26,6 @@
 .inc "lib/util.asm"
 .inc "lib/parse.asm"
 .inc "lib/ari.asm"
-.inc "lib/fmt.asm"
 .equ FORTH_RAMSTART STDIO_RAMEND
 .inc "forth/main.asm"
 .inc "forth/util.asm"
diff --git a/emul/forth/glue1.asm b/emul/forth/glue1.asm
index 2bbdc89..6cb2ee3 100644
--- a/emul/forth/glue1.asm
+++ b/emul/forth/glue1.asm
@@ -17,7 +17,6 @@
 .inc "lib/util.asm"
 .inc "lib/parse.asm"
 .inc "lib/ari.asm"
-.inc "lib/fmt.asm"
 .equ FORTH_RAMSTART STDIO_RAMEND
 .inc "forth/main.asm"
 .inc "forth/util.asm"