summaryrefslogtreecommitdiff
path: root/src/semantic/translate.rs
blob: 9da6411f68695d1d2c6bbb692cf0c5078b665bd3 (plain)
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
use ::ast::{self, WithLocation};
use super::environment;
use super::types::Ty;

#[derive(Debug)]
pub enum TypeError {
    Unimplemented,
    Mismatch {
        expected: Ty,
        actual: Ty,
    },
    UnboundName,
}

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)
}

macro_rules! err {
    ($exp: expr, $err: expr) => {
        Err(WithLocation::new($err, $exp.start, $exp.end))
    }
}

fn trans_exp<'a>(venv: &mut TypeEnvironment<'a>, exp: &WithLocation<ast::Expression>) -> Result {
    use ast::Expression::*;

    match &exp.value {
        &Let(ref decls, ref body) => {
            let mut new_env = TypeEnvironment::new(None);
            for decl in decls.iter() {
                let decl_ty = trans_exp(venv, &*decl.value.value)?;
                if let Some(_) = decl.value.type_ {
                    return err!(decl, TypeError::Unimplemented);
                }
                else {
                    new_env.add_binding(decl.value.name.value.clone(), decl_ty);
                }
            }
            new_env.set_parent(venv);
            trans_exp(&mut new_env, &*body)
        },
        &UnaryOp(ref op, ref operand) => {
            use ast::UnaryOp::*;
            let operand_ty = trans_exp(venv, operand)?;
            match op {
                &Neg | &Pos => {
                    match operand_ty {
                        Ty::Int => {
                            Ok(Ty::Int)
                        }
                        other => {
                            err!(operand, TypeError::Mismatch {
                                expected: Ty::Int,
                                actual: other,
                            })
                        }
                    }
                }
                &Not => {
                    err!(exp, TypeError::Unimplemented)
                }
            }
        },
        &BinOp(ref op, ref left, ref right) => {
            use ast::BinOp::*;
            let left_ty = trans_exp(venv, left)?;
            let right_ty = trans_exp(venv, right)?;
            match op {
                &Add => {
                    match (left_ty, right_ty) {
                        (Ty::Int, Ty::Int) => {
                            Ok(Ty::Int)
                        }
                        (Ty::String, Ty::String) => {
                            Ok(Ty::String)
                        }
                        (Ty::Int, other) => {
                            err!(right, TypeError::Mismatch {
                                expected: Ty::Int,
                                actual: other,
                            })
                        }
                        (Ty::String, other) => {
                            err!(right, TypeError::Mismatch {
                                expected: Ty::String,
                                actual: other,
                            })
                        }
                        _ => {
                            err!(exp, TypeError::Unimplemented)
                        }
                    }
                }
                _ => {
                    err!(exp, TypeError::Unimplemented)
                }
            }
        },
        &Number(_) => Ok(Ty::Int),
        &String(_) => Ok(Ty::String),
        &Name(ref name) => {
            if let Some(ty) = venv.lookup(name) {
                Ok(*ty)
            }
            else {
                err!(exp, TypeError::UnboundName)
            }
        },
        &Nil => {
            Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end))
        },
    }
}