|
|
@ -27,168 +27,133 @@ static void pushd(State&, DoubleCell);
|
|
|
|
LIBALEE_SECTION
|
|
|
|
LIBALEE_SECTION
|
|
|
|
void CoreWords::run(Cell ins, State& state)
|
|
|
|
void CoreWords::run(Cell ins, State& state)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Cell cell;
|
|
|
|
|
|
|
|
DoubleCell dcell;
|
|
|
|
|
|
|
|
|
|
|
|
Addr index = ins;
|
|
|
|
Addr index = ins;
|
|
|
|
|
|
|
|
auto& ip = state.ip();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
execute:
|
|
|
|
if (index >= Dictionary::Begin) {
|
|
|
|
if (index >= Dictionary::Begin) {
|
|
|
|
auto& ip = state.ip();
|
|
|
|
// must be calling a defined subroutine
|
|
|
|
state.pushr(ip);
|
|
|
|
state.pushr(ip);
|
|
|
|
ip = static_cast<Addr>(index - sizeof(Cell));
|
|
|
|
ip = index;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
wordstbl[index](state);
|
|
|
|
} else switch (index) {
|
|
|
|
}
|
|
|
|
case token("_lit"): // Execution semantics of `literal`.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
Cell CoreWords::findi(State& state, Word word)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return findi(word.begin(&state.dict), word.size());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
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::token(";")) ? 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)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CoreWords::word_lit(State& state) { // Execution semantics of `literal`.
|
|
|
|
|
|
|
|
state.push(state.beyondip());
|
|
|
|
state.push(state.beyondip());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_drop(State& state) {
|
|
|
|
case token("drop"):
|
|
|
|
state.pop();
|
|
|
|
state.pop();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_dup(State& state) {
|
|
|
|
case token("dup"):
|
|
|
|
state.push(state.top());
|
|
|
|
state.push(state.top());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_swap(State& state) {
|
|
|
|
case token("swap"):
|
|
|
|
std::swap(state.top(), state.pick(1));
|
|
|
|
std::swap(state.top(), state.pick(1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_pick(State& state) {
|
|
|
|
case token("pick"):
|
|
|
|
state.push(state.pick(state.pop()));
|
|
|
|
state.push(state.pick(state.pop()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_sys(State& state) { // Calls user-defined "system" handler.
|
|
|
|
case token("sys"): // Calls user-defined "system" handler.
|
|
|
|
user_sys(state);
|
|
|
|
user_sys(state);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_add(State& state) {
|
|
|
|
case token("+"):
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() += cell;
|
|
|
|
state.top() += cell;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_sub(State& state) {
|
|
|
|
case token("-"):
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() -= cell;
|
|
|
|
state.top() -= cell;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_mul(State& state) { // ( n n -- d )
|
|
|
|
case token("m*"): // ( n n -- d )
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
auto dcell = (DoubleCell)state.pop() * cell;
|
|
|
|
dcell = state.pop() * cell;
|
|
|
|
pushd(state, dcell);
|
|
|
|
pushd(state, dcell);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_div(State& state) { // ( d n -- n )
|
|
|
|
case token("_/"): // ( d n -- n )
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
auto dcell = (DoubleCell)popd(state);
|
|
|
|
dcell = popd(state);
|
|
|
|
state.push(static_cast<Cell>(dcell / cell));
|
|
|
|
state.push(static_cast<Cell>(dcell / cell));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_mod(State& state) { // ( d n -- n )
|
|
|
|
case token("_%"): // ( d n -- n )
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
auto dcell = (DoubleCell)popd(state);
|
|
|
|
dcell = popd(state);
|
|
|
|
state.push(static_cast<Cell>(dcell % cell));
|
|
|
|
state.push(static_cast<Cell>(dcell % cell));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_peek(State& state) { // ( addr cell? -- n )
|
|
|
|
case token("_@"): // ( 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;
|
|
|
|
void CoreWords::word_poke(State& state) { // ( n addr cell? -- )
|
|
|
|
case token("_!"): // ( n addr cell? -- )
|
|
|
|
auto 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());
|
|
|
|
else
|
|
|
|
else
|
|
|
|
state.dict.writebyte(addr, state.pop() & 0xFFu);
|
|
|
|
state.dict.writebyte(addr, state.pop() & 0xFFu);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_rpush(State& state) {
|
|
|
|
case token(">r"):
|
|
|
|
state.pushr(state.pop());
|
|
|
|
state.pushr(state.pop());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_rpop(State& state) {
|
|
|
|
case token("r>"):
|
|
|
|
state.push(state.popr());
|
|
|
|
state.push(state.popr());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_eq(State& state) {
|
|
|
|
case token("="):
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() = state.top() == cell ? -1 : 0;
|
|
|
|
state.top() = state.top() == cell ? -1 : 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_lt(State& state) {
|
|
|
|
case token("<"):
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() = state.top() < cell ? -1 : 0;
|
|
|
|
state.top() = state.top() < cell ? -1 : 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_and(State& state) {
|
|
|
|
case token("&"):
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() &= cell;
|
|
|
|
state.top() &= cell;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_or(State& state) {
|
|
|
|
case token("|"):
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() |= cell;
|
|
|
|
state.top() |= cell;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_xor(State& state) {
|
|
|
|
case token("^"):
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() ^= cell;
|
|
|
|
state.top() ^= cell;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_shl(State& state) {
|
|
|
|
case token("<<"):
|
|
|
|
auto 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;
|
|
|
|
void CoreWords::word_shr(State& state) {
|
|
|
|
case token(">>"):
|
|
|
|
auto 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;
|
|
|
|
void CoreWords::word_colon(State& state) { // Begins definition/compilation of new word.
|
|
|
|
case token(":"): // 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())
|
|
|
|
state.input();
|
|
|
|
state.input();
|
|
|
|
state.dict.addDefinition(state.dict.input());
|
|
|
|
state.dict.addDefinition(state.dict.input());
|
|
|
|
state.compiling(true);
|
|
|
|
state.compiling(true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_tick(State& state) { // Collects input word and finds execution token.
|
|
|
|
case token("_'"): // Collects input word 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());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_execute(State& state) {
|
|
|
|
case token("execute"):
|
|
|
|
run(state.pop(), state);
|
|
|
|
index = state.pop();
|
|
|
|
}
|
|
|
|
goto execute;
|
|
|
|
void CoreWords::word_exit(State& state) {
|
|
|
|
case token("exit"):
|
|
|
|
state.ip() = state.popr();
|
|
|
|
ip = state.popr();
|
|
|
|
state.verify(state.ip() != 0, Error::exit);
|
|
|
|
state.verify(ip != 0, Error::exit);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_semic(State& state) { // Concludes word definition.
|
|
|
|
case token(";"): // Concludes word definition.
|
|
|
|
state.dict.add(token("exit"));
|
|
|
|
state.dict.add(token("exit"));
|
|
|
|
state.compiling(false);
|
|
|
|
state.compiling(false);
|
|
|
|
|
|
|
|
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
auto dcell = (DoubleCell)cell - state.dict.latest();
|
|
|
|
dcell = cell - state.dict.latest();
|
|
|
|
if (dcell >= Dictionary::MaxDistance) {
|
|
|
|
if (dcell >= Dictionary::MaxDistance) {
|
|
|
|
// Large distance to previous entry: store in dedicated cell.
|
|
|
|
// Large distance to previous entry: store in dedicated cell.
|
|
|
|
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell),
|
|
|
|
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell),
|
|
|
@ -196,56 +161,59 @@ void CoreWords::word_semic(State& state) { // Concludes word definition.
|
|
|
|
dcell = Dictionary::MaxDistance;
|
|
|
|
dcell = Dictionary::MaxDistance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.dict.write(cell,
|
|
|
|
state.dict.write(cell,
|
|
|
|
(state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << Dictionary::DistancePos));
|
|
|
|
(state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
|
|
|
|
state.dict.latest(cell);
|
|
|
|
state.dict.latest(cell);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_jmp0(State& state) { // Jump if popped value equals zero.
|
|
|
|
case token("_jmp0"): // Jump if popped value equals zero.
|
|
|
|
if (state.pop()) {
|
|
|
|
if (state.pop()) {
|
|
|
|
state.beyondip();
|
|
|
|
state.beyondip();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
state.ip() = static_cast<Addr>(state.beyondip() - sizeof(Cell));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[[fallthrough]];
|
|
|
|
void CoreWords::word_jmp(State& state) { // Unconditional jump.
|
|
|
|
case token("_jmp"): // Unconditional jump.
|
|
|
|
state.ip() = static_cast<Addr>(state.beyondip() - sizeof(Cell));
|
|
|
|
ip = state.beyondip();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
void CoreWords::word_depth(State& state) {
|
|
|
|
case token("depth"):
|
|
|
|
state.push(static_cast<Cell>(state.size()));
|
|
|
|
state.push(static_cast<Cell>(state.size()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_rdepth(State& state) {
|
|
|
|
case token("_rdepth"):
|
|
|
|
state.push(static_cast<Cell>(state.rsize()));
|
|
|
|
state.push(static_cast<Cell>(state.rsize()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_in(State& state) { // Fetches more input from the user input source.
|
|
|
|
case token("_in"): // Fetches more input from the user input source.
|
|
|
|
state.input();
|
|
|
|
state.input();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_ev(State& state) { // Evaluates words from current input source.
|
|
|
|
case token("_ev"): // Evaluates words from current input source.
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto st = state.save();
|
|
|
|
const auto st = state.save();
|
|
|
|
state.ip() = 0;
|
|
|
|
ip = 0;
|
|
|
|
Parser::parseSource(state);
|
|
|
|
Parser::parseSource(state);
|
|
|
|
state.load(st);
|
|
|
|
state.load(st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CoreWords::word_find(State& state) {
|
|
|
|
break;
|
|
|
|
auto cell = state.pop();
|
|
|
|
case token("find"):
|
|
|
|
|
|
|
|
cell = state.pop();
|
|
|
|
find(state,
|
|
|
|
find(state,
|
|
|
|
Word::fromLength(static_cast<Addr>(cell + 1),
|
|
|
|
Word::fromLength(static_cast<Addr>(cell + 1),
|
|
|
|
state.dict.readbyte(cell)));
|
|
|
|
state.dict.readbyte(cell)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_uma(State& state) { // ( d u u -- d ): Unsigned multiply-add.
|
|
|
|
case token("_uma"): // ( d u u -- d ): Unsigned multiply-add.
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto plus = state.pop();
|
|
|
|
const auto plus = state.pop();
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
auto dcell = popd(state);
|
|
|
|
dcell = popd(state);
|
|
|
|
dcell *= static_cast<Addr>(cell);
|
|
|
|
dcell *= static_cast<Addr>(cell);
|
|
|
|
dcell += static_cast<Addr>(plus);
|
|
|
|
dcell += static_cast<Addr>(plus);
|
|
|
|
pushd(state, dcell);
|
|
|
|
pushd(state, dcell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CoreWords::word_ult(State& state) {
|
|
|
|
break;
|
|
|
|
auto cell = state.pop();
|
|
|
|
case token("u<"):
|
|
|
|
|
|
|
|
cell = state.pop();
|
|
|
|
state.top() = static_cast<Addr>(state.top()) <
|
|
|
|
state.top() = static_cast<Addr>(state.top()) <
|
|
|
|
static_cast<Addr>(cell) ? -1 : 0;
|
|
|
|
static_cast<Addr>(cell) ? -1 : 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
void CoreWords::word_ummod(State& state) {
|
|
|
|
case token("um/mod"):
|
|
|
|
auto cell = state.pop();
|
|
|
|
cell = state.pop();
|
|
|
|
auto dcell = popd(state);
|
|
|
|
dcell = popd(state);
|
|
|
|
|
|
|
|
|
|
|
|
state.push(static_cast<Cell>(
|
|
|
|
state.push(static_cast<Cell>(
|
|
|
|
static_cast<DoubleAddr>(dcell) %
|
|
|
|
static_cast<DoubleAddr>(dcell) %
|
|
|
@ -253,5 +221,49 @@ void CoreWords::word_ummod(State& state) {
|
|
|
|
state.push(static_cast<Cell>(
|
|
|
|
state.push(static_cast<Cell>(
|
|
|
|
static_cast<DoubleAddr>(dcell) /
|
|
|
|
static_cast<DoubleAddr>(dcell) /
|
|
|
|
static_cast<Addr>(cell)));
|
|
|
|
static_cast<Addr>(cell)));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // Compacted literals (WordCount <= ins < Begin).
|
|
|
|
|
|
|
|
state.push(ins - WordCount);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ip += sizeof(Cell);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
Cell CoreWords::findi(State& state, Word word)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return findi(word.begin(&state.dict), word.size());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIBALEE_SECTION
|
|
|
|
|
|
|
|
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::token(";")) ? 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)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|