From 1c76451acc06a3ab39a35925e99e7ca44f8115fa Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 3 Mar 2023 12:44:10 -0500 Subject: [PATCH] revise parsing for better compliance --- alee.cpp | 9 ++------- core.fth | 17 +++++++++-------- dictionary.cpp | 25 +++++++++++++++---------- dictionary.hpp | 2 +- parser.cpp | 10 +++++----- test/core.fr | 45 ++++++++++++++++++++++----------------------- 6 files changed, 54 insertions(+), 54 deletions(-) diff --git a/alee.cpp b/alee.cpp index d3e4c4c..b2d8fd0 100644 --- a/alee.cpp +++ b/alee.cpp @@ -52,18 +52,13 @@ int main(int argc, char *argv[]) static void readchar(State& state) { - auto len = state.dict.read(Dictionary::Input); - Addr addr = Dictionary::Input + sizeof(Cell) + - Dictionary::InputCells - len - 1; - - for (Addr i = 0; i < len; ++i, ++addr) - state.dict.writebyte(addr, state.dict.readbyte(addr + 1)); + auto idx = state.dict.read(Dictionary::Input); + Addr addr = Dictionary::Input + sizeof(Cell) + idx; auto c = std::cin.get(); if (isupper(c)) c += 32; state.dict.writebyte(addr, c ? c : ' '); - state.dict.write(Dictionary::Input, len + 1); } static void save(State& state) diff --git a/core.fth b/core.fth index 40ad2b0..1d9a10a 100644 --- a/core.fth +++ b/core.fth @@ -25,7 +25,7 @@ : imm _latest @ dup @ 1 5 << | swap ! ; : immediate imm ; : state 3 cells ; -: _input 4 cells ; +: >in 4 cells ; : , here ! 1 cells allot ; @@ -138,11 +138,12 @@ : min 2dup <= if drop else nip then ; : max 2dup <= if nip else drop then ; -: key begin _input @ dup 0 <= while drop _in repeat - dup 1- _input ! - _input cell+ 80 chars + swap - c@ ; +: key >in @ 5 cells + + begin dup c@ 0 = while _in repeat + c@ 1 >in +! ; +: key? >in @ 5 cells + c@ 0 <> ; : word here dup >r char+ >r - begin key 2dup <> while + begin key? if key 2dup <> else 0 0 then while r> tuck c! char+ >r repeat 2drop r> r> tuck - 1- over c! ; : count dup char+ swap c@ ; @@ -150,7 +151,8 @@ : [char] char postpone literal ; imm : ( begin [char] ) key <> while repeat ; imm -: \ _input @ begin dup 0 > while key drop 1- repeat drop ; imm +: \ >in @ 5 cells + + begin dup c@ while 0 over c! char+ repeat drop ; imm : type begin dup 0 > while swap dup c@ emit char+ swap 1- repeat 2drop ; : s" state @ if ['] _jmp , here 0 , then @@ -197,8 +199,7 @@ -1 constant true 0 constant false -: >in _input 80 chars + cell+ _input @ - 4 chars - ; -: source _input @ 6 chars + >in 3 chars - swap ; +: source >in cell+ 0 begin 2dup + c@ while char+ repeat ; : quit begin _rdepth 1 > while r> drop repeat postpone [ ; : abort begin depth 0 > while drop repeat quit ; diff --git a/dictionary.cpp b/dictionary.cpp index 27fcb47..ff80957 100644 --- a/dictionary.cpp +++ b/dictionary.cpp @@ -94,27 +94,32 @@ Addr Dictionary::getexec(Addr addr) noexcept Word Dictionary::input() noexcept { - auto len = read(Dictionary::Input); - if (len != 0) { - Addr wordstart = Dictionary::Input + sizeof(Cell) + Dictionary::InputCells - - len; + auto idx = read(Dictionary::Input); + auto ch = readbyte(Dictionary::Input + sizeof(Cell) + idx); + + if (ch) { + Addr wordstart = Dictionary::Input + sizeof(Cell) + idx; Addr wordend = wordstart; - while (len) { - auto b = readbyte(wordend); + do { + ch = readbyte(wordend); - if (isspace(b)) { + if (isspace(ch) || ch == '\0') { if (wordstart != wordend) { - writebyte(Dictionary::Input, len - 1); + if (isspace(ch)) + ++idx; + writebyte(Dictionary::Input, idx); return {wordstart, wordend}; + } else if (ch == '\0') { + return {}; } ++wordstart; } ++wordend; - --len; - } + ++idx; + } while (ch); } return {}; diff --git a/dictionary.hpp b/dictionary.hpp index 48d4e1b..dbf1dcc 100644 --- a/dictionary.hpp +++ b/dictionary.hpp @@ -32,7 +32,7 @@ public: constexpr static Addr Latest = sizeof(Cell) * 2; constexpr static Addr Compiling = sizeof(Cell) * 3; constexpr static Addr Input = sizeof(Cell) * 4; // len data... - constexpr static Addr InputCells = 80; // bytes! + constexpr static Addr InputCells = 82; // bytes! constexpr static Addr Begin = sizeof(Cell) * 5 + InputCells; void initialize(); diff --git a/parser.cpp b/parser.cpp index 9390a79..22c2cc3 100644 --- a/parser.cpp +++ b/parser.cpp @@ -24,15 +24,15 @@ int Parser::parse(State& state, const char *str) { - const auto size = std::strlen(str); - auto addr = Dictionary::Input; - state.dict.write(addr, size + 1); + state.dict.write(addr, 0); - addr += sizeof(Cell) + Dictionary::InputCells - size - 1; + addr += sizeof(Cell); while (*str) state.dict.writebyte(addr++, *str++); - state.dict.writebyte(addr, ' '); + + while (addr < Dictionary::Input + Dictionary::InputCells) + state.dict.writebyte(addr++, '\0'); return parseSource(state); } diff --git a/test/core.fr b/test/core.fr index 10386c4..9b866a2 100644 --- a/test/core.fr +++ b/test/core.fr @@ -793,34 +793,33 @@ T{ W1 -> HERE 2 + }T \ T{ GE6 -> 123 }T \ T{ : GE7 GE2 GE5 ; -> }T \ T{ GE7 -> 124 }T -\ -\ \ ------------------------------------------------------------------------ -\ TESTING SOURCE >IN WORD -\ + +\ ------------------------------------------------------------------------ +." TESTING SOURCE >IN WORD" CR + \ : GS1 S" SOURCE" 2DUP EVALUATE \ >R SWAP >R = R> R> = ; \ T{ GS1 -> }T -\ -\ VARIABLE SCANS -\ : RESCAN? -1 SCANS +! SCANS @ IF 0 >IN ! THEN ; -\ -\ T{ 2 SCANS ! -\ 345 RESCAN? -\ -> 345 345 }T -\ + +VARIABLE SCANS +: RESCAN? -1 SCANS +! SCANS @ IF 0 >IN ! THEN ; +T{ 2 SCANS ! +345 RESCAN? +-> 345 345 }T + \ : GS2 5 SCANS ! S" 123 RESCAN?" EVALUATE ; \ T{ GS2 -> 123 123 123 123 123 }T -\ -\ : GS3 WORD COUNT SWAP C@ ; -\ T{ BL GS3 HELLO -> 5 CHAR H }T -\ T{ CHAR " GS3 GOODBYE" -> 7 CHAR G }T -\ T{ BL GS3 -\ DROP -> 0 }T \ BLANK LINE RETURN ZERO-LENGTH STRING -\ -\ : GS4 SOURCE >IN ! DROP ; -\ T{ GS4 123 456 -\ -> }T -\ + +: GS3 WORD COUNT SWAP C@ ; +T{ BL GS3 HELLO -> 5 CHAR H }T +T{ CHAR " GS3 GOODBYE" -> 7 CHAR G }T +T{ BL GS3 +DROP -> 0 }T \ BLANK LINE RETURN ZERO-LENGTH STRING + +: GS4 SOURCE >IN ! DROP ; +T{ GS4 123 456 +-> }T + \ \ ------------------------------------------------------------------------ \ TESTING <# # #S #> HOLD SIGN BASE >NUMBER HEX DECIMAL \