From fd80b5f69dc9a14f970395154422ef3db99a287f Mon Sep 17 00:00:00 2001 From: Kyler Date: Fri, 28 Nov 2025 00:02:13 -0700 Subject: [PATCH] Lexing in REPL --- SLS_C/include/sls/main.h | 1 + SLS_C/src/main.c | 8 ++++++-- SLS_C/src/repl.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/SLS_C/include/sls/main.h b/SLS_C/include/sls/main.h index 819596c..7f92e2f 100644 --- a/SLS_C/include/sls/main.h +++ b/SLS_C/include/sls/main.h @@ -30,5 +30,6 @@ #define COMPILER_VER 0 #endif +void print_version(); #endif // SLS_MAIN_H diff --git a/SLS_C/src/main.c b/SLS_C/src/main.c index e4bf89f..0b12858 100644 --- a/SLS_C/src/main.c +++ b/SLS_C/src/main.c @@ -11,6 +11,11 @@ #include "sls/bool.h" #include "sls/repl.h" +void print_version() { + printf("YREA SLS (" SLS_NAME ") " SLS_VER " (" GIT_COMMIT_HASH ")\n"); + printf("Compiled with " COMPILER_NAME " %d at " __DATE__ " " __TIME__ "\n", COMPILER_VER); +} + int main(int argc, char *argv[]) { Boolean version = FALSE; Boolean file = FALSE; @@ -25,8 +30,7 @@ int main(int argc, char *argv[]) { } if (version) { - printf("YREA SLS (" SLS_NAME ") " SLS_VER " (" GIT_COMMIT_HASH ")\n"); - printf("Compiled with " COMPILER_NAME " %d at " __DATE__ " " __TIME__ "\n", COMPILER_VER); + print_version(); return 0; } else if (file) { return 1; diff --git a/SLS_C/src/repl.c b/SLS_C/src/repl.c index 152c5da..8c39e4c 100644 --- a/SLS_C/src/repl.c +++ b/SLS_C/src/repl.c @@ -3,11 +3,50 @@ // REPL // November 2025 +#include +#include +#include + #include "sls/repl.h" +#include "sls/main.h" +#include "sls/errors.h" +#include "sls/lexer.h" +#include "sls/string.h" + +static const size_t BUFFER_SIZE = 256; +static const SlsStr REPL_FILE_NAME = SLS_STR_CONST(""); int repl(int argc, char *argv[]) { (void)argc; (void)argv; + print_version(); + printf("===== YREA SLS REPL =====\n"); + printf("Type `#exit` to exit.\n"); + + LexerInfo lexer_info; + char buf[BUFFER_SIZE]; + while (fgets(buf, sizeof buf, stdin)) { + size_t len = strlen(buf); + if (len && buf[len - 1] == '\n') buf[len - 1] = '\0'; + if (strncmp("#exit", buf, BUFFER_SIZE) == 0) return 0; + SlsStr code = sls_str_malloc(buf, BUFFER_SIZE); + init_lexer(&lexer_info, REPL_FILE_NAME, code); + LexerResult result = lexical_analysis(&lexer_info); + if (result.type == SLS_ERROR) { + printf("%s\n", result.error.message.str); + } else { + LexerTokenResult *head = result.result; + while (head) { + if (head->type == SLS_ERROR) + printf("%s\n", head->error.message.str); + else + printf("%s\n", TOKEN_TYPES_NAMES[head->result.type]); + head = head->next; + } + } + sls_str_free(&code); + } + return 1; }