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
|
use ::ast::{self, WithLocation};
use super::environment;
use super::types::Ty;
#[derive(Debug)]
pub enum TypeError {
Unimplemented,
Mismatch {
expected: Ty,
actual: Ty,
},
}
type TypeEnvironment<'a> = environment::Environment<'a, String, Ty>;
pub type Result = ::std::result::Result<Ty, WithLocation<TypeError>>;
pub fn translate(program: &ast::Program) -> Result {
let mut env = TypeEnvironment::new(None);
trans_exp(&mut env, &*program.0)
}
fn trans_exp<'a>(venv: &mut TypeEnvironment<'a>, exp: &WithLocation<ast::Expression>) -> Result {
use ast::Expression::*;
match &exp.value {
&Let(ref decls, ref body) => {
Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end))
},
&UnaryOp(ref op, ref operand) => {
Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end))
},
&BinOp(ref op, ref left, ref right) => {
use ast::BinOp::*;
match op {
&Add => {
let left = trans_exp(venv, left)?;
let right = trans_exp(venv, right)?;
match (left, right) {
(Ty::Int, Ty::Int) => {
Ok(Ty::Int)
}
(Ty::String, Ty::String) => {
Ok(Ty::String)
}
(Ty::Int, other) => {
Err(WithLocation::new(TypeError::Mismatch {
expected: Ty::Int,
actual: other,
}, exp.start, exp.end))
}
(Ty::String, other) => {
Err(WithLocation::new(TypeError::Mismatch {
expected: Ty::String,
actual: other,
}, exp.start, exp.end))
}
_ => {
Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end))
}
}
}
_ => {
Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end))
}
}
},
&Number(_) => Ok(Ty::Int),
&String(_) => Ok(Ty::String),
&Name(ref name) => {
Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end))
},
&Nil => {
Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end))
},
}
}
|