Refactored how logging works

Made it central reusable logic. No longer needs to be passed around, opened or closed.
This commit is contained in:
Pasha Bibko
2025-05-05 23:55:22 +01:00
parent 0f11fe006b
commit 5339df9b36
22 changed files with 231 additions and 193 deletions

View File

@@ -31,7 +31,7 @@ namespace LX::AST
llvm::Value* GenIR(InfoLLVM& LLVM) override;
// Function to log the node to a file, will throw an error if called on this class //
void Log(std::ofstream* log, unsigned depth) override;
void Log(unsigned depth) override;
// The nodes that are contained within this node //
std::vector<std::unique_ptr<Node>> nodes;
@@ -48,7 +48,7 @@ namespace LX::AST
llvm::Value* GenIR(InfoLLVM& LLVM) override;
// Function to log the node to a file //
void Log(std::ofstream* log, unsigned depth) override;
void Log(unsigned depth) override;
private:
// The number it stores //
@@ -67,7 +67,7 @@ namespace LX::AST
llvm::Value* GenIR(InfoLLVM& LLVM) override;
// Function to log the node to a file //
void Log(std::ofstream* log, unsigned depth) override;
void Log(unsigned depth) override;
private:
// The sides of the operation //
@@ -89,7 +89,7 @@ namespace LX::AST
llvm::Value* GenIR(InfoLLVM& LLVM) override;
// Function to log the node to a file //
void Log(std::ofstream* log, unsigned depth) override;
void Log(unsigned depth) override;
private:
// What it is returning (can be null) //
@@ -107,7 +107,7 @@ namespace LX::AST
llvm::Value* GenIR(InfoLLVM& LLVM) override;
// Function to log the node to a file //
void Log(std::ofstream* log, unsigned depth) override;
void Log(unsigned depth) override;
private:
// Name of the variable //
@@ -127,7 +127,7 @@ namespace LX::AST
llvm::Value* GenIR(InfoLLVM& LLVM) override;
// Function to log the node to a file //
void Log(std::ofstream* log, unsigned depth) override;
void Log(unsigned depth) override;
private:
// Name of the variable //
@@ -148,7 +148,7 @@ namespace LX::AST
llvm::Value* GenIR(InfoLLVM& LLVM) override;
// Function to log the node to a file //
void Log(std::ofstream* log, unsigned depth) override;
void Log(unsigned depth) override;
private:
// The name of the variable //

View File

@@ -4,61 +4,59 @@
namespace LX::AST
{
void MultiNode::Log(std::ofstream* log, unsigned depth)
void MultiNode::Log(unsigned depth)
{
throw int(); // <- TODO: Make an error for this
}
void NumberLiteral::Log(std::ofstream* log, unsigned depth)
void NumberLiteral::Log(unsigned depth)
{
(*log) << std::string(depth, '\t');
(*log) << "Number: " << m_Number << "\n";
Log::out(std::string(depth, '\t'), "Number: ", m_Number);
}
void Operation::Log(std::ofstream* log, unsigned depth)
void Operation::Log(unsigned depth)
{
(*log) << std::string(depth, '\t');
(*log) << "Operation {" << ToString(m_Operand) << "}:\n";
(*log) << std::string(depth + 1, '\t') << "LHS:\n";
m_Lhs.get()->Log(log, depth + 2);
(*log) << std::string(depth + 1, '\t') << "RHS:\n";
m_Rhs.get()->Log(log, depth + 2);
Log::out(std::string(depth, '\t'), "Operation {", ToString(m_Operand), "}:");
Log::out(std::string(depth + 1, '\t'), "LHS:");
m_Lhs->Log(depth + 2);
Log::out(std::string(depth + 1, '\t'), "RHS:");
m_Rhs->Log(depth + 2);
}
void ReturnStatement::Log(std::ofstream* log, unsigned depth)
void ReturnStatement::Log(unsigned depth)
{
(*log) << std::string(depth, '\t');
if (m_Val == nullptr)
Log::out<Log::Format::NONE>(std::string(depth, '\t'), "Return");
if (m_Val != nullptr)
{
(*log) << "Return\n";
Log::out(':');
m_Val->Log(depth + 1);
}
else
{
(*log) << "Return:\n";
m_Val->Log(log, depth + 1);
Log::out<Log::Format::NONE>('\n');
}
}
void VariableDeclaration::Log(std::ofstream* log, unsigned depth)
void VariableDeclaration::Log(unsigned depth)
{
(*log) << std::string(depth, '\t');
(*log) << "Variable declaration: " << m_Name << "\n";
Log::out(std::string(depth, '\t'), "Variable declaration: ", m_Name);
}
void VariableAssignment::Log(std::ofstream* log, unsigned depth)
void VariableAssignment::Log(unsigned depth)
{
(*log) << std::string(depth, '\t');
(*log) << "Variable assignment:\n";
(*log) << std::string(depth + 1, '\t') << "To: " << m_Name << "\n";
(*log) << std::string(depth + 1, '\t') << "Value:\n";
m_Value.get()->Log(log, depth + 2);
Log::out(std::string(depth, '\t'), "Variable assignment:");
Log::out(std::string(depth + 1, '\t'), "To: ", m_Name);
Log::out(std::string(depth + 1, '\t'), "Value:");
m_Value->Log(depth + 2);
}
void VariableAccess::Log(std::ofstream* log, unsigned depth)
void VariableAccess::Log(unsigned depth)
{
(*log) << std::string(depth, '\t');
(*log) << "Variable: " << m_Name << '\n';
Log::out(std::string(depth, '\t'), "Variable: ", m_Name);
}
}

View File

@@ -29,16 +29,13 @@ namespace LX
struct Parser
{
// Passes constructor args to members //
Parser(std::vector<Token>& _tokens, std::ofstream* _log)
: tokens(_tokens), log(_log), index(0), len(_tokens.size()), scopeDepth(0)
Parser(std::vector<Token>& _tokens)
: tokens(_tokens), index(0), len(_tokens.size()), scopeDepth(0)
{}
// Tokens created by the lexer //
std::vector<Token>& tokens;
// Log to output to (can be null) //
std::ofstream* log;
// Length of the the token vector //
const size_t len;
@@ -203,14 +200,14 @@ 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)
{
// Logs the start of the parsing
SafeLog(log, LOG_BREAK, "Started parsing tokens", LOG_BREAK);
Log::LogNewSection("Started parsing tokens");
// Creates the output storer and the parser //
FileAST output;
Parser p(tokens, log);
Parser p(tokens);
// Loops over the tokens and calls the correct parsing function //
// Which depends on their type and current state of the parser //
@@ -261,7 +258,7 @@ namespace LX
for (std::unique_ptr<AST::Node>& containedNode : ((AST::MultiNode*)node.get())->nodes)
{
// Logs the node to the log //
if (log != nullptr) { node->Log(log, 0); }
node->Log(0);
// Adds it to the vector //
func.body.push_back(std::move(containedNode));
@@ -272,7 +269,7 @@ namespace LX
else
{
// Logs the node to the log //
if (log != nullptr) { node->Log(log, 0); }
node->Log(0);
// Adds it to the vector //
func.body.push_back(std::move(node));
@@ -296,7 +293,7 @@ namespace LX
// Logs that AST has finished parsing //
// TODO: Make this output the AST in a human-readable form //
SafeLog(log, "AST length: ", output.functions[0].body.size());
Log::out("AST length: ", output.functions[0].body.size());
// Returns the output and shrinks all uneccesarry allocated memory
output.functions.shrink_to_fit();