Skip to content

Commit c044948

Browse files
authored
Merge pull request #315 from johndoesstuff/fix/error-at-with-line-and-column
Add line and column numbers to error diagnostics
2 parents 3cbbdee + c33b078 commit c044948

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/globals.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,11 +1448,16 @@ void error(char *msg)
14481448

14491449
strcpy(diagnostic + i, "^ Error occurs here");
14501450

1451-
/* TODO: Implement line/column tracking for precise error location
1452-
* reporting. Current implementation only shows source position offset.
1453-
*/
1454-
printf("[Error]: %s\nOccurs at source location %d.\n%s\n", msg,
1455-
SOURCE->size, diagnostic);
1451+
/* Backtrack SOURCE to find line of position */
1452+
int line = 1;
1453+
for (i = 0; i < start_idx; i++) {
1454+
if (SOURCE->elements[i] == '\n')
1455+
line++;
1456+
}
1457+
int column = SOURCE->size - start_idx + 1;
1458+
1459+
printf("[Error]: %s\nOccurs at source location %d:%d.\n%s\n", msg, line,
1460+
column, diagnostic);
14561461
abort();
14571462
}
14581463

0 commit comments

Comments
 (0)