fix input parsing

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

@ -34,8 +34,8 @@ yes 6.1.0480 <
6.1.0490 <# 6.1.0490 <#
yes 6.1.0530 = yes 6.1.0530 =
yes 6.1.0540 > yes 6.1.0540 >
6.1.0550 >BODY yes 6.1.0550 >BODY
6.1.0560 >IN yes 6.1.0560 >IN
6.1.0570 >NUMBER 6.1.0570 >NUMBER
yes 6.1.0580 >R yes 6.1.0580 >R
yes 6.1.0630 ?DUP yes 6.1.0630 ?DUP
@ -48,7 +48,7 @@ yes 6.1.0705 ALIGN
yes 6.1.0706 ALIGNED yes 6.1.0706 ALIGNED
yes 6.1.0710 ALLOT yes 6.1.0710 ALLOT
yes 6.1.0720 AND yes 6.1.0720 AND
6.1.0750 BASE yes 6.1.0750 BASE
yes 6.1.0760 BEGIN yes 6.1.0760 BEGIN
yes 6.1.0770 BL yes 6.1.0770 BL
yes 6.1.0850 C! yes 6.1.0850 C!
@ -63,7 +63,7 @@ yes 6.1.0950 CONSTANT
yes 6.1.0980 COUNT yes 6.1.0980 COUNT
yes 6.1.0990 CR yes 6.1.0990 CR
yes 6.1.1000 CREATE yes 6.1.1000 CREATE
6.1.1170 DECIMAL yes 6.1.1170 DECIMAL
6.1.1200 DEPTH 6.1.1200 DEPTH
yes 6.1.1240 DO yes 6.1.1240 DO
yes 6.1.1250 DOES> yes 6.1.1250 DOES>
@ -110,7 +110,7 @@ yes 6.1.2165 S"
6.1.2170 S>D 6.1.2170 S>D
6.1.2210 SIGN 6.1.2210 SIGN
6.1.2214 SM/REM 6.1.2214 SM/REM
6.1.2216 SOURCE yes 6.1.2216 SOURCE
yes 6.1.2220 SPACE yes 6.1.2220 SPACE
yes 6.1.2230 SPACES yes 6.1.2230 SPACES
yes 6.1.2250 STATE yes 6.1.2250 STATE

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

@ -40,15 +40,13 @@ void compileliteral()
bool haskey() bool haskey()
{ {
return DICT[DIdxSrcLen] > 0; return *((char *)&DICT[DIdxInBuf]) < DICT[DIdxSrcLen];
} }
void addkey(int k) void addkey(int k)
{ {
--DICT[DIdxSource]; auto addr = DICT[DIdxSource] + (DICT[DIdxSrcLen]++);
++DICT[DIdxSrcLen]; auto ptr = reinterpret_cast<char *>(addr);
auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]);
*ptr = static_cast<char>(k); *ptr = static_cast<char>(k);
} }
@ -59,10 +57,8 @@ int key()
getinput(); getinput();
auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]); auto ptr = reinterpret_cast<char *>(DICT[DIdxSource]);
++DICT[DIdxSource]; int idx = (*((char *)&DICT[DIdxInBuf]))++;
--DICT[DIdxSrcLen]; return ptr[idx];
return *ptr;
} }
Cell *comma(Cell n) Cell *comma(Cell n)

@ -57,35 +57,27 @@ static Error parseword(const char *start, const char *end)
static Error parseSource() static Error parseSource()
{ {
auto result = Error::none; auto result = Error::none;
char *start = nullptr; char *start = (char *)DICT[DIdxSource];
char *end; char *end = start;
char *s; auto& i = *((char *)&DICT[DIdxInBuf]);
while (result == Error::none && haskey()) { while (result == Error::none && i < DICT[DIdxSrcLen]) {
s = (char *)DICT[DIdxSource]; if (isspace(*end) || *end == '\0') {
if (end - start < 1) {
++DICT[DIdxSource]; start = ++end;
--DICT[DIdxSrcLen]; ++i;
if (isspace(*s)) {
if (start) {
result = parseword(start, end + 1);
start = nullptr;
}
} else {
if (!start) {
start = s;
end = start;
} else { } 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; return result;
} }
@ -93,9 +85,11 @@ static Error parseSource()
Error parse() Error parse()
{ {
// Reset source buffer and try to fill it with getinput(). // Reset source buffer and try to fill it with getinput().
DICT[DIdxSource] = (Cell)&DICT[DIdxBegin]; DICT[DIdxSource] = (Cell)&DICT[DIdxInBuf] + 1;
DICT[DIdxSrcLen] = 0; DICT[DIdxSrcLen] = 0;
*((char *)&DICT[DIdxInBuf]) = 0;
getinput(); getinput();
addkey('\0');
return parseSource(); return parseSource();
} }

@ -94,7 +94,7 @@ void getinput()
if (std::cin.good()) { if (std::cin.good()) {
std::getline(std::cin, line); std::getline(std::cin, line);
std::for_each(line.rbegin(), line.rend(), addkey); std::for_each(line.begin(), line.end(), addkey);
} }
} }

Loading…
Cancel
Save