You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
alee-forth/libalee/corewords.cpp

260 lines
7.1 KiB
C++

2 years ago
/**
* Alee Forth: A portable and concise Forth implementation in modern C++.
* Copyright (C) 2023 Clyne Sullivan <clyne@bitgloo.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "corewords.hpp"
#include "parser.hpp"
2 years ago
#include <utility>
2 years ago
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);
}
2 years ago
void CoreWords::run(Cell ins, State& state)
{
Cell cell;
DoubleCell dcell;
2 years ago
Addr index = ins;
auto& ip = state.ip();
2 years ago
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:
if (index >= Dictionary::Begin) {
// must be calling a defined subroutine
state.pushr(ip);
ip = index;
return;
} else switch (index) {
case 0: // _lit
state.push(state.beyondip());
break;
case 1: // drop
state.pop();
break;
case 2: // dup
state.push(state.top());
break;
case 3: // swap
std::swap(state.top(), state.pick(1));
break;
case 4: // pick
state.push(state.pick(state.pop()));
break;
case 5: // sys
user_sys(state);
break;
case 6: // add
cell = state.pop();
state.top() += cell;
break;
case 7: // sub
cell = state.pop();
state.top() -= cell;
break;
case 8: // mul ( n n -- d )
cell = state.pop();
dcell = state.pop() * cell;
2 years ago
pushd(state, dcell);
break;
case 9: // div ( d n -- n )
cell = state.pop();
2 years ago
dcell = popd(state);
state.push(static_cast<Cell>(dcell / cell));
break;
case 10: // mod ( d n -- n )
cell = state.pop();
2 years ago
dcell = popd(state);
state.push(static_cast<Cell>(dcell % cell));
break;
case 11: // peek
if (state.pop())
state.push(state.dict.read(state.pop()));
else
state.push(state.dict.readbyte(state.pop()));
break;
case 12: // poke
cell = state.pop();
if (auto addr = state.pop(); cell)
state.dict.write(addr, state.pop());
else
2 years ago
state.dict.writebyte(addr, state.pop() & 0xFFu);
break;
case 13: // pushr
state.pushr(state.pop());
break;
case 14: // popr
state.push(state.popr());
break;
case 15: // equal
cell = state.pop();
state.top() = state.top() == cell ? -1 : 0;
break;
case 16: // lt
cell = state.pop();
state.top() = state.top() < cell ? -1 : 0;
break;
case 17: // and
cell = state.pop();
state.top() &= cell;
break;
case 18: // or
cell = state.pop();
state.top() |= cell;
break;
case 19: // xor
cell = state.pop();
state.top() ^= cell;
break;
case 20: // shl
cell = state.pop();
reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell);
break;
case 21: // shr
cell = state.pop();
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
break;
case 22: // colon
state.push(state.dict.alignhere());
state.dict.write(Dictionary::CompToken, state.top());
while (!state.dict.hasInput())
state.input();
state.dict.addDefinition(state.dict.input());
state.compiling(true);
break;
case 23: // tick
while (!state.dict.hasInput())
state.input();
find(state, state.dict.input());
break;
case 24: // execute
index = state.pop();
goto execute;
case 25: // exit
ip = state.popr();
state.verify(ip != 0, Error::exit);
break;
case 26: // semic
state.dict.add(findi("exit"));
state.compiling(false);
2 years ago
cell = state.pop();
dcell = cell - state.dict.latest();
if (dcell > (1 << (sizeof(Cell) * 8 - 6)) - 1) {
state.dict.write(static_cast<Addr>(cell) + sizeof(Cell), static_cast<Cell>(dcell));
dcell = ((1 << (sizeof(Cell) * 8 - 6)) - 1);
}
state.dict.write(cell, (state.dict.read(cell) & 0x1F) | static_cast<Cell>(dcell << 6));
2 years ago
state.dict.latest(cell);
break;
case 27: // _jmp0
if (state.pop()) {
state.beyondip();
break;
}
[[fallthrough]];
case 28: // _jmp
ip = state.beyondip();
return;
case 29: // depth
2 years ago
state.push(static_cast<Cell>(state.size()));
break;
case 30: // _rdepth
2 years ago
state.push(static_cast<Cell>(state.rsize()));
break;
case 31: // _in
state.input();
break;
case 32: // _ex
{
const auto st = state.save();
ip = 0;
Parser::parseSource(state);
state.load(st);
}
break;
2 years ago
case 33: // find
2 years ago
cell = state.pop();
find(state,
Word::fromLength(static_cast<Addr>(cell + 1),
state.dict.readbyte(cell)));
2 years ago
break;
2 years ago
case 34: // _uma
{
const auto plus = state.pop();
2 years ago
cell = state.pop();
2 years ago
dcell = popd(state);
2 years ago
dcell *= static_cast<Addr>(cell);
dcell += static_cast<Addr>(plus);
2 years ago
pushd(state, dcell);
2 years ago
}
2 years ago
break;
2 years ago
case 35: // u<
cell = state.pop();
state.top() = static_cast<Addr>(state.top()) <
static_cast<Addr>(cell) ? -1 : 0;
break;
case 36: // um/mod
cell = state.pop();
2 years ago
dcell = popd(state);
2 years ago
2 years ago
state.push(static_cast<Cell>(
static_cast<DoubleAddr>(dcell) %
static_cast<Addr>(cell)));
state.push(static_cast<Cell>(
static_cast<DoubleAddr>(dcell) /
static_cast<Addr>(cell)));
2 years ago
break;
default:
2 years ago
state.push(ins - WordCount);
break;
}
ip += sizeof(Cell);
}
2 years ago
Cell CoreWords::findi(State& state, Word word)
2 years ago
{
return findi(word.begin(&state.dict), word.size());
2 years ago
}