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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::collections::HashSet;
use rush_parser::ast::{AssignOp, InfixOp, PrefixOp, Type};
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedProgram<'src> {
pub globals: Vec<AnalyzedLetStmt<'src>>,
pub functions: Vec<AnalyzedFunctionDefinition<'src>>,
pub main_fn: AnalyzedBlock<'src>,
pub used_builtins: HashSet<&'src str>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedFunctionDefinition<'src> {
pub used: bool,
pub name: &'src str,
pub params: Vec<AnalyzedParameter<'src>>,
pub return_type: Type,
pub block: AnalyzedBlock<'src>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AnalyzedParameter<'src> {
pub mutable: bool,
pub name: &'src str,
pub type_: Type,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedBlock<'src> {
pub result_type: Type,
pub stmts: Vec<AnalyzedStatement<'src>>,
pub expr: Option<AnalyzedExpression<'src>>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AnalyzedStatement<'src> {
Let(AnalyzedLetStmt<'src>),
Return(AnalyzedReturnStmt<'src>),
Loop(AnalyzedLoopStmt<'src>),
While(AnalyzedWhileStmt<'src>),
For(AnalyzedForStmt<'src>),
Break,
Continue,
Expr(AnalyzedExpression<'src>),
}
impl AnalyzedStatement<'_> {
pub fn result_type(&self) -> Type {
match self {
Self::Let(_) => Type::Unit,
Self::Return(_) => Type::Never,
Self::Loop(node) => match node.never_terminates {
true => Type::Never,
false => Type::Unit,
}, Self::While(node) => match node.never_terminates {
true => Type::Never,
false => Type::Unit,
}, Self::For(node) => match node.never_terminates {
true => Type::Never,
false => Type::Unit,
}, Self::Break => Type::Never,
Self::Continue => Type::Never,
Self::Expr(expr) => expr.result_type(),
}
}
pub fn constant(&self) -> bool {
match self {
Self::Expr(expr) => expr.constant(),
_ => false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedLetStmt<'src> {
pub name: &'src str,
pub expr: AnalyzedExpression<'src>,
pub mutable: bool,
pub used: bool,
}
pub type AnalyzedReturnStmt<'src> = Option<AnalyzedExpression<'src>>;
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedLoopStmt<'src> {
pub block: AnalyzedBlock<'src>,
pub never_terminates: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedWhileStmt<'src> {
pub cond: AnalyzedExpression<'src>,
pub block: AnalyzedBlock<'src>,
pub never_terminates: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedForStmt<'src> {
pub ident: &'src str,
pub initializer: AnalyzedExpression<'src>,
pub cond: AnalyzedExpression<'src>,
pub update: AnalyzedExpression<'src>,
pub block: AnalyzedBlock<'src>,
pub never_terminates: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AnalyzedExpression<'src> {
Block(Box<AnalyzedBlock<'src>>),
If(Box<AnalyzedIfExpr<'src>>),
Int(i64),
Float(f64),
Bool(bool),
Char(u8),
Ident(AnalyzedIdentExpr<'src>),
Prefix(Box<AnalyzedPrefixExpr<'src>>),
Infix(Box<AnalyzedInfixExpr<'src>>),
Assign(Box<AnalyzedAssignExpr<'src>>),
Call(Box<AnalyzedCallExpr<'src>>),
Cast(Box<AnalyzedCastExpr<'src>>),
Grouped(Box<AnalyzedExpression<'src>>),
}
impl AnalyzedExpression<'_> {
pub fn result_type(&self) -> Type {
match self {
Self::Block(expr) => expr.result_type,
Self::Int(_) => Type::Int,
Self::Float(_) => Type::Float,
Self::Bool(_) => Type::Bool,
Self::Char(_) => Type::Char,
Self::Ident(expr) => expr.result_type,
Self::If(expr) => expr.result_type,
Self::Prefix(expr) => expr.result_type,
Self::Infix(expr) => expr.result_type,
Self::Assign(expr) => expr.result_type,
Self::Call(expr) => expr.result_type,
Self::Cast(expr) => expr.result_type,
Self::Grouped(expr) => expr.result_type(),
}
}
pub fn constant(&self) -> bool {
matches!(
self,
Self::Int(_) | Self::Float(_) | Self::Bool(_) | Self::Char(_)
)
}
pub fn as_constant(&self) -> Option<Self> {
match self {
AnalyzedExpression::Int(_)
| AnalyzedExpression::Float(_)
| AnalyzedExpression::Bool(_)
| AnalyzedExpression::Char(_) => {
Some(self.clone())
}
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedIfExpr<'src> {
pub result_type: Type,
pub cond: AnalyzedExpression<'src>,
pub then_block: AnalyzedBlock<'src>,
pub else_block: Option<AnalyzedBlock<'src>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AnalyzedIdentExpr<'src> {
pub result_type: Type,
pub ident: &'src str,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedPrefixExpr<'src> {
pub result_type: Type,
pub op: PrefixOp,
pub expr: AnalyzedExpression<'src>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedInfixExpr<'src> {
pub result_type: Type,
pub lhs: AnalyzedExpression<'src>,
pub op: InfixOp,
pub rhs: AnalyzedExpression<'src>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedAssignExpr<'src> {
pub result_type: Type,
pub assignee: &'src str,
pub op: AssignOp,
pub expr: AnalyzedExpression<'src>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedCallExpr<'src> {
pub result_type: Type,
pub func: &'src str,
pub args: Vec<AnalyzedExpression<'src>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnalyzedCastExpr<'src> {
pub result_type: Type,
pub expr: AnalyzedExpression<'src>,
pub type_: Type,
}