47 lines
833 B
C++

#ifndef EXPR_HH
#define EXPR_HH
#include "../tokenizer.hh"
#include <string>
#include <variant>
struct Expr {};
struct Binary : public Expr {
Binary(Expr left, Token op, Expr right) : left{left}, op{op}, right{right} {};
Expr left;
Token op;
Expr right;
};
struct Grouping : public Expr {
Grouping(Expr expr) : expression{expr} {};
Expr expression;
};
struct Literal : public Expr {
Literal(Object value) : value{value} {};
Object value;
};
struct Unary : public Expr {
Unary(Token op, Expr right) : op{op}, right{right} {};
Token op;
Expr right;
};
using Expression = std::variant<Expr, Binary, Grouping, Literal, Unary>;
class AstPrinter {
public:
std::string print(const Expression &expr);
private:
std::string parenthesize(const std::string &name, std::vector<Expression> exprs);
};
#endif