From b82f1c1f7e7680aabc64547ef45d4ca13962bc17 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sun, 3 Mar 2024 09:46:47 -0500 Subject: [PATCH] fix input parsing --- compat.txt | 10 +++++----- core.fth | 1 + source/core.cpp | 14 +++++--------- source/parse.cpp | 44 +++++++++++++++++++------------------------- sprit.cpp | 2 +- 5 files changed, 31 insertions(+), 40 deletions(-) diff --git a/compat.txt b/compat.txt index 3a06bbb..faadb69 100644 --- a/compat.txt +++ b/compat.txt @@ -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 diff --git a/core.fth b/core.fth index ec34191..635cdc6 100644 --- a/core.fth +++ b/core.fth @@ -151,3 +151,4 @@ 5 constant five five . fibs ." hello world" +source type diff --git a/source/core.cpp b/source/core.cpp index 5842ad8..e2da109 100644 --- a/source/core.cpp +++ b/source/core.cpp @@ -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(DICT[DIdxSource]); + auto addr = DICT[DIdxSource] + (DICT[DIdxSrcLen]++); + auto ptr = reinterpret_cast(addr); *ptr = static_cast(k); } @@ -59,10 +57,8 @@ int key() getinput(); auto ptr = reinterpret_cast(DICT[DIdxSource]); - ++DICT[DIdxSource]; - --DICT[DIdxSrcLen]; - - return *ptr; + int idx = (*((char *)&DICT[DIdxInBuf]))++; + return ptr[idx]; } Cell *comma(Cell n) diff --git a/source/parse.cpp b/source/parse.cpp index 97ae4fe..27c514d 100644 --- a/source/parse.cpp +++ b/source/parse.cpp @@ -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(); } diff --git a/sprit.cpp b/sprit.cpp index 53f850b..7b36d2f 100644 --- 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); } }