From f4b969986d53858a21a35042b92461b1d8ed2c39 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 21 Mar 2020 17:21:01 -0400 Subject: [PATCH] forth: Forth-ify "."" --- emul/Makefile | 2 +- forth/core.fs | 10 +++--- forth/dictionary.txt | 3 ++ forth/forth.asm | 97 ++++++++++++++++++++-------------------------------- 4 files changed, 47 insertions(+), 65 deletions(-) diff --git a/emul/Makefile b/emul/Makefile index e3cea9a..a36ec88 100644 --- a/emul/Makefile +++ b/emul/Makefile @@ -7,7 +7,7 @@ AVRABIN = zasm/avra SHELLAPPS = zasm ed SHELLTGTS = ${SHELLAPPS:%=cfsin/%} # Those Forth source files are in a particular order -FORTHSRCS = core.fs str.fs parse.fs readln.fs fmt.fs +FORTHSRCS = core.fs str.fs parse.fs readln.fs fmt.fs high.fs FORTHSRC_PATHS = ${FORTHSRCS:%=../forth/%} CFSIN_CONTENTS = $(SHELLTGTS) cfsin/user.h OBJS = emul.o libz80/libz80.o diff --git a/forth/core.fs b/forth/core.fs index a2fc0cc..9738429 100644 --- a/forth/core.fs +++ b/forth/core.fs @@ -44,17 +44,17 @@ : > CMP 1 = ; : / /MOD SWAP DROP ; : MOD /MOD DROP ; -: ABORT" [COMPILE] ." COMPILE ABORT ; IMMEDIATE -( In addition to pushing H this compiles 2 >R so that loop variables are sent - to PS at runtime ) +( In addition to pushing H this compiles 2 >R so that loop + variables are sent to PS at runtime ) : DO COMPILE SWAP COMPILE >R COMPILE >R H ; IMMEDIATE -( One could think that we should have a sub word to avoid all these COMPILE, - but we can't because otherwise it messes with the RS ) +( One could think that we should have a sub word to avoid all + these COMPILE, but we can't because otherwise it messes with + the RS ) : LOOP COMPILE R> 1 LITN COMPILE + COMPILE DUP COMPILE >R COMPILE I' COMPILE = COMPILE SKIP? COMPILE (bbr) diff --git a/forth/dictionary.txt b/forth/dictionary.txt index 5ff6340..8f28c62 100644 --- a/forth/dictionary.txt +++ b/forth/dictionary.txt @@ -143,10 +143,13 @@ CMP n1 n2 -- n Compare n1 and n2 and set n to -1, 0, or 1. NOT f -- f Push the logical opposite of f *** Strings *** +LIT -- Write a LIT entry. You're expected to write actual + string to HERE right afterwards. LIT< x -- Read following word and write to HERE as a string literal. LITS a -- Write word at addr a as a atring literal. SCMP a1 a2 -- n Compare strings a1 and a2. See CMP +SCPY a -- Copy string at addr a into HERE. SLEN a -- n Push length of str at a. *** I/O *** diff --git a/forth/forth.asm b/forth/forth.asm index 5ff98b0..63de954 100644 --- a/forth/forth.asm +++ b/forth/forth.asm @@ -754,42 +754,10 @@ PRINT: inc hl jr .loop - - .db '.', '"' - .fill 5 - .dw PRINT - .db 1 ; IMMEDIATE -PRINTI: - .dw compiledWord - .dw NUMBER - .dw LIT - .dw WR - ; BBR mark - .dw CIN - .dw DUP - .dw NUMBER - .dw '"' - .dw CMP - .dw CSKIP - .dw FBR - .db 6 - .dw CWR - .dw BBR - .db 19 - ; FBR mark - ; null terminate string - .dw NUMBER - .dw 0 - .dw CWR - .dw NUMBER - .dw PRINT - .dw WR - .dw EXIT - ; ( c port -- ) .db "PC!" .fill 4 - .dw PRINTI + .dw PRINT .db 0 PSTORE: .dw nativeWord @@ -895,29 +863,26 @@ DEFINE: .dw compiledWord .dw WORD .dw FIND_ + .dw NOT .dw CSKIP - .dw .maybeNum + .dw FBR + .db 7 + ; Maybe number + .dw PARSEI + .dw LITN + .dw EXIT + ; FBR mark .dw DUP .dw ISIMMED .dw CSKIP - .dw .word + .dw FBR + .db 5 ; is immediate. just execute. .dw EXECUTE .dw EXIT - -.word: - .dw compiledWord + ; FBR mark + ; just a word, write .dw WR - .dw R2P ; exit .compile - .dw DROP - .dw EXIT - -.maybeNum: - .dw compiledWord - .dw PARSEI - .dw LITN - .dw R2P ; exit .compile - .dw DROP .dw EXIT @@ -988,19 +953,11 @@ LITN: ld (HERE), hl jp next - .db "LITS" + .db "SCPY" .fill 3 .dw LITN - .db 1 ; IMMEDIATE -LITS: - .dw compiledWord - .dw NUMBER - .dw LIT - .dw WR - .dw .scpy - .dw EXIT - -.scpy: + .db 0 +SCPY: .dw nativeWord pop hl ld de, (HERE) @@ -1009,6 +966,28 @@ LITS: jp next + .db "LIT" + .fill 4 + .dw SCPY + .db 0 +LIT_: + .dw compiledWord + .dw NUMBER + .dw LIT + .dw WR + .dw EXIT + + .db "LITS" + .fill 3 + .dw LIT_ + .db 0 +LITS: + .dw compiledWord + .dw LIT_ + .dw SCPY + .dw EXIT + + .db "LIT<" .fill 3 .dw LITS