fix input parsing

main
Clyne 9 months ago
parent 98e2c14086
commit b82f1c1f7e
Signed by: clyne
GPG Key ID: 1B74EE6C49C96795

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

@ -151,3 +151,4 @@
5 constant five
five .
fibs ." hello world"
source type

@ -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<char *>(DICT[DIdxSource]);
auto addr = DICT[DIdxSource] + (DICT[DIdxSrcLen]++);
auto ptr = reinterpret_cast<char *>(addr);
*ptr = static_cast<char>(k);
}
@ -59,10 +57,8 @@ int key()
getinput();
auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]);
++DICT[DIdxSource];
--DICT[DIdxSrcLen];
return *ptr;
int idx = (*((char *)&DICT[DIdxInBuf]))++;
return ptr[idx];
}
Cell *comma(Cell n)

@ -57,34 +57,26 @@ 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;
}
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 {
if (!start) {
start = s;
++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();
}

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

Loading…
Cancel
Save