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

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

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

    pub fn join_map<U, V, F>(self, other: WithLocation<U>, f: F) -> WithLocation<V>
        where F: FnOnce(WithLocation<T>, WithLocation<U>) -> V
    {
        let loc1 = self.location;
        let loc2 = other.location;
        WithLocation {
            value: f(self, other),
            location: (loc1.0, loc2.1),
        }
    }
}

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

#[derive(Debug)]
pub enum BinOp {
    Add,
    Sub,
    Mul,
    Div,
    FloorDiv,
}

#[derive(Debug)]
pub enum Expression {
    BinOp(BinOp, Box<WithLocation<Expression>>, Box<WithLocation<Expression>>),
    Number(u64),
    Nil,
}