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.
98 lines
4.0 KiB
C++
98 lines
4.0 KiB
C++
#include <algorithm>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include "core.hpp"
|
|
#include "parse.hpp"
|
|
#include "state.hpp"
|
|
#include "types.hpp"
|
|
|
|
// TODO:
|
|
// sys m* _/ _% _' depth _rdepth _in _ev find _uma u< um/mod
|
|
|
|
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 *)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 (pop() == 0)
|
|
jump((FuncList)*IP);
|
|
}>()),
|
|
Word(",", WordWrap<commaSP>()),
|
|
Word("emit", WordWrap<[] { std::putchar(pop()); }>()),
|
|
Word("key", WordWrap<[] { push(key()); }>()),
|
|
Word("key?", WordWrap<[] { push(haskey()); }, tobool>()),
|
|
Word("execute", WordWrap<[] { (void)executor((FuncList *)pop()); }>()),
|
|
Word(":", WordWrap<colon>()),
|
|
Word(";", WordWrap<semic>()).markImmediate(),
|
|
Word("exit", fexit),
|
|
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("align", WordWrap<align>()),
|
|
Word("literal", WordWrap<[] { if (STATE) compileliteral(); }>()).markImmediate(),
|
|
Word("\'", WordWrap<tick>()),
|
|
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 << pop() << ' '; }>())
|
|
);
|
|
|
|
void getinput()
|
|
{
|
|
std::string line;
|
|
|
|
if (std::cin.good()) {
|
|
std::getline(std::cin, line);
|
|
std::for_each(line.rbegin(), line.rend(), addkey);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
initialize(words);
|
|
|
|
while (std::cin.good()) {
|
|
auto result = parse();
|
|
|
|
if (result == Error::none)
|
|
std::cout << (STATE ? "compiled" : "ok") << std::endl;
|
|
else
|
|
std::cout << "error " << static_cast<int>(result) << std::endl;
|
|
}
|
|
}
|
|
|