Lexing in REPL

This commit is contained in:
Kyler Olsen 2025-11-28 00:02:13 -07:00
parent 98f6ba8eab
commit fd80b5f69d
3 changed files with 46 additions and 2 deletions

View File

@ -30,5 +30,6 @@
#define COMPILER_VER 0 #define COMPILER_VER 0
#endif #endif
void print_version();
#endif // SLS_MAIN_H #endif // SLS_MAIN_H

View File

@ -11,6 +11,11 @@
#include "sls/bool.h" #include "sls/bool.h"
#include "sls/repl.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[]) { int main(int argc, char *argv[]) {
Boolean version = FALSE; Boolean version = FALSE;
Boolean file = FALSE; Boolean file = FALSE;
@ -25,8 +30,7 @@ int main(int argc, char *argv[]) {
} }
if (version) { if (version) {
printf("YREA SLS (" SLS_NAME ") " SLS_VER " (" GIT_COMMIT_HASH ")\n"); print_version();
printf("Compiled with " COMPILER_NAME " %d at " __DATE__ " " __TIME__ "\n", COMPILER_VER);
return 0; return 0;
} else if (file) { } else if (file) {
return 1; return 1;

View File

@ -3,11 +3,50 @@
// REPL // REPL
// November 2025 // November 2025
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "sls/repl.h" #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("<STDIN>");
int repl(int argc, char *argv[]) { int repl(int argc, char *argv[]) {
(void)argc; (void)argc;
(void)argv; (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; return 1;
} }