From: Tim Vaughan Date: Fri, 6 May 2016 08:22:35 +0000 (+1200) Subject: Implemented COUNT, renamed TELL to TYPE X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=f756abef44feaa53e5f3cf2b7ff2f9bc7940eb10;p=forth.jl.git Implemented COUNT, renamed TELL to TYPE --- diff --git a/src/forth.jl b/src/forth.jl index b1d782c..bf2c8fe 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -857,7 +857,7 @@ LITSTRING = defPrimWord("LITSTRING", () -> begin return NEXT end) -TELL = defPrimWord("TELL", () -> begin +TYPE = defPrimWord("TYPE", () -> begin len = popPS() addr = popPS() str = getString(addr, len) @@ -986,17 +986,7 @@ EOF_WORD = defPrimWord("\x04", () -> begin 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 @@ -1006,8 +996,6 @@ elseif isfile(Pkg.dir("forth/src/lib.4th")) initFileName = Pkg.dir("forth/src/lib.4th") end - -#### VM loop #### function run(;initialize=true) # Begin with STDIN as source push!(sources, STDIN) diff --git a/src/lib.4th b/src/lib.4th index 955cadf..c38faa1 100644 --- a/src/lib.4th +++ b/src/lib.4th @@ -33,6 +33,11 @@ : LITERAL IMMEDIATE ['] LIT , , ; +: CHAR + WORD + DROP @ +; + : [CHAR] IMMEDIATE CHAR ['] LIT , , @@ -454,7 +459,7 @@ : ." IMMEDIATE ( -- ) [COMPILE] S" ( read the string, and compile LITSTRING, etc. ) - ['] TELL , ( compile the final TELL ) + ['] TYPE , ( compile the final TYPE ) ; : .( @@ -469,6 +474,11 @@ 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 ------------------------------------------------------ ) @@ -621,7 +631,14 @@ ; : 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). ) @@ -684,7 +701,7 @@ [CHAR] S EMIT [CHAR] " EMIT SPACE ( print S" ) 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 )