summaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: d60edc0841852ce37945067b40c05793bbfcf324 (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
#[derive(Debug)]
pub struct WithLocation<T> {
    pub value: T,
    pub start: usize,
    pub end: usize,
}

impl<T> WithLocation<T> {
    pub fn new(value: T, start: usize, end: usize) -> WithLocation<T> {
        WithLocation {
            value: value,
            start: start,
            end: end,
        }
    }

    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> WithLocation<U> {
        WithLocation {
            value: f(self.value),
            start: self.start,
            end: self.end,
        }
    }

    pub fn join_map<U, V, F>(self, other: WithLocation<U>, f: F) -> WithLocation<V>
        where F: FnOnce(WithLocation<T>, WithLocation<U>) -> V
    {
        let start = self.start;
        let end = other.end;
        WithLocation {
            value: f(self, other),
            start: start,
            end: end,
        }
    }
}

#[derive(Debug)]
pub struct Program(pub Box<WithLocation<Expression>>);

#[derive(Debug)]
pub enum UnaryOp {
    Pos,
    Neg,
    Not,
}

#[derive(Debug)]
pub enum BinOp {
    Add,
    Sub,
    Mul,
    Div,
    FloorDiv,
    Eq,
    Neq,
    Gt,
    Lt,
    Ge,
    Le,
    And,
    Or,
}

#[derive(Debug)]
pub enum Expression {
    Let(Vec<WithLocation<VarDec>>, Box<WithLocation<Expression>>),
    UnaryOp(UnaryOp, Box<WithLocation<Expression>>),
    BinOp(BinOp, Box<WithLocation<Expression>>, Box<WithLocation<Expression>>),
    Number(u64),
    String(String),
    Name(String),
    Nil,
}

#[derive(Debug)]
pub struct VarDec {
    pub name: WithLocation<String>,
    pub type_: Option<String>,
    pub value: Box<WithLocation<Expression>>,
}

impl VarDec {
    pub fn new(name: WithLocation<String>, type_: Option<String>,
               value: Box<WithLocation<Expression>>) -> VarDec {
        VarDec {
            name: name,
            type_: type_,
            value: value,
        }
    }
}