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/CodeGen/FunctionGen.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'lib/CodeGen/FunctionGen.cc') diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc index 89aa3d8..0a22278 100644 --- a/lib/CodeGen/FunctionGen.cc +++ b/lib/CodeGen/FunctionGen.cc @@ -9,6 +9,7 @@ Type *FunctionGen::getLLVMType(const std::string &TypeName) { Type *Resolved = Types.getLLVMType(TypeName); if (!Resolved) { fprintf(stderr, "Error: Unknown type %s\n", TypeName.c_str()); + HadError = true; return Type::getInt64Ty(Context); } return Resolved; @@ -78,6 +79,7 @@ Function *FunctionGen::emitFunctionDef(FunctionDefAST *FuncAST, if (!TheFunction) return nullptr; if (!TheFunction->empty()) { fprintf(stderr, "Error: Function %s cannot be redefined.\n", Proto->getName().c_str()); + HadError = true; return nullptr; } @@ -103,7 +105,6 @@ Function *FunctionGen::emitFunctionDef(FunctionDefAST *FuncAST, } } - verifyFunction(*TheFunction); NamedValues = OldNamedValues; Symbols = OldSymbols; return TheFunction; @@ -113,7 +114,8 @@ static Value *GenerateCall(llvm::Module &Module, llvm::IRBuilder<> &Builder, llvm::LLVMContext &Context, const std::string &CalleeName, - const std::vector &Args) { + const std::vector &Args, + bool &HadError) { Function *CalleeF = Module.getFunction(CalleeName); if (!CalleeF) { std::vector ArgTypes; @@ -124,6 +126,7 @@ static Value *GenerateCall(llvm::Module &Module, if (CalleeF->arg_size() != Args.size()) { fprintf(stderr, "Error: Incorrect # arguments passed to %s\n", CalleeName.c_str()); + HadError = true; return nullptr; } @@ -135,11 +138,11 @@ static Value *GenerateCall(llvm::Module &Module, } llvm::Value *FunctionGen::emitCallExpr(CallExprAST *Call, const std::vector &Args) { - return GenerateCall(Module, Builder, Context, Call->getCallee(), Args); + return GenerateCall(Module, Builder, Context, Call->getCallee(), Args, HadError); } void FunctionGen::emitCallStmt(CallStmtAST *Call, const std::vector &Args) { - GenerateCall(Module, Builder, Context, Call->getCallee(), Args); + GenerateCall(Module, Builder, Context, Call->getCallee(), Args, HadError); } void FunctionGen::emitReturn(ReturnStmtAST *Ret, llvm::Value *RetVal) { -- cgit v1.3.1-4-g156e