summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/file.c16
-rw-r--r--src/utils/log.c63
-rw-r--r--src/utils/log.h3
3 files changed, 82 insertions, 0 deletions
diff --git a/src/utils/file.c b/src/utils/file.c
index c2dd614..f2244bf 100644
--- a/src/utils/file.c
+++ b/src/utils/file.c
@@ -5,6 +5,11 @@
#include <stddef.h>
#include <stdio.h>
+size_t fileCodes_capacity = 0;
+char **fileCodes = NULL;
+const char **fileCodes_names = 0;
+size_t fileCodes_length = 0;
+
char *readWholeFile(const char *filePath) {
FILE *file = fopen(filePath, "r");
@@ -23,5 +28,16 @@ char *readWholeFile(const char *filePath) {
fclose(file);
+ if (fileCodes_capacity == fileCodes_length) {
+ fileCodes_capacity += fileCodes_capacity / 2 + 1;
+ fileCodes =
+ a404m_realloc(fileCodes, fileCodes_capacity * sizeof(*fileCodes));
+ fileCodes_names = a404m_realloc(
+ fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names));
+ }
+ fileCodes[fileCodes_length] = str;
+ fileCodes_names[fileCodes_length] = filePath;
+ fileCodes_length += 1;
+
return str;
}
diff --git a/src/utils/log.c b/src/utils/log.c
index f54394a..e84f3b4 100644
--- a/src/utils/log.c
+++ b/src/utils/log.c
@@ -1,8 +1,10 @@
#include "log.h"
#include <stdarg.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
void _printLogBack(const char *format, const char *file, int line, ...) {
va_list args;
@@ -13,3 +15,64 @@ void _printLogBack(const char *format, const char *file, int line, ...) {
printf("\e[0;31mError: %s at compiler %s:%d\e[0m\n", errorStr, file, line);
free(errorStr);
}
+
+extern char **fileCodes;
+extern const char **fileCodes_names;
+extern size_t fileCodes_length;
+
+void _printErrorBack(const char *format, const char *file, int line,
+ char *begin, char *end, ...) {
+ va_list args;
+ va_start(args, end);
+ char *errorStr;
+ vasprintf(&errorStr, format, args);
+
+ size_t file_index = SIZE_MAX;
+ for (size_t i = 0; i < fileCodes_length; ++i) {
+ char *fileCode_begin = fileCodes[i];
+ char *fileCode_end = fileCodes[i] + strlen(fileCodes[i]);
+ if (begin >= fileCode_begin && end <= fileCode_end) {
+ file_index = i;
+ break;
+ }
+ }
+
+ const char firstColor[] = "\e[0;36m";
+ const char secondColor[] = "\e[0;31m";
+ fprintf(stderr, "%sError: %s at compiler %s:%d\e[0m\n", firstColor, errorStr,
+ file, line);
+ free(errorStr);
+
+ if (file_index == SIZE_MAX) {
+ return;
+ }
+
+ size_t file_line = 1;
+ char *file_line_begin = fileCodes[file_index];
+ char *file_line_end = fileCodes[file_index];
+ for (char *iter = fileCodes[file_index]; *iter != '\0'; ++iter) {
+ if (*iter == '\n') {
+ if (iter <= begin) {
+ file_line_begin = iter + 1;
+ } else if (iter >= end) {
+ file_line_end = iter;
+ }
+ if (iter <= end) {
+ ++file_line;
+ }
+ }
+ }
+
+ fprintf(stderr, "\e%sAt %s:%ld\n", firstColor, fileCodes_names[file_index],
+ file_line);
+
+ for (char *iter = file_line_begin; iter < file_line_end; ++iter) {
+ if (iter == begin) {
+ fprintf(stderr, "%s", secondColor);
+ } else if (iter == end) {
+ fprintf(stderr, "%s", firstColor);
+ }
+ fprintf(stderr, "%c", *iter);
+ }
+ fprintf(stderr, "\e[0m");
+}
diff --git a/src/utils/log.h b/src/utils/log.h
index 620b2a3..4057394 100644
--- a/src/utils/log.h
+++ b/src/utils/log.h
@@ -1,7 +1,10 @@
#pragma once
#define printLog(format,...) _printLogBack(format, __FILE_NAME__, __LINE__, ## __VA_ARGS__)
+#define printError(format,begin,end,...) _printErrorBack(format, __FILE_NAME__, __LINE__, begin, end, ## __VA_ARGS__)
#define UNREACHABLE printLog("Unreachable");exit(1)
extern void _printLogBack(const char *format, const char *file, int line, ...);
+
+extern void _printErrorBack(const char *format, const char *file, int line, char *begin, char *end, ...);