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.
254 lines
6.3 KiB
C++
254 lines
6.3 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"
|
||
|
|
||
2 years ago
|
#include <cstring>
|
||
|
#include <utility>
|
||
|
|
||
2 years ago
|
Word getword(State& state)
|
||
2 years ago
|
{
|
||
2 years ago
|
auto word = state.dict.input();
|
||
|
while (word.size() == 0) {
|
||
|
state.input(state);
|
||
|
word = state.dict.input();
|
||
|
}
|
||
|
return word;
|
||
|
}
|
||
2 years ago
|
void newdef(State& state, Word word)
|
||
2 years ago
|
{
|
||
2 years ago
|
auto& dict = state.dict;
|
||
|
|
||
2 years ago
|
auto addr = dict.alignhere();
|
||
|
dict.addDefinition(word);
|
||
2 years ago
|
state.push(addr);
|
||
2 years ago
|
};
|
||
|
void tick(State& state)
|
||
|
{
|
||
|
auto word = getword(state);
|
||
2 years ago
|
if (auto j = state.dict.find(word); j > 0) {
|
||
2 years ago
|
state.push(state.dict.getexec(j));
|
||
2 years ago
|
auto imm = state.dict.read(j) & CoreWords::Immediate;
|
||
|
state.push(imm ? 1 : -1);
|
||
|
} else if (auto i = CoreWords::findi(state, word); i >= 0) {
|
||
|
state.push(i);
|
||
|
state.push(i == CoreWords::Semicolon ? 1 : -1);
|
||
|
} else {
|
||
|
state.push(0);
|
||
2 years ago
|
state.push(0);
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
void CoreWords::run(unsigned int index, State& state)
|
||
|
{
|
||
2 years ago
|
Cell cell;
|
||
2 years ago
|
DoubleCell dcell;
|
||
2 years ago
|
|
||
2 years ago
|
execute:
|
||
2 years ago
|
if (/*(index & 1) == 0 &&*/ index >= WordCount) {
|
||
2 years ago
|
// must be calling a defined subroutine
|
||
|
state.pushr(state.ip);
|
||
2 years ago
|
state.ip = index;
|
||
|
return;
|
||
2 years ago
|
} else switch (index & 0x1F) {
|
||
2 years ago
|
case 0: // _lit
|
||
2 years ago
|
state.push(/*(index & 0xFF00) ? ((Addr)index >> 8u) - 1 :*/ state.beyondip());
|
||
2 years ago
|
break;
|
||
|
case 1: // drop
|
||
2 years ago
|
state.pop();
|
||
|
break;
|
||
2 years ago
|
case 2: // dup
|
||
2 years ago
|
state.push(state.top());
|
||
|
break;
|
||
2 years ago
|
case 3: // swap
|
||
2 years ago
|
std::swap(state.top(), state.pick(1));
|
||
|
break;
|
||
2 years ago
|
case 4: // pick
|
||
2 years ago
|
state.push(state.pick(state.pop()));
|
||
|
break;
|
||
2 years ago
|
case 5: // sys
|
||
2 years ago
|
user_sys(state);
|
||
|
break;
|
||
2 years ago
|
case 6: // add
|
||
2 years ago
|
cell = state.pop();
|
||
|
state.top() += cell;
|
||
|
break;
|
||
2 years ago
|
case 7: // sub
|
||
2 years ago
|
cell = state.pop();
|
||
|
state.top() -= cell;
|
||
|
break;
|
||
2 years ago
|
case 8: // mul ( n n -- d )
|
||
2 years ago
|
cell = state.pop();
|
||
2 years ago
|
dcell = state.pop() * cell;
|
||
|
state.push(dcell);
|
||
|
state.push(dcell >> (sizeof(Cell) * 8));
|
||
2 years ago
|
break;
|
||
2 years ago
|
case 9: // div ( d n -- n )
|
||
2 years ago
|
cell = state.pop();
|
||
2 years ago
|
dcell = state.pop();
|
||
|
dcell <<= sizeof(Cell) * 8;
|
||
2 years ago
|
dcell |= static_cast<Addr>(state.pop());
|
||
2 years ago
|
state.push(dcell / cell);
|
||
2 years ago
|
break;
|
||
2 years ago
|
case 10: // mod ( d n -- n )
|
||
2 years ago
|
cell = state.pop();
|
||
2 years ago
|
dcell = state.pop();
|
||
|
dcell <<= sizeof(Cell) * 8;
|
||
2 years ago
|
dcell |= static_cast<Addr>(state.pop());
|
||
2 years ago
|
state.push(dcell % cell);
|
||
2 years ago
|
break;
|
||
2 years ago
|
case 11: // peek
|
||
2 years ago
|
if (state.pop())
|
||
|
state.push(state.dict.read(state.pop()));
|
||
|
else
|
||
|
state.push(state.dict.readbyte(state.pop()));
|
||
|
break;
|
||
2 years ago
|
case 12: // poke
|
||
2 years ago
|
cell = state.pop();
|
||
|
if (auto addr = state.pop(); cell)
|
||
|
state.dict.write(addr, state.pop());
|
||
|
else
|
||
|
state.dict.writebyte(addr, state.pop());
|
||
|
break;
|
||
2 years ago
|
case 13: // pushr
|
||
2 years ago
|
state.pushr(state.pop());
|
||
|
break;
|
||
2 years ago
|
case 14: // popr
|
||
2 years ago
|
state.push(state.popr());
|
||
|
break;
|
||
2 years ago
|
case 15: // equal
|
||
2 years ago
|
cell = state.pop();
|
||
2 years ago
|
state.top() = state.top() == cell ? -1 : 0;
|
||
2 years ago
|
break;
|
||
2 years ago
|
case 16: // lt
|
||
2 years ago
|
cell = state.pop();
|
||
2 years ago
|
state.top() = state.top() < cell ? -1 : 0;
|
||
2 years ago
|
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();
|
||
2 years ago
|
reinterpret_cast<Addr&>(state.top()) <<= static_cast<Addr>(cell);
|
||
2 years ago
|
break;
|
||
|
case 21: // shr
|
||
|
cell = state.pop();
|
||
2 years ago
|
reinterpret_cast<Addr&>(state.top()) >>= static_cast<Addr>(cell);
|
||
2 years ago
|
break;
|
||
|
case 22: // colon
|
||
2 years ago
|
newdef(state, getword(state));
|
||
2 years ago
|
state.compiling(true);
|
||
|
break;
|
||
|
case 23: // tick
|
||
2 years ago
|
tick(state);
|
||
2 years ago
|
break;
|
||
|
case 24: // execute
|
||
2 years ago
|
index = state.pop();
|
||
|
goto execute;
|
||
2 years ago
|
case 25: // exit
|
||
|
state.ip = state.popr();
|
||
2 years ago
|
if (state.ip == 0) {
|
||
|
std::longjmp(state.jmpbuf,
|
||
|
static_cast<int>(State::Error::exit));
|
||
|
}
|
||
2 years ago
|
break;
|
||
|
case 26: // semic
|
||
2 years ago
|
{
|
||
|
state.dict.add(findi("exit"));
|
||
2 years ago
|
state.compiling(false);
|
||
2 years ago
|
|
||
|
auto addr = state.pop();
|
||
|
state.dict.write(addr,
|
||
|
(state.dict.read(addr) & 0x1F) |
|
||
|
((addr - state.dict.latest()) << 6));
|
||
|
state.dict.latest(addr);
|
||
|
}
|
||
2 years ago
|
break;
|
||
2 years ago
|
case 27: // _jmp0
|
||
|
if (state.pop()) {
|
||
2 years ago
|
state.beyondip();
|
||
2 years ago
|
break;
|
||
|
}
|
||
|
[[fallthrough]];
|
||
|
case 28: // _jmp
|
||
2 years ago
|
state.ip = state.beyondip();
|
||
|
return;
|
||
2 years ago
|
case 29: // depth
|
||
2 years ago
|
state.push(state.size());
|
||
|
break;
|
||
2 years ago
|
case 30: // _rdepth
|
||
2 years ago
|
state.push(state.rsize());
|
||
|
break;
|
||
2 years ago
|
case 31: // _in
|
||
|
state.input(state);
|
||
2 years ago
|
break;
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
state.ip += sizeof(Cell);
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
int CoreWords::findi(const char *word)
|
||
2 years ago
|
{
|
||
2 years ago
|
std::size_t i = 0;
|
||
2 years ago
|
int wordsi = 0;
|
||
|
|
||
2 years ago
|
while (i < sizeof(wordsarr)) {
|
||
2 years ago
|
auto end = i;
|
||
2 years ago
|
while (wordsarr[end])
|
||
2 years ago
|
++end;
|
||
2 years ago
|
|
||
2 years ago
|
if (!std::strcmp(word, wordsarr + i))
|
||
|
return wordsi;
|
||
2 years ago
|
|
||
|
++wordsi;
|
||
|
i = end + 1;
|
||
|
}
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
2 years ago
|
int CoreWords::findi(State& state, Word word)
|
||
2 years ago
|
{
|
||
2 years ago
|
std::size_t i = 0;
|
||
2 years ago
|
int wordsi = 0;
|
||
|
|
||
2 years ago
|
while (i < sizeof(wordsarr)) {
|
||
2 years ago
|
auto end = i;
|
||
2 years ago
|
while (wordsarr[end])
|
||
2 years ago
|
++end;
|
||
2 years ago
|
|
||
2 years ago
|
if (state.dict.equal(word, wordsarr + i, end - i))
|
||
2 years ago
|
return wordsi;
|
||
2 years ago
|
|
||
|
++wordsi;
|
||
|
i = end + 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
return -1;
|
||
2 years ago
|
}
|
||
|
|