|
|
|
@ -10,66 +10,65 @@
|
|
|
|
|
// TODO:
|
|
|
|
|
// sys m* _/ _% _' depth _rdepth _in _ev find _uma u< um/mod
|
|
|
|
|
|
|
|
|
|
static void peek() { *SP = *(Cell *)(*SP); }
|
|
|
|
|
static void commaSP() { comma(*SP--); }
|
|
|
|
|
static void push(Cell value) { *++SP = value; }
|
|
|
|
|
static void pop() { --SP; }
|
|
|
|
|
static void tobool() { if (*SP) *SP = -1; }
|
|
|
|
|
static void peek() { *sp() = *(Cell *)(*sp()); }
|
|
|
|
|
static void commaSP() { comma(pop()); }
|
|
|
|
|
static void discard() { auto v = pop(); (void)v; }
|
|
|
|
|
static void tobool() { if (*sp()) *sp() = -1; }
|
|
|
|
|
|
|
|
|
|
constinit WordSet words (
|
|
|
|
|
Word("[", WordWrap<[] { STATE = 0; }>()).markImmediate(),
|
|
|
|
|
Word("]", WordWrap<[] { STATE = -1; }>()),
|
|
|
|
|
Word("@", WordWrap<peek>()),
|
|
|
|
|
Word("c@", WordWrap<peek, [] { *SP &= 0xFF; }>()),
|
|
|
|
|
Word("!", WordWrap<[] { auto a = (Cell *)*SP--; *a = *SP--; }>()),
|
|
|
|
|
Word("c!", WordWrap<[] { auto a = (char *)*SP--; *a = *SP--; }>()),
|
|
|
|
|
Word("_d", WordWrap<[] { *SP += (Cell)DICT.data(); }>()),
|
|
|
|
|
Word("c@", WordWrap<peek, [] { *sp() &= 0xFF; }>()),
|
|
|
|
|
Word("!", WordWrap<[] { auto a = (Cell *)pop(); *a = pop(); }>()),
|
|
|
|
|
Word("c!", WordWrap<[] { auto a = (char *)pop(); *a = pop(); }>()),
|
|
|
|
|
Word("_d", WordWrap<[] { *sp() += (Cell)DICT.data(); }>()),
|
|
|
|
|
Word("_jmp", WordWrap<[] { jump((FuncList)*++IP); }>()),
|
|
|
|
|
Word("_jmp0", WordWrap<[] {
|
|
|
|
|
++IP;
|
|
|
|
|
if (*SP-- == 0)
|
|
|
|
|
if (pop() == 0)
|
|
|
|
|
jump((FuncList)*IP);
|
|
|
|
|
}>()),
|
|
|
|
|
Word(",", WordWrap<commaSP>()),
|
|
|
|
|
Word("emit", WordWrap<[] { std::putchar(*SP); }, pop>()),
|
|
|
|
|
Word("emit", WordWrap<[] { std::putchar(pop()); }>()),
|
|
|
|
|
Word("key", WordWrap<[] { push(key()); }>()),
|
|
|
|
|
Word("key?", WordWrap<[] { push(haskey()); }, tobool>()),
|
|
|
|
|
Word("execute", WordWrap<[] { executor((FuncList *)*SP--); }>()),
|
|
|
|
|
Word("execute", WordWrap<[] { (void)executor((FuncList *)pop()); }>()),
|
|
|
|
|
Word(":", WordWrap<colon>()),
|
|
|
|
|
Word(";", WordWrap<semic>()).markImmediate(),
|
|
|
|
|
Word("exit", fexit),
|
|
|
|
|
Word("drop", WordWrap<pop>()),
|
|
|
|
|
Word("dup", WordWrap<[] { push(*SP); }>()),
|
|
|
|
|
Word("swap", WordWrap<[] { std::swap(*SP, *(SP - 1)); }>()),
|
|
|
|
|
Word("pick", WordWrap<[] { auto t = *(SP - *SP - 1); *SP = t; }>()),
|
|
|
|
|
Word("cells", WordWrap<[] { *SP *= sizeof(Cell); }>()),
|
|
|
|
|
Word("+", WordWrap<[] { *(SP - 1) += *SP; }, pop>()),
|
|
|
|
|
Word("-", WordWrap<[] { *(SP - 1) -= *SP; }, pop>()),
|
|
|
|
|
Word("*", WordWrap<[] { *(SP - 1) *= *SP; }, pop>()),
|
|
|
|
|
Word("/", WordWrap<[] { *(SP - 1) /= *SP; }, pop>()),
|
|
|
|
|
Word("mod", WordWrap<[] { *(SP - 1) %= *SP; }, pop>()),
|
|
|
|
|
Word("=", WordWrap<[] { *(SP - 1) = *(SP - 1) == *SP; }, pop, tobool>()),
|
|
|
|
|
Word("<", WordWrap<[] { *(SP - 1) = *(SP - 1) < *SP; }, pop, tobool>()),
|
|
|
|
|
Word("or", WordWrap<[] { *(SP - 1) |= *SP; }, pop>()),
|
|
|
|
|
Word("and", WordWrap<[] { *(SP - 1) &= *SP; }, pop>()),
|
|
|
|
|
Word("xor", WordWrap<[] { *(SP - 1) ^= *SP; }, pop>()),
|
|
|
|
|
Word("lshift", WordWrap<[] { *(SP - 1) <<= *SP; }, pop>()),
|
|
|
|
|
Word("rshift", WordWrap<[] { *(SP - 1) >>= *SP; }, pop>()),
|
|
|
|
|
Word(">r", WordWrap<[] { *++RP = *SP; }, pop>()),
|
|
|
|
|
Word("r>", WordWrap<[] { push(*RP--); }>()),
|
|
|
|
|
Word("drop", WordWrap<discard>()),
|
|
|
|
|
Word("dup", WordWrap<[] { push(*sp()); }>()),
|
|
|
|
|
Word("swap", WordWrap<[] { std::swap(*sp(), *(sp() - 1)); }>()),
|
|
|
|
|
Word("pick", WordWrap<[] { auto t = *(sp() - *sp() - 1); *sp() = t; }>()),
|
|
|
|
|
Word("cells", WordWrap<[] { *sp() *= sizeof(Cell); }>()),
|
|
|
|
|
Word("+", WordWrap<[] { *(sp() - 1) += *sp(); }, discard>()),
|
|
|
|
|
Word("-", WordWrap<[] { *(sp() - 1) -= *sp(); }, discard>()),
|
|
|
|
|
Word("*", WordWrap<[] { *(sp() - 1) *= *sp(); }, discard>()),
|
|
|
|
|
Word("/", WordWrap<[] { *(sp() - 1) /= *sp(); }, discard>()),
|
|
|
|
|
Word("mod", WordWrap<[] { *(sp() - 1) %= *sp(); }, discard>()),
|
|
|
|
|
Word("=", WordWrap<[] { *(sp() - 1) = *(sp() - 1) == *sp(); }, discard, tobool>()),
|
|
|
|
|
Word("<", WordWrap<[] { *(sp() - 1) = *(sp() - 1) < *sp(); }, discard, tobool>()),
|
|
|
|
|
Word("or", WordWrap<[] { *(sp() - 1) |= *sp(); }, discard>()),
|
|
|
|
|
Word("and", WordWrap<[] { *(sp() - 1) &= *sp(); }, discard>()),
|
|
|
|
|
Word("xor", WordWrap<[] { *(sp() - 1) ^= *sp(); }, discard>()),
|
|
|
|
|
Word("lshift", WordWrap<[] { *(sp() - 1) <<= *sp(); }, discard>()),
|
|
|
|
|
Word("rshift", WordWrap<[] { *(sp() - 1) >>= *sp(); }, discard>()),
|
|
|
|
|
Word(">r", WordWrap<[] { rpush(pop()); }>()),
|
|
|
|
|
Word("r>", WordWrap<[] { push(rpop()); }>()),
|
|
|
|
|
Word("immediate", WordWrap<[] { ((Word *)LATEST)->markImmediate(); }>()),
|
|
|
|
|
Word("aligned", WordWrap<[] { *SP = aligned(*SP); }>()),
|
|
|
|
|
Word("aligned", WordWrap<[] { *sp() = aligned(*sp()); }>()),
|
|
|
|
|
Word("align", WordWrap<align>()),
|
|
|
|
|
Word("literal", WordWrap<[] { if (STATE) compileliteral(); }>()).markImmediate(),
|
|
|
|
|
Word("\'", WordWrap<tick>()),
|
|
|
|
|
Word("_i", WordWrap<[] { *SP = ((Word *)*SP)->immediate(); }, tobool>()),
|
|
|
|
|
Word("_i", WordWrap<[] { *sp() = ((Word *)*sp())->immediate(); }, tobool>()),
|
|
|
|
|
Word("[']", WordWrap<tick, compileliteral>()).markImmediate(),
|
|
|
|
|
Word("compile,", WordWrap<peek, commaSP>()),
|
|
|
|
|
Word("word", WordWrap<word>()),
|
|
|
|
|
Word("_b", WordWrap<[] {
|
|
|
|
|
std::putchar('#'); // Gives a good breakpoint spot for gdb
|
|
|
|
|
}>()),
|
|
|
|
|
Word(".", WordWrap<[] { std::cout << *SP << ' '; }, pop>())
|
|
|
|
|
Word(".", WordWrap<[] { std::cout << pop() << ' '; }>())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
void getinput()
|
|
|
|
@ -87,8 +86,12 @@ int main()
|
|
|
|
|
initialize(words);
|
|
|
|
|
|
|
|
|
|
while (std::cin.good()) {
|
|
|
|
|
parse();
|
|
|
|
|
std::cout << (STATE ? "compiled" : "ok") << std::endl;
|
|
|
|
|
auto result = parse();
|
|
|
|
|
|
|
|
|
|
if (result == Error::none)
|
|
|
|
|
std::cout << (STATE ? "compiled" : "ok") << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "error " << static_cast<int>(result) << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|