-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.h
58 lines (47 loc) · 1.28 KB
/
expr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
#include "ost/token.h"
#include <string>
#include <fmt/core.h>
// Make visitor class available
struct Visitor;
struct Expr
{
virtual ~Expr() {}
virtual std::string accept(Visitor *visitor) = 0;
};
struct Binary : public Expr
{
std::shared_ptr<Expr> left;
std::shared_ptr<Token> op;
std::shared_ptr<Expr> right;
Binary(std::shared_ptr<Expr> left, std::shared_ptr<Token> op, std::shared_ptr<Expr> right)
: left{left}, op{op}, right{right} {}
std::string accept(Visitor *visitor);
};
struct Unary : public Expr
{
std::shared_ptr<Expr> left;
std::shared_ptr<Token> op;
Unary(std::shared_ptr<Token> op, std::shared_ptr<Expr> left) : left{left}, op{op} {}
std::string accept(Visitor *visitor);
};
struct Literal : public Expr
{
Token value;
Literal(Token value) : value{value} {}
std::string accept(Visitor *visitor);
};
struct Grouping : public Expr
{
std::shared_ptr<Expr> expr;
Grouping(std::shared_ptr<Expr> expr) : expr{expr} {}
std::string accept(Visitor *visitor);
};
// Define Visitor class
struct Visitor
{
virtual std::string visit_binary(Binary *binary) = 0;
virtual std::string visit_literal(Literal *literal) = 0;
virtual std::string visit_grouping(Grouping *grouping) = 0;
virtual std::string visit_unary(Unary *unary) = 0;
};