2018-03-03 01:03:32 +00:00
|
|
|
#include "defs.h"
|
|
|
|
#include "eval.h"
|
|
|
|
#include "word.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static uint8_t dict[DICT_SIZE] = {0};
|
|
|
|
static size_t last = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
append_word(const char *name, const uint8_t len, void(*target)(void))
|
|
|
|
{
|
|
|
|
store_native(dict+last, name, len, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
execute(const char *name, const uint8_t len)
|
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t body = 0;
|
|
|
|
while (true) {
|
|
|
|
if (!match_word(dict+offset, name, len)) {
|
|
|
|
if ((offset = word_link(dict+offset)) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
body = word_body(dict+offset);
|
2018-03-05 20:04:09 +00:00
|
|
|
cwexec((uintptr_t)(dict + body + offset));
|
2018-03-03 01:03:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
store_native(uint8_t *entry, const char *name, const uint8_t len, void(*target)(void))
|
|
|
|
{
|
|
|
|
uintptr_t target_p = (uintptr_t)target;
|
|
|
|
size_t link = 2 + len + (2 * sizeof(uintptr_t));
|
|
|
|
|
|
|
|
/* write the header */
|
|
|
|
entry[0] = len;
|
|
|
|
entry[1] = 0; // flags aren't used yet
|
|
|
|
memcpy(entry+2, name, len);
|
|
|
|
memcpy(entry+2+len, &link, sizeof(link));
|
|
|
|
|
|
|
|
/* write the native executor codeword and the function pointer */
|
|
|
|
memcpy(entry, (uint8_t *)(&nexec_p), sizeof(uintptr_t));
|
|
|
|
memcpy(entry + sizeof(uintptr_t), (uint8_t *)(&target_p), sizeof(uintptr_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
match_word(uint8_t *entry, const char *name, const uint8_t len)
|
|
|
|
{
|
|
|
|
if (entry[0] != len) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(entry+2, name, len) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
word_link(uint8_t *entry)
|
|
|
|
{
|
|
|
|
size_t link;
|
|
|
|
|
|
|
|
if (entry[0] == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(&link, entry+2+entry[0], sizeof(link));
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
word_body(uint8_t *entry)
|
|
|
|
{
|
|
|
|
return 2 + entry[0] + sizeof(size_t);
|
|
|
|
}
|