]> code.bitgloo.com Git - clyne/sprit-forth.git/commitdiff
fix word parsing
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 25 Nov 2023 02:46:47 +0000 (21:46 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 25 Nov 2023 02:46:47 +0000 (21:46 -0500)
Makefile
source/core.cpp
source/core.hpp
source/parse.cpp
sprit.cpp

index 677edec487557960dbd5210fc2a366f75e272c26..dbc8c10d47341f239f7d4e6bee862007b9eeebd6 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 SRC := $(wildcard source/*.cpp)
 OBJ := $(subst .cpp,.o,$(SRC))
 
-CXXFLAGS += -std=c++20 -ggdb -g3 -Os \
+CXXFLAGS += -std=c++20 -ggdb -g3 -O0 \
            -Wall -Wextra -pedantic \
            -Isource
 
index 0b520e8131709096feff18982d5d760467e53858..52dbcbb164fe9e13548e9ebfbddcdafada71ebbe 100644 (file)
@@ -83,7 +83,7 @@ void align()
     HERE = aligned(HERE);
 }
 
-static void word(int ch)
+static void readword(int ch)
 {
     int k;
     do {
@@ -102,7 +102,6 @@ static void word(int ch)
 
         k = key();
     } while (k != ch);
-    addkey(k);
 
     // Add a null terminator.
     ptr = reinterpret_cast<char *>(HERE);
@@ -110,12 +109,12 @@ static void word(int ch)
     ++HERE;
 }
 
-void wordword()
+void word()
 {
     auto here = (char *)HERE;
     ++HERE;
 
-    word(*SP);
+    readword(*SP);
 
     here[0] = strlen(here + 1);
     HERE = (Cell)here;
@@ -127,7 +126,7 @@ void colon()
     // Collect (and store) the word's name.
     align();
     auto name = HERE;
-    word(' ');
+    readword(' ');
     align();
 
     // Build the Word structure.
@@ -171,7 +170,7 @@ void tick()
 {
     // Get the name to look up.
     auto name = (char *)HERE;
-    word(' ');
+    readword(' ');
 
     // Look up the name and push the result.
     int len = HERE - (Cell)name - 1;
index 914c39bd9a5cdaaca6dbb26c96ad3571b228a58b..f5149cf7ec27a3e9582f25d9273caca246ba5c81 100644 (file)
@@ -44,8 +44,8 @@ int key();          /** Gets the next key available from the source buffer. */
 
 Cell *comma(Cell n);     /** Stores `n` to HERE++, returns `n`'s storage. */
 Addr aligned(Addr addr); /** Aligns the given address and returns it. */
-void align();    /** Aligns HERE to the next Cell boundary. */
-void wordword(); /** Definition of WORD. */
+void align(); /** Aligns HERE to the next Cell boundary. */
+void word();  /** Definition of WORD. */
 void colon(); /** Begins definition of a new word. */
 void semic(); /** Ends the current word definition which becomes new LATEST. */
 void tick();  /** Gets the execution token for the source buffer's next word. */
index 74f7e6af729bdc312c47d8d8916f0842b75d417c..cebf702de8b161ad8c6936d5770e5cd06b0ed5f6 100644 (file)
@@ -42,25 +42,34 @@ static void parseword(const char *start, const char *end)
 
 void parseSource()
 {
-    auto start = (char *)DICT[DIdxSource];
-    auto end = start;
+    char *start = nullptr;
+    char *end;
+    char *s;
 
-    // Try to build words while we have input available.
     while (haskey()) {
-        end = (char *)++DICT[DIdxSource];
+        s = (char *)DICT[DIdxSource];
+
+        ++DICT[DIdxSource];
         --DICT[DIdxSrcLen];
 
-        if (isspace(*end)) {
-            // May have collected a word.
-            // parseword() will check if start != end.
-            parseword(start, end);
-            start = (char *)(DICT[DIdxSource] + 1);
+        if (isspace(*s)) {
+            if (start) {
+                parseword(start, end + 1);
+                start = nullptr;
+            }
+        } else {
+            if (!start) {
+                start = s;
+                end = start;
+            } else {
+                ++end;
+            }
         }
     }
 
     // Parse the final word if it is non-empty.
-    if (start != end)
-        parseword(start, end);
+    if (start)
+        parseword(start, s + 1);
 }
 
 void parse()
index 3da1423d1d1d3d7f7fb518b4b3e30ca57ec6bd02..cee9b6c3211d533b23e0e4c23695c5596e1b9791 100644 (file)
--- a/sprit.cpp
+++ b/sprit.cpp
@@ -65,7 +65,7 @@ constinit WordSet words (
     Word("_i",        WordWrap<[] { *SP = ((Word *)*SP)->immediate(); }, tobool>()),
     Word("[']",       WordWrap<tick, compileliteral>()).markImmediate(),
     Word("compile,",  WordWrap<peek, commaSP>()),
-    Word("word",      WordWrap<wordword>()),
+    Word("word",      WordWrap<word>()),
     Word("_b",        WordWrap<[] {
         std::putchar('#'); // Gives a good breakpoint spot for gdb
     }>()),