|
|
@ -21,21 +21,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
|
|
void find(State& state, Word word)
|
|
|
|
static void find(State&, Word);
|
|
|
|
{
|
|
|
|
static DoubleCell popd(State&);
|
|
|
|
Cell tok = 0;
|
|
|
|
static void pushd(State&, DoubleCell);
|
|
|
|
Cell imm = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (auto j = state.dict.find(word); j > 0) {
|
|
|
|
|
|
|
|
tok = state.dict.getexec(j);
|
|
|
|
|
|
|
|
imm = (state.dict.read(j) & Dictionary::Immediate) ? 1 : -1;
|
|
|
|
|
|
|
|
} else if (tok = CoreWords::findi(state, word); tok >= 0) {
|
|
|
|
|
|
|
|
imm = (tok == CoreWords::Semicolon) ? 1 : -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
state.push(tok);
|
|
|
|
|
|
|
|
state.push(imm);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CoreWords::run(Cell ins, State& state)
|
|
|
|
void CoreWords::run(Cell ins, State& state)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -43,21 +31,8 @@ void CoreWords::run(Cell ins, State& state)
|
|
|
|
DoubleCell dcell;
|
|
|
|
DoubleCell dcell;
|
|
|
|
|
|
|
|
|
|
|
|
Addr index = ins;
|
|
|
|
Addr index = ins;
|
|
|
|
|
|
|
|
|
|
|
|
auto& ip = state.ip();
|
|
|
|
auto& ip = state.ip();
|
|
|
|
|
|
|
|
|
|
|
|
auto popd = [](State& s) {
|
|
|
|
|
|
|
|
DoubleCell dcell = s.pop();
|
|
|
|
|
|
|
|
dcell <<= sizeof(Cell) * 8;
|
|
|
|
|
|
|
|
dcell |= static_cast<Addr>(s.pop());
|
|
|
|
|
|
|
|
return dcell;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto pushd = [](State& s, DoubleCell d) {
|
|
|
|
|
|
|
|
s.push(static_cast<Cell>(d));
|
|
|
|
|
|
|
|
s.push(static_cast<Cell>(d >> (sizeof(Cell) * 8)));
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
execute:
|
|
|
|
execute:
|
|
|
|
if (index >= Dictionary::Begin) {
|
|
|
|
if (index >= Dictionary::Begin) {
|
|
|
|
// must be calling a defined subroutine
|
|
|
|
// must be calling a defined subroutine
|
|
|
@ -65,7 +40,7 @@ execute:
|
|
|
|
ip = index;
|
|
|
|
ip = index;
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
} else switch (index) {
|
|
|
|
} else switch (index) {
|
|
|
|
case 0: // _lit
|
|
|
|
case 0: // _lit: Execution semantics of `literal`.
|
|
|
|
state.push(state.beyondip());
|
|
|
|
state.push(state.beyondip());
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 1: // drop
|
|
|
|
case 1: // drop
|
|
|
@ -80,7 +55,7 @@ execute:
|
|
|
|
case 4: // pick
|
|
|
|
case 4: // pick
|
|
|
|
state.push(state.pick(state.pop()));
|
|
|
|
state.push(state.pick(state.pop()));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 5: // sys
|
|
|
|
case 5: // sys: Calls user-defined "system" handler.
|
|
|
|
user_sys(state);
|
|
|
|
user_sys(state);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 6: // add
|
|
|
|
case 6: // add
|
|
|
@ -106,13 +81,13 @@ execute:
|
|
|
|
dcell = popd(state);
|
|
|
|
dcell = popd(state);
|
|
|
|
state.push(static_cast<Cell>(dcell % cell));
|
|
|
|
state.push(static_cast<Cell>(dcell % cell));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 11: // peek
|
|
|
|
case 11: // peek ( addr cell? -- n )
|
|
|
|
if (state.pop())
|
|
|
|
if (state.pop())
|
|
|
|
state.push(state.dict.read(state.pop()));
|
|
|
|
state.push(state.dict.read(state.pop()));
|
|
|
|
else
|
|
|
|
else
|
|
|
|
state.push(state.dict.readbyte(state.pop()));
|
|
|
|
state.push(state.dict.readbyte(state.pop()));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 12: // poke
|
|
|
|
case 12: // poke ( n addr cell? -- )
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
if (auto addr = state.pop(); cell)
|
|
|
|
if (auto addr = state.pop(); cell)
|
|
|
|
state.dict.write(addr, state.pop());
|
|
|
|
state.dict.write(addr, state.pop());
|
|
|
@ -153,7 +128,7 @@ execute:
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
|
|
|
|
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 22: // colon
|
|
|
|
case 22: // colon: Begins definition/compilation of new word.
|
|
|
|
state.push(state.dict.alignhere());
|
|
|
|
state.push(state.dict.alignhere());
|
|
|
|
state.dict.write(Dictionary::CompToken, state.top());
|
|
|
|
state.dict.write(Dictionary::CompToken, state.top());
|
|
|
|
while (!state.dict.hasInput())
|
|
|
|
while (!state.dict.hasInput())
|
|
|
@ -161,7 +136,7 @@ execute:
|
|
|
|
state.dict.addDefinition(state.dict.input());
|
|
|
|
state.dict.addDefinition(state.dict.input());
|
|
|
|
state.compiling(true);
|
|
|
|
state.compiling(true);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 23: // tick
|
|
|
|
case 23: // tick: Collects word from input and finds execution token.
|
|
|
|
while (!state.dict.hasInput())
|
|
|
|
while (!state.dict.hasInput())
|
|
|
|
state.input();
|
|
|
|
state.input();
|
|
|
|
find(state, state.dict.input());
|
|
|
|
find(state, state.dict.input());
|
|
|
@ -173,26 +148,27 @@ execute:
|
|
|
|
ip = state.popr();
|
|
|
|
ip = state.popr();
|
|
|
|
state.verify(ip != 0, Error::exit);
|
|
|
|
state.verify(ip != 0, Error::exit);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 26: // semic
|
|
|
|
case 26: // semic: Concludes word definition.
|
|
|
|
state.dict.add(findi("exit"));
|
|
|
|
state.dict.add(findi("exit"));
|
|
|
|
state.compiling(false);
|
|
|
|
state.compiling(false);
|
|
|
|
|
|
|
|
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
dcell = cell - state.dict.latest();
|
|
|
|
dcell = cell - state.dict.latest();
|
|
|
|
if (dcell > (1 << (sizeof(Cell) * 8 - 6)) - 1) {
|
|
|
|
if (dcell > (1 << (sizeof(Cell) * 8 - 6)) - 1) {
|
|
|
|
|
|
|
|
// Large distance to previous entry: store in dedicated cell.
|
|
|
|
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell), static_cast<Cell>(dcell));
|
|
|
|
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell), static_cast<Cell>(dcell));
|
|
|
|
dcell = ((1 << (sizeof(Cell) * 8 - 6)) - 1);
|
|
|
|
dcell = ((1 << (sizeof(Cell) * 8 - 6)) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.dict.write(cell, (state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
|
|
|
|
state.dict.write(cell, (state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
|
|
|
|
state.dict.latest(cell);
|
|
|
|
state.dict.latest(cell);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 27: // _jmp0
|
|
|
|
case 27: // _jmp0: Jump if popped value equals zero.
|
|
|
|
if (state.pop()) {
|
|
|
|
if (state.pop()) {
|
|
|
|
state.beyondip();
|
|
|
|
state.beyondip();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
[[fallthrough]];
|
|
|
|
case 28: // _jmp
|
|
|
|
case 28: // _jmp: Unconditional jump.
|
|
|
|
ip = state.beyondip();
|
|
|
|
ip = state.beyondip();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
case 29: // depth
|
|
|
|
case 29: // depth
|
|
|
@ -201,10 +177,10 @@ execute:
|
|
|
|
case 30: // _rdepth
|
|
|
|
case 30: // _rdepth
|
|
|
|
state.push(static_cast<Cell>(state.rsize()));
|
|
|
|
state.push(static_cast<Cell>(state.rsize()));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 31: // _in
|
|
|
|
case 31: // _in: Fetches more input from the user input source.
|
|
|
|
state.input();
|
|
|
|
state.input();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 32: // _ex
|
|
|
|
case 32: // _ev: Evaluates words from current input source.
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const auto st = state.save();
|
|
|
|
const auto st = state.save();
|
|
|
|
ip = 0;
|
|
|
|
ip = 0;
|
|
|
@ -218,7 +194,7 @@ execute:
|
|
|
|
Word::fromLength(static_cast<Addr>(cell + 1),
|
|
|
|
Word::fromLength(static_cast<Addr>(cell + 1),
|
|
|
|
state.dict.readbyte(cell)));
|
|
|
|
state.dict.readbyte(cell)));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 34: // _uma
|
|
|
|
case 34: // _uma ( d u u -- d ): Unsigned multiply-add.
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const auto plus = state.pop();
|
|
|
|
const auto plus = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
@ -244,7 +220,7 @@ execute:
|
|
|
|
static_cast<DoubleAddr>(dcell) /
|
|
|
|
static_cast<DoubleAddr>(dcell) /
|
|
|
|
static_cast<Addr>(cell)));
|
|
|
|
static_cast<Addr>(cell)));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default: // Compacted literals (WordCount <= ins < Begin).
|
|
|
|
state.push(ins - WordCount);
|
|
|
|
state.push(ins - WordCount);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -257,3 +233,33 @@ Cell CoreWords::findi(State& state, Word word)
|
|
|
|
return findi(word.begin(&state.dict), word.size());
|
|
|
|
return findi(word.begin(&state.dict), word.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void find(State& state, Word word)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Cell tok = 0;
|
|
|
|
|
|
|
|
Cell imm = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (auto j = state.dict.find(word); j > 0) {
|
|
|
|
|
|
|
|
tok = state.dict.getexec(j);
|
|
|
|
|
|
|
|
imm = (state.dict.read(j) & Dictionary::Immediate) ? 1 : -1;
|
|
|
|
|
|
|
|
} else if (tok = CoreWords::findi(state, word); tok >= 0) {
|
|
|
|
|
|
|
|
imm = (tok == CoreWords::Semicolon) ? 1 : -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
state.push(tok);
|
|
|
|
|
|
|
|
state.push(imm);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DoubleCell popd(State& s)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
DoubleCell dcell = s.pop();
|
|
|
|
|
|
|
|
dcell <<= sizeof(Cell) * 8;
|
|
|
|
|
|
|
|
dcell |= static_cast<Addr>(s.pop());
|
|
|
|
|
|
|
|
return dcell;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void pushd(State& s, DoubleCell d)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
s.push(static_cast<Cell>(d));
|
|
|
|
|
|
|
|
s.push(static_cast<Cell>(d >> (sizeof(Cell) * 8)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|