-
Notifications
You must be signed in to change notification settings - Fork 6
/
Expr.h
106 lines (82 loc) · 2.68 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once
#include <any>
#include <memory>
#include <utility> // std::move
#include <vector>
#include "Token.h"
struct Assign;
struct Binary;
struct Grouping;
struct Literal;
struct Unary;
struct Variable;
struct ExprVisitor {
virtual std::any visitAssignExpr(std::shared_ptr<Assign> expr) = 0;
virtual std::any visitBinaryExpr(std::shared_ptr<Binary> expr) = 0;
virtual std::any visitGroupingExpr(std::shared_ptr<Grouping> expr) = 0;
virtual std::any visitLiteralExpr(std::shared_ptr<Literal> expr) = 0;
virtual std::any visitUnaryExpr(std::shared_ptr<Unary> expr) = 0;
virtual std::any visitVariableExpr(std::shared_ptr<Variable> expr) = 0;
virtual ~ExprVisitor() = default;
};
struct Expr {
virtual std::any accept(ExprVisitor& visitor) = 0;
};
struct Assign: Expr, public std::enable_shared_from_this<Assign> {
Assign(Token name, std::shared_ptr<Expr> value)
: name{std::move(name)}, value{std::move(value)}
{}
std::any accept(ExprVisitor& visitor) override {
return visitor.visitAssignExpr(shared_from_this());
}
const Token name;
const std::shared_ptr<Expr> value;
};
struct Binary: Expr, public std::enable_shared_from_this<Binary> {
Binary(std::shared_ptr<Expr> left, Token op, std::shared_ptr<Expr> right)
: left{std::move(left)}, op{std::move(op)}, right{std::move(right)}
{}
std::any accept(ExprVisitor& visitor) override {
return visitor.visitBinaryExpr(shared_from_this());
}
const std::shared_ptr<Expr> left;
const Token op;
const std::shared_ptr<Expr> right;
};
struct Grouping: Expr, public std::enable_shared_from_this<Grouping> {
Grouping(std::shared_ptr<Expr> expression)
: expression{std::move(expression)}
{}
std::any accept(ExprVisitor& visitor) override {
return visitor.visitGroupingExpr(shared_from_this());
}
const std::shared_ptr<Expr> expression;
};
struct Literal: Expr, public std::enable_shared_from_this<Literal> {
Literal(std::any value)
: value{std::move(value)}
{}
std::any accept(ExprVisitor& visitor) override {
return visitor.visitLiteralExpr(shared_from_this());
}
const std::any value;
};
struct Unary: Expr, public std::enable_shared_from_this<Unary> {
Unary(Token op, std::shared_ptr<Expr> right)
: op{std::move(op)}, right{std::move(right)}
{}
std::any accept(ExprVisitor& visitor) override {
return visitor.visitUnaryExpr(shared_from_this());
}
const Token op;
const std::shared_ptr<Expr> right;
};
struct Variable: Expr, public std::enable_shared_from_this<Variable> {
Variable(Token name)
: name{std::move(name)}
{}
std::any accept(ExprVisitor& visitor) override {
return visitor.visitVariableExpr(shared_from_this());
}
const Token name;
};