This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.rs
172 lines (133 loc) · 3.34 KB
/
ast.rs
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
168
169
170
171
172
#[deriving(Eq)]
pub struct Sym(@str);
#[deriving(Eq)]
pub struct RecordName(@DottedName, @str);
impl to_str::ToStr for RecordName {
fn to_str(&self) -> ~str {
match *self {
RecordName(@DottedName([]), sym) => sym.to_str(),
RecordName(dn, sym) => fmt!("%s.%s", dn.to_str(), sym.to_str())
}
}
}
#[deriving(Eq)]
pub struct DottedName(@[Sym]);
impl to_str::ToStr for DottedName {
fn to_str(&self) -> ~str {
match *self {
DottedName(syms) => str::connect(syms.map(|sym| sym.to_str()), ~".")
}
}
}
#[deriving(Eq)]
pub enum Lit {
IntegerLiteral(@str),
FloatingLiteral(@str),
StringLiteral(@str),
BytesLiteral(@str)
}
pub enum Pat {
// _
AnyPattern,
// ...
ManyPattern,
// A(a, b, c, ...)
RecordPattern(RecordName, @[Pat]),
// a | b
DisjunctivePattern(@Pat, @Pat),
// a & b
ConjunctivePattern(@Pat, @Pat),
// [a, b, c, ...]
ListPattern(@[Pat]),
// a as a'
BoundPattern(Sym, @Pat),
// some kind of literal
LiteralPattern(Lit),
// a
SymbolPattern(Sym),
// ()
UnitPattern
}
pub impl Pat {
fn specificity(&self) -> uint {
match *self {
AnyPattern => 0,
ManyPattern => 0,
RecordPattern(_, pats) => pats.map(|pat| pat.specificity()).foldr(0, |x, acc| x + acc),
DisjunctivePattern(l, r) => uint::min(l.specificity(), r.specificity()),
ConjunctivePattern(l, r) => uint::max(l.specificity(), r.specificity()),
ListPattern(pats) => pats.map(|pat| pat.specificity()).foldr(0, |x, acc| x + acc),
BoundPattern(_, pat) => pat.specificity(),
LiteralPattern(_) => 1,
SymbolPattern(_) => 1,
UnitPattern => 1
}
}
}
pub struct Exp {
exp: BareExp,
lineno: uint
}
pub enum BareExp {
// if a then b [ else c ]
IfThenElseExpression(@Exp, @Exp, option::Option<@Exp>),
// while a do b
WhileDoExpression(@Exp, @Exp),
// match a with case b' -> b case c' -> c
MatchWithExpression(@Exp, @[(Pat, Exp)]),
// a OP b
BinaryExpression(@Exp, @str, @Exp),
// some kind of literal
LiteralExpression(Lit),
// { a; b; c } or { a; b; c; } if boolean field is true
BlockExpression(@[Stmt], bool),
// a(b, c, d, ...)
CallExpression(@Exp, @[Exp]),
// [a, b, c, d]
ListExpression(@[Exp]),
// fn (a) -> b
LambdaExpression(@[Pat], @Exp),
// a.b
AccessExpression(@Exp, Sym),
// a:b
FunctionBindExpression(@Exp, Sym),
// a
SymbolExpression(Sym),
// a = b
AssignmentExpression(@Exp, @Exp),
// ()
UnitExpression
}
pub struct Stmt {
stmt: BareStmt,
lineno: uint
}
pub enum BareStmt {
// let a = b
LetBindingStatement(Pat, Exp),
// fn f(a) = b
FnBindingStatement(DottedName, Sym, @[Pat], Exp),
// ...
ExpressionStatement(Exp)
}
pub fn mk_expression_statement(exp: Exp) -> Stmt {
Stmt {
stmt: ExpressionStatement(exp),
lineno: exp.lineno
}
}
pub struct ImportDeclaration {
module: DottedName,
qualified: bool,
lineno: uint
}
pub struct RecordDeclaration {
name: RecordName,
slots: @[Sym],
lineno: uint
}
pub struct Program {
imports: @[ImportDeclaration],
records: @[RecordDeclaration],
body: @[Stmt]
}