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/ParserFunction.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/ParserFunction.cc')
| -rw-r--r-- | lib/Parser/ParserFunction.cc | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/lib/Parser/ParserFunction.cc b/lib/Parser/ParserFunction.cc index 05b1e12..06348c4 100644 --- a/lib/Parser/ParserFunction.cc +++ b/lib/Parser/ParserFunction.cc @@ -5,7 +5,7 @@ using namespace cps; -std::vector<std::tuple<std::string, std::string, bool>> Parser::ParsePrototypeArgs() { +std::optional<std::vector<std::tuple<std::string, std::string, bool>>> Parser::ParsePrototypeArgs() { std::vector<std::tuple<std::string, std::string, bool>> Args; if (CurTok != '(') return Args; getNextToken(); @@ -24,21 +24,21 @@ std::vector<std::tuple<std::string, std::string, bool>> Parser::ParsePrototypeAr if (CurTok != tok_identifier) { fprintf(stderr, "Error: Expected argument name\n"); - return Args; + return std::nullopt; } std::string Name = Lex.IdentifierStr; getNextToken(); if (CurTok != tok_colon) { fprintf(stderr, "Error: Expected ':' after argument name\n"); - return Args; + return std::nullopt; } getNextToken(); std::string Type = ParseTypeName(false); if (Type.empty()) { fprintf(stderr, "Error: Expected argument type for '%s'\n", Name.c_str()); - return Args; + return std::nullopt; } Args.emplace_back(Name, Type, IsRef); @@ -46,7 +46,7 @@ std::vector<std::tuple<std::string, std::string, bool>> Parser::ParsePrototypeAr if (CurTok == ')') break; if (CurTok != ',') { fprintf(stderr, "Error: Expected ',' or ')'\n"); - return Args; + return std::nullopt; } getNextToken(); } @@ -65,6 +65,7 @@ std::unique_ptr<StmtAST> Parser::ParseFunction() { getNextToken(); auto Args = ParsePrototypeArgs(); + if (!Args) return nullptr; std::string RetType = "INTEGER"; if (CurTok == tok_returns) { @@ -76,7 +77,7 @@ std::unique_ptr<StmtAST> Parser::ParseFunction() { } } - auto Proto = std::make_unique<PrototypeAST>(Name, std::move(Args), RetType); + auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), RetType); std::vector<std::unique_ptr<StmtAST>> Body; while (CurTok != tok_endfunction && CurTok != tok_eof) { @@ -105,7 +106,8 @@ std::unique_ptr<StmtAST> Parser::ParseProcedure() { getNextToken(); auto Args = ParsePrototypeArgs(); - auto Proto = std::make_unique<PrototypeAST>(Name, std::move(Args), "VOID"); + if (!Args) return nullptr; + auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), "VOID"); std::vector<std::unique_ptr<StmtAST>> Body; while (CurTok != tok_endprocedure && CurTok != tok_eof) { |
