mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Refactored error handling
Now uses base error class which has an abstract function for overiding how it is displayed to the console.
This commit is contained in:
@@ -113,7 +113,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)common;$(ProjectDir)inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
@@ -131,7 +131,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)common;$(ProjectDir)inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
@@ -144,11 +144,9 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\Console.cpp" />
|
||||
<ClCompile Include="src\Generator.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="inc\Console.h" />
|
||||
<ClInclude Include="inc\Lexer.h" />
|
||||
<ClInclude Include="inc\LLVM.h" />
|
||||
<ClInclude Include="inc\Parser.h" />
|
||||
|
||||
@@ -14,14 +14,8 @@
|
||||
<ClCompile Include="src\Generator.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Console.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="inc\Console.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="inc\Lexer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <Error.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// Lexer foward declares fstream components so we can use them here //
|
||||
#include <Lexer.h>
|
||||
|
||||
#include <Error.h>
|
||||
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -7,11 +7,6 @@
|
||||
|
||||
namespace LX
|
||||
{
|
||||
template<typename T, typename... Args>
|
||||
// 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<typename... Args>
|
||||
// Helper function for logging //
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#include <Console.h>
|
||||
|
||||
#include <Windows.h>
|
||||
#include <iostream>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -4,36 +4,41 @@
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include <llvm/Support/Error.h>
|
||||
|
||||
#include <ThrowIf.h>
|
||||
#include <Console.h>
|
||||
#include <Parser.h>
|
||||
#include <Lexer.h>
|
||||
#include <Util.h>
|
||||
|
||||
#include <Error.h>
|
||||
|
||||
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<LX::InvalidInputFilePath>(std::filesystem::exists(inpPath) == false);
|
||||
LX::ThrowIf<LX::InvalidFilePath>(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<LX::InvalidInputFilePath>(inpFile.is_open() == false);
|
||||
LX::ThrowIf<LX::InvalidFilePath>(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<LX::InvalidOutputFilePath>(outFile.is_open() == false);
|
||||
LX::ThrowIf<LX::InvalidFilePath>(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<std::ofstream>(logPath);
|
||||
LX::ThrowIf<LX::InvalidLogFilePath>(log->is_open() == false);
|
||||
LX::ThrowIf<LX::InvalidFilePath>(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::vector<LX::Token>tokens = 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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -112,7 +112,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)inc;$(SolutionDir)IR-Generator\inc</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)common;$(ProjectDir)inc;$(SolutionDir)IR-Generator\inc</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
@@ -128,7 +128,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)inc;$(SolutionDir)IR-Generator\inc</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)common;$(ProjectDir)inc;$(SolutionDir)IR-Generator\inc</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
|
||||
@@ -1,17 +1,58 @@
|
||||
#include <Lexer.h>
|
||||
#include <Util.h>
|
||||
|
||||
#include <ThrowIf.h>
|
||||
#include <Console.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <string_view>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
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<InvalidCharInSource>(true, info.column, info.line, info.index, contents[info.index]);
|
||||
}
|
||||
|
||||
// Log dumps A LOT of info //
|
||||
|
||||
@@ -112,7 +112,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)inc;$(SolutionDir)IR-Generator\inc</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)common;$(ProjectDir)inc;$(SolutionDir)IR-Generator\inc</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
@@ -128,7 +128,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)inc;$(SolutionDir)IR-Generator\inc</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)common;$(ProjectDir)inc;$(SolutionDir)IR-Generator\inc</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
@@ -144,6 +144,7 @@
|
||||
<ClCompile Include="src\AST-Loggers.cpp" />
|
||||
<ClCompile Include="src\GenIR.cpp" />
|
||||
<ClCompile Include="src\Parser.cpp" />
|
||||
<ClCompile Include="src\ParserErrors.cpp" />
|
||||
<ClCompile Include="src\Scope.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -29,6 +29,9 @@
|
||||
<ClCompile Include="src\Scope.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\ParserErrors.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="inc\AST.h">
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <Parser.h>
|
||||
|
||||
#include <ThrowIf.h>
|
||||
|
||||
#include <LLVM.h>
|
||||
#include <Util.h>
|
||||
#include <AST.h>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <Parser.h>
|
||||
|
||||
#include <ThrowIf.h>
|
||||
|
||||
#include <Util.h>
|
||||
#include <AST.h>
|
||||
|
||||
@@ -28,7 +30,7 @@ namespace LX
|
||||
// Generates the IR within the function by looping over the nodes //
|
||||
for (auto& node : funcAST.body)
|
||||
{
|
||||
ThrowIf<int>(IsValidTopLevelNode(node->m_Type) == false); // <- TODO: replace with actual error type
|
||||
ThrowIf<IRGenerationError>(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<int>(llvm::verifyFunction(*func)); // <- TODO: Make error type
|
||||
ThrowIf<IRGenerationError>(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);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Parser.h>
|
||||
|
||||
#include <ThrowIf.h>
|
||||
#include <Util.h>
|
||||
#include <AST.h>
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
78
Parser/src/ParserErrors.cpp
Normal file
78
Parser/src/ParserErrors.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include <Parser.h>
|
||||
|
||||
#include <Console.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Parser.h>
|
||||
|
||||
#include <ThrowIf.h>
|
||||
#include <Util.h>
|
||||
#include <AST.h>
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
60
common/Console.h
Normal file
60
common/Console.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
24
common/Error.h
Normal file
24
common/Error.h
Normal file
@@ -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;
|
||||
31
common/ThrowIf.h
Normal file
31
common/ThrowIf.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
// Type traits is included for std::is_base_of_v //
|
||||
#include <type_traits>
|
||||
|
||||
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<typename Error, typename... Args>
|
||||
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<LX::RuntimeError, Error>,
|
||||
"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>(args)...);
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
example/Main.exe
Normal file
BIN
example/Main.exe
Normal file
Binary file not shown.
@@ -1,12 +1,6 @@
|
||||
func add(int a, int b)
|
||||
{
|
||||
return a + b
|
||||
}
|
||||
|
||||
func main()
|
||||
{
|
||||
int a
|
||||
a = 7834
|
||||
int a @
|
||||
|
||||
int b
|
||||
b = 6
|
||||
|
||||
BIN
example/main.obj
Normal file
BIN
example/main.obj
Normal file
Binary file not shown.
Reference in New Issue
Block a user