fix: add internal lookup for hidden compiler words
Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) <aider@aider.chat>
This commit is contained in:
@@ -74,6 +74,7 @@ Cell* compile_pop(void);
|
|||||||
|
|
||||||
Word* add_primitive(const char* name, void (*code)(Word*), uint8_t flags);
|
Word* add_primitive(const char* name, void (*code)(Word*), uint8_t flags);
|
||||||
Word* lookup_word(const char* name);
|
Word* lookup_word(const char* name);
|
||||||
|
Word* lookup_word_internal(const char* name);
|
||||||
|
|
||||||
char* next_token(void);
|
char* next_token(void);
|
||||||
|
|
||||||
|
|||||||
@@ -26,3 +26,10 @@ Word* lookup_word(const char* name) {
|
|||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Word* lookup_word_internal(const char* name) {
|
||||||
|
for (Word* w = dict_head; w != NULL; w = w->prev) {
|
||||||
|
if (strcmp(w->name, name) == 0) return w;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|||||||
+1
-37
@@ -58,40 +58,4 @@ void process_token(const char* token) {
|
|||||||
} else { // Not a known word: try to parse as number
|
} else { // Not a known word: try to parse as number
|
||||||
char* end;
|
char* end;
|
||||||
long v = strtol(token, &end, 10);
|
long v = strtol(token, &end, 10);
|
||||||
if (end != token && *end == '\0') { // Valid integer
|
if
|
||||||
if (state == 0) { // Interpret mode: push number
|
|
||||||
data_push((int32_t)v);
|
|
||||||
} else { // Compile mode: compile lit + number
|
|
||||||
Word* lit_w = lookup_word("lit");
|
|
||||||
if (lit_w == NULL) {
|
|
||||||
printf("Fatal: lit word not found\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (compile_idx + 2 > COMPILE_BUF_SIZE) {
|
|
||||||
printf("Compile buffer full\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
compile_buf[compile_idx++] = (Cell){.word = lit_w};
|
|
||||||
compile_buf[compile_idx++] = (Cell){.num = (int32_t)v};
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
printf("Unknown word: '%s'\n", token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void outer_interpreter(void) {
|
|
||||||
while (1) {
|
|
||||||
printf("ok ");
|
|
||||||
fflush(stdout);
|
|
||||||
if (fgets(input_buf, INPUT_BUF_SIZE, stdin) == NULL) {
|
|
||||||
break; // EOF
|
|
||||||
}
|
|
||||||
input_ptr = input_buf;
|
|
||||||
char* tok;
|
|
||||||
while ((tok = next_token()) != NULL) {
|
|
||||||
process_token(tok);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
+6
-6
@@ -388,7 +388,7 @@ void do_dot_quote(Word* w) {
|
|||||||
}
|
}
|
||||||
size_t len = input_ptr - start;
|
size_t len = input_ptr - start;
|
||||||
// Compile do_dot_quote_inner
|
// Compile do_dot_quote_inner
|
||||||
Word* inner_w = lookup_word("do_dot_quote_inner");
|
Word* inner_w = lookup_word_internal("do_dot_quote_inner");
|
||||||
if (inner_w == NULL) {
|
if (inner_w == NULL) {
|
||||||
printf("Fatal: do_dot_quote_inner not found\n");
|
printf("Fatal: do_dot_quote_inner not found\n");
|
||||||
return;
|
return;
|
||||||
@@ -708,7 +708,7 @@ void do_if(Word* w) {
|
|||||||
printf("IF only valid in compile mode\n");
|
printf("IF only valid in compile mode\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Word* zbranch = lookup_word("0branch");
|
Word* zbranch = lookup_word_internal("0branch");
|
||||||
if (zbranch == NULL) {
|
if (zbranch == NULL) {
|
||||||
printf("Fatal: 0branch not found\n");
|
printf("Fatal: 0branch not found\n");
|
||||||
return;
|
return;
|
||||||
@@ -744,7 +744,7 @@ void do_else(Word* w) {
|
|||||||
Cell* if_offset = compile_pop();
|
Cell* if_offset = compile_pop();
|
||||||
if (if_offset == NULL) return;
|
if (if_offset == NULL) return;
|
||||||
|
|
||||||
Word* branch_w = lookup_word("branch");
|
Word* branch_w = lookup_word_internal("branch");
|
||||||
if (branch_w == NULL) {
|
if (branch_w == NULL) {
|
||||||
printf("Fatal: branch not found\n");
|
printf("Fatal: branch not found\n");
|
||||||
return;
|
return;
|
||||||
@@ -783,7 +783,7 @@ void do_until(Word* w) {
|
|||||||
Cell* begin_addr = compile_pop();
|
Cell* begin_addr = compile_pop();
|
||||||
if (begin_addr == NULL) return;
|
if (begin_addr == NULL) return;
|
||||||
|
|
||||||
Word* zbranch = lookup_word("0branch");
|
Word* zbranch = lookup_word_internal("0branch");
|
||||||
if (zbranch == NULL) {
|
if (zbranch == NULL) {
|
||||||
printf("Fatal: 0branch not found\n");
|
printf("Fatal: 0branch not found\n");
|
||||||
return;
|
return;
|
||||||
@@ -808,7 +808,7 @@ void do_while(Word* w) {
|
|||||||
Cell* begin_addr = compile_pop();
|
Cell* begin_addr = compile_pop();
|
||||||
if (begin_addr == NULL) return;
|
if (begin_addr == NULL) return;
|
||||||
|
|
||||||
Word* zbranch = lookup_word("0branch");
|
Word* zbranch = lookup_word_internal("0branch");
|
||||||
if (zbranch == NULL) {
|
if (zbranch == NULL) {
|
||||||
printf("Fatal: 0branch not found\n");
|
printf("Fatal: 0branch not found\n");
|
||||||
return;
|
return;
|
||||||
@@ -837,7 +837,7 @@ void do_repeat(Word* w) {
|
|||||||
Cell* while_offset = compile_pop();
|
Cell* while_offset = compile_pop();
|
||||||
if (while_offset == NULL) return;
|
if (while_offset == NULL) return;
|
||||||
|
|
||||||
Word* branch_w = lookup_word("branch");
|
Word* branch_w = lookup_word_internal("branch");
|
||||||
if (branch_w == NULL) {
|
if (branch_w == NULL) {
|
||||||
printf("Fatal: branch not found\n");
|
printf("Fatal: branch not found\n");
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user