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 --- include/cps/ArithmeticHandler.h | 5 +++-- include/cps/ArrayHandler.h | 7 +++++++ include/cps/CodeGen.h | 4 ++++ include/cps/FunctionGen.h | 8 +++++--- include/cps/Lexer.h | 2 ++ include/cps/Parser.h | 10 +++++++--- 6 files changed, 28 insertions(+), 8 deletions(-) (limited to 'include/cps') diff --git a/include/cps/ArithmeticHandler.h b/include/cps/ArithmeticHandler.h index 8d6ea27..ca6266d 100644 --- a/include/cps/ArithmeticHandler.h +++ b/include/cps/ArithmeticHandler.h @@ -8,10 +8,11 @@ namespace cps { class ArithmeticHandler { llvm::LLVMContext &Context; llvm::IRBuilder<> &Builder; + bool &HadError; public: - ArithmeticHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B) - : Context(Ctx), Builder(B) {} + ArithmeticHandler(llvm::LLVMContext &Ctx, llvm::IRBuilder<> &B, bool &HadErrorFlag) + : Context(Ctx), Builder(B), HadError(HadErrorFlag) {} llvm::Value *emitBinaryOp(int Op, llvm::Value *LHS, llvm::Value *RHS, int Line); }; diff --git a/include/cps/ArrayHandler.h b/include/cps/ArrayHandler.h index f15004f..6f563b7 100644 --- a/include/cps/ArrayHandler.h +++ b/include/cps/ArrayHandler.h @@ -42,6 +42,13 @@ class ArrayHandler { llvm::Value *getArrayBasePointer(const std::string &Name); llvm::Value *getElementPointer(const std::string &Name, llvm::Value *Offset); const ArrayMetadata *getMetadata(const std::string &Name) const; + bool emitCheckedIndices(const std::string &Name, + const std::vector> &IndexExprs, + int Line, + bool RequireExact, + CodeGen &CG, + std::vector &Out); + llvm::Value *loadElement(const std::string &Name, llvm::Value *Offset, CodeGen &CG); void emitPrintLoop(const std::string &Name, int CurrentDim, std::vector CurrentIndices, diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h index 07ff3e8..268ba7e 100644 --- a/include/cps/CodeGen.h +++ b/include/cps/CodeGen.h @@ -59,6 +59,8 @@ class CodeGen { llvm::Value *FalseStr; // "FALSE" llvm::Value *EmptyStringStr; // "" + bool HadError = false; + void SetupExternalFunctions(); llvm::AllocaInst *CreateEntryBlockAlloca(llvm::Function *TheFunction, const std::string &VarName); llvm::AllocaInst *CreateEntryBlockAlloca(llvm::Function *TheFunction, llvm::Type *AllocType, const std::string &VarName); @@ -80,6 +82,8 @@ public: ~CodeGen(); void compile(const std::vector> &Statements); void print(); + bool hadError() const { return HadError; } + void reportError(const char *Fmt, ...); llvm::Value *emitExpr(ExprAST *Expr); void emitStmt(StmtAST *Stmt); diff --git a/include/cps/FunctionGen.h b/include/cps/FunctionGen.h index 7ebb7f4..1dc3901 100644 --- a/include/cps/FunctionGen.h +++ b/include/cps/FunctionGen.h @@ -16,9 +16,10 @@ class FunctionGen { llvm::Module &Module; llvm::IRBuilder<> &Builder; TypeSystem &Types; - + std::map &NamedValues; std::map &Symbols; + bool &HadError; void createArgumentAllocas(llvm::Function *F, const std::vector> &Args); @@ -28,8 +29,9 @@ public: llvm::IRBuilder<> &B, TypeSystem &TS, std::map &NV, - std::map &Sym) - : Context(C), Module(M), Builder(B), Types(TS), NamedValues(NV), Symbols(Sym) {} + std::map &Sym, + bool &HadErrorFlag) + : Context(C), Module(M), Builder(B), Types(TS), NamedValues(NV), Symbols(Sym), HadError(HadErrorFlag) {} llvm::Type *getLLVMType(const std::string &TypeName); diff --git a/include/cps/Lexer.h b/include/cps/Lexer.h index e8d2609..f1def3e 100644 --- a/include/cps/Lexer.h +++ b/include/cps/Lexer.h @@ -80,6 +80,7 @@ enum Token { class Lexer { int CurrentLine = 1; + bool HadError = false; public: std::string IdentifierStr; std::string StringVal; @@ -89,6 +90,7 @@ public: int gettok(); int getLine() const { return CurrentLine; } + bool hadError() const { return HadError; } }; } diff --git a/include/cps/Parser.h b/include/cps/Parser.h index 19f49be..87d1dee 100644 --- a/include/cps/Parser.h +++ b/include/cps/Parser.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -12,7 +13,8 @@ namespace cps { class Parser { Lexer &Lex; int CurTok; - + unsigned NumErrors = 0; + std::map BinopPrecedence; int getNextToken(); @@ -31,6 +33,7 @@ class Parser { std::unique_ptr ParseStringBuiltin(const std::string &FuncName); std::unique_ptr ParseStatement(); + std::unique_ptr ParseStatementImpl(); std::unique_ptr ParseIfStmt(); std::unique_ptr ParseWhileStmt(); std::unique_ptr ParseRepeatStmt(); @@ -42,11 +45,12 @@ class Parser { std::unique_ptr ParseProcedure(); std::unique_ptr ParseCallStmt(); std::unique_ptr ParseReturnStmt(); - std::vector> ParsePrototypeArgs(); - + std::optional>> ParsePrototypeArgs(); + public: Parser(Lexer &L); std::vector> Parse(); + bool hadError() const { return NumErrors != 0; } }; } // namespace cps -- cgit v1.3.1-4-g156e