Files
ai-forth-experiment/forth_dict.c
T
haxala1r 0d9b7e3424 feat: add modular Forth interpreter implementation
Co-authored-by: aider (openrouter/tencent/hy3-preview:free) <aider@aider.chat>
2026-05-03 16:50:43 +03:00

29 lines
735 B
C

#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;
}