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