Skip to content
This repository was archived by the owner on Oct 12, 2026. It is now read-only.

Commit 10abe22

Browse files
committed
Started tidying ast namespace.
1 parent 0cf50b8 commit 10abe22

9 files changed

Lines changed: 128 additions & 1 deletion

File tree

‎include/lbd/frontend/ast/Ast.hpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace lbd::frontend::ast
1212
{
1313
struct IdentifierAstNode
1414
{
15-
// TODO: This can be a std::string_view.
1615
std::string value;
1716
source::Location location;
1817

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <lbd/frontend/ast/AstNodeKind.hpp>
4+
#include <lbd/source/Range.hpp>
5+
6+
namespace lbd::frontend::ast
7+
{
8+
class AstNode
9+
{
10+
public:
11+
AstNode(AstNodeKind kind, source::Range range);
12+
virtual ~AstNode() = default;
13+
14+
virtual void print(std::ostream& outputStream, size_t indent) const noexcept = 0;
15+
friend std::ostream& operator<<(std::ostream& outputStream, const AstNode& node);
16+
17+
private:
18+
const AstNodeKind kind;
19+
const source::Range range;
20+
21+
protected:
22+
static void printIndent(std::ostream& outputStream, size_t indent) noexcept;
23+
};
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
namespace lbd::frontend::ast
4+
{
5+
enum class AstNodeKind
6+
{
7+
IDENTIFIER_EXPRESSION,
8+
STRING_EXPRESSION,
9+
NUMBER_EXPRESSION,
10+
LAMBDA_EXPRESSION,
11+
FUNCTION_APPLICATION_EXPRESSION,
12+
SYMBOL_DEFINITION,
13+
EXPRESSION_STATEMENT,
14+
SYMBOL_DEFINITION_STATEMENT,
15+
};
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <lbd/frontend/ast/AstNode.hpp>
4+
5+
namespace lbd::frontend::ast::expression
6+
{
7+
class Expression : public AstNode
8+
{
9+
using AstNode::AstNode;
10+
};
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <lbd/frontend/ast/expression/Expression.hpp>
4+
5+
namespace lbd::frontend::ast::expression
6+
{
7+
class IdentifierExpression final : Expression
8+
{
9+
public:
10+
IdentifierExpression(const source::Range& range, std::string name);
11+
12+
void print(std::ostream& outputStream, size_t indent) const noexcept override;
13+
14+
private:
15+
std::string name;
16+
};
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <lbd/frontend/ast/expression/Expression.hpp>
4+
5+
namespace lbd::frontend::ast::expression
6+
{
7+
class StringExpression final : Expression
8+
{
9+
public:
10+
StringExpression(const source::Range& range, std::string value);
11+
12+
void print(std::ostream& outputStream, size_t indent) const noexcept override;
13+
14+
private:
15+
std::string value;
16+
};
17+
}

‎src/frontend/ast/AstNode.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <lbd/frontend/ast/AstNode.hpp>
2+
3+
namespace lbd::frontend::ast
4+
{
5+
AstNode::AstNode(const AstNodeKind kind, const source::Range range) : kind(kind), range(range) {}
6+
7+
std::ostream& operator<<(std::ostream& outputStream, const AstNode& node)
8+
{
9+
node.print(outputStream, 0);
10+
return outputStream;
11+
}
12+
13+
void AstNode::printIndent(std::ostream& outputStream, const size_t indent) noexcept
14+
{
15+
for (size_t i = 0; i < indent; ++i) outputStream << " ";
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <lbd/frontend/ast/expression/IdentifierExpression.hpp>
2+
3+
namespace lbd::frontend::ast::expression
4+
{
5+
IdentifierExpression::IdentifierExpression(const source::Range& range, std::string name)
6+
: Expression(AstNodeKind::IDENTIFIER_EXPRESSION, range), name(std::move(name)) {}
7+
8+
void IdentifierExpression::print(std::ostream& outputStream, const size_t indent) const noexcept
9+
{
10+
printIndent(outputStream, indent);
11+
outputStream << name;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <lbd/frontend/ast/expression/StringExpression.hpp>
2+
3+
namespace lbd::frontend::ast::expression
4+
{
5+
StringExpression::StringExpression(const source::Range& range, std::string value)
6+
: Expression(AstNodeKind::STRING_EXPRESSION, range), value(std::move(value)) {}
7+
8+
void StringExpression::print(std::ostream& outputStream, const size_t indent) const noexcept
9+
{
10+
printIndent(outputStream, indent);
11+
outputStream << '\"' << value << '\"';
12+
}
13+
}

0 commit comments

Comments
 (0)