fix word parsing

main
Clyne 10 months ago
parent 649785d466
commit bbff2c86a1
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -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

@ -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;

@ -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. */

@ -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()

@ -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
}>()),

Loading…
Cancel
Save