revise parsing for better compliance

llvm
Clyne 2 years ago
parent 9a58f8a55d
commit 1c76451acc

@ -52,18 +52,13 @@ int main(int argc, char *argv[])
static void readchar(State& state) static void readchar(State& state)
{ {
auto len = state.dict.read(Dictionary::Input); auto idx = state.dict.read(Dictionary::Input);
Addr addr = Dictionary::Input + sizeof(Cell) + Addr addr = Dictionary::Input + sizeof(Cell) + idx;
Dictionary::InputCells - len - 1;
for (Addr i = 0; i < len; ++i, ++addr)
state.dict.writebyte(addr, state.dict.readbyte(addr + 1));
auto c = std::cin.get(); auto c = std::cin.get();
if (isupper(c)) if (isupper(c))
c += 32; c += 32;
state.dict.writebyte(addr, c ? c : ' '); state.dict.writebyte(addr, c ? c : ' ');
state.dict.write(Dictionary::Input, len + 1);
} }
static void save(State& state) static void save(State& state)

@ -25,7 +25,7 @@
: imm _latest @ dup @ 1 5 << | swap ! ; : imm _latest @ dup @ 1 5 << | swap ! ;
: immediate imm ; : immediate imm ;
: state 3 cells ; : state 3 cells ;
: _input 4 cells ; : >in 4 cells ;
: , here ! 1 cells allot ; : , here ! 1 cells allot ;
@ -138,11 +138,12 @@
: min 2dup <= if drop else nip then ; : min 2dup <= if drop else nip then ;
: max 2dup <= if nip else drop then ; : max 2dup <= if nip else drop then ;
: key begin _input @ dup 0 <= while drop _in repeat : key >in @ 5 cells +
dup 1- _input ! begin dup c@ 0 = while _in repeat
_input cell+ 80 chars + swap - c@ ; c@ 1 >in +! ;
: key? >in @ 5 cells + c@ 0 <> ;
: word here dup >r char+ >r : 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 r> tuck c! char+ >r repeat
2drop r> r> tuck - 1- over c! ; 2drop r> r> tuck - 1- over c! ;
: count dup char+ swap c@ ; : count dup char+ swap c@ ;
@ -150,7 +151,8 @@
: [char] char postpone literal ; imm : [char] char postpone literal ; imm
: ( begin [char] ) key <> while repeat ; 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 ; : type begin dup 0 > while swap dup c@ emit char+ swap 1- repeat 2drop ;
: s" state @ if ['] _jmp , here 0 , then : s" state @ if ['] _jmp , here 0 , then
@ -197,8 +199,7 @@
-1 constant true -1 constant true
0 constant false 0 constant false
: >in _input 80 chars + cell+ _input @ - 4 chars - ; : source >in cell+ 0 begin 2dup + c@ while char+ repeat ;
: source _input @ 6 chars + >in 3 chars - swap ;
: quit begin _rdepth 1 > while r> drop repeat postpone [ ; : quit begin _rdepth 1 > while r> drop repeat postpone [ ;
: abort begin depth 0 > while drop repeat quit ; : abort begin depth 0 > while drop repeat quit ;

@ -94,27 +94,32 @@ Addr Dictionary::getexec(Addr addr) noexcept
Word Dictionary::input() noexcept Word Dictionary::input() noexcept
{ {
auto len = read(Dictionary::Input); auto idx = read(Dictionary::Input);
if (len != 0) { auto ch = readbyte(Dictionary::Input + sizeof(Cell) + idx);
Addr wordstart = Dictionary::Input + sizeof(Cell) + Dictionary::InputCells
- len; if (ch) {
Addr wordstart = Dictionary::Input + sizeof(Cell) + idx;
Addr wordend = wordstart; Addr wordend = wordstart;
while (len) { do {
auto b = readbyte(wordend); ch = readbyte(wordend);
if (isspace(b)) { if (isspace(ch) || ch == '\0') {
if (wordstart != wordend) { if (wordstart != wordend) {
writebyte(Dictionary::Input, len - 1); if (isspace(ch))
++idx;
writebyte(Dictionary::Input, idx);
return {wordstart, wordend}; return {wordstart, wordend};
} else if (ch == '\0') {
return {};
} }
++wordstart; ++wordstart;
} }
++wordend; ++wordend;
--len; ++idx;
} } while (ch);
} }
return {}; return {};

@ -32,7 +32,7 @@ public:
constexpr static Addr Latest = sizeof(Cell) * 2; constexpr static Addr Latest = sizeof(Cell) * 2;
constexpr static Addr Compiling = sizeof(Cell) * 3; constexpr static Addr Compiling = sizeof(Cell) * 3;
constexpr static Addr Input = sizeof(Cell) * 4; // len data... 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; constexpr static Addr Begin = sizeof(Cell) * 5 + InputCells;
void initialize(); void initialize();

@ -24,15 +24,15 @@
int Parser::parse(State& state, const char *str) int Parser::parse(State& state, const char *str)
{ {
const auto size = std::strlen(str);
auto addr = Dictionary::Input; 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) while (*str)
state.dict.writebyte(addr++, *str++); state.dict.writebyte(addr++, *str++);
state.dict.writebyte(addr, ' ');
while (addr < Dictionary::Input + Dictionary::InputCells)
state.dict.writebyte(addr++, '\0');
return parseSource(state); return parseSource(state);
} }

@ -793,34 +793,33 @@ T{ W1 -> HERE 2 + }T
\ T{ GE6 -> 123 }T \ T{ GE6 -> 123 }T
\ T{ : GE7 GE2 GE5 ; -> }T \ T{ : GE7 GE2 GE5 ; -> }T
\ T{ GE7 -> 124 }T \ T{ GE7 -> 124 }T
\
\ \ ------------------------------------------------------------------------ \ ------------------------------------------------------------------------
\ TESTING SOURCE >IN WORD ." TESTING SOURCE >IN WORD" CR
\
\ : GS1 S" SOURCE" 2DUP EVALUATE \ : GS1 S" SOURCE" 2DUP EVALUATE
\ >R SWAP >R = R> R> = ; \ >R SWAP >R = R> R> = ;
\ T{ GS1 -> <TRUE> <TRUE> }T \ T{ GS1 -> <TRUE> <TRUE> }T
\
\ VARIABLE SCANS VARIABLE SCANS
\ : RESCAN? -1 SCANS +! SCANS @ IF 0 >IN ! THEN ; : RESCAN? -1 SCANS +! SCANS @ IF 0 >IN ! THEN ;
\ T{ 2 SCANS !
\ T{ 2 SCANS ! 345 RESCAN?
\ 345 RESCAN? -> 345 345 }T
\ -> 345 345 }T
\
\ : GS2 5 SCANS ! S" 123 RESCAN?" EVALUATE ; \ : GS2 5 SCANS ! S" 123 RESCAN?" EVALUATE ;
\ T{ GS2 -> 123 123 123 123 123 }T \ T{ GS2 -> 123 123 123 123 123 }T
\
\ : GS3 WORD COUNT SWAP C@ ; : GS3 WORD COUNT SWAP C@ ;
\ T{ BL GS3 HELLO -> 5 CHAR H }T T{ BL GS3 HELLO -> 5 CHAR H }T
\ T{ CHAR " GS3 GOODBYE" -> 7 CHAR G }T T{ CHAR " GS3 GOODBYE" -> 7 CHAR G }T
\ T{ BL GS3 T{ BL GS3
\ DROP -> 0 }T \ BLANK LINE RETURN ZERO-LENGTH STRING DROP -> 0 }T \ BLANK LINE RETURN ZERO-LENGTH STRING
\
\ : GS4 SOURCE >IN ! DROP ; : GS4 SOURCE >IN ! DROP ;
\ T{ GS4 123 456 T{ GS4 123 456
\ -> }T -> }T
\
\ \ ------------------------------------------------------------------------ \ \ ------------------------------------------------------------------------
\ TESTING <# # #S #> HOLD SIGN BASE >NUMBER HEX DECIMAL \ TESTING <# # #S #> HOLD SIGN BASE >NUMBER HEX DECIMAL
\ \

Loading…
Cancel
Save