|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <string> |
| 4 | +#include <variant> |
| 5 | +#include <vector> |
| 6 | +#include <lbd/frontend/type.h> |
| 7 | +#include <lbd/source/Location.hpp> |
| 8 | + |
| 9 | +namespace lbd::frontend::ast |
| 10 | +{ |
| 11 | + struct IdentifierAstNode |
| 12 | + { |
| 13 | + // TODO: This can be a std::string_view. |
| 14 | + std::string value; |
| 15 | + source::Location location; |
| 16 | + |
| 17 | + friend std::ostream& operator<<(std::ostream& outputStream, const IdentifierAstNode& node); |
| 18 | + }; |
| 19 | + |
| 20 | + struct StringAstNode |
| 21 | + { |
| 22 | + std::string value; |
| 23 | + source::Location location; |
| 24 | + |
| 25 | + friend std::ostream& operator<<(std::ostream& outputStream, const StringAstNode& node); |
| 26 | + }; |
| 27 | + |
| 28 | + // TODO: Rename this to NumberAstNode. |
| 29 | + struct FloatAstNode |
| 30 | + { |
| 31 | + double value; |
| 32 | + source::Location location; |
| 33 | + |
| 34 | + friend std::ostream& operator<<(std::ostream& outputStream, const FloatAstNode& node); |
| 35 | + }; |
| 36 | + |
| 37 | + struct Expression; |
| 38 | + |
| 39 | + struct LambdaExpression |
| 40 | + { |
| 41 | + IdentifierAstNode argument; |
| 42 | + interpreter::type::Type argumentType; |
| 43 | + std::unique_ptr<Expression> expression; |
| 44 | + source::Location location; |
| 45 | + |
| 46 | + void print(std::ostream& outputStream, size_t indent) const; |
| 47 | + |
| 48 | + friend std::ostream& operator<<(std::ostream& outputStream, const LambdaExpression& lambdaExpression); |
| 49 | + }; |
| 50 | + |
| 51 | + struct FunctionApplication |
| 52 | + { |
| 53 | + IdentifierAstNode functionName; |
| 54 | + std::vector<std::unique_ptr<Expression>> arguments; |
| 55 | + source::Location location; |
| 56 | + |
| 57 | + void print(std::ostream& outputStream, size_t indent) const; |
| 58 | + |
| 59 | + friend std::ostream& operator<<(std::ostream& outputStream, const FunctionApplication& functionApplication); |
| 60 | + }; |
| 61 | + |
| 62 | + struct Expression |
| 63 | + { |
| 64 | + using ExpressionVariant = std::variant< |
| 65 | + IdentifierAstNode, |
| 66 | + StringAstNode, |
| 67 | + FloatAstNode, |
| 68 | + LambdaExpression, |
| 69 | + FunctionApplication |
| 70 | + >; |
| 71 | + |
| 72 | + ExpressionVariant value; |
| 73 | + |
| 74 | + explicit Expression(IdentifierAstNode value); |
| 75 | + |
| 76 | + explicit Expression(StringAstNode value); |
| 77 | + |
| 78 | + explicit Expression(FloatAstNode value); |
| 79 | + |
| 80 | + explicit Expression(LambdaExpression value); |
| 81 | + |
| 82 | + explicit Expression(FunctionApplication value); |
| 83 | + |
| 84 | + void print(std::ostream& outputStream, size_t indent) const; |
| 85 | + |
| 86 | + friend std::ostream& operator<<(std::ostream& outputStream, const Expression& expression); |
| 87 | + |
| 88 | + [[nodiscard]] source::Location getLocation() const; |
| 89 | + |
| 90 | + Expression(const Expression&) = delete; |
| 91 | + |
| 92 | + Expression& operator=(const Expression&) = delete; |
| 93 | + |
| 94 | + Expression(Expression&&) noexcept = default; |
| 95 | + |
| 96 | + Expression& operator=(Expression&&) noexcept = default; |
| 97 | + }; |
| 98 | + |
| 99 | + struct DefinitionAstNode |
| 100 | + { |
| 101 | + IdentifierAstNode definitionName; |
| 102 | + interpreter::type::Type definitionType; |
| 103 | + Expression expression; |
| 104 | + source::Location location; |
| 105 | + |
| 106 | + void print(std::ostream& outputStream, size_t indent) const; |
| 107 | + |
| 108 | + friend std::ostream& operator<<(std::ostream& outputStream, const DefinitionAstNode& node); |
| 109 | + }; |
| 110 | + |
| 111 | + // NOTE: Owning and move-only, not copyable. |
| 112 | + struct AstNode |
| 113 | + { |
| 114 | + using NodeVariant = std::variant<Expression, DefinitionAstNode>; |
| 115 | + |
| 116 | + NodeVariant value; |
| 117 | + |
| 118 | + void print(std::ostream& outputStream, size_t indent) const; |
| 119 | + |
| 120 | + friend std::ostream& operator<<(std::ostream& outputStream, const AstNode& node); |
| 121 | + }; |
| 122 | +} |
0 commit comments