aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-07-26 12:02:32 +0000
committer_ov4 <ov4@ov4.org>2026-07-26 12:02:32 +0000
commit4df7c22b99349dcf2175d69679eafa175d49aa9f (patch)
tree1b44c329ed3e3f75787d9410d211ad0ed597b681 /include
parent0b4cdf7b28b3855d2dee18d91e9319d66a8ec25f (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 'include')
-rw-r--r--include/cps/ArithmeticHandler.h5
-rw-r--r--include/cps/ArrayHandler.h7
-rw-r--r--include/cps/CodeGen.h4
-rw-r--r--include/cps/FunctionGen.h8
-rw-r--r--include/cps/Lexer.h2
-rw-r--r--include/cps/Parser.h10
6 files changed, 28 insertions, 8 deletions
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<std::unique_ptr<ExprAST>> &IndexExprs,
+ int Line,
+ bool RequireExact,
+ CodeGen &CG,
+ std::vector<llvm::Value*> &Out);
+ llvm::Value *loadElement(const std::string &Name, llvm::Value *Offset, CodeGen &CG);
void emitPrintLoop(const std::string &Name,
int CurrentDim,
std::vector<llvm::Value*> 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<std::unique_ptr<StmtAST>> &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<std::string, llvm::Value*> &NamedValues;
std::map<std::string, SymbolInfo> &Symbols;
+ bool &HadError;
void createArgumentAllocas(llvm::Function *F, const std::vector<std::tuple<std::string, std::string, bool>> &Args);
@@ -28,8 +29,9 @@ public:
llvm::IRBuilder<> &B,
TypeSystem &TS,
std::map<std::string, llvm::Value*> &NV,
- std::map<std::string, SymbolInfo> &Sym)
- : Context(C), Module(M), Builder(B), Types(TS), NamedValues(NV), Symbols(Sym) {}
+ std::map<std::string, SymbolInfo> &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 <vector>
#include <memory>
#include <map>
+#include <optional>
#include <tuple>
#include <string>
@@ -12,7 +13,8 @@ namespace cps {
class Parser {
Lexer &Lex;
int CurTok;
-
+ unsigned NumErrors = 0;
+
std::map<int, int> BinopPrecedence;
int getNextToken();
@@ -31,6 +33,7 @@ class Parser {
std::unique_ptr<ExprAST> ParseStringBuiltin(const std::string &FuncName);
std::unique_ptr<StmtAST> ParseStatement();
+ std::unique_ptr<StmtAST> ParseStatementImpl();
std::unique_ptr<StmtAST> ParseIfStmt();
std::unique_ptr<StmtAST> ParseWhileStmt();
std::unique_ptr<StmtAST> ParseRepeatStmt();
@@ -42,11 +45,12 @@ class Parser {
std::unique_ptr<StmtAST> ParseProcedure();
std::unique_ptr<StmtAST> ParseCallStmt();
std::unique_ptr<StmtAST> ParseReturnStmt();
- std::vector<std::tuple<std::string, std::string, bool>> ParsePrototypeArgs();
-
+ std::optional<std::vector<std::tuple<std::string, std::string, bool>>> ParsePrototypeArgs();
+
public:
Parser(Lexer &L);
std::vector<std::unique_ptr<StmtAST>> Parse();
+ bool hadError() const { return NumErrors != 0; }
};
} // namespace cps