]> code.bitgloo.com Git - clyne/sprit-forth.git/commitdiff
fix input parsing
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 3 Mar 2024 14:46:47 +0000 (09:46 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 3 Mar 2024 14:46:47 +0000 (09:46 -0500)
compat.txt
core.fth
source/core.cpp
source/parse.cpp
sprit.cpp

index 3a06bbbad66311f31d5fe6a1d994d39a7b95ae36..faadb691ea2b336ef26fc22585d3cfaa5a3464ac 100644 (file)
@@ -34,8 +34,8 @@ yes 6.1.0480 <
     6.1.0490 <#
 yes 6.1.0530 =
 yes 6.1.0540 >
-    6.1.0550 >BODY
-    6.1.0560 >IN
+yes 6.1.0550 >BODY
+yes 6.1.0560 >IN
     6.1.0570 >NUMBER
 yes 6.1.0580 >R
 yes 6.1.0630 ?DUP
@@ -48,7 +48,7 @@ yes 6.1.0705 ALIGN
 yes 6.1.0706 ALIGNED
 yes 6.1.0710 ALLOT
 yes 6.1.0720 AND
-    6.1.0750 BASE
+yes 6.1.0750 BASE
 yes 6.1.0760 BEGIN
 yes 6.1.0770 BL
 yes 6.1.0850 C!
@@ -63,7 +63,7 @@ yes 6.1.0950 CONSTANT
 yes 6.1.0980 COUNT
 yes 6.1.0990 CR
 yes 6.1.1000 CREATE
-    6.1.1170 DECIMAL
+yes 6.1.1170 DECIMAL
     6.1.1200 DEPTH
 yes 6.1.1240 DO
 yes 6.1.1250 DOES>
@@ -110,7 +110,7 @@ yes 6.1.2165 S"
     6.1.2170 S>D
     6.1.2210 SIGN
     6.1.2214 SM/REM
-    6.1.2216 SOURCE
+yes 6.1.2216 SOURCE
 yes 6.1.2220 SPACE
 yes 6.1.2230 SPACES
 yes 6.1.2250 STATE
index ec34191713c9ebb8454fc90903b10eace67cd5d2..635cdc6975d6ac057da6974f68f51976011f1acb 100644 (file)
--- a/core.fth
+++ b/core.fth
 5 constant five
 five .
 fibs ." hello world"
+source type
index 5842ad829686fe74d892295dddbe9e342fc9ee86..e2da1093b77851c93184d036ed1a84ee9429833e 100644 (file)
@@ -40,15 +40,13 @@ void compileliteral()
 
 bool haskey()
 {
-    return DICT[DIdxSrcLen] > 0;
+    return *((char *)&DICT[DIdxInBuf]) < DICT[DIdxSrcLen];
 }
 
 void addkey(int k)
 {
-    --DICT[DIdxSource];
-    ++DICT[DIdxSrcLen];
-
-    auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]);
+    auto addr = DICT[DIdxSource] + (DICT[DIdxSrcLen]++);
+    auto ptr = reinterpret_cast<char *>(addr);
     *ptr = static_cast<char>(k);
 }
 
@@ -59,10 +57,8 @@ int key()
         getinput();
 
     auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]);
-    ++DICT[DIdxSource];
-    --DICT[DIdxSrcLen];
-
-    return *ptr;
+    int idx = (*((char *)&DICT[DIdxInBuf]))++;
+    return ptr[idx];
 }
 
 Cell *comma(Cell n)
index 97ae4fed8d83856839d69a446d6e6ff76532a4c2..27c514d0eb63362822b4fde4139df164f98962c5 100644 (file)
@@ -57,35 +57,27 @@ static Error parseword(const char *start, const char *end)
 static Error parseSource()
 {
     auto result = Error::none;
-    char *start = nullptr;
-    char *end;
-    char *s;
-
-    while (result == Error::none && haskey()) {
-        s = (char *)DICT[DIdxSource];
-
-        ++DICT[DIdxSource];
-        --DICT[DIdxSrcLen];
-
-        if (isspace(*s)) {
-            if (start) {
-                result = parseword(start, end + 1);
-                start = nullptr;
-            }
-        } else {
-            if (!start) {
-                start = s;
-                end = start;
+    char *start = (char *)DICT[DIdxSource];
+    char *end = start;
+    auto& i = *((char *)&DICT[DIdxInBuf]);
+
+    while (result == Error::none && i < DICT[DIdxSrcLen]) {
+        if (isspace(*end) || *end == '\0') {
+            if (end - start < 1) {
+                start = ++end;
+                ++i;
             } else {
-                ++end;
+                ++i; // discard the space
+                result = parseword(start, end);
+                start = (char *)DICT[DIdxSource] + i;
+                end = start;
             }
+        } else {
+            ++end;
+            ++i;
         }
     }
 
-    // Parse the final word if it is non-empty.
-    if (start)
-        result = parseword(start, s + 1);
-
     return result;
 }
 
@@ -93,9 +85,11 @@ static Error parseSource()
 Error parse()
 {
     // Reset source buffer and try to fill it with getinput().
-    DICT[DIdxSource] = (Cell)&DICT[DIdxBegin];
+    DICT[DIdxSource] = (Cell)&DICT[DIdxInBuf] + 1;
     DICT[DIdxSrcLen] = 0;
+    *((char *)&DICT[DIdxInBuf]) = 0;
     getinput();
+    addkey('\0');
 
     return parseSource();
 }
index 53f850b32a0095b8051ce3155ffa6eab0daeef52..7b36d2f4b339579aa8eeb2108b1c5a68d03a759d 100644 (file)
--- a/sprit.cpp
+++ b/sprit.cpp
@@ -94,7 +94,7 @@ void getinput()
 
     if (std::cin.good()) {
         std::getline(std::cin, line);
-        std::for_each(line.rbegin(), line.rend(), addkey);
+        std::for_each(line.begin(), line.end(), addkey);
     }
 }