mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Refactored how logging works
Made it central reusable logic. No longer needs to be passed around, opened or closed.
This commit is contained in:
@@ -122,7 +122,7 @@
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)bin\$(Platform)\$(Configuration)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>Parser.lib;Lexer.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>Parser.lib;Lexer.lib;Common.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
@@ -144,7 +144,7 @@
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)bin\$(Platform)\$(Configuration)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>Parser.lib;Lexer.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>Parser.lib;Lexer.lib;Common.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
@@ -158,6 +158,11 @@
|
||||
<ClInclude Include="inc\Lexer.h" />
|
||||
<ClInclude Include="inc\Parser.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Common\Common.vcxproj">
|
||||
<Project>{0abb03b7-e61a-4040-a512-fc05aa35b2a6}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
||||
@@ -128,5 +128,5 @@ namespace LX
|
||||
std::string ToString(Token::TokenType t);
|
||||
|
||||
// Lexer function to take in a file and output a vector of tokens //
|
||||
const std::vector<Token> LexicalAnalyze(const std::string& contents, const std::streamsize len, std::ofstream* log);
|
||||
const std::vector<Token> LexicalAnalyze(const std::string& contents, const std::streamsize len);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace LX::AST
|
||||
virtual llvm::Value* GenIR(InfoLLVM& LLVM) = 0;
|
||||
|
||||
// Function to log the node to a file //
|
||||
virtual void Log(std::ofstream* log, unsigned depth) = 0;
|
||||
virtual void Log(unsigned depth) = 0;
|
||||
|
||||
// Function for generating C/C++ code (Currently not implemented) //
|
||||
//virtual void GenC() = 0;
|
||||
@@ -143,7 +143,7 @@ namespace LX
|
||||
};
|
||||
|
||||
// Turns the tokens of a file into it's abstract syntax tree equivalent //
|
||||
FileAST TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, std::ofstream* log);
|
||||
FileAST TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens);
|
||||
|
||||
// Turns an abstract binary tree into LLVM intermediate representation //
|
||||
void GenerateIR(FileAST& ast, const std::string& name, const std::filesystem::path& IRPath);
|
||||
|
||||
@@ -33,20 +33,16 @@ namespace LX
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_outPath, const char* a_logPath)
|
||||
extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_outPath)
|
||||
{
|
||||
// Creates the file paths outside of the try-catch so they can be used in errors //
|
||||
std::filesystem::path inpPath;
|
||||
std::filesystem::path outPath;
|
||||
std::filesystem::path logPath;
|
||||
|
||||
// Creates the contents string outside of the try-catch so they can be used in errors //
|
||||
std::string contents;
|
||||
LX::Token::source = &contents;
|
||||
|
||||
// Creates the log-file out of the try-catch so it can be closed propely if an error is thrown //
|
||||
std::unique_ptr<std::ofstream> log = nullptr;
|
||||
|
||||
try
|
||||
{
|
||||
// Turns the file paths into the C++ type for handling them //
|
||||
@@ -69,33 +65,22 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
|
||||
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 //
|
||||
if (a_logPath != nullptr)
|
||||
{
|
||||
logPath = a_logPath;
|
||||
log = std::make_unique<std::ofstream>(logPath);
|
||||
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::vector<LX::Token>tokens = LX::LexicalAnalyze(contents, len);
|
||||
|
||||
// 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());
|
||||
LX::FileAST AST = LX::TurnTokensIntoAbstractSyntaxTree(tokens);
|
||||
|
||||
// Turns the AST into LLVM IR //
|
||||
LX::GenerateIR(AST, inpPath.filename().string(), outPath);
|
||||
LX::SafeFlush(log.get());
|
||||
|
||||
// Returns success
|
||||
return 0;
|
||||
@@ -103,9 +88,8 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
|
||||
|
||||
catch(LX::RuntimeError& e)
|
||||
{
|
||||
// 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(); }
|
||||
// Logs the error. Does not need to close it as it is done after this function returns //
|
||||
LX::Log::LogNewSection("Error thrown of type: ", e.ErrorType());
|
||||
|
||||
// Logs the errors type to the console if built as Debug //
|
||||
#ifdef _DEBUG
|
||||
@@ -122,8 +106,8 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
|
||||
// Catches any std errors, there should be none //
|
||||
catch (std::exception& e)
|
||||
{
|
||||
// Closes the log if it is open to get as much info as possible //
|
||||
if (log != nullptr) { log->close(); }
|
||||
// Logs the error. Does not need to close it as it is done after this function returns //
|
||||
LX::Log::LogNewSection("std::exception thrown: ", e.what());
|
||||
|
||||
// Prints the std exception to the console //
|
||||
// Any errors here are problems with the code //
|
||||
@@ -134,24 +118,9 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Catches errors that i was too lazy to code //
|
||||
catch (int)
|
||||
{
|
||||
// Closes the log if it is open to get as much info as possible //
|
||||
if (log != nullptr) { log->close(); }
|
||||
|
||||
std::cout << "An placeholder error occured. Maybe use a language that wasn't coded by a lazy person.\n" << std::endl;
|
||||
|
||||
// Exit code -1 means an undefined error //
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Default catches any non-specified errors //
|
||||
catch (...)
|
||||
{
|
||||
// Closes the log if it is open to get as much info as possible //
|
||||
if (log != nullptr) { log->close(); }
|
||||
|
||||
// Exit code -1 means an undefined error //
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user