From: Tim Vaughan Date: Thu, 2 Jun 2016 11:04:52 +0000 (+1200) Subject: Factored FIND. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=a02d0aced80ab33dad899f91106ede749882b883;p=forth.jl.git Factored FIND. --- diff --git a/src/forth.jl b/src/forth.jl index 9668025..37ce5d1 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -726,39 +726,30 @@ mem[CURRENT] = FORTH_CFA CONTEXT, CONTEXT_CFA = defNewVar("CONTEXT", zeros(Int64, 10)) mem[CONTEXT] = FORTH_CFA -FIND_CFA = defPrimWord("FIND", () -> begin - +FINDVOCAB_CFA = defPrimWord("FINDVOCAB", () -> begin + vocabCFA = popPS() countedAddr = popPS() + wordAddr = countedAddr + 1 wordLen = mem[countedAddr] word = lowercase(getString(wordAddr, wordLen)) - context = mem[CONTEXT:(CONTEXT+mem[NUMCONTEXT]-1)] - + lfa = vocabCFA+1 lenAndFlags = 0 - lfa = 0 - - for vocabCFA in reverse(context) - lfa = vocabCFA+1 - while (lfa = mem[lfa]) > 0 + while (lfa = mem[lfa]) > 0 - lenAndFlags = mem[lfa+1] - len = lenAndFlags & F_LENMASK - hidden = (lenAndFlags & F_HIDDEN) == F_HIDDEN + lenAndFlags = mem[lfa+1] + len = lenAndFlags & F_LENMASK + hidden = (lenAndFlags & F_HIDDEN) == F_HIDDEN - if hidden || len != wordLen - continue - end - - thisWord = lowercase(getString(lfa+2, len)) - - if thisWord == word - break - end + if hidden || len != wordLen + continue end - if lfa>0 + thisWord = lowercase(getString(lfa+2, len)) + + if thisWord == word break end end @@ -779,6 +770,31 @@ FIND_CFA = defPrimWord("FIND", () -> begin return NEXT end) +FIND_CFA = defPrimWord("FIND", () -> begin + + countedAddr = popPS() + context = mem[CONTEXT:(CONTEXT+mem[NUMCONTEXT]-1)] + + for vocabCFA in reverse(context) + pushPS(countedAddr) + pushPS(vocabCFA) + callPrim(mem[FINDVOCAB_CFA]) + + callPrim(mem[DUP_CFA]) + if popPS() != 0 + return NEXT + else + popPS() + popPS() + end + end + + pushPS(countedAddr) + pushPS(0) + + return NEXT +end) + # Branching