return NEXT
end)
-TELL = defPrimWord("TELL", () -> begin
+TYPE = defPrimWord("TYPE", () -> begin
len = popPS()
addr = popPS()
str = getString(addr, len)
end
end, flags=F_IMMED)
-# Odds and Ends
-
-CHAR = defPrimWord("CHAR", () -> begin
- callPrim(mem[WORD])
- wordLen = popPS()
- wordAddr = popPS()
- word = getString(wordAddr, wordLen)
- pushPS(Int64(word[1]))
-
- return NEXT
-end)
+#### VM loop ####
initialized = false
initFileName = nothing
initFileName = Pkg.dir("forth/src/lib.4th")
end
-
-#### VM loop ####
function run(;initialize=true)
# Begin with STDIN as source
push!(sources, STDIN)
: LITERAL IMMEDIATE ['] LIT , , ;
+: CHAR
+ WORD
+ DROP @
+;
+
: [CHAR] IMMEDIATE
CHAR
['] LIT , ,
: ." IMMEDIATE ( -- )
[COMPILE] S" ( read the string, and compile LITSTRING, etc. )
- ['] TELL , ( compile the final TELL )
+ ['] TYPE , ( compile the final TYPE )
;
: .(
AGAIN
;
+( Converts address of counted string into address of
+ start of string and length of string. )
+: COUNT ( addr1 -- addr2 n )
+ DUP 1+ SWAP @ ;
+
( CONSTANTS AND VARIABLES ------------------------------------------------------ )
;
: SEE
- WORD FIND ( find the dictionary entry to decompile )
+ WORD 2DUP FIND ( find the dictionary entry to decompile )
+
+ ?DUP 0= IF
+ ." Word '" TYPE ." ' not found in dictionary."
+ EXIT
+ THEN
+
+ -ROT 2DROP
( Now we search again, looking for the next word in the dictionary. This gives us
the length of the word that we will be decompiling. (Well, mostly it does). )
[CHAR] S EMIT [CHAR] " EMIT SPACE ( print S"<space> )
1+ DUP @ ( get the length word )
SWAP 1+ SWAP ( end start+1 length )
- 2DUP TELL ( print the string )
+ 2DUP TYPE ( print the string )
[CHAR] " EMIT SPACE ( finish the string with a final quote )
+ ( end start+1+len, aligned )
1- ( because we're about to add 4 below )