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.
95 lines
3.8 KiB
C++
95 lines
3.8 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(*SP--); }
|
|
static void push(Cell value) { *++SP = value; }
|
|
static void pop() { --SP; }
|
|
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("_jmp", WordWrap<[] { jump((FuncList)*++IP); }>()),
|
|
Word("_jmp0", WordWrap<[] {
|
|
++IP;
|
|
if (*SP-- == 0)
|
|
jump((FuncList)*IP);
|
|
}>()),
|
|
Word(",", WordWrap<commaSP>()),
|
|
Word("emit", WordWrap<[] { std::putchar(*SP); }, pop>()),
|
|
Word("key", WordWrap<[] { push(key()); }>()),
|
|
Word("key?", WordWrap<[] { push(haskey()); }, tobool>()),
|
|
Word("execute", WordWrap<[] { executor((FuncList *)*SP--); }>()),
|
|
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("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<wordword>()),
|
|
Word("_b", WordWrap<[] {
|
|
std::putchar('#'); // Gives a good breakpoint spot for gdb
|
|
}>()),
|
|
Word(".", WordWrap<[] { std::cout << *SP << ' '; }, 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()) {
|
|
parse();
|
|
std::cout << (STATE ? "compiled" : "ok") << std::endl;
|
|
}
|
|
}
|
|
|