diff options
| author | hachem <im@hachem.wtf> | 2026-08-31 02:01:44 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-08-31 02:01:44 +0200 |
| commit | ce9f0d32c33333161cf9a5c2b7446d80fa0f13b5 (patch) | |
| tree | 3ceb2594de443f7928c5300d42c00428778eae11 /src/sema/sema.c | |
| parent | cf24f576891ece60bdf04928d3c39dd0bbb9b5f6 (diff) | |
feat: add diagnostics module with source-anchored caret errors
Diffstat (limited to 'src/sema/sema.c')
| -rw-r--r-- | src/sema/sema.c | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/sema/sema.c b/src/sema/sema.c index 55f4a93..f86159c 100644 --- a/src/sema/sema.c +++ b/src/sema/sema.c @@ -9,7 +9,7 @@ static bool names_equal(struct Token a, struct Token b) return a.length == b.length && memcmp(a.start, b.start, a.length) == 0; } -static bool check_duplicate_names(struct Program* program) +static bool check_duplicate_names(struct Source source, struct Program* program) { size_t count = program->const_count + program->data_count + program->proc_count; if (count == 0) @@ -38,8 +38,10 @@ static bool check_duplicate_names(struct Program* program) for (size_t j = 0; j < i; j += 1) if (names_equal(names[i], names[j])) { - fprintf(stderr, "error: line %u: '%.*s' is already defined\n", - names[i].line, (int)names[i].length, names[i].start); + char message[128]; + snprintf(message, sizeof(message), "'%.*s' is already defined", + (int)names[i].length, names[i].start); + report_error(source, names[i], message); ok = false; } @@ -47,7 +49,7 @@ static bool check_duplicate_names(struct Program* program) return ok; } -static bool check_entry_point(struct Program* program) +static bool check_entry_point(struct Source source, struct Program* program) { if (!program->config.has_entry) return true; @@ -57,18 +59,20 @@ static bool check_entry_point(struct Program* program) if (names_equal(program->procs[i].name, entry)) return true; - fprintf(stderr, "error: line %u: entry point '%.*s' is not defined\n", - entry.line, (int)entry.length, entry.start); + char message[128]; + snprintf(message, sizeof(message), "entry point '%.*s' is not defined", + (int)entry.length, entry.start); + report_error(source, entry, message); return false; } -bool analyze_program(struct Program* program) +bool analyze_program(struct Source source, struct Program* program) { bool ok = true; - if (!check_duplicate_names(program)) + if (!check_duplicate_names(source, program)) ok = false; - if (!check_entry_point(program)) + if (!check_entry_point(source, program)) ok = false; return ok; |
