feat: add modular Forth interpreter implementation

Co-authored-by: aider (openrouter/tencent/hy3-preview:free) <aider@aider.chat>
This commit is contained in:
2026-05-03 16:50:43 +03:00
parent c9584ccb26
commit 0d9b7e3424
5 changed files with 619 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
#include "forth.h"
Word* add_primitive(const char* name, void (*code)(Word*), uint8_t flags) {
if (dict_idx >= DICT_SIZE) {
printf("Dictionary full\n");
return NULL;
}
Word* w = &dict[dict_idx++];
w->prev = dict_head;
dict_head = w;
size_t len = strlen(name);
if (len > MAX_NAME_LEN) len = MAX_NAME_LEN;
w->flags = flags | (uint8_t)len;
strncpy(w->name, name, len);
w->name[len] = '\0';
w->code = code;
w->body = NULL;
return w;
}
Word* lookup_word(const char* name) {
for (Word* w = dict_head; w != NULL; w = w->prev) {
if (w->flags & (1 << 6)) continue; // Skip hidden words
if (strcmp(w->name, name) == 0) return w;
}
return NULL;
}