]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
evaluate complete
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 4 Mar 2023 12:02:22 +0000 (07:02 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 4 Mar 2023 12:02:22 +0000 (07:02 -0500)
core.fth
dictionary.cpp
dictionary.hpp
parser.cpp
test/core.fr

index 2d769c22de80642536c70b0846fe1326b3ee627c..fd073a853f6432d723b47ce21f0a42ce11436f28 100644 (file)
--- a/core.fth
+++ b/core.fth
@@ -26,7 +26,8 @@
 : immediate imm ;
 : state    3 cells ;
 : _source  4 cells ;
-: >in      5 cells ;
+: _sourceu 5 cells ;
+: >in      6 cells ;
 
 : ,        here ! 1 cells allot ;
 
            dup @ _latest ! cell+ @ here swap - allot ;
 : :noname  0 , here ] ;
 
-: evaluate _source @ >r >in @ >r
-           0 >in ! _source ! 5 sys
-           r> >in ! r> _source ! ;
+: evaluate _source @ >r _sourceu @ >r >in @ >r
+           0 >in ! _sourceu ! _source ! 5 sys
+           r> >in ! r> _sourceu ! r> _source ! ;
index 643260774e31b8ee11f44a53c9a848b7f573b5b3..48230c44e10ed7bc5acd4ec716c641bc0cc2b97c 100644 (file)
@@ -95,33 +95,35 @@ Addr Dictionary::getexec(Addr addr) noexcept
 
 Word Dictionary::input() noexcept
 {
-    auto idx = read(Dictionary::Input);
     auto src = read(Dictionary::Source);
-    auto ch = readbyte(src + idx);
-
-    if (ch) {
-        Addr wordstart = src + idx;
-        Addr wordend = wordstart;
-
-        do {
-            ch = readbyte(wordend);
-
-            if (isspace(ch) || ch == '\0') {
-                if (wordstart != wordend) {
-                    if (isspace(ch))
-                        ++idx;
-                    writebyte(Dictionary::Input, idx);
-                    return {wordstart, wordend};
-                } else if (ch == '\0') {
-                    return {};
-                }
-
-                ++wordstart;
+    auto end = read(Dictionary::SourceLen);
+    auto idx = read(Dictionary::Input);
+
+    Addr wordstart = src + idx;
+    Addr wordend = wordstart;
+
+    while (idx < end) {
+        auto ch = readbyte(wordend);
+
+        if (ch == '\0')
+            break;
+
+        if (isspace(ch)) {
+            if (wordstart != wordend) {
+                writebyte(Dictionary::Input, idx + 1);
+                return {wordstart, wordend};
             }
 
-            ++wordend;
-            ++idx;
-        } while (ch);
+            ++wordstart;
+        }
+
+        ++wordend;
+        ++idx;
+    }
+
+    if (wordstart != wordend) {
+        writebyte(Dictionary::Input, idx + 1);
+        return {wordstart, wordend};
     }
 
     return {};
index 20ac7e4ba989f2d9f5a765bc77807701a1a23643..235bd78f032795674a884cfb866434177c30b578 100644 (file)
@@ -32,9 +32,10 @@ public:
     constexpr static Addr Latest     = sizeof(Cell) * 2;
     constexpr static Addr Compiling  = sizeof(Cell) * 3;
     constexpr static Addr Source     = sizeof(Cell) * 4;
-    constexpr static Addr Input      = sizeof(Cell) * 5; // len data...
+    constexpr static Addr SourceLen  = sizeof(Cell) * 5;
+    constexpr static Addr Input      = sizeof(Cell) * 6; // len data...
     constexpr static Addr InputCells = 82; // bytes!
-    constexpr static Addr Begin      = sizeof(Cell) * 6 + InputCells;
+    constexpr static Addr Begin      = sizeof(Cell) * 7 + InputCells;
 
     void initialize();
 
index 22c2cc37c08897d60d45e6b294081066e3b09096..44b5cec31d1a8af973294c98c1d408e68e5813e7 100644 (file)
@@ -26,6 +26,7 @@ int Parser::parse(State& state, const char *str)
 {
     auto addr = Dictionary::Input;
     state.dict.write(addr, 0);
+    state.dict.write(Dictionary::SourceLen, std::strlen(str));
 
     addr += sizeof(Cell);
     while (*str)
index 9b866a2abb9542e3f6a86cc4c0f66c9c551de978..2d8b8e42428298aa1528fe65c67834ba284029f2 100644 (file)
@@ -776,23 +776,23 @@ T{ ' W1 >BODY -> HERE }T
 T{ W1 -> HERE 1 + }T
 T{ W1 -> HERE 2 + }T
 
-\ ------------------------------------------------------------------------
-\ TESTING EVALUATE
-\ 
-: GE1 S" 123" ; IMMEDIATE
-: GE2 S" 123 1+" ; IMMEDIATE
-: GE3 S" : GE4 345 ;" ;
-: GE5 EVALUATE ; IMMEDIATE
-\ 
-T{ GE1 EVALUATE -> 123 }T         ( TEST EVALUATE IN INTERP. STATE )
-T{ GE2 EVALUATE -> 124 }T
-T{ GE3 EVALUATE -> }T
-T{ GE4 -> 345 }T
-\ 
-T{ : GE6 GE1 GE5 ; -> }T         ( TEST EVALUATE IN COMPILE STATE )
-T{ GE6 -> 123 }T
-T{ : GE7 GE2 GE5 ; -> }T
-T{ GE7 -> 124 }T
+\ ------------------------------------------------------------------------
+." TESTING EVALUATE" CR
+
+: GE1 S" 123" ; IMMEDIATE
+: GE2 S" 123 1+" ; IMMEDIATE
+: GE3 S" : GE4 345 ;" ;
+: GE5 EVALUATE ; IMMEDIATE
+
+T{ GE1 EVALUATE -> 123 }T         ( TEST EVALUATE IN INTERP. STATE )
+T{ GE2 EVALUATE -> 124 }T
+T{ GE3 EVALUATE -> }T
+T{ GE4 -> 345 }T
+
+T{ : GE6 GE1 GE5 ; -> }T         ( TEST EVALUATE IN COMPILE STATE )
+T{ GE6 -> 123 }T
+T{ : GE7 GE2 GE5 ; -> }T
+T{ GE7 -> 124 }T
 
 \ ------------------------------------------------------------------------
 ." TESTING SOURCE >IN WORD" CR