diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/cps/ArithmeticHandler.h | 5 | ||||
| -rw-r--r-- | include/cps/ArrayHandler.h | 7 | ||||
| -rw-r--r-- | include/cps/CodeGen.h | 4 | ||||
| -rw-r--r-- | include/cps/FunctionGen.h | 8 | ||||
| -rw-r--r-- | include/cps/Lexer.h | 2 | ||||
| -rw-r--r-- | include/cps/Parser.h | 10 |
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 |
