Set up interpreter in repl

This commit is contained in:
Kyler Olsen 2025-11-28 20:50:06 -07:00
parent ba8aee6d78
commit c100bfcf7b
1 changed files with 12 additions and 4 deletions

View File

@ -12,6 +12,7 @@
#include "sls/errors.h" #include "sls/errors.h"
#include "sls/lexer.h" #include "sls/lexer.h"
#include "sls/string.h" #include "sls/string.h"
#include "sls/interpreter.h"
static const size_t BUFFER_SIZE = 256; static const size_t BUFFER_SIZE = 256;
static const SlsStr REPL_FILE_NAME = SLS_STR_CONST("<STDIN>"); static const SlsStr REPL_FILE_NAME = SLS_STR_CONST("<STDIN>");
@ -25,11 +26,16 @@ int repl(int argc, char *argv[]) {
printf("Type `#exit` to exit.\n"); printf("Type `#exit` to exit.\n");
LexerInfo lexer_info; LexerInfo lexer_info;
InterpreterState *interpreter_state = interpreter_create();
char buf[BUFFER_SIZE]; char buf[BUFFER_SIZE];
while (fgets(buf, sizeof buf, stdin)) { while (fgets(buf, sizeof buf, stdin)) {
size_t len = strlen(buf); size_t len = strlen(buf);
if (len && buf[len - 1] == '\n') buf[len - 1] = '\0'; if (len && buf[len - 1] == '\n') buf[len - 1] = '\0';
if (strncmp("#exit", buf, BUFFER_SIZE) == 0) return 0; if (strncmp("#exit", buf, BUFFER_SIZE) == 0) {
interpreter_delete(interpreter_state);
return 0;
}
SlsStr code = sls_str_malloc(buf, BUFFER_SIZE); SlsStr code = sls_str_malloc(buf, BUFFER_SIZE);
init_lexer(&lexer_info, REPL_FILE_NAME, code); init_lexer(&lexer_info, REPL_FILE_NAME, code);
LexerResult result = lexical_analysis(&lexer_info); LexerResult result = lexical_analysis(&lexer_info);
@ -38,15 +44,17 @@ int repl(int argc, char *argv[]) {
} else { } else {
LexerTokenResult *head = result.result; LexerTokenResult *head = result.result;
while (head) { while (head) {
if (head->type == SLS_ERROR) if (head->type == SLS_ERROR) {
printf("%s\n", head->error.message.str); printf("%s\n", head->error.message.str);
else break;
printf("%s\n", TOKEN_TYPES_NAMES[head->result.type]); } else
execute(interpreter_state, head);
head = head->next; head = head->next;
} }
} }
sls_str_free(&code); sls_str_free(&code);
} }
interpreter_delete(interpreter_state);
return 1; return 1;
} }