]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
revise parsing for better compliance
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 3 Mar 2023 17:44:10 +0000 (12:44 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 3 Mar 2023 17:44:10 +0000 (12:44 -0500)
alee.cpp
core.fth
dictionary.cpp
dictionary.hpp
parser.cpp
test/core.fr

index d3e4c4cd29b10762b7e59813253423a0c530c54e..b2d8fd0452d74061bba4cc4603ad458ea8fed2c8 100644 (file)
--- 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)
index 40ad2b0905fc955382aab97e091cd3c41bee6826..1d9a10a5aa13b62afd956762956a923db61798d0 100644 (file)
--- 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 ;
 
 : 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@ ;
 : [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
 -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 ;
index 27fcb4783af31ef4243fa96682c3ff557d8d5177..ff80957e7a50b1242af70e3a328590c0404b2f3e 100644 (file)
@@ -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 {};
index 48d4e1b0ab018b1c1bd81d6b191eae40c4ddfd63..dbf1dcc68ba506f93e5d24b958616bd0bbe05af5 100644 (file)
@@ -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();
index 9390a79cf170ed84aff2990659b9f8e79eef9394..22c2cc37c08897d60d45e6b294081066e3b09096 100644 (file)
 
 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);
 }
index 10386c49a44b19e48d30c35e2f05864fa648fda3..9b866a2abb9542e3f6a86cc4c0f66c9c551de978 100644 (file)
@@ -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 -> <TRUE> <TRUE> }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
 \