-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathtype_check.rs
More file actions
263 lines (240 loc) · 7.22 KB
/
type_check.rs
File metadata and controls
263 lines (240 loc) · 7.22 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#[cfg(test)]
use expect_test::expect;
use salsa::Accumulator;
#[cfg(test)]
use test_log::test;
use crate::ir::{
Diagnostic, Expression, Function, FunctionId, Program, Span, StatementData, VariableId,
};
// ANCHOR: parse_statements
#[salsa::tracked]
pub fn type_check_program<'db>(db: &'db dyn crate::Db, program: Program<'db>) {
for statement in program.statements(db) {
match &statement.data {
StatementData::Function(f) => type_check_function(db, *f, program),
StatementData::Print(e) => CheckExpression::new(db, program, &[]).check(e),
}
}
}
#[salsa::tracked]
pub fn type_check_function<'db>(
db: &'db dyn crate::Db,
function: Function<'db>,
program: Program<'db>,
) {
CheckExpression::new(db, program, function.args(db)).check(function.body(db))
}
#[salsa::tracked]
pub fn find_function<'db>(
db: &'db dyn crate::Db,
program: Program<'db>,
name: FunctionId<'db>,
) -> Option<Function<'db>> {
program
.statements(db)
.iter()
.flat_map(|s| match &s.data {
StatementData::Function(f) if f.name(db) == name => Some(*f),
_ => None,
})
.next()
}
struct CheckExpression<'input, 'db> {
db: &'db dyn crate::Db,
program: Program<'db>,
names_in_scope: &'input [VariableId<'db>],
}
impl<'input, 'db> CheckExpression<'input, 'db> {
pub fn new(
db: &'db dyn crate::Db,
program: Program<'db>,
names_in_scope: &'input [VariableId<'db>],
) -> Self {
CheckExpression {
db,
program,
names_in_scope,
}
}
}
impl<'db> CheckExpression<'_, 'db> {
fn check(&self, expression: &Expression<'db>) {
match &expression.data {
crate::ir::ExpressionData::Op(left, _, right) => {
self.check(left);
self.check(right);
}
crate::ir::ExpressionData::Number(_) => {}
crate::ir::ExpressionData::Variable(v) => {
if !self.names_in_scope.contains(v) {
self.report_error(
expression.span,
format!("the variable `{}` is not declared", v.text(self.db)),
);
}
}
crate::ir::ExpressionData::Call(f, args) => {
if self.find_function(*f).is_none() {
self.report_error(
expression.span,
format!("the function `{}` is not declared", f.text(self.db)),
);
}
for arg in args {
self.check(arg);
}
}
}
}
fn find_function(&self, f: FunctionId<'db>) -> Option<Function<'db>> {
find_function(self.db, self.program, f)
}
fn report_error(&self, span: Span, message: String) {
Diagnostic::new(span.start(self.db), span.end(self.db), message).accumulate(self.db);
}
}
/// Create a new database with the given source text and parse the result.
/// Returns the statements and the diagnostics generated.
#[cfg(test)]
fn check_string(
source_text: &str,
expected_diagnostics: expect_test::Expect,
edits: &[(&str, expect_test::Expect)],
) {
use salsa::{Database, Setter};
use crate::db::CalcDatabaseImpl;
use crate::ir::SourceProgram;
use crate::parser::parse_statements;
// Create the database
let mut db = CalcDatabaseImpl::default();
db.enable_logging();
// Create the source program
let source_program = SourceProgram::new(&db, source_text.to_string());
// Invoke the parser
let program = parse_statements(&db, source_program);
// Read out any diagnostics
db.attach(|db| {
let rendered_diagnostics: String =
type_check_program::accumulated::<Diagnostic>(db, program)
.into_iter()
.map(|d| d.render(db, source_program))
.collect::<Vec<_>>()
.join("\n");
expected_diagnostics.assert_eq(&rendered_diagnostics);
});
// Apply edits and check diagnostics/logs after each one
for (new_source_text, expected_diagnostics) in edits {
source_program
.set_text(&mut db)
.to(new_source_text.to_string());
db.attach(|db| {
let program = parse_statements(db, source_program);
expected_diagnostics
.assert_debug_eq(&type_check_program::accumulated::<Diagnostic>(db, program));
});
}
}
#[test]
fn check_print() {
check_string("print 1 + 2", expect![""], &[]);
}
#[test]
fn check_bad_variable_in_program() {
check_string(
"print a + b",
expect![[r#"
error: the variable `a` is not declared
--> input:2:7
|
2 | print a + b
| ^^ here
error: the variable `b` is not declared
--> input:2:11
|
2 | print a + b
| ^ here"#]],
&[],
);
}
#[test]
fn check_bad_function_in_program() {
check_string(
"print a(22)",
expect![[r#"
error: the function `a` is not declared
--> input:2:7
|
2 | print a(22)
| ^^^^^ here"#]],
&[],
);
}
#[test]
fn check_bad_variable_in_function() {
check_string(
"
fn add_one(a) = a + b
print add_one(22)
",
expect![[r#"
error: the variable `b` is not declared
--> input:4:33
|
4 | fn add_one(a) = a + b
| _________________________________^
5 | | print add_one(22)
| |____________^ here"#]],
&[],
);
}
#[test]
fn check_bad_function_in_function() {
check_string(
"
fn add_one(a) = add_two(a) + b
print add_one(22)
",
expect![[r#"
error: the function `add_two` is not declared
--> input:4:29
|
4 | fn add_one(a) = add_two(a) + b
| ^^^^^^^^^^ here
error: the variable `b` is not declared
--> input:4:42
|
4 | fn add_one(a) = add_two(a) + b
| __________________________________________^
5 | | print add_one(22)
| |____________^ here"#]],
&[],
);
}
#[test]
fn fix_bad_variable_in_function() {
check_string(
"
fn double(a) = a * b
fn quadruple(a) = double(double(a))
print quadruple(2)
",
expect![[r#"
error: the variable `b` is not declared
--> input:4:32
|
4 | fn double(a) = a * b
| ________________________________^
5 | | fn quadruple(a) = double(double(a))
| |____________^ here"#]],
&[(
"
fn double(a) = a * 2
fn quadruple(a) = double(double(a))
print quadruple(2)
",
expect![[r#"
[]
"#]],
)],
);
}