add _nx word, use for most of core

not seeing performance improvement yet
native
Clyne 1 year ago
parent a3fca07d87
commit 6e4c0430de
Signed by: clyne
GPG Key ID: 1B74EE6C49C96795

@ -40,6 +40,9 @@ int main(int argc, char *argv[])
SplitMemDict<sizeof(alee_dat)> dict (alee_dat); SplitMemDict<sizeof(alee_dat)> dict (alee_dat);
State state (dict, readchar); State state (dict, readchar);
dict.initialize();
CoreWords::initialize(state);
std::vector args (argv + 1, argv + argc); std::vector args (argv + 1, argv + argc);
for (const auto& a : args) { for (const auto& a : args) {
std::ifstream file (a); std::ifstream file (a);

@ -46,6 +46,7 @@ int main(int argc, char *argv[])
#endif // ALEE_MSP430 #endif // ALEE_MSP430
dict.initialize(); dict.initialize();
CoreWords::initialize(state);
{ {
std::vector args (argv + 1, argv + argc); std::vector args (argv + 1, argv + argc);

@ -24,6 +24,48 @@ static void find(State&, Word);
static DoubleCell popd(State&); static DoubleCell popd(State&);
static void pushd(State&, DoubleCell); 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 LIBALEE_SECTION
void CoreWords::run(Cell ins, State& state) void CoreWords::run(Cell ins, State& state)
{ {
@ -38,45 +80,18 @@ execute:
return; return;
} else switch (index) { } else switch (index) {
case token("_lit"): word_lit(state); break;// Execution semantics of `literal`. case token("_lit"): word_lit(state); break;// Execution semantics of `literal`.
case token("drop"): word_drop(state); break;
case token("dup"): word_dup(state); break;
case token("swap"): word_swap(state); break;
case token("pick"): word_pick(state); break;
case token("sys"): word_sys(state); break; // Calls user-defined "system" handler.
case token("+"): word_add(state); break;
case token("-"): word_sub(state); break;
case token("m*"): word_mul(state); break; // ( n n -- d )
case token("_/"): word_div(state); break; // ( d n -- n )
case token("_%"): word_mod(state); break; // ( d n -- n )
case token("_@"): word_peek(state); break; // ( addr cell? -- n )
case token("_!"): word_poke(state); break; // ( n addr cell? -- )
case token(">r"): word_rpush(state); break;
case token("r>"): word_rpop(state); break;
case token("="): word_eq(state); break;
case token("<"): word_lt(state); break;
case token("&"): word_and(state); break;
case token("|"): word_or(state); break;
case token("^"): word_xor(state); break;
case token("<<"): word_shl(state); break;
case token(">>"): word_shr(state); break;
case token(":"): word_colon(state); break; // Begins definition/compilation of new word.
case token("_'"): word_tick(state); break; // Collects input word and finds execution token.
case token("execute"): case token("execute"):
// TODO reimplement // TODO reimplement
index = state.pop(); index = state.pop();
goto execute; goto execute;
case token("exit"): word_exit(state); break; case token("exit"): word_exit(state); break;
case token(";"): word_semic(state); break; // Concludes word definition. case token(";"): word_semic(state); break; // Concludes word definition.
case token("_jmp0"): word_jmp0(state); break; // Jump if popped value equals zero. case token("_nx"):
case token("_jmp"): word_jmp(state); break; // Unconditional jump. { auto f = state.beyondip();
case token("depth"): word_depth(state); break; state.ip() = state.popr();
case token("_rdepth"): word_rdepth(state); break; ((void (*)(State&))f)(state);
case token("_in"): word_in(state); break; // Fetches more input from the user input source. state.verify(state.ip() != 0, Error::exit); }
case token("_ev"): word_ev(state); break; // Evaluates words from current input source. break;
case token("find"): word_find(state); break;
case token("_uma"): word_uma(state); break; // ( d u u -- d ): Unsigned multiply-add.
case token("u<"): word_ult(state); break;
case token("um/mod"): word_ummod(state); break;
} }
ip += sizeof(Cell); ip += sizeof(Cell);

@ -43,6 +43,8 @@ void user_sys(State& state);
class CoreWords class CoreWords
{ {
public: public:
static void initialize(State& state);
/** /**
* Searches for the token/index of the given word if it is part of the * Searches for the token/index of the given word if it is part of the
* fundamental word-set. * fundamental word-set.
@ -81,7 +83,7 @@ public:
"<<\0>>\0:\0_'\0execute\0" "<<\0>>\0:\0_'\0execute\0"
"exit\0;\0_jmp0\0_jmp\0" "exit\0;\0_jmp0\0_jmp\0"
"depth\0_rdepth\0_in\0_ev\0find\0" "depth\0_rdepth\0_in\0_ev\0find\0"
"_uma\0u<\0um/mod\0"; "_uma\0u<\0um/mod\0_nx\0";
/** /**
* Count of total fundamental words. * Count of total fundamental words.
@ -114,6 +116,7 @@ private:
return -1; return -1;
} }
public:
static void word_lit(State&); static void word_lit(State&);
static void word_drop(State&); static void word_drop(State&);
static void word_dup(State&); static void word_dup(State&);

@ -28,6 +28,22 @@ void Dictionary::initialize()
write(Source, Input + sizeof(Cell)); 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<Cell>(dcell << DistancePos));
latest(h);
}
LIBALEE_SECTION LIBALEE_SECTION
Addr Dictionary::allot(Cell amount) noexcept Addr Dictionary::allot(Cell amount) noexcept
{ {

@ -159,6 +159,8 @@ public:
*/ */
void addDefinition(Word word) noexcept; void addDefinition(Word word) noexcept;
void addNativeWord(const char *s, void (*func)(State&));
/** /**
* Searches the dictionary for an entry for the given word. * Searches the dictionary for an entry for the given word.
* @param word The dictionary-stored word to search for. * @param word The dictionary-stored word to search for.

Loading…
Cancel
Save