From 616ed1ca21d610a1db6e63194a3172e43d69b6d9 Mon Sep 17 00:00:00 2001
From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com>
Date: Mon, 5 May 2025 15:44:59 +0100
Subject: [PATCH] Refactored error handling
Now uses base error class which has an abstract function for overiding how it is displayed to the console.
---
IR-Generator/IR-Generator.vcxproj | 6 +-
IR-Generator/IR-Generator.vcxproj.filters | 6 -
IR-Generator/inc/Console.h | 27 ---
IR-Generator/inc/Lexer.h | 12 +-
IR-Generator/inc/Parser.h | 18 +-
IR-Generator/inc/Util.h | 5 -
IR-Generator/src/Console.cpp | 22 ---
IR-Generator/src/Generator.cpp | 194 +++++-----------------
LX-Build/Main.cs | 7 +-
LX-Compiler.sln | 7 +
Lexer/Lexer.vcxproj | 4 +-
Lexer/src/Lexer.cpp | 43 ++++-
Parser/Parser.vcxproj | 5 +-
Parser/Parser.vcxproj.filters | 3 +
Parser/src/AST-LLVM.cpp | 2 +
Parser/src/GenIR.cpp | 8 +-
Parser/src/Parser.cpp | 6 +-
Parser/src/ParserErrors.cpp | 78 +++++++++
Parser/src/Scope.cpp | 1 +
README.md | 6 +-
common/Console.h | 60 +++++++
common/Error.h | 24 +++
common/ThrowIf.h | 31 ++++
example/Main.exe | Bin 0 -> 2560 bytes
example/main.lx | 8 +-
example/main.obj | Bin 0 -> 543 bytes
26 files changed, 340 insertions(+), 243 deletions(-)
delete mode 100644 IR-Generator/inc/Console.h
delete mode 100644 IR-Generator/src/Console.cpp
create mode 100644 Parser/src/ParserErrors.cpp
create mode 100644 common/Console.h
create mode 100644 common/Error.h
create mode 100644 common/ThrowIf.h
create mode 100644 example/Main.exe
create mode 100644 example/main.obj
diff --git a/IR-Generator/IR-Generator.vcxproj b/IR-Generator/IR-Generator.vcxproj
index 760a044..c480f35 100644
--- a/IR-Generator/IR-Generator.vcxproj
+++ b/IR-Generator/IR-Generator.vcxproj
@@ -113,7 +113,7 @@
true_DEBUG;_CONSOLE;%(PreprocessorDefinitions)true
- $(ProjectDir)inc;%(AdditionalIncludeDirectories)
+ $(SolutionDir)common;$(ProjectDir)inc;%(AdditionalIncludeDirectories)stdcpp20
@@ -131,7 +131,7 @@
trueNDEBUG;_CONSOLE;%(PreprocessorDefinitions)true
- $(ProjectDir)inc;%(AdditionalIncludeDirectories)
+ $(SolutionDir)common;$(ProjectDir)inc;%(AdditionalIncludeDirectories)stdcpp20
@@ -144,11 +144,9 @@
-
-
diff --git a/IR-Generator/IR-Generator.vcxproj.filters b/IR-Generator/IR-Generator.vcxproj.filters
index 9beeead..70c5e72 100644
--- a/IR-Generator/IR-Generator.vcxproj.filters
+++ b/IR-Generator/IR-Generator.vcxproj.filters
@@ -14,14 +14,8 @@
Source Files
-
- Source Files
-
-
- Header Files
- Header Files
diff --git a/IR-Generator/inc/Console.h b/IR-Generator/inc/Console.h
deleted file mode 100644
index 6feeb46..0000000
--- a/IR-Generator/inc/Console.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#pragma once
-
-#include
-
-namespace LX
-{
- enum class Color
- {
- BLACK = 0,
- BLUE = 1,
- GREEN = 2,
- AQUA = 3,
- RED = 4,
- PURPLE = 5,
- YELLOW = 6,
- LIGHT_GRAY = 7,
- LIGHT_BLUE = 9,
- LIGHT_GREEN = 10,
- LIGHT_AQUA = 11,
- LIGHT_RED = 12,
- LIGHT_PURPLE = 13,
- LIGHT_YELLOW = 14,
- WHITE = 15
- };
-
- void PrintStringAsColor(const std::string& str, Color c);
-}
diff --git a/IR-Generator/inc/Lexer.h b/IR-Generator/inc/Lexer.h
index 3784d2e..77f6f70 100644
--- a/IR-Generator/inc/Lexer.h
+++ b/IR-Generator/inc/Lexer.h
@@ -1,5 +1,8 @@
#pragma once
+#include
+
+#include
#include
#include
@@ -21,8 +24,15 @@ namespace std
namespace LX
{
// Error type with index and character to alert the user that LX does not understand that symbol //
- struct InvalidCharInSource
+ struct InvalidCharInSource : public RuntimeError
{
+ GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS;
+
+ InvalidCharInSource(std::streamsize _col, std::streamsize _line, std::streamsize _index, char _invalid);
+
+ static std::string* s_Source;
+ static std::filesystem::path* s_SourceFile;
+
std::streamsize col;
std::streamsize line;
std::streamsize index;
diff --git a/IR-Generator/inc/Parser.h b/IR-Generator/inc/Parser.h
index 6c44c82..c2fc376 100644
--- a/IR-Generator/inc/Parser.h
+++ b/IR-Generator/inc/Parser.h
@@ -3,6 +3,8 @@
// Lexer foward declares fstream components so we can use them here //
#include
+#include
+
#include
#include
@@ -76,11 +78,19 @@ namespace LX::AST
namespace LX
{
// Thrown if there was an error during IR Generation //
- struct IRGenerationError {};
+ CREATE_EMPTY_LX_ERROR_TYPE(IRGenerationError);
// Thrown if there was an unexpected (incorrect) token //
- struct UnexpectedToken
+ struct UnexpectedToken : public RuntimeError
{
+ GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS;
+
+ UnexpectedToken(Token::TokenType _expected, std::string _override, Token _got);
+
+ //
+ static std::string* s_Source;
+ static std::filesystem::path* s_SourceFile;
+
// The token type that should be there //
Token::TokenType expected;
@@ -96,10 +106,10 @@ namespace LX
{
public:
// Error thrown if the user tried to create a variable that already existed //
- struct __declspec(novtable) VariableAlreadyExists final {};
+ CREATE_EMPTY_LX_ERROR_TYPE(VariableAlreadyExists);
// Error thrown if user tries to access variable that does not exist //
- struct __declspec(novtable) VariableDoesntExist final {};
+ CREATE_EMPTY_LX_ERROR_TYPE(VariableDoesntExist);
// Default constructor //
Scope()
diff --git a/IR-Generator/inc/Util.h b/IR-Generator/inc/Util.h
index 6ae60f9..6882b0d 100644
--- a/IR-Generator/inc/Util.h
+++ b/IR-Generator/inc/Util.h
@@ -7,11 +7,6 @@
namespace LX
{
- template
- // Helper function to throw given error if condition is true //
- // Also micro-optimises to predict there is no errors thrown //
- inline void ThrowIf(const bool condition, Args... args)
- { if (condition) [[unlikely]] { throw T(args...); }}
template
// Helper function for logging //
diff --git a/IR-Generator/src/Console.cpp b/IR-Generator/src/Console.cpp
deleted file mode 100644
index 1513663..0000000
--- a/IR-Generator/src/Console.cpp
+++ /dev/null
@@ -1,22 +0,0 @@
-#include
-
-#include
-#include
-
-namespace LX
-{
- void PrintStringAsColor(const std::string& str, Color c)
- {
- // Gets a handle to the console //
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
-
- // Sets the color of the console to the desired color //
- SetConsoleTextAttribute(hConsole, (WORD)c);
-
- // Outputs the text //
- std::cout << str;
-
- // Resets the color //
- SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY);
- }
-}
diff --git a/IR-Generator/src/Generator.cpp b/IR-Generator/src/Generator.cpp
index abde1e0..154acb4 100644
--- a/IR-Generator/src/Generator.cpp
+++ b/IR-Generator/src/Generator.cpp
@@ -4,36 +4,41 @@
#include
#include
-#include
-
+#include
#include
#include
#include
#include
+#include
+
namespace LX
{
// Different errors thrown by main //
-
- struct IncorrectCommandLineArgs {};
- struct InvalidInputFilePath {};
- struct InvalidOutputFilePath {};
- struct InvalidLogFilePath {};
-
- // Util function for getting a line of the source at a given index (used for errors) //
- static std::string GetLineAtIndexOf(const std::string src, const std::streamsize index)
+ struct InvalidFilePath : public RuntimeError
{
- // Finds the start of the line //
- size_t start = src.rfind('\n', index);
- if (start == std::string::npos) { start = 0; } // None means first line
- else { start = start + 1; } // Skips new line char
+ GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS;
- // Finds the end of the line //
- size_t end = src.find('\n', index);
- if (end == std::string::npos) { end = src.size(); } // None means last line
+ InvalidFilePath(const std::string& _name, const std::filesystem::path& _path)
+ : name(_name), path(_path)
+ {}
- // Returns the string between start and end //
- return src.substr(start, end - start);
+ std::string name;
+ std::filesystem::path path;
+ };
+
+ void InvalidFilePath::PrintToConsole() const
+ {
+ // Tells the user the input file could not be found and how to fix the issue //
+ LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
+ std::cout << "Invalid " << name << ": ";
+ LX::PrintStringAsColor(path.string().c_str(), LX::Color::WHITE);
+ std::cout << "\n\nMake sure the file exists and the process has the correct path to the file\n";
+ }
+
+ const char* InvalidFilePath::ErrorType() const
+ {
+ return "Invalid File Path";
}
}
@@ -58,9 +63,9 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
outPath = a_outPath;
// Checks the input file exists and opens it //
- LX::ThrowIf(std::filesystem::exists(inpPath) == false);
+ LX::ThrowIf(std::filesystem::exists(inpPath) == false, "input file path", inpPath);
std::ifstream inpFile(inpPath, std::ios::binary | std::ios::ate); // Opens in binary at the end for microptimisation //
- LX::ThrowIf(inpFile.is_open() == false);
+ LX::ThrowIf(inpFile.is_open() == false, "input file path", inpPath);
// Copies the file into the string //
const std::streamsize len = inpFile.tellg(); // Gets length of file because it was opened at the end
@@ -70,7 +75,7 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
// Opens / Creates the output file //
std::ofstream outFile(outPath);
- LX::ThrowIf(outFile.is_open() == false);
+ LX::ThrowIf(outFile.is_open() == false, "output file path", outPath);
outFile.close(); // Opened just to check we can
// Opens the log file (if there is one specified //
@@ -78,147 +83,49 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
{
logPath = a_logPath;
log = std::make_unique(logPath);
- LX::ThrowIf(log->is_open() == false);
+ LX::ThrowIf(log->is_open() == false, "log file path", logPath);
}
// Prints the full paths to the console to let the user know compiling is being done //
std::cout << std::filesystem::absolute(inpPath) << " -> " << std::filesystem::absolute(outPath) << std::endl;
// Create tokens out of the input file //
+ LX::InvalidCharInSource::s_Source = &contents;
+ LX::InvalidCharInSource::s_SourceFile = &inpPath;
std::vectortokens = LX::LexicalAnalyze(contents, len, log.get());
LX::SafeFlush(log.get());
- std::cout << "\t|- Created tokens" << std::endl;
// Turns the tokens into an AST //
+ LX::UnexpectedToken::s_Source = &contents;
+ LX::UnexpectedToken::s_SourceFile = &inpPath;
+
LX::FileAST AST = LX::TurnTokensIntoAbstractSyntaxTree(tokens, log.get());
LX::SafeFlush(log.get());
- std::cout << "\t|- Created AST" << std::endl;
// Turns the AST into LLVM IR //
LX::GenerateIR(AST, inpPath.filename().string(), outPath);
LX::SafeFlush(log.get());
- std::cout << "\t|- Generated LLVM IR" << std::endl;
// Returns success
return 0;
}
- catch (LX::InvalidInputFilePath)
+ catch(LX::RuntimeError& e)
{
- // Tells the user the input file could not be found and how to fix the issue //
- LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
- std::cout << "Invalid file path: ";
- LX::PrintStringAsColor(inpPath.string(), LX::Color::WHITE);
- std::cout << "\n\nMake sure the file exists and the process has the correct path to the file\n";
-
- // Returns Exit id of 2 so other process can be alerted of the error //
- return 2;
- }
-
- catch (LX::InvalidOutputFilePath)
- {
- // Tells the user that the output file could not be found/created //
- LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
- std::cout << "Invalid file path: ";
- LX::PrintStringAsColor(outPath.string(), LX::Color::WHITE);
- std::cout << "\n\nThe file could not be created or written to.\n";
- std::cout << "Check it is a valid file path and it has the permissions to modify the file\n";
-
- // Returns Exit id of 3 so other process can be alerted of the error //
- return 3;
- }
-
- catch (LX::InvalidLogFilePath)
- {
- // Tells the user that the log file cound not be found/created //
- LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
- std::cout << "Invalid file path: ";
- LX::PrintStringAsColor(logPath.string(), LX::Color::WHITE);
- std::cout << "\n\nThe file could not be created or written to.\n";
- std::cout << "Check it is a valid file path and it has the permissions to modify the file\n";
-
- // Returns Exit id of 4 so other process can be alerted of the error //
- return 4;
- }
-
- catch (LX::InvalidCharInSource& e)
- {
- // Adds the error to the log and closes it to save all the output //
- LX::SafeLog(log.get(), LX::LOG_BREAK, "Error thrown from Lexer:\n\tInvalid character: ", e.invalid, " on line: ", e.line, LX::LOG_BREAK);
+ // Closes the log to save everything outputted to it after logging the error //
+ LX::SafeLog(log.get(), LX::LOG_BREAK, "Error thrown of type: ", e.ErrorType(), LX::LOG_BREAK);
if (log != nullptr) { log->close(); }
- // Calculates the length of the line number in the console so it is formatted correctly //
- std::ostringstream oss;
- oss << std::setw(3) << e.line;
- size_t lineNumberWidthInConsole = std::max(oss.str().size(), (size_t)3);
-
- // Gets the line of the error //
- std::string line = LX::GetLineAtIndexOf(contents, e.index);
-
- // Prints the error with the relevant information to the console //
- std::cout << "\n";
- LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
- std::cout << "Invalid character found in ";
- LX::PrintStringAsColor(inpPath.filename().string(), LX::Color::WHITE);
- std::cout << " {";
- LX::PrintStringAsColor(std::string(1, e.invalid), LX::Color::LIGHT_RED);
- std::cout << "}:\n";
- std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << e.line << " | " << line << "\n";
- std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(e.col - 1) << "";
- LX::PrintStringAsColor("^", LX::Color::LIGHT_RED);
+ // Logs the errors type to the console if built as Debug //
+ #ifdef _DEBUG
+ std::cout << "LX::RuntimeError thrown of type: ";
+ LX::PrintStringAsColor(e.ErrorType(), LX::Color::WHITE);
std::cout << "\n";
+ #endif // _DEBUG
- // Returns Exit id of 5 so other process can be alerted of the error //
- return 5;
- }
-
- catch (LX::UnexpectedToken& e)
- {
- // Calculates the length of the line number in the console so it is formatted correctly //
- std::ostringstream oss;
- oss << std::setw(3) << e.got.line;
- size_t lineNumberWidthInConsole = std::max(oss.str().size(), (size_t)3);
-
- // Gets the line of the error //
- std::string line = LX::GetLineAtIndexOf(contents, e.got.index);
-
- // Prints the error to the console with the relevant info //
- std::cout << "\n";
- LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
- std::cout << "Incorrect syntax in ";
- LX::PrintStringAsColor(inpPath.filename().string(), LX::Color::WHITE);
- std::cout << ", found ";
- LX::PrintStringAsColor(LX::ToString(e.got.type), LX::Color::WHITE);
- std::cout << " expected: ";
-
- // Allows the error to have a custom type that is printed to the console //
- if (e.expected == LX::Token::UNDEFINED) { LX::PrintStringAsColor(e.override, LX::Color::WHITE); }
- else { LX::PrintStringAsColor(LX::ToString(e.expected), LX::Color::WHITE); }
- std::cout << "\n";
-
- // Prints the code with the error to the console //
- std::string errorSquiggle(e.got.length, '~');
- std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << e.got.line << " | " << line << "\n";
- std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(e.got.column) << "";
- LX::PrintStringAsColor(errorSquiggle, LX::Color::LIGHT_RED);
- std::cout << "\n";
-
- // Returns Exit id of 6 so other process can be alerted of the error //
- return 6;
- }
-
- catch (LX::Scope::VariableAlreadyExists)
- {
- std::cout << "Tried to create a variable that already exists\n";
-
- return 7;
- }
-
- catch (LX::Scope::VariableDoesntExist)
- {
- std::cout << "Tried to access a variable that doesn't exist\n";
-
- return 8;
+ // Prints the error to the console and returns //
+ e.PrintToConsole();
+ return -1;
}
// Catches any std errors, there should be none //
@@ -236,19 +143,6 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
return -1;
}
- // Catches any LLVM errors, there should be none //
- catch (llvm::Error& e)
- {
- // Closes the log if it is open to get as much info as possible //
- if (log != nullptr) { log->close(); }
-
- // Prints the LLVM error to the console //
- std::cout << "A LLVM error occured. Please report this on the github page.\n" << std::endl;
-
- // Exit code -1 means an undefined error //
- return -1;
- }
-
// Catches errors that i was too lazy to code //
catch (int)
{
diff --git a/LX-Build/Main.cs b/LX-Build/Main.cs
index 28d6791..113b0c8 100644
--- a/LX-Build/Main.cs
+++ b/LX-Build/Main.cs
@@ -45,11 +45,10 @@ namespace LX_Build
LX_API.Init();
// Generates LLVM IR with the example files //
- int genExitCode = LX_API.GenIR("example/main.lx", "example/main.ll", "example/log");
- if (genExitCode != 0)
+ if (LX_API.GenIR("example/main.lx", "example/main.ll", "example/log") != 0)
{
- Console.WriteLine("An error occured whilst generating LLVM IR");
- Console.WriteLine($"Error code: {genExitCode}");
+ // Quits if the IR Generation fails //
+ // The C++ script handles all of the error message outputting //
return;
}
diff --git a/LX-Compiler.sln b/LX-Compiler.sln
index 9c593f4..027dd86 100644
--- a/LX-Compiler.sln
+++ b/LX-Compiler.sln
@@ -20,6 +20,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LX-Build", "LX-Build\LX-Bui
{C88042E2-0E09-4383-93F8-C79F9EE1E897} = {C88042E2-0E09-4383-93F8-C79F9EE1E897}
EndProjectSection
EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common-Include", "Common-Include", "{4AD62954-631A-4D0F-877E-E1C66E8CEC00}"
+ ProjectSection(SolutionItems) = preProject
+ common\Console.h = common\Console.h
+ common\Error.h = common\Error.h
+ common\ThrowIf.h = common\ThrowIf.h
+ EndProjectSection
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
diff --git a/Lexer/Lexer.vcxproj b/Lexer/Lexer.vcxproj
index 730d426..120ebdb 100644
--- a/Lexer/Lexer.vcxproj
+++ b/Lexer/Lexer.vcxproj
@@ -112,7 +112,7 @@
true_DEBUG;_CONSOLE;%(PreprocessorDefinitions)true
- $(ProjectDir)inc;$(SolutionDir)IR-Generator\inc
+ $(SolutionDir)common;$(ProjectDir)inc;$(SolutionDir)IR-Generator\incstdcpp20
@@ -128,7 +128,7 @@
trueNDEBUG;_CONSOLE;%(PreprocessorDefinitions)true
- $(ProjectDir)inc;$(SolutionDir)IR-Generator\inc
+ $(SolutionDir)common;$(ProjectDir)inc;$(SolutionDir)IR-Generator\incstdcpp20
diff --git a/Lexer/src/Lexer.cpp b/Lexer/src/Lexer.cpp
index ce25689..700bb89 100644
--- a/Lexer/src/Lexer.cpp
+++ b/Lexer/src/Lexer.cpp
@@ -1,17 +1,58 @@
#include
#include
+#include
+#include
+
+#include
#include
#include
#include
#include
#include
+#include
#include
#include
namespace LX
{
+ std::string* InvalidCharInSource::s_Source = nullptr;
+ std::filesystem::path* InvalidCharInSource::s_SourceFile = nullptr;
+
+ InvalidCharInSource::InvalidCharInSource(std::streamsize _col, std::streamsize _line, std::streamsize _index, char _invalid)
+ : col(_col), line(_line), index(_index), invalid(_invalid)
+ {
+ // Calculates the length of the line number in the console so it is formatted correctly //
+ std::ostringstream oss;
+ oss << std::setw(3) << line;
+ size_t lineNumberWidthInConsole = std::max(oss.str().size(), (size_t)3);
+
+ // Gets the line of the error //
+ std::string errorLine = LX::GetLineAtIndexOf(*s_Source, index);
+
+ // Prints the error with the relevant information to the console //
+ std::cout << "\n";
+ LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
+ std::cout << "Invalid character found in ";
+ LX::PrintStringAsColor(s_SourceFile->filename().string(), LX::Color::WHITE);
+ std::cout << " {";
+ LX::PrintStringAsColor(std::string(1, invalid), LX::Color::LIGHT_RED);
+ std::cout << "}:\n";
+ std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << line << " | " << errorLine << "\n";
+ std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(col - 1) << "";
+ LX::PrintStringAsColor("^", LX::Color::LIGHT_RED);
+ std::cout << "\n";
+ }
+
+ void InvalidCharInSource::PrintToConsole() const
+ {}
+
+ const char* InvalidCharInSource::ErrorType() const
+ {
+ return "Invalid char in source";
+ }
+
// Helper macro for outputting token type //
#define TOKEN_CASE(type) case type: return #type;
@@ -311,7 +352,7 @@ namespace LX
// Throws an error with all the relevant information //
else
{
- throw InvalidCharInSource(info.column, info.line, info.index, contents[info.index]);
+ ThrowIf(true, info.column, info.line, info.index, contents[info.index]);
}
// Log dumps A LOT of info //
diff --git a/Parser/Parser.vcxproj b/Parser/Parser.vcxproj
index 95d4545..9256d96 100644
--- a/Parser/Parser.vcxproj
+++ b/Parser/Parser.vcxproj
@@ -112,7 +112,7 @@
true_DEBUG;_CONSOLE;%(PreprocessorDefinitions)true
- $(ProjectDir)inc;$(SolutionDir)IR-Generator\inc
+ $(SolutionDir)common;$(ProjectDir)inc;$(SolutionDir)IR-Generator\incstdcpp20
@@ -128,7 +128,7 @@
trueNDEBUG;_CONSOLE;%(PreprocessorDefinitions)true
- $(ProjectDir)inc;$(SolutionDir)IR-Generator\inc
+ $(SolutionDir)common;$(ProjectDir)inc;$(SolutionDir)IR-Generator\incstdcpp20
@@ -144,6 +144,7 @@
+
diff --git a/Parser/Parser.vcxproj.filters b/Parser/Parser.vcxproj.filters
index 4a73ab0..e6aae93 100644
--- a/Parser/Parser.vcxproj.filters
+++ b/Parser/Parser.vcxproj.filters
@@ -29,6 +29,9 @@
Source Files
+
+ Source Files
+
diff --git a/Parser/src/AST-LLVM.cpp b/Parser/src/AST-LLVM.cpp
index 1797614..cd3d4b4 100644
--- a/Parser/src/AST-LLVM.cpp
+++ b/Parser/src/AST-LLVM.cpp
@@ -1,5 +1,7 @@
#include
+#include
+
#include
#include
#include
diff --git a/Parser/src/GenIR.cpp b/Parser/src/GenIR.cpp
index 93ca827..75f86cf 100644
--- a/Parser/src/GenIR.cpp
+++ b/Parser/src/GenIR.cpp
@@ -1,5 +1,7 @@
#include
+#include
+
#include
#include
@@ -28,7 +30,7 @@ namespace LX
// Generates the IR within the function by looping over the nodes //
for (auto& node : funcAST.body)
{
- ThrowIf(IsValidTopLevelNode(node->m_Type) == false); // <- TODO: replace with actual error type
+ ThrowIf(IsValidTopLevelNode(node->m_Type) == false); // <- TODO: replace with actual error type
node->GenIR(LLVM);
}
@@ -39,7 +41,7 @@ namespace LX
}
// Verifies the function works //
- ThrowIf(llvm::verifyFunction(*func)); // <- TODO: Make error type
+ ThrowIf(llvm::verifyFunction(*func)); // <- TODO: Make error type
}
// Turns an abstract binary tree into LLVM intermediate representation //
@@ -55,8 +57,6 @@ namespace LX
// Loops over the functions to generate their LLVM IR //
for (auto& func : ast.functions)
{
- std::cout << "\t|\t|- Generating function: " << func.name << "\n";
-
LLVM.scope = &func.scope; // Sets the current scope for the builder
GenerateFunctionIR(func, LLVM);
}
diff --git a/Parser/src/Parser.cpp b/Parser/src/Parser.cpp
index 686eda3..af5f074 100644
--- a/Parser/src/Parser.cpp
+++ b/Parser/src/Parser.cpp
@@ -1,5 +1,6 @@
#include
+#include
#include
#include
@@ -7,6 +8,9 @@
namespace LX
{
+ std::string* UnexpectedToken::s_Source = nullptr;
+ std::filesystem::path* UnexpectedToken::s_SourceFile = nullptr;
+
// Util function for working out if a token is a two sided operator //
static bool IsTwoSidedOperator(Token::TokenType t)
{
@@ -204,7 +208,7 @@ namespace LX
FileAST TurnTokensIntoAbstractSyntaxTree(std::vector& tokens, std::ofstream* log)
{
// Logs the start of the parsing
- SafeLog(log, LOG_BREAK, "Started parsing tokens", LOG_BREAK);
+ SafeLog(log, LOG_BREAK, "Started parsing tokens", LOG_BREAK);
// Creates the output storer and the parser //
FileAST output;
diff --git a/Parser/src/ParserErrors.cpp b/Parser/src/ParserErrors.cpp
new file mode 100644
index 0000000..328f355
--- /dev/null
+++ b/Parser/src/ParserErrors.cpp
@@ -0,0 +1,78 @@
+#include
+
+#include
+
+#include
+#include
+#include
+
+namespace LX
+{
+ void IRGenerationError::PrintToConsole() const
+ {
+ }
+
+ const char* IRGenerationError::ErrorType() const
+ {
+ return "IR Generation Error";
+ }
+
+ UnexpectedToken::UnexpectedToken(Token::TokenType _expected, std::string _override, Token _got)
+ : expected(_expected), override(_override), got(_got)
+ {}
+
+ void UnexpectedToken::PrintToConsole() const
+ {
+ // Calculates the length of the line number in the console so it is formatted correctly //
+ std::ostringstream oss;
+ oss << std::setw(3) << got.line;
+ size_t lineNumberWidthInConsole = std::max(oss.str().size(), (size_t)3);
+
+ // Gets the line of the error //
+ std::string line = LX::GetLineAtIndexOf(*s_Source, got.index);
+
+ // Prints the error to the console with the relevant info //
+ std::cout << "\n";
+ LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
+ std::cout << "Incorrect syntax in ";
+ LX::PrintStringAsColor(s_SourceFile->filename().string(), LX::Color::WHITE);
+ std::cout << ", found ";
+ LX::PrintStringAsColor(LX::ToString(got.type).c_str(), LX::Color::WHITE);
+ std::cout << " expected: ";
+
+ // Allows the error to have a custom type that is printed to the console //
+ if (expected == LX::Token::UNDEFINED) { LX::PrintStringAsColor(override.c_str(), LX::Color::WHITE); }
+ else { LX::PrintStringAsColor(LX::ToString(expected).c_str(), LX::Color::WHITE); }
+ std::cout << "\n";
+
+ // Prints the code with the error to the console //
+ std::string errorSquiggle(got.length, '~');
+ std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << got.line << " | " << line << "\n";
+ std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(got.column) << "";
+ LX::PrintStringAsColor(errorSquiggle.c_str(), LX::Color::LIGHT_RED);
+ std::cout << "\n";
+ }
+
+ const char* UnexpectedToken::ErrorType() const
+ {
+ return "Unexpected Token";
+ }
+
+ void Scope::VariableAlreadyExists::PrintToConsole() const
+ {
+ }
+
+ const char* Scope::VariableAlreadyExists::ErrorType() const
+ {
+ return "Variable Already Exists";
+ }
+
+ void Scope::VariableDoesntExist::PrintToConsole() const
+ {
+ }
+
+ const char* Scope::VariableDoesntExist::ErrorType() const
+ {
+ return "Variable Doesn't exist";
+ }
+}
diff --git a/Parser/src/Scope.cpp b/Parser/src/Scope.cpp
index 4c9789a..110bfad 100644
--- a/Parser/src/Scope.cpp
+++ b/Parser/src/Scope.cpp
@@ -1,5 +1,6 @@
#include
+#include
#include
#include
diff --git a/README.md b/README.md
index b6de6f6..b577126 100644
--- a/README.md
+++ b/README.md
@@ -11,14 +11,14 @@ This is my custom compiled language written in C++ based off of the LLVM toolcha
### Codebase
- Errors
- - Inherit from a common class
- All simple errors (no members) use the same type
- - ThrowIf Requires an error class
- - No temporary throw int;
- Logging
- Less templates
- Standard for formatting
- Choose what is logged
+- Refactor
+ - Use dynamic linking for debug builds (faster build times)
+ - General clean up
### Stuff I want to do later (unordered)
- I/O manager (Console, Files)
diff --git a/common/Console.h b/common/Console.h
new file mode 100644
index 0000000..84b88d8
--- /dev/null
+++ b/common/Console.h
@@ -0,0 +1,60 @@
+#pragma once
+
+#define NOMINMAX
+#include
+
+#include
+#include
+
+namespace LX
+{
+ enum class Color
+ {
+ BLACK = 0,
+ BLUE = 1,
+ GREEN = 2,
+ AQUA = 3,
+ RED = 4,
+ PURPLE = 5,
+ YELLOW = 6,
+ LIGHT_GRAY = 7,
+ LIGHT_BLUE = 9,
+ LIGHT_GREEN = 10,
+ LIGHT_AQUA = 11,
+ LIGHT_RED = 12,
+ LIGHT_PURPLE = 13,
+ LIGHT_YELLOW = 14,
+ WHITE = 15
+ };
+
+ inline void PrintStringAsColor(const std::string& str, Color c)
+ {
+ // Gets a handle to the console //
+ static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
+
+ // Sets the color of the console to the desired color //
+ SetConsoleTextAttribute(hConsole, (WORD)c);
+
+ // Outputs the text //
+ std::cout << str;
+
+ // Resets the color //
+ SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY);
+ }
+
+ // Util function for getting a line of the source at a given index (used for errors) //
+ inline std::string GetLineAtIndexOf(const std::string src, const std::streamsize index)
+ {
+ // Finds the start of the line //
+ size_t start = src.rfind('\n', index);
+ if (start == std::string::npos) { start = 0; } // None means first line
+ else { start = start + 1; } // Skips new line char
+
+ // Finds the end of the line //
+ size_t end = src.find('\n', index);
+ if (end == std::string::npos) { end = src.size(); } // None means last line
+
+ // Returns the string between start and end //
+ return src.substr(start, end - start);
+ }
+}
diff --git a/common/Error.h b/common/Error.h
new file mode 100644
index 0000000..a0c67a4
--- /dev/null
+++ b/common/Error.h
@@ -0,0 +1,24 @@
+#pragma once
+
+namespace LX
+{
+ // Base error class for all LX thrown errors //
+ // Holds nothing apart from the v-table //
+ struct RuntimeError
+ {
+ // Prints the error to the console //
+ // Include Common/Console.h for printing util functions //
+ virtual void PrintToConsole() const = 0;
+
+ // Returns a C-String of the type that was thrown //
+ virtual const char* ErrorType() const = 0;
+ };
+}
+
+// Helper macro to autogenerate a basic error type in a .h file //
+// Still requires function definitions in a .cpp file //
+#define CREATE_EMPTY_LX_ERROR_TYPE(name)\
+struct name : public LX::RuntimeError{ GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS };
+
+// Helper macro to autogenerate function declarations of functions required by LX::RuntimeError //
+#define GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS void PrintToConsole() const; const char* ErrorType() const;
diff --git a/common/ThrowIf.h b/common/ThrowIf.h
new file mode 100644
index 0000000..66c383b
--- /dev/null
+++ b/common/ThrowIf.h
@@ -0,0 +1,31 @@
+#pragma once
+
+// Type traits is included for std::is_base_of_v //
+#include
+
+namespace LX
+{
+ // Foward declares LX::RuntimeError so it can be used to see if a class derives from //
+ struct RuntimeError;
+
+ // Util function to throw an error if the condition is met //
+ // Given error type must derive from LX::RuntimeError //
+ template
+ inline void ThrowIf(const bool condition, Args&&... args)
+ {
+ // Checks that the error type will be caught by the error checker //
+ static_assert
+ (
+ std::is_base_of_v,
+ "Must throw a type that derives from LX::RuntimeError"
+ );
+
+ // Checks if the condition is met and micro-optimises that errors will not be thrown //
+ if (condition) [[unlikely]]
+ {
+ // Throws a COPY of the error and not itself //
+ // How C++ works, no way around it //
+ throw Error(std::forward(args)...);
+ }
+ }
+}
diff --git a/example/Main.exe b/example/Main.exe
new file mode 100644
index 0000000000000000000000000000000000000000..6cb4e4ff9739f3a4a5dc1467ba902eed80ea1e9c
GIT binary patch
literal 2560
zcmeZ`s$gJbU|?VYVr1Ze%)!B~0E+X;@8Vo{SF)%Oz
zMPLpR0J1@tfx!XfPB0rN&jw-x0gPsF0CJ#um>CipVVae0s{sH
z9#j#U@bpSjD@uSS3xZ4sIT6|KATc0NU|>klD@sW$Nn~K)fC_-s!95QW1O^Zg=@mfK
zf!z-D16Unc6G#OFIDnPHc%$@44uOE+Prw8ZfWBmqWU)?(oq;kS&YLKe^@!f7WIdE97I6@e-?6|yhM+-+XDQP
z?!b%l1*^jPQ_t7yJg+9ampID|FeNU1{r=r)S0q05+Ri?y3~nq8E`ef(GLiWza@}dc%4B?LWZKy)fV?~hh^*ZjA6gVc2kbDvHjFxZbUd75
y43GPlj(avw=}*ES(w-+Er78X{gV_ohjHxA>^klXO4M1<