-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.rs
189 lines (163 loc) · 5.19 KB
/
expr.rs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::token::Token;
use crate::literal::Literal;
/// Represents a [`logical`](Expr::Logical) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LogicalData {
pub left: Box<Expr>,
pub operator: Token,
pub right: Box<Expr>,
}
/// Represents an [`unary`](Expr::Unary) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct UnaryData {
pub operator: Token,
pub expr: Box<Expr>,
}
/// Represents a [`binary`](Expr::Binary) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BinaryData {
pub left: Box<Expr>,
pub operator: Token,
pub right: Box<Expr>,
}
/// Represents a [`grouping`](Expr::Grouping) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct GroupingData {
pub expr: Box<Expr>,
}
/// Represents a [`variable`](Expr::Variable) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct VariableData {
pub name: Token,
}
/// Represents an [`assign`](Expr::Assign) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct AssignData {
pub name: Token,
pub value: Box<Expr>,
}
/// Represents a [`call`](Expr::Call) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CallData {
pub callee: Box<Expr>,
pub paren: Token,
pub arguments: Vec<Expr>,
}
/// Represents a [`get`](Expr::Get) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct GetData {
pub object: Box<Expr>,
pub name: Token,
}
/// Represents a [`set`](Expr::Set) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SetData {
pub object: Box<Expr>,
pub name: Token,
pub value: Box<Expr>,
}
/// Represents a [`this`](Expr::This) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ThisData {
pub keyword: Token,
}
/// Represents a [`super`](Expr::Super) expression's data in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SuperData {
pub keyword: Token,
pub method: Token,
}
/// Represents an expression in the language.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Expr {
/// A [`literal`](crate::literal::Literal) value.
/// - `1`
/// - `"hello"`
/// - `true`
/// - `null`
Literal(Literal),
/// A logical expression.
/// - `true and false`
/// - `1 or "hello"`
Logical(LogicalData),
/// An unary expression.
/// - `-1`
/// - `!true`
Unary(UnaryData),
/// A binary expression.
/// - `1 + 2`
/// - `1 != 2`
/// - `1 <= 2`
/// - `1 / 2`
Binary(BinaryData),
/// A grouping expression.
/// - `(1 + 2)`
/// - `(true and false) or (1 <= 2)`
/// - `((1 + 2) * 3) / 4`
Grouping(GroupingData),
/// A variable expression.
/// - `x`
Variable(VariableData),
/// An assignment expression.
/// - `x = 1`
/// - `x = "hello"`
/// - `x = func()`
Assign(AssignData),
/// A call expression.
/// - `func()`
/// - `func(arg1, 23)`
/// - `instance.method()`
Call(CallData),
/// A get expression.
/// - `instance.property`
/// - `instance.property.method()`
Get(GetData),
/// A set expression.
/// - `instance.property = 1`
/// - `instance.property = "hello"`
Set(SetData),
/// A this expression.
/// - `this`
/// - `this.property`
This(ThisData),
/// A super expression.
/// - `super.method()`
/// - `super.method(arg1, 23)`
Super(SuperData),
}
impl Expr {
/// Accepts a visitor and returns the result of the visit.
/// This is used to implement the visitor pattern.
pub fn accept<T>(&self, visitor: &mut impl ExprVisitor<T>) -> T {
use Expr::*;
match self {
Literal(_) => visitor.visit_literal_expr(self),
Logical(_) => visitor.visit_logical_expr(self),
Unary(_) => visitor.visit_unary_expr(self),
Binary(_) => visitor.visit_binary_expr(self),
Grouping(_) => visitor.visit_grouping_expr(self),
Variable(_) => visitor.visit_variable_expr(self),
Assign(_) => visitor.visit_assign_expr(self),
Call(_) => visitor.visit_call_expr(self),
Get(_) => visitor.visit_get_expr(self),
Set(_) => visitor.visit_set_expr(self),
This(_) => visitor.visit_this_expr(self),
Super(_) => visitor.visit_super_expr(self),
}
}
}
/// A visitor for expressions.
pub trait ExprVisitor<T> {
fn visit_literal_expr(&mut self, expr: &Expr) -> T;
fn visit_logical_expr(&mut self, expr: &Expr) -> T;
fn visit_unary_expr(&mut self, expr: &Expr) -> T;
fn visit_binary_expr(&mut self, expr: &Expr) -> T;
fn visit_grouping_expr(&mut self, expr: &Expr) -> T;
fn visit_variable_expr(&mut self, expr: &Expr) -> T;
fn visit_assign_expr(&mut self, expr: &Expr) -> T;
fn visit_call_expr(&mut self, expr: &Expr) -> T;
fn visit_get_expr(&mut self, expr: &Expr) -> T;
fn visit_set_expr(&mut self, expr: &Expr) -> T;
fn visit_this_expr(&mut self, expr: &Expr) -> T;
fn visit_super_expr(&mut self, expr: &Expr) -> T;
}