fix: complete number parsing and add outer interpreter
Co-authored-by: aider (openrouter/moonshotai/kimi-k2.6) <aider@aider.chat>
This commit is contained in:
+37
-1
@@ -58,4 +58,40 @@ 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
|
if (end != token && *end == '\0') { // Valid integer
|
||||||
|
if (state == 0) { // Interpret mode: push number
|
||||||
|
data_push((int32_t)v);
|
||||||
|
} else { // Compile mode: compile lit + number
|
||||||
|
Word* lit_w = lookup_word_internal("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");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user