From ec3b03c4fd2fc9ecad4dd810ba7b6e4766ff286b Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 1 Jan 2024 11:45:50 -0500 Subject: [PATCH] remove addNativeWord --- alee-standalone.cpp | 3 +- alee.cpp | 1 - libalee/corewords.cpp | 70 +++--------------------------------------- libalee/corewords.hpp | 42 +++++++++++++++++++++++-- libalee/dictionary.cpp | 16 ---------- libalee/dictionary.hpp | 2 -- libalee/state.cpp | 4 ++- libalee/state.hpp | 4 +++ 8 files changed, 53 insertions(+), 89 deletions(-) diff --git a/alee-standalone.cpp b/alee-standalone.cpp index 95c72e5..e5c85fe 100644 --- a/alee-standalone.cpp +++ b/alee-standalone.cpp @@ -40,8 +40,7 @@ int main(int argc, char *argv[]) SplitMemDict dict (alee_dat); State state (dict, readchar); - dict.initialize(); - CoreWords::initialize(state); + //dict.initialize(); std::vector args (argv + 1, argv + argc); for (const auto& a : args) { diff --git a/alee.cpp b/alee.cpp index e8194c6..cc9abde 100644 --- a/alee.cpp +++ b/alee.cpp @@ -46,7 +46,6 @@ int main(int argc, char *argv[]) #endif // ALEE_MSP430 dict.initialize(); - CoreWords::initialize(state); { std::vector args (argv + 1, argv + argc); diff --git a/libalee/corewords.cpp b/libalee/corewords.cpp index c564cdb..b66570f 100644 --- a/libalee/corewords.cpp +++ b/libalee/corewords.cpp @@ -24,77 +24,18 @@ static void find(State&, Word); static DoubleCell popd(State&); static void pushd(State&, DoubleCell); -LIBALEE_SECTION -void CoreWords::initialize(State& state) -{ - auto& d = state.dict; - //d.addNativeWord("_lit", word_lit); - d.addNativeWord("drop", word_drop); - d.addNativeWord("dup", word_dup); - d.addNativeWord("swap", word_swap); - d.addNativeWord("pick", word_pick); - d.addNativeWord("sys", word_sys); - d.addNativeWord("+", word_add); - d.addNativeWord("-", word_sub); - d.addNativeWord("m*", word_mul); - d.addNativeWord("_/", word_div); - d.addNativeWord("_%", word_mod); - d.addNativeWord("_@", word_peek); - d.addNativeWord("_!", word_poke); - d.addNativeWord(">r", word_rpush); - d.addNativeWord("r>", word_rpop); - d.addNativeWord("=", word_eq); - d.addNativeWord("<", word_lt); - d.addNativeWord("&", word_and); - d.addNativeWord("|", word_or); - d.addNativeWord("^", word_xor); - d.addNativeWord("<<", word_shl); - d.addNativeWord(">>", word_shr); - d.addNativeWord(":", word_colon); - d.addNativeWord("_'", word_tick); - //d.addNativeWord("exit", word_exit); - //d.addNativeWord(";", word_semic); - d.addNativeWord("_jmp0", word_jmp0); - d.addNativeWord("_jmp", word_jmp); - d.addNativeWord("depth", word_depth); - d.addNativeWord("_rdepth", word_rdepth); - d.addNativeWord("_in", word_in); - d.addNativeWord("_ev", word_ev); - d.addNativeWord("find", word_find); - d.addNativeWord("_uma", word_uma); - d.addNativeWord("u<", word_ult); - d.addNativeWord("um/mod", word_ummod); -} - LIBALEE_SECTION void CoreWords::run(Cell ins, State& state) { Addr index = ins; - auto& ip = state.ip(); -execute: if (index >= Dictionary::Begin) { - // must be calling a defined subroutine + auto& ip = state.ip(); state.pushr(ip); - ip = index; - return; - } else switch (index) { - case token("_lit"): word_lit(state); break;// Execution semantics of `literal`. - case token("execute"): - // TODO reimplement - index = state.pop(); - goto execute; - case token("exit"): word_exit(state); break; - case token(";"): word_semic(state); break; // Concludes word definition. - case token("_nx"): - { auto f = state.beyondip(); - state.ip() = state.popr(); - ((void (*)(State&))f)(state); - state.verify(state.ip() != 0, Error::exit); } - break; + ip = static_cast(index - sizeof(Cell)); + } else { + wordstbl[index](state); } - - ip += sizeof(Cell); } LIBALEE_SECTION @@ -236,8 +177,7 @@ void CoreWords::word_tick(State& state) { // Collects input word and finds execu find(state, state.dict.input()); } void CoreWords::word_execute(State& state) { - /*index =*/ state.pop(); - /* TODO goto execute; */ + run(state.pop(), state); } void CoreWords::word_exit(State& state) { state.ip() = state.popr(); diff --git a/libalee/corewords.hpp b/libalee/corewords.hpp index 5d2e5ca..f301dd4 100644 --- a/libalee/corewords.hpp +++ b/libalee/corewords.hpp @@ -43,8 +43,6 @@ void user_sys(State& state); class CoreWords { public: - static void initialize(State& state); - /** * Searches for the token/index of the given word if it is part of the * fundamental word-set. @@ -154,6 +152,46 @@ public: static void word_uma(State&); static void word_ult(State&); static void word_ummod(State&); + + constexpr static void (*wordstbl[])(State&) = { + word_lit, + word_drop, + word_dup, + word_swap, + word_pick, + word_sys, + word_add, + word_sub, + word_mul, + word_div, + word_mod, + word_peek, + word_poke, + word_rpush, + word_rpop, + word_eq, + word_lt, + word_and, + word_or, + word_xor, + word_shl, + word_shr, + word_colon, + word_tick, + word_execute, + word_exit, + word_semic, + word_jmp0, + word_jmp, + word_depth, + word_rdepth, + word_in, + word_ev, + word_find, + word_uma, + word_ult, + word_ummod + }; }; #endif // ALEEFORTH_COREWORDS_HPP diff --git a/libalee/dictionary.cpp b/libalee/dictionary.cpp index e946c59..e4f6761 100644 --- a/libalee/dictionary.cpp +++ b/libalee/dictionary.cpp @@ -28,22 +28,6 @@ void Dictionary::initialize() write(Source, Input + sizeof(Cell)); } -LIBALEE_SECTION -void Dictionary::addNativeWord(const char *s, void (*func)(State&)) -{ - const Addr h = read(Here); - Addr n = h + sizeof(Cell); - for (; *s; ++s, ++n) - writebyte(n, *s); - addDefinition(Word(h + sizeof(Cell), n)); - add(CoreWords::token("_nx")); - add((Cell)func); - - const auto dcell = h - latest(); - write(h, (read(h) & 0x1F) | Native | static_cast(dcell << DistancePos)); - latest(h); -} - LIBALEE_SECTION Addr Dictionary::allot(Cell amount) noexcept { diff --git a/libalee/dictionary.hpp b/libalee/dictionary.hpp index d465e60..23e4079 100644 --- a/libalee/dictionary.hpp +++ b/libalee/dictionary.hpp @@ -158,8 +158,6 @@ public: */ void addDefinition(Word word) noexcept; - void addNativeWord(const char *s, void (*func)(State&)); - /** * Searches the dictionary for an entry for the given word. * @param word The dictionary-stored word to search for. diff --git a/libalee/state.cpp b/libalee/state.cpp index ed1562f..cb3a2d3 100644 --- a/libalee/state.cpp +++ b/libalee/state.cpp @@ -54,8 +54,10 @@ Error State::execute(Addr addr) if (context.ip >= Dictionary::Begin) { // longjmp will exit this loop. - for (;;) + for (;;) { + context.ip += sizeof(Cell); CoreWords::run(dict.read(context.ip), *this); + } } else { // addr was a CoreWord, all done now. context.ip = 0; diff --git a/libalee/state.hpp b/libalee/state.hpp index a5e49b5..0a61a20 100644 --- a/libalee/state.hpp +++ b/libalee/state.hpp @@ -28,6 +28,8 @@ #include #include +//#define verify(C, E) + /** * Size of the primary data stack, number of cells. */ @@ -179,6 +181,8 @@ public: return dict.read(context.ip); } +//#undef verify + /** * Asserts the given condition is true, longjmp-ing if false. * Used as an exception handler and the method of exiting execution.