summaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: d9ce4064058342c18223e48c44d3b22744b0428c (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
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
use std::ops::Deref;

#[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,
        }
    }
}

impl<T> Deref for WithLocation<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

#[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<Declaration>>, 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 RecordField {
    name: WithLocation<String>,
    ty: WithLocation<String>,
}

impl RecordField {
    pub fn new(name: WithLocation<String>, ty: WithLocation<String>) -> RecordField {
        RecordField { name, ty }
    }
}

#[derive(Debug)]
pub enum Ty {
    Name(String),
    Array(Box<Ty>),
    Record(Vec<WithLocation<RecordField>>),
}

#[derive(Debug)]
pub struct Declaration {
    pub name: WithLocation<String>,
    pub declaration: DeclarationBody,
}

#[derive(Debug)]
pub enum DeclarationBody {
    Var {
        ty: Option<WithLocation<Ty>>,
        value: Box<WithLocation<Expression>>,
    },
    Ty {
        ty: WithLocation<Ty>,
    },
    Fun {
        ty: Box<WithLocation<Ty>>,
    },
}

impl Declaration {
    pub fn new_var(name: WithLocation<String>, type_: Option<WithLocation<Ty>>,
                   value: Box<WithLocation<Expression>>) -> Declaration {
        Declaration {
            name: name,
            declaration: DeclarationBody::Var {
                ty: type_,
                value: value,
            }
        }
    }

    pub fn new_ty(name: WithLocation<String>, type_: WithLocation<Ty>) -> Declaration {
        Declaration {
            name: name,
            declaration: DeclarationBody::Ty {
                ty: type_,
            }
        }
    }
}