-
Notifications
You must be signed in to change notification settings - Fork 1
/
tdop.m
322 lines (258 loc) · 7.46 KB
/
tdop.m
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
%-----------------------------------------------------------------------------%
:- module tdop.
:- interface.
:- import_module list.
:- type ps == list(token).
:- type token
---> name(name)
; integer(string)
; lparen
; rparen
; comma
; equals
; plus
; minus
; star
; slash
; caret
; bang
; question
; colon
; lt
; gt.
:- type name == string.
:- type expr
---> name(name)
; integer(string)
; assign(name, expr)
; cond(expr, expr, expr)
; prefix(prefixop, expr)
; postfix(expr, postfixop)
; infix(expr, infixop, expr)
; call(expr, list(expr)).
:- type prefixop
---> plus
; minus
; bang.
:- type postfixop
---> bang.
:- type infixop
---> plus
; minus
; star
; slash
; caret
; lt
; gt.
:- pred parse_expr(expr::out, ps::in, ps::out) is semidet.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module int.
%-----------------------------------------------------------------------------%
:- type expr_prec == {expr, prec}.
:- type prec == int.
:- func prec_min = prec.
:- func prec_assign = prec.
:- func prec_cond = prec.
:- func prec_compare = prec.
:- func prec_sum = prec.
:- func prec_product = prec.
:- func prec_prefix = prec.
:- func prec_exponent = prec.
:- func prec_postfix = prec.
:- func prec_call = prec.
prec_min = 0. % lowest (non-binding operators)
prec_assign = 10.
prec_cond = 20.
prec_compare = 25.
prec_sum = 30.
prec_product = 40.
prec_prefix = 50.
prec_exponent = 60.
prec_postfix = 70.
prec_call = 80.
:- func lower(prec) = prec.
lower(P) = P-1.
%-----------------------------------------------------------------------------%
:- pred consume(token::out, ps::in, ps::out) is semidet.
consume(Token, [Token | PS], PS).
%-----------------------------------------------------------------------------%
parse_expr(E, !PS) :-
expr(prec_min, E, !PS).
:- pred expr(prec::in, expr::out, ps::in, ps::out) is semidet.
expr(Prec, E, !PS) :-
consume(Token, !PS),
prefix_parser(Token, Parser, PrefixPrec),
Parser(Token, E0, !PS),
expr_loop(Prec, {E0, PrefixPrec}, {E, _}, !PS).
:- pred expr_loop(prec::in, expr_prec::in, expr_prec::out, ps::in, ps::out)
is semidet.
expr_loop(Prec, E0, E, !PS) :-
(
consume(Token, !PS),
infix_parser(Token, Parser, PrecR),
Prec < PrecR
->
Parser(E0, Token, PrecR, E1, !PS),
expr_loop(Prec, {E1, PrecR}, E, !PS)
;
E = E0
).
%-----------------------------------------------------------------------------%
% "nud" (null denotation) - values and prefix operators
:- type prefix_parser == pred(token, expr, ps, ps).
:- inst prefix_parser == (pred(in, out, in, out) is semidet).
:- pred prefix_parser(token, prefix_parser, prec).
:- mode prefix_parser(in, out(prefix_parser), out) is semidet.
prefix_parser(Token, Parser, Prec) :-
(
Token = name(_),
Parser = name,
Prec = prec_min
;
Token = integer(_),
Parser = integer,
Prec = prec_min
;
Token = lparen,
Parser = group,
Prec = prec_min
;
Token = plus,
Parser = prefixop(plus),
Prec = prec_prefix
;
Token = minus,
Parser = prefixop(minus),
Prec = prec_prefix
;
Token = bang,
Parser = prefixop(bang),
Prec = prec_prefix
).
:- pred name `with_type` prefix_parser.
:- mode name `with_inst` prefix_parser.
name(name(N), name(N), !PS).
:- pred integer `with_type` prefix_parser.
:- mode integer `with_inst` prefix_parser.
integer(integer(N), integer(N), !PS).
:- pred group `with_type` prefix_parser.
:- mode group `with_inst` prefix_parser.
group(lparen, E, !PS) :-
parse_expr(E, !PS),
consume(rparen, !PS).
:- pred prefixop(prefixop) `with_type` prefix_parser.
:- mode prefixop(in) `with_inst` prefix_parser.
prefixop(Op, _Token, E, !PS) :-
expr(prec_prefix, E0, !PS),
E = prefix(Op, E0).
%-----------------------------------------------------------------------------%
% "led" (left denotation) - infix and suffix operators
:- type infix_parser == pred(expr_prec, token, prec, expr, ps, ps).
:- inst infix_parser == (pred(in, in, in, out, in, out) is semidet).
:- pred infix_parser(token, infix_parser, prec).
:- mode infix_parser(in, out(infix_parser), out) is semidet.
infix_parser(Token, Parser, Prec) :-
(
Token = equals,
Parser = assign,
Prec = prec_assign
;
Token = question,
Parser = cond,
Prec = prec_cond
;
Token = lt,
Parser = infixnon(lt),
Prec = prec_compare
;
Token = gt,
Parser = infixnon(gt),
Prec = prec_compare
;
Token = plus,
Parser = infixl(plus),
Prec = prec_sum
;
Token = minus,
Parser = infixl(minus),
Prec = prec_sum
;
Token = star,
Parser = infixl(star),
Prec = prec_product
;
Token = slash,
Parser = infixl(slash),
Prec = prec_product
;
Token = caret,
Parser = infixr(caret),
Prec = prec_exponent
;
Token = bang,
Parser = postfix(bang),
Prec = prec_postfix
;
Token = lparen,
Parser = call,
Prec = prec_call
).
:- pred assign `with_type` infix_parser.
:- mode assign `with_inst` infix_parser.
assign({E0, _}, _Token, Prec, E, !PS) :-
E0 = name(Name),
expr(lower(Prec), E1, !PS),
E = assign(Name, E1).
:- pred cond `with_type` infix_parser.
:- mode cond `with_inst` infix_parser.
cond({E0, _}, _Token, _Prec, E, !PS) :-
parse_expr(E1, !PS),
consume(colon, !PS),
parse_expr(E2, !PS),
E = cond(E0, E1, E2).
:- pred infixl(infixop) `with_type` infix_parser.
:- mode infixl(in) `with_inst` infix_parser.
infixl(Op, {E0, _}, _Token, Prec, E, !PS) :-
expr(Prec, E1, !PS),
E = infix(E0, Op, E1).
:- pred infixr(infixop) `with_type` infix_parser.
:- mode infixr(in) `with_inst` infix_parser.
infixr(Op, {E0, _}, _Token, Prec, E, !PS) :-
expr(lower(Prec), E1, !PS),
E = infix(E0, Op, E1).
:- pred infixnon(infixop) `with_type` infix_parser.
:- mode infixnon(in) `with_inst` infix_parser.
infixnon(Op, {E0, Prec0}, _Token, Prec, E, !PS) :-
% Precedence of left expr must not be the same as the operator.
Prec0 \= Prec,
expr(Prec, E1, !PS),
E = infix(E0, Op, E1).
:- pred postfix(postfixop) `with_type` infix_parser.
:- mode postfix(in) `with_inst` infix_parser.
postfix(Op, {E0, _}, _Token, _Prec, E, !PS) :-
E = postfix(E0, Op),
semidet_true.
:- pred call `with_type` infix_parser.
:- mode call `with_inst` infix_parser.
call({E0, _}, _Token, _Prec, E, !PS) :-
( consume(rparen, !PS) ->
Args = []
;
args1(Args, !PS),
consume(rparen, !PS)
),
E = call(E0, Args).
:- pred args1(list(expr), ps, ps).
:- mode args1(out, in, out) is semidet.
args1(Args, !PS) :-
parse_expr(E, !PS),
( consume(comma, !PS) ->
args1(Es, !PS),
Args = [E | Es]
;
Args = [E]
).
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sts=4 sw=4 et