From 4df7c22b99349dcf2175d69679eafa175d49aa9f Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sun, 26 Jul 2026 12:02:32 +0000 Subject: refactor: batch 1 - correctness and error contract - MID/RIGHT/LEFT integer args now coerced at call sites (RIGHT(s, n/2) no longer emits invalid IR); drop emitLeft's private conversion - FOR loop direction decided at run time via select on STEP sign; descending FOR works, STEP evaluated once per iteration - ArrayHandler: extract emitCheckedIndices/loadElement; index count is validated before emission, fixing a compiler-crash (vector OOB UB) on OUTPUT with too many indices - Error contract: Lexer/Parser/CodeGen track errors; all silent failure paths now print Error: diagnostics with line numbers; unterminated char literal no longer truncates the rest of the program; module is verified with verifyModule; driver exits non-zero on any error Co-Authored-By: Claude Fable 5 --- lib/Lexer/Lexer.cc | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'lib/Lexer') diff --git a/lib/Lexer/Lexer.cc b/lib/Lexer/Lexer.cc index 9a8e767..d678097 100644 --- a/lib/Lexer/Lexer.cc +++ b/lib/Lexer/Lexer.cc @@ -27,7 +27,8 @@ int Lexer::gettok() { if (LastChar == '"') { LastChar = getchar(); } else { - fprintf(stderr, "Error: Unterminated string literal\n"); + fprintf(stderr, "Error: Unterminated string literal at line %d\n", CurrentLine); + HadError = true; } return tok_string_literal; @@ -36,16 +37,26 @@ int Lexer::gettok() { if (LastChar == '\'') { int CharCode = getchar(); if (CharCode == EOF || CharCode == '\n') { - fprintf(stderr, "Error: Unterminated char literal\n"); - return tok_eof; + fprintf(stderr, "Error: Unterminated char literal at line %d\n", CurrentLine); + HadError = true; + CharVal = '?'; + StringVal.assign(1, CharVal); + // Leave the newline/EOF for the main loop so line counting stays right. + LastChar = CharCode; + return tok_char_literal; } int ClosingQuote = getchar(); if (ClosingQuote != '\'') { - fprintf(stderr, "Error: CHAR literal must contain exactly one character\n"); + fprintf(stderr, "Error: CHAR literal must contain exactly one character (line %d)\n", CurrentLine); + HadError = true; while (ClosingQuote != EOF && ClosingQuote != '\n' && ClosingQuote != '\'') { ClosingQuote = getchar(); } + CharVal = static_cast(CharCode); + StringVal.assign(1, CharVal); + LastChar = (ClosingQuote == '\'') ? getchar() : ClosingQuote; + return tok_char_literal; } CharVal = static_cast(CharCode); -- cgit v1.3.1-4-g156e