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

Commit 05bc529

Browse files
committed
chore: refactory ast
1 parent c81ae29 commit 05bc529

15 files changed

Lines changed: 520 additions & 1 deletion

‎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) noexcept;
11+
12+
void print(std::ostream& outputStream, size_t indent) const noexcept override;
13+
14+
private:
15+
std::string name;
16+
};
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <lbd/frontend/ast/expression/Expression.hpp>
4+
#include <lbd/frontend/ast/expression/IdentifierExpression.hpp>
5+
6+
namespace lbd::frontend::ast::expression
7+
{
8+
class NumberExpression final : Expression
9+
{
10+
public:
11+
NumberExpression(const source::Range& range, std::string value) noexcept;
12+
13+
void print(std::ostream& outputStream, size_t indent) const noexcept override;
14+
15+
private:
16+
IdentifierExpression argumentIdentifierExpression;
17+
interpreter::type::Type argumentType;
18+
std::unique_ptr<Expression> expression;
19+
source::Location location;
20+
};
21+
}
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 NumberExpression final : Expression
8+
{
9+
public:
10+
NumberExpression(const source::Range& range, double value) noexcept;
11+
12+
void print(std::ostream& outputStream, size_t indent) const noexcept override;
13+
14+
private:
15+
double value;
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) noexcept;
11+
12+
void print(std::ostream& outputStream, size_t indent) const noexcept override;
13+
14+
private:
15+
std::string value;
16+
};
17+
}

‎include/lbd/type/Type.hpp‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <vector>
5+
#include <lbd/type/TypeKind.hpp>
6+
7+
namespace lbd::type
8+
{
9+
class Type;
10+
using TypePtr = std::shared_ptr<Type>;
11+
12+
struct TypePtrHash
13+
{
14+
size_t operator()(const TypePtr& t) const noexcept;
15+
};
16+
17+
struct TypePtrEq
18+
{
19+
bool operator()(const TypePtr& a, const TypePtr& b) const noexcept;
20+
};
21+
22+
// Usage: std::unordered_map<TypePtr, TypePtr, TypePtrHash, TypePtrEq> typeSubstitutionMap;
23+
24+
struct Constraint
25+
{
26+
std::string className; // Eq, Ord, Show.
27+
TypePtr type; // Most likely will be a VARIABLE: a, b.
28+
29+
[[nodiscard]] bool operator==(const Constraint& other) const noexcept;
30+
};
31+
32+
class Type
33+
{
34+
public:
35+
// Factory functions.
36+
[[nodiscard]] static TypePtr named(const std::string& name);
37+
[[nodiscard]] static TypePtr variable(const std::string& name);
38+
[[nodiscard]] static TypePtr applied(const TypePtr& base, std::vector<TypePtr> arguments);
39+
[[nodiscard]] static TypePtr function(const TypePtr& from, const TypePtr& to);
40+
[[nodiscard]] static TypePtr qualified(std::vector<Constraint> constraints, const TypePtr& qualifiedType);
41+
[[nodiscard]] static TypePtr any();
42+
43+
// Observers.
44+
[[nodiscard]] TypeKind getKind() const noexcept;
45+
[[nodiscard]] const std::string& getName() const noexcept;
46+
[[nodiscard]] const TypePtr& getBase() const noexcept;
47+
[[nodiscard]] const std::vector<TypePtr>& getArguments() const noexcept;
48+
[[nodiscard]] const TypePtr& getFrom() const noexcept;
49+
[[nodiscard]] const TypePtr& getTo() const noexcept;
50+
[[nodiscard]] const std::vector<Constraint>& getConstraints() const noexcept;
51+
[[nodiscard]] const TypePtr& getQualifiedType() const noexcept;
52+
53+
// Comparison.
54+
[[nodiscard]] bool equals(const Type& other) const noexcept;
55+
[[nodiscard]] bool accepts(const Type& other) const noexcept;
56+
[[nodiscard]] bool operator==(const Type& other) const noexcept;
57+
[[nodiscard]] size_t hash() const noexcept;
58+
59+
friend std::ostream& operator<<(std::ostream&, const Type&) noexcept;
60+
61+
private:
62+
TypeKind kind;
63+
64+
// NAMED, VARIABLE.
65+
std::string name;
66+
67+
// APPLIED.
68+
TypePtr base;
69+
std::vector<TypePtr> arguments;
70+
71+
// FUNCTION.
72+
TypePtr from;
73+
TypePtr to;
74+
75+
// QUALIFIED.
76+
std::vector<Constraint> constraints;
77+
TypePtr qualifiedType;
78+
};
79+
}

‎include/lbd/type/TypeKind.hpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
namespace lbd::type
4+
{
5+
enum class TypeKind
6+
{
7+
NAMED, // Number, String
8+
VARIABLE, // a, b
9+
APPLIED, // List<Number>, Map<String, Number>
10+
FUNCTION, // String -> Number
11+
QUALIFIED, // Eq a => a -> a
12+
ANY,
13+
};
14+
}

0 commit comments

Comments
 (0)