From c33b078c9e7f9f4a2438823e2dd2cde51e6709ae Mon Sep 17 00:00:00 2001 From: schmingledorp Date: Mon, 24 Nov 2025 15:55:54 -0500 Subject: [PATCH] Add line and column numbers to error diagnostics The original error() function only showed the raw byte offset and a caret line, this change adds 1-based line and column numbers while leaving everything else unchanged. This is done by reusing start_idx and backtracking through SOURCE to count newlines. --- src/globals.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/globals.c b/src/globals.c index 27697cb9..efc42b33 100644 --- a/src/globals.c +++ b/src/globals.c @@ -1396,11 +1396,16 @@ void error(char *msg) strcpy(diagnostic + i, "^ Error occurs here"); - /* TODO: Implement line/column tracking for precise error location - * reporting. Current implementation only shows source position offset. - */ - printf("[Error]: %s\nOccurs at source location %d.\n%s\n", msg, - SOURCE->size, diagnostic); + /* Backtrack SOURCE to find line of position */ + int line = 1; + for (i = 0; i < start_idx; i++) { + if (SOURCE->elements[i] == '\n') + line++; + } + int column = SOURCE->size - start_idx + 1; + + printf("[Error]: %s\nOccurs at source location %d:%d.\n%s\n", msg, line, + column, diagnostic); abort(); }