|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <lbd/fe/loc.h> |
| 5 | +#include <lbd/intp/types.h> |
| 6 | +#include <string> |
| 7 | +#include <variant> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace fe::ast { |
| 11 | + struct IdenAstNode { |
| 12 | + std::string value; |
| 13 | + loc::Loc loc; |
| 14 | + |
| 15 | + friend std::ostream &operator<<(std::ostream &os, const IdenAstNode &node); |
| 16 | + }; |
| 17 | + |
| 18 | + struct StringAstNode { |
| 19 | + std::string value; |
| 20 | + loc::Loc loc; |
| 21 | + |
| 22 | + friend std::ostream &operator<<(std::ostream &os, const StringAstNode &node); |
| 23 | + }; |
| 24 | + |
| 25 | + struct FloatAstNode { |
| 26 | + double value; |
| 27 | + loc::Loc loc; |
| 28 | + |
| 29 | + friend std::ostream &operator<<(std::ostream &os, const FloatAstNode &node); |
| 30 | + }; |
| 31 | + |
| 32 | + struct Expression; |
| 33 | + |
| 34 | + struct LambdaExpression { |
| 35 | + IdenAstNode arg; |
| 36 | + intp::types::Type arg_type; |
| 37 | + std::unique_ptr<Expression> expr; |
| 38 | + loc::Loc loc; |
| 39 | + intp::types::CompoundType lmd_expr_type; // Lambda Expression Type |
| 40 | + |
| 41 | + void print(std::ostream &os, size_t indent) const; |
| 42 | + |
| 43 | + friend std::ostream &operator<<(std::ostream &os, const LambdaExpression &l_expr); |
| 44 | + }; |
| 45 | + |
| 46 | + struct FunctionApplication { |
| 47 | + IdenAstNode fn_name; |
| 48 | + std::vector<std::unique_ptr<Expression> > args; |
| 49 | + loc::Loc loc; |
| 50 | + |
| 51 | + void print(std::ostream &os, size_t indent) const; |
| 52 | + |
| 53 | + friend std::ostream &operator<<(std::ostream &os, const FunctionApplication &fn_apl); |
| 54 | + }; |
| 55 | + |
| 56 | + struct Expression { |
| 57 | + using ExpressionVariant = std::variant< |
| 58 | + IdenAstNode, |
| 59 | + StringAstNode, |
| 60 | + FloatAstNode, |
| 61 | + LambdaExpression, |
| 62 | + FunctionApplication |
| 63 | + >; |
| 64 | + |
| 65 | + ExpressionVariant value; |
| 66 | + |
| 67 | + explicit Expression(IdenAstNode value); |
| 68 | + |
| 69 | + explicit Expression(StringAstNode value); |
| 70 | + |
| 71 | + explicit Expression(FloatAstNode value); |
| 72 | + |
| 73 | + explicit Expression(LambdaExpression value); |
| 74 | + |
| 75 | + explicit Expression(FunctionApplication value); |
| 76 | + |
| 77 | + void print(std::ostream &os, size_t indent) const; |
| 78 | + |
| 79 | + friend std::ostream &operator<<(std::ostream &os, const Expression &expr); |
| 80 | + |
| 81 | + [[nodiscard]] loc::Loc get_loc() const; |
| 82 | + |
| 83 | + Expression(const Expression &) = delete; |
| 84 | + |
| 85 | + Expression &operator=(const Expression &) = delete; |
| 86 | + |
| 87 | + Expression(Expression &&) noexcept = default; |
| 88 | + |
| 89 | + Expression &operator=(Expression &&) noexcept = default; |
| 90 | + }; |
| 91 | + |
| 92 | + struct DefAstNode { |
| 93 | + IdenAstNode def_name; |
| 94 | + intp::types::Type typ; |
| 95 | + Expression expr; |
| 96 | + loc::Loc loc; |
| 97 | + |
| 98 | + void print(std::ostream &os, size_t indent) const; |
| 99 | + |
| 100 | + friend std::ostream &operator<<(std::ostream &os, const DefAstNode &node); |
| 101 | + }; |
| 102 | + |
| 103 | + struct AstNode { |
| 104 | + using NodeVariant = std::variant<Expression, DefAstNode>; |
| 105 | + |
| 106 | + NodeVariant value; |
| 107 | + |
| 108 | + void print(std::ostream &os, size_t indent) const; |
| 109 | + |
| 110 | + friend std::ostream &operator<<(std::ostream &os, const AstNode &node); |
| 111 | + }; |
| 112 | + |
| 113 | + struct Program { |
| 114 | + std::vector<AstNode> nodes; |
| 115 | + |
| 116 | + friend std::ostream &operator<<(std::ostream &os, const Program &p); |
| 117 | + }; |
| 118 | +} |
0 commit comments