summaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: f40c66b60ca89de7de46b8e6c3274f81071ccaab (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright ⓒ 2017 David Li.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

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),
    Call(String, Vec<WithLocation<Expression>>),
    Nil,
}

#[derive(Debug)]
pub struct RecordField {
    pub name: WithLocation<String>,
    pub ty: WithLocation<Ty>,
}

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

#[derive(Debug)]
pub enum Ty {
    Name(String),
    Array(Box<WithLocation<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: Option<WithLocation<Ty>>,
        params: Vec<WithLocation<RecordField>>,
        body: Box<WithLocation<Expression>>,
    },
}

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_,
            }
        }
    }

    pub fn new_fun(name: WithLocation<String>,
                   params: Vec<WithLocation<RecordField>>,
                   type_: Option<WithLocation<Ty>>,
                   body: Box<WithLocation<Expression>>) -> Declaration {
        Declaration {
            name: name,
            declaration: DeclarationBody::Fun {
                ty: type_,
                params: params,
                body: body,
            }
        }
    }
}