From 3444b5b58a78c12a79365bb35e54cfa029fccd99 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 12 Oct 2021 10:53:20 +0200 Subject: Add error dispatch procedure --- src/bootstrap/main.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'src/bootstrap/main.c') diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c index 113ee48..dadc887 100755 --- a/src/bootstrap/main.c +++ b/src/bootstrap/main.c @@ -5,11 +5,18 @@ #include "string_view.c" #include "read_line.c" +#include "errors.c" #include "lexer.c" void process_source(const StringView *source) { Tokens tokens = tokenize(source); + if (errors_n != 0) { + if (tokens.buf != NULL) { + free(tokens.buf); + } + return; + } // Print tokens. for (size_t i = 0; i < tokens.size; i++) { @@ -17,7 +24,9 @@ process_source(const StringView *source) { print_token(tok); } - free(tokens.buf); + if (tokens.buf != NULL) { + free(tokens.buf); + } } #define REPL_PROMPT "bdl> " @@ -32,6 +41,24 @@ run_repl(void) { return; } process_source(&sv); + + // Check if there were any errors. + if (errors_n != 0) { + for (size_t i = 0; i < errors_n; i++) { + Error err = errors[i]; + for (size_t j = 0; j < err.col + sizeof(REPL_PROMPT) - 2; j++) { + putchar(' '); + } + printf("|\n"); + for (size_t j = 0; j < err.col + sizeof(REPL_PROMPT) - 2; j++) { + putchar(' '); + } + printf("%s\n", error_msgs[err.value]); + } + errors_n = 0; + continue; + } + if (sv.n != 0) { printf("\n"); } @@ -62,6 +89,19 @@ run_file(char *file_name) { process_source(&sv); + // Check if there were any errors. + if (errors_n != 0) { + for (size_t i = 0; i < errors_n; i++) { + Error err = errors[i]; + printf("%s", file_name); + if (err.line != 0) { + printf(":%ld:%ld", err.line, err.col); + } + printf("%s\n", error_msgs[err.value]); + } + errors_n = 0; + } + free(source); fclose(file); } @@ -91,6 +131,19 @@ run_stdin(void) { process_source(&sv); + // Check if there were any errors. + if (errors_n != 0) { + for (size_t i = 0; i < errors_n; i++) { + Error err = errors[i]; + printf("stdin"); + if (err.line != 0) { + printf(":%ld:%ld: ", err.line, err.col); + } + printf("%s\n", error_msgs[err.value]); + } + errors_n = 0; + } + free(source); } -- cgit v1.2.1