-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
171 lines (147 loc) · 2.83 KB
/
parser.mly
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
%{ (* Emacs, use -*- tuareg -*- to open this file. *)
open Source
let parsing_error pos msg =
Printf.eprintf "%s:\n %s\n" (Position.string_of_pos pos) msg;
exit 1
let var_or_primitive = function
| Id "cos" -> Primitive Cos
| Id "sin" -> Primitive Sin
| Id "exp" -> Primitive Exp
| Id "inv" -> Primitive Inv
| Id "neg" -> Primitive Neg
| x -> Var x
let last xs = List.(hd (rev xs))
let rec tuple = function
| [] -> assert false (* By syntax. *)
| [x] -> x
| x :: xs -> Position.(
let pos = join (position x) (position (last xs)) in
with_pos pos (Pair (tuple xs, x))
)
let tuple xs = tuple (List.rev xs)
%}
%token EOF
%token FUN LET IN FLOAT FST SND
%token ARROW LPAREN RPAREN COLON EQUAL COMMA
%token TIMES PLUS MINUS
%token<string> ID
%token<float> LFLOAT
%type<Source.term'> term
%start<Source.program_with_locations> program
%right ARROW IN
%left PLUS
%left TIMES
%%
program: a=definition* EOF
{
a
}
| error {
parsing_error (Position.lex_join $startpos $endpos) "Syntax error."
}
term: a=abstraction {
a
}
| d=definition IN t=located(term)
{
make_let (fst d) (snd d) t
}
| lhs=located(term) b=located(binop) rhs=located(term)
{
let with_pos = Position.(with_pos (join (position lhs) (position b))) in
let b = Position.(with_pos (position b) (Primitive (value b))) in
App (with_pos (App (b, lhs)), rhs)
}
| t=simple_term
{
t
}
simple_term:
a=located(simple_term) b=located(atomic_term)
{
App (a, b)
}
| FST a=located(atomic_term)
{
Fst a
}
| SND a=located(atomic_term)
{
Snd a
}
| m=located(MINUS) t=located(atomic_term)
{
let with_pos = Position.(with_pos (position m)) in
App (with_pos (Primitive Neg), t)
}
| a=atomic_term
{
a
}
atomic_term:
x=identifier {
var_or_primitive x
}
| l=literal {
Literal l
}
| LPAREN ts=separated_nonempty_list(COMMA, located (term)) RPAREN {
Position.value (tuple ts)
}
literal:
f=LFLOAT
{
Float f
}
%inline abstraction: FUN bs=located(binding)+ ARROW t=located(term)
{
make_lambda_abstraction bs t
}
%inline definition: LET b=located(binding) EQUAL t=located(term)
{
(b, t)
}
| LET x=located(identifier) arg=located(binding)
COLON oty=typ
EQUAL t=located(term)
{
let pos = Position.position x in
let x = Position.value x in
let b = Position.with_pos pos (x, TyArrow (snd (Position.value arg), oty)) in
(b, Position.(with_pos (position t) (make_lambda_abstraction [arg] t)))
}
%inline binop :
| PLUS { Add }
| TIMES { Mul }
binding: LPAREN x=identifier COLON ty=typ RPAREN
{
(x, ty)
}
identifier: x=ID
{
Id x
}
typ: x=type_constant
{
TyConstant x
}
| lhs=typ ARROW rhs=typ
{
TyArrow (lhs, rhs)
}
| lhs=typ TIMES rhs=typ
{
TyPair (lhs, rhs)
}
| LPAREN t=typ RPAREN
{
t
}
type_constant:
FLOAT
{
TyFloat
}
%inline located(X): x=X {
Position.with_poss $startpos $endpos x
}