From: Tim Vaughan Date: Wed, 1 Jun 2016 12:06:56 +0000 (+1200) Subject: Implemented CURRENT, FORTH, ORDER. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=ebff3ca5a9b2dd5a6608b696e7862aa7264f6682;p=forth.jl.git Implemented CURRENT, FORTH, ORDER. --- diff --git a/src/forth.jl b/src/forth.jl index 63e7d57..890cf24 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -17,15 +17,15 @@ primNames = Array{ASCIIString,1}() nextVarAddr = 1 H = nextVarAddr; nextVarAddr += 1 # Next free memory address -FORTH_LATEST = nextVarAddr; nextVarAddr += 1 # LFA of latest word in system dict +FORTH_LATEST = nextVarAddr; nextVarAddr += 1 # FORTH dict latest CURRENT = nextVarAddr; nextVarAddr += 1 # Current compilation dict RSP0 = nextVarAddr # bottom of RS PSP0 = RSP0 + size_RS # bottom of PS TIB = PSP0 + size_PS # address of terminal input buffer mem[H] = TIB + size_TIB # location of bottom of dictionary -mem[FORTH_LATEST] = 0 # no previous definition -mem[CURRENT] = FORTH_LATEST # Compile words to system dict initially +mem[FORTH_LATEST] = 0 # zero FORTH dict latest (no previous def) +mem[CURRENT] = FORTH_LATEST-1 # Compile words to system dict initially DICT = mem[H] # Save bottom of dictionary as constant @@ -117,8 +117,8 @@ dictWrite(int::Int64) = dictWrite([int]) dictWriteString(string::ASCIIString) = dictWrite([Int64(c) for c in string]) function createHeader(name::AbstractString, flags::Int64) - mem[mem[H]] = mem[mem[CURRENT]] - mem[mem[CURRENT]] = mem[H] + mem[mem[H]] = mem[mem[CURRENT]+1] + mem[mem[CURRENT]+1] = mem[H] mem[H] += 1 dictWrite(length(name) | flags | NFA_MARK) @@ -213,7 +213,6 @@ end) # Dictionary entries for core built-in variables, constants H_CFA = defExistingVar("H", H) -CURRENT_CFA = defExistingVar("CURRENT", CURRENT) PSP0_CFA = defConst("PSP0", PSP0) RSP0_CFA = defConst("RSP0", RSP0) @@ -714,9 +713,15 @@ dictWrite(defPrim(() -> begin mem[CONTEXT] = reg.W return NEXT end, name="FORTH")) -dictWrite(FORTH_LATEST) +dictWrite(0) # cell for latest + +CURRENT_CFA = defExistingVar("CURRENT", CURRENT) + +# Switch to new FORTH vocabulary cfa +mem[FORTH_CFA+1] = mem[mem[CURRENT]+1] +mem[CURRENT] = FORTH_CFA -CONTEXT, CONTEXT_CFA = defNewVar("CONTEXT", zeros(Int64, 100)) +CONTEXT, CONTEXT_CFA = defNewVar("CONTEXT", zeros(Int64, 10)) mem[CONTEXT] = FORTH_CFA NUMCONTEXT, NUMCONTEXT_CFA = defNewVar("#CONTEXT", 1) @@ -733,7 +738,7 @@ FIND_CFA = defPrimWord("FIND", () -> begin lfa = 0 for vocabCFA in reverse(context) - lfa = mem[vocabCFA+1] + lfa = vocabCFA+1 while (lfa = mem[lfa]) > 0 @@ -893,7 +898,7 @@ end, name="DODOES") DOES_HELPER_CFA = defPrimWord("(DOES>)", () -> begin - pushPS(mem[mem[CURRENT]]) + pushPS(mem[mem[CURRENT]+1]) callPrim(mem[FROMLINK_CFA]) cfa = popPS() @@ -923,7 +928,7 @@ RBRAC_CFA = defPrimWord("]", () -> begin end, flags=F_IMMED) HIDDEN_CFA = defPrimWord("HIDDEN", () -> begin - lenAndFlagsAddr = mem[mem[CURRENT]] + 1 + lenAndFlagsAddr = mem[mem[CURRENT]+1] + 1 mem[lenAndFlagsAddr] = mem[lenAndFlagsAddr] $ F_HIDDEN return NEXT end) @@ -943,7 +948,7 @@ SEMICOLON_CFA = defWord(";", EXIT_CFA], flags=F_IMMED) IMMEDIATE_CFA = defPrimWord("IMMEDIATE", () -> begin - lenAndFlagsAddr = mem[mem[CURRENT]] + 1 + lenAndFlagsAddr = mem[mem[CURRENT]+1] + 1 mem[lenAndFlagsAddr] = mem[lenAndFlagsAddr] $ F_IMMED return NEXT end, flags=F_IMMED) diff --git a/src/lib_1_basic.4th b/src/lib_1_basic.4th index 00b200d..21c7b93 100644 --- a/src/lib_1_basic.4th +++ b/src/lib_1_basic.4th @@ -66,7 +66,7 @@ \ LFA of most recent definition : LATEST - CURRENT @ ; + CURRENT @ 1+ ; \ Compile in recursive call to current word : RECURSE IMMEDIATE diff --git a/src/lib_9_vocab.4th b/src/lib_9_vocab.4th index 2b10730..68b7f6e 100644 --- a/src/lib_9_vocab.4th +++ b/src/lib_9_vocab.4th @@ -10,3 +10,19 @@ BL WORD FIND DROP >NAME DUP @ F_HIDDEN OR SWAP ! ; + +: VOCAB>LATEST ( vcfa -- vlatest ) + 1+ @ @ ; + +: ORDER + + \ Search order + context #context @ 1- + context + do + i @ >name .name + loop + + \ Current (definitions) + 9 emit + current @ >name .name +;