diff options
| author | _ov4 <ov4@ov4.org> | 2026-07-26 12:02:32 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-07-26 12:02:32 +0000 |
| commit | 4df7c22b99349dcf2175d69679eafa175d49aa9f (patch) | |
| tree | 1b44c329ed3e3f75787d9410d211ad0ed597b681 /lib/Parser/Parser.cc | |
| parent | 0b4cdf7b28b3855d2dee18d91e9319d66a8ec25f (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'lib/Parser/Parser.cc')
| -rw-r--r-- | lib/Parser/Parser.cc | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index 322cdb9..049f99d 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -271,12 +271,18 @@ std::vector<std::unique_ptr<StmtAST>> Parser::Parse() { } std::unique_ptr<StmtAST> Parser::ParseDeclare() { - getNextToken(); - if (CurTok != tok_identifier) return nullptr; + getNextToken(); + if (CurTok != tok_identifier) { + fprintf(stderr, "Error: Expected identifier after DECLARE at line %d\n", Lex.getLine()); + return nullptr; + } std::string Name = Lex.IdentifierStr; getNextToken(); - - if (CurTok != tok_colon) return nullptr; + + if (CurTok != tok_colon) { + fprintf(stderr, "Error: Expected ':' after variable name '%s' at line %d\n", Name.c_str(), Lex.getLine()); + return nullptr; + } getNextToken(); if (CurTok == tok_array) { @@ -485,6 +491,12 @@ std::unique_ptr<StmtAST> Parser::ParseForStmt() { } std::unique_ptr<StmtAST> Parser::ParseStatement() { + auto Stmt = ParseStatementImpl(); + if (!Stmt) ++NumErrors; + return Stmt; +} + +std::unique_ptr<StmtAST> Parser::ParseStatementImpl() { if (CurTok == tok_declare) { return ParseDeclare(); } @@ -515,7 +527,10 @@ std::unique_ptr<StmtAST> Parser::ParseStatement() { return std::make_unique<ArrayAssignStmtAST>(Name, std::move(Indices), std::move(Expr), Line); } - if (CurTok != tok_assign) return nullptr; + if (CurTok != tok_assign) { + fprintf(stderr, "Error: Expected '<-' after identifier '%s' at line %d\n", Name.c_str(), Line); + return nullptr; + } getNextToken(); auto Expr = ParseExpression(); if (!Expr) return nullptr; @@ -523,7 +538,10 @@ std::unique_ptr<StmtAST> Parser::ParseStatement() { } else if (CurTok == tok_input) { getNextToken(); - if (CurTok != tok_identifier) return nullptr; + if (CurTok != tok_identifier) { + fprintf(stderr, "Error: Expected variable name after INPUT at line %d\n", Lex.getLine()); + return nullptr; + } std::string Name = Lex.IdentifierStr; getNextToken(); return std::make_unique<InputStmtAST>(Name); @@ -559,5 +577,6 @@ std::unique_ptr<StmtAST> Parser::ParseStatement() { return ParseReturnStmt(); } + fprintf(stderr, "Error: Unexpected token at line %d when expecting a statement\n", Lex.getLine()); return nullptr; } |
