Added executing a file
This commit is contained in:
parent
53983d8e92
commit
90492053f2
|
|
@ -6,4 +6,11 @@
|
|||
#ifndef SLS_FILE_H
|
||||
#define SLS_FILE_H
|
||||
|
||||
#include "sls/bool.h"
|
||||
#include "sls/string.h"
|
||||
#include "sls/interpreter.h"
|
||||
|
||||
Boolean exec_file(InterpreterState *interpreter_state, SlsStr filename);
|
||||
int file(SlsStr filename);
|
||||
|
||||
#endif // SLS_FILE_H
|
||||
|
|
|
|||
|
|
@ -6,6 +6,6 @@
|
|||
#ifndef SLS_REPL_H
|
||||
#define SLS_REPL_H
|
||||
|
||||
int repl(int argc, char *argv[]);
|
||||
int repl();
|
||||
|
||||
#endif // SLS_REPL_H
|
||||
|
|
|
|||
|
|
@ -3,4 +3,67 @@
|
|||
// File
|
||||
// November 2025
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "sls/file.h"
|
||||
#include "sls/bool.h"
|
||||
#include "sls/string.h"
|
||||
#include "sls/interpreter.h"
|
||||
|
||||
Boolean exec_file(InterpreterState *interpreter_state, SlsStr filename) {
|
||||
FILE *file_o = fopen(filename.str, "r");
|
||||
if (file_o == NULL) {
|
||||
printf("Cannot read file: %s\n", filename.str);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (fseek(file_o, 0, SEEK_END)) return FALSE;
|
||||
size_t file_length = ftell(file_o);
|
||||
rewind(file_o);
|
||||
|
||||
LexerInfo lexer_info;
|
||||
SlsStr code = sls_str_new(file_length);
|
||||
for (size_t i = 0; i < file_length; i++) {
|
||||
int character = fgetc(file_o);
|
||||
if (character == EOF) return FALSE;
|
||||
else code.str[i] = (char)character;
|
||||
}
|
||||
init_lexer(&lexer_info, filename, code);
|
||||
|
||||
LexerResult result = lexical_analysis(&lexer_info);
|
||||
if (result.type == SLS_ERROR) {
|
||||
printf("%s\n", result.error.message.str);
|
||||
sls_str_free(&result.error.message);
|
||||
return FALSE;
|
||||
} else {
|
||||
LexerTokenResult *head = result.result;
|
||||
while (head) {
|
||||
if (head->type == SLS_ERROR) {
|
||||
printf("%s\n", head->error.message.str);
|
||||
sls_str_free(&result.error.message);
|
||||
clean_token_result(result.result);
|
||||
return FALSE;
|
||||
} else
|
||||
if (!execute(interpreter_state, head)) {
|
||||
printf("A runtime error occurred!\n");
|
||||
break;
|
||||
}
|
||||
head = head->next;
|
||||
}
|
||||
clean_token_result(result.result);
|
||||
}
|
||||
sls_str_free(&code);
|
||||
|
||||
if (fclose(file_o)) return FALSE;
|
||||
else return TRUE;
|
||||
}
|
||||
|
||||
int file(SlsStr filename) {
|
||||
printf("Executing file: %s\n", filename.str);
|
||||
InterpreterState *interpreter_state = interpreter_create();
|
||||
if (interpreter_state == NULL) return 1;
|
||||
Boolean success = exec_file(interpreter_state, filename);
|
||||
interpreter_delete(interpreter_state);
|
||||
if (success) return 0;
|
||||
else return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,27 +9,32 @@
|
|||
|
||||
#include "sls/meta.h"
|
||||
#include "sls/bool.h"
|
||||
#include "sls/string.h"
|
||||
#include "sls/repl.h"
|
||||
#include "sls/file.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
Boolean version = FALSE;
|
||||
Boolean file = FALSE;
|
||||
char *filename = NULL;
|
||||
|
||||
(void)file;
|
||||
(void)filename;
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--version") == 0) version = TRUE;
|
||||
if (strcmp(argv[i], "-v") == 0) version = TRUE;
|
||||
if (argc == 2) {
|
||||
if (strcmp(argv[1], "--version") == 0) version = TRUE;
|
||||
else if (strcmp(argv[1], "-v") == 0) version = TRUE;
|
||||
else filename = argv[1];
|
||||
} else if (argc > 2) {
|
||||
printf("To many arguments!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (version) {
|
||||
print_version();
|
||||
return 0;
|
||||
} else if (file) {
|
||||
return 1;
|
||||
} else if (filename != NULL) {
|
||||
SlsStr sls_filename = sls_str_malloc(filename, strlen(filename));
|
||||
int rv = file(sls_filename);
|
||||
sls_str_free(&sls_filename);
|
||||
return rv;
|
||||
} else {
|
||||
return repl(0, NULL);
|
||||
return repl();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,10 +71,7 @@ void print_top_of_stack(InterpreterState *interpreter_state) {
|
|||
}
|
||||
}
|
||||
|
||||
int repl(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
int repl() {
|
||||
print_version();
|
||||
printf("===== YREA SLS REPL =====\n");
|
||||
printf("Type `#exit` to exit.\n");
|
||||
|
|
@ -82,6 +79,7 @@ int repl(int argc, char *argv[]) {
|
|||
|
||||
LexerInfo lexer_info;
|
||||
InterpreterState *interpreter_state = interpreter_create();
|
||||
if (interpreter_state == NULL) return 1;
|
||||
char buf[256];
|
||||
while (fgets(buf, sizeof(buf), stdin)) {
|
||||
size_t len = strlen(buf);
|
||||
|
|
|
|||
Loading…
Reference in New Issue