From: Tim Vaughan Date: Wed, 25 May 2016 05:43:08 +0000 (+1200) Subject: Working on standardizing FIND. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=forth.jl.git;a=commitdiff_plain;h=5da35bb425354054043fef285a44d488f713e572 Working on standardizing FIND. --- diff --git a/src/forth.jl b/src/forth.jl index 80f6b76..5956a0d 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -649,6 +649,19 @@ end) # Dictionary searches +TOCFA = defPrimWord(">CFA", () -> begin + + addr = popPS() + lenAndFlags = mem[addr+1] + len = lenAndFlags & F_LENMASK + + pushPS(addr + 2 + len) + + return NEXT +end) + +TOBODY = defWord(">BODY", [INCR, EXIT]) + FIND = defPrimWord("FIND", () -> begin countedAddr = popPS() @@ -676,23 +689,23 @@ FIND = defPrimWord("FIND", () -> begin end end - pushPS(latest) - - return NEXT -end) - -TOCFA = defPrimWord(">CFA", () -> begin - - addr = popPS() - lenAndFlags = mem[addr+1] - len = lenAndFlags & F_LENMASK + if latest > 0 + pushPS(latest) + callPrim(mem[TOCFA]) + if (lenAndFlags & F_IMMED) == F_IMMED + pushPS(1) + else + pushPS(-1) + end + else + pushPS(countedAddr) + pushPS(0) + end - pushPS(addr + 2 + len) return NEXT end) -TOPFA = defWord(">PFA", [TOCFA, INCR, EXIT]) # Branching diff --git a/src/lib.4th b/src/lib.4th index 2312aeb..840ed4b 100644 --- a/src/lib.4th +++ b/src/lib.4th @@ -517,7 +517,7 @@ DOES> @ : TO IMMEDIATE ( n -- ) BL WORD ( get the name of the value ) FIND ( look it up in the dictionary ) - >PFA ( get a pointer to the first data field (the 'LIT') ) + >CFA >BODY ( get a pointer to the first data field (the 'LIT') ) STATE @ IF ( compiling? ) ['] LIT , ( compile LIT ) , ( compile the address of the value ) @@ -531,7 +531,7 @@ DOES> @ : +TO IMMEDIATE BL WORD ( get the name of the value ) FIND ( look it up in the dictionary ) - >PFA ( get a pointer to the first data field (the 'LIT') ) + >CFA >BODY ( get a pointer to the first data field (the 'LIT') ) STATE @ IF ( compiling? ) ['] LIT , ( compile LIT ) , ( compile the address of the value ) @@ -688,7 +688,7 @@ DOES> @ 4 SPACES - >PFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data ) + >CFA >BODY ( get the data address, ie. points after DOCOL | end-of-word start-of-data ) ( now we start decompiling until we hit the end of the word ) BEGIN ( end start )