mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Improved syntax for functions
Added logging system for AST nodes as well.
This commit is contained in:
@@ -16,9 +16,6 @@ namespace LX
|
||||
llvm::Module module;
|
||||
llvm::IRBuilder<> builder;
|
||||
};
|
||||
|
||||
// Function to turn a AST node into string //
|
||||
std::string ToString(std::unique_ptr<AST::Node>& node);
|
||||
}
|
||||
|
||||
namespace LX::AST
|
||||
@@ -33,6 +30,9 @@ namespace LX::AST
|
||||
// Function for generating LLVN IR (Intermediate representation) //
|
||||
llvm::Value* GenIR(InfoLLVM& LLVM) override;
|
||||
|
||||
// Function to log the node to a file //
|
||||
void Log(std::ofstream* log, unsigned depth) override;
|
||||
|
||||
private:
|
||||
// The number it stores //
|
||||
// Yes the number is stored as a string, It's horrible I know //
|
||||
@@ -49,6 +49,9 @@ namespace LX::AST
|
||||
// Function for generating LLVN IR (Intermediate representation) //
|
||||
llvm::Value* GenIR(InfoLLVM& LLVM) override;
|
||||
|
||||
// Function to log the node to a file //
|
||||
void Log(std::ofstream* log, unsigned depth) override;
|
||||
|
||||
private:
|
||||
// The sides of the operation //
|
||||
// Unary operations are handled by a different class //
|
||||
@@ -68,6 +71,9 @@ namespace LX::AST
|
||||
// Function for generating LLVN IR (Intermediate representation) //
|
||||
llvm::Value* GenIR(InfoLLVM& LLVM) override;
|
||||
|
||||
// Function to log the node to a file //
|
||||
void Log(std::ofstream* log, unsigned depth) override;
|
||||
|
||||
private:
|
||||
// What it is returning (can be null) //
|
||||
std::unique_ptr<Node> m_Val;
|
||||
|
||||
@@ -1,17 +1,43 @@
|
||||
#include <AST.h>
|
||||
|
||||
namespace LX
|
||||
{
|
||||
std::string ToString(std::unique_ptr<AST::Node>& node)
|
||||
{
|
||||
if (node == nullptr) { return "NULL Node"; }
|
||||
#include <fstream>
|
||||
|
||||
switch (node->m_Type)
|
||||
namespace LX::AST
|
||||
{
|
||||
void Node::Log(std::ofstream* log, unsigned depth)
|
||||
{
|
||||
(*log) << std::string(depth, '\t') << "NULL node";
|
||||
}
|
||||
|
||||
void NumberLiteral::Log(std::ofstream* log, unsigned depth)
|
||||
{
|
||||
(*log) << std::string(depth, '\t');
|
||||
(*log) << "Number: " << m_Number << "\n";
|
||||
}
|
||||
|
||||
void Operation::Log(std::ofstream* 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);
|
||||
}
|
||||
|
||||
void ReturnStatement::Log(std::ofstream* log, unsigned depth)
|
||||
{
|
||||
(*log) << std::string(depth, '\t');
|
||||
|
||||
if (m_Val == nullptr)
|
||||
{
|
||||
case AST::Node::IDENTIFIER: return "IDENTIFIER";
|
||||
case AST::Node::OPERATION: return "OPERATION";
|
||||
case AST::Node::RETURN_STATEMENT: return "return";
|
||||
case AST::Node::NUMBER_LITERAL: return "number";
|
||||
(*log) << "Return\n";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
(*log) << "Return:\n";
|
||||
m_Val->Log(log, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,12 +42,13 @@ namespace LX
|
||||
case Token::NUMBER_LITERAL:
|
||||
return std::make_unique<AST::NumberLiteral>(p.tokens[p.index++].GetContents());
|
||||
|
||||
//
|
||||
// TODO: Fix this //
|
||||
case Token::OPEN_BRACKET:
|
||||
p.scopeDepth++;
|
||||
p.index++;
|
||||
return nullptr;
|
||||
|
||||
// TODO: Fix this //
|
||||
case Token::CLOSE_BRACE:
|
||||
ThrowIf<UnexpectedToken>(p.scopeDepth == 0, Token::UNDEFINED, "need a different error", p.tokens[p.index]);
|
||||
p.scopeDepth--;
|
||||
@@ -148,19 +149,31 @@ namespace LX
|
||||
ThrowIf<UnexpectedToken>(p.tokens[p.index].type != Token::IDENTIFIER, Token::IDENTIFIER, "", p.tokens[p.index]);
|
||||
func.name = p.tokens[p.index++].GetContents();
|
||||
|
||||
// Checks for opening paren '(' //
|
||||
ThrowIf<UnexpectedToken>(p.tokens[p.index].type != Token::OPEN_PAREN, Token::OPEN_PAREN, "", p.tokens[p.index]);
|
||||
p.index++;
|
||||
|
||||
// Loops over all the arguments of the function //
|
||||
while (p.index < p.len && (p.tokens[p.index].type == Token::CLOSE_PAREN) == false)
|
||||
{
|
||||
p.index++;
|
||||
}
|
||||
|
||||
// Skips over close bracket //
|
||||
p.index++;
|
||||
|
||||
// Checks for opening bracket '{' //
|
||||
ThrowIf<UnexpectedToken>(p.tokens[p.index].type != Token::OPEN_BRACKET, Token::OPEN_BRACKET, "", p.tokens[p.index]);
|
||||
p.index++;
|
||||
|
||||
// Loops over the body until it reaches the end //
|
||||
// TODO: Detect the end instead of looping over the entire token vector
|
||||
while (p.index < p.len && (p.tokens[p.index].type == Token::CLOSE_BRACKET && p.scopeDepth == 0) == false)
|
||||
{
|
||||
// Actually parses the function
|
||||
std::unique_ptr<AST::Node> node = Parse(p);
|
||||
|
||||
// Logs the node to the log //
|
||||
SafeLog(log, ToString(node));
|
||||
if (log != nullptr) { node->Log(log, 0); }
|
||||
|
||||
// Adds it to the vector
|
||||
func.body.push_back(std::move(node));
|
||||
|
||||
Reference in New Issue
Block a user