66 lines
1.1 KiB
C++

#ifndef EXPR_HH
#define EXPR_HH
#include "../tokenizer.hh"
#include <memory>
#include <string>
#include <variant>
struct Expr;
using ExprPtr = std::shared_ptr<Expr>;
struct Binary {
Binary(ExprPtr left, Token op, ExprPtr right) : left{left}, op{op}, right{right} {};
ExprPtr left;
Token op;
ExprPtr right;
};
struct Grouping {
Grouping(ExprPtr expr) : expression{expr} {};
ExprPtr expression;
};
struct Literal {
Literal(Object value) : value{value} {};
Object value;
};
struct Unary {
Unary(Token op, ExprPtr right) : op{op}, right{right} {};
Token op;
ExprPtr right;
};
enum class ExprType {
BINARY,
GROUPING,
LITERAL,
UNARY,
};
using Expression = std::variant<Binary, Grouping, Literal, Unary>;
struct Expr {
Expr(ExprType type, Expression value) : type{type}, value{value} {}
ExprType type;
Expression value;
};
template <typename T>
Expr make_expr(T value);
class AstPrinter {
public:
std::string print(const Expr &expr);
private:
std::string parenthesize(const std::string &name, std::vector<Expr> exprs);
};
#endif