From bbff2c86a173d7ff65ef7a378c8de7de62af9f9b Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 24 Nov 2023 21:46:47 -0500 Subject: [PATCH] fix word parsing --- Makefile | 2 +- source/core.cpp | 11 +++++------ source/core.hpp | 4 ++-- source/parse.cpp | 31 ++++++++++++++++++++----------- sprit.cpp | 2 +- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 677edec..dbc8c10 100644 --- 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 diff --git a/source/core.cpp b/source/core.cpp index 0b520e8..52dbcbb 100644 --- a/source/core.cpp +++ b/source/core.cpp @@ -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(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; diff --git a/source/core.hpp b/source/core.hpp index 914c39b..f5149cf 100644 --- a/source/core.hpp +++ b/source/core.hpp @@ -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. */ diff --git a/source/parse.cpp b/source/parse.cpp index 74f7e6a..cebf702 100644 --- a/source/parse.cpp +++ b/source/parse.cpp @@ -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() diff --git a/sprit.cpp b/sprit.cpp index 3da1423..cee9b6c 100644 --- a/sprit.cpp +++ b/sprit.cpp @@ -65,7 +65,7 @@ constinit WordSet words ( Word("_i", WordWrap<[] { *SP = ((Word *)*SP)->immediate(); }, tobool>()), Word("[']", WordWrap()).markImmediate(), Word("compile,", WordWrap()), - Word("word", WordWrap()), + Word("word", WordWrap()), Word("_b", WordWrap<[] { std::putchar('#'); // Gives a good breakpoint spot for gdb }>()),