-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.sml
437 lines (380 loc) · 14.8 KB
/
codegen.sml
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
structure Codegen
:> CODEGEN
=
struct
structure S = SymbolSet
structure D = SymbolDict
open Automaton
exception Error
fun labelToString l =
(case l of
Syntax.IdentLabel s =>
Symbol.toValue s
| Syntax.NumericLabel n =>
Int.toString n)
fun isUnit dom = List.null dom
fun isSolearg dom =
(case dom of
[(Syntax.NumericLabel _, _)] => true
| _ => false)
fun isTuple dom =
(case dom of
(Syntax.NumericLabel _, _) :: _ :: _ => true
| _ => false)
(* Converts a word to a big-endian byte list. *)
fun wordToBytelist w acc =
if w = 0w0 then
acc
else
let
val lo = Word.andb (w, 0wxff)
val hi = Word.>> (w, 0w8)
in
wordToBytelist hi (lo :: acc)
end
fun duplicateOnto n x acc =
if n = 0 then
acc
else
duplicateOnto (n-1) x (x :: acc)
(* intToChars size n
if 0 <= n < 2^(8 * size)
then l is a big-endian character list representing n
|l| = stateSize
and
return l
*)
fun intToChars size n =
let
val l =
map
(fn w => Char.chr (Word.toInt w))
(wordToBytelist (Word.fromInt n) [])
in
duplicateOnto (size - length l) (Char.chr 0) l
end
fun writeTableEntry write stateSize adjust entry =
app
(fn ch => write (Char.toString ch))
(intToChars stateSize (entry + adjust))
fun writeProgram outfile (options, types, terminals, nonterminals, actions, automaton as (stateCount, states, rules, start)) =
let
val functorName =
(case D.find options (Symbol.fromValue "name") of
SOME name => name
| NONE =>
(
print "Error: no functor name specified.\n";
raise Error
))
val (terminalOrdinals, terminalCount) =
D.foldl
(fn (terminal, _, (ordinals, count)) =>
(D.insert ordinals terminal count,
count+1))
(D.singleton (Symbol.fromValue "$") 0, 1)
terminals
val (nonterminalOrdinals, nonterminalCount) =
D.foldl
(fn (nonterminal, _, (ordinals, count)) =>
(D.insert ordinals nonterminal count,
count+1))
(D.empty, 0)
nonterminals
val minorLimit = Int.max (terminalCount, nonterminalCount)
val (minorLimit', minorSize) =
if minorLimit <= 32 then
(32, "5")
else if minorLimit <= 64 then
(64, "6")
else if minorLimit <= 128 then
(128, "7")
else if minorLimit <= 256 then
(256, "8")
else if minorLimit <= 512 then
(512, "9")
else if minorLimit = D.size terminals then
(
print "Error: too many terminals.\n";
raise Error
)
else
(
print "Error: too many nonterminals.\n";
raise Error
)
val majorLimit =
Int.max (stateCount, Vector.length rules + 1)
val (majorSize, adjust) =
if majorLimit <= 127 then
(1, 128)
else if majorLimit <= 32767 then
(2, 32768)
else if majorLimit = stateCount then
(
print "Error: too many states.\n";
raise Error
)
else
(
print "Error: too many rules.\n";
raise Error
)
val outs = TextIO.openOut outfile
fun write str = TextIO.output (outs, str)
in
write "\nfunctor ";
write functorName;
write "\n (structure Streamable : STREAMABLE\n structure Arg :\n sig\n";
S.app
(fn tp =>
(
write " type ";
write (Symbol.toValue tp);
write "\n"
))
types;
write "\n";
app
(fn (action, dom, cod) =>
(
write " val ";
write (Symbol.toValue action);
write " : ";
if isUnit dom then
write "unit"
else if isSolearg dom then
write (Symbol.toValue (#2 (hd dom)))
else if isTuple dom then
(
foldl
(fn ((_, t), first) =>
(* Ignore the label, because we know the list is sorted and complete. *)
(
if first then
()
else
write " * ";
write (Symbol.toValue t);
false
))
true
dom;
()
)
else
(
write "{ ";
foldl
(fn ((l, t), first) =>
(
if first then
()
else
write ", ";
write (labelToString l);
write ":";
write (Symbol.toValue t);
false
))
true
dom;
write " }"
);
write " -> ";
write (Symbol.toValue cod);
write "\n"
))
actions;
write "\n datatype terminal =\n";
D.foldl
(fn (symbol, (tpo, _, _), first) =>
(
write " ";
if first then
write " "
else
write "| ";
write (Symbol.toValue symbol);
(case tpo of
NONE => ()
| SOME tp =>
(
write " of ";
write (Symbol.toValue tp)
));
write "\n";
false
))
true
terminals;
write "\n val error : terminal Streamable.t -> exn\n end)\n :>\n sig\n val parse : Arg.terminal Streamable.t -> Arg.";
write (Symbol.toValue (#2 (D.lookup nonterminals start)));
write " * Arg.terminal Streamable.t\n end\n=\n\n";
write "(*\n\n";
WriteAutomaton.writeAutomaton outs automaton;
write "\n*)\n\n";
write "struct\nlocal\nstructure Value = struct\ndatatype nonterminal =\nnonterminal\n";
S.app
(fn tp =>
(
write "| ";
write (Symbol.toValue tp);
write " of Arg.";
write (Symbol.toValue tp);
write "\n"
))
types;
write "end\nstructure ParseEngine = ParseEngineFun (structure Streamable = Streamable\ntype terminal = Arg.terminal\ntype value = Value.nonterminal\nval dummy = Value.nonterminal\nfun read terminal =\n(case terminal of\n";
D.foldl
(fn (terminal, (tpo, _, _), first) =>
(
if first then
()
else
write "| ";
(case tpo of
NONE =>
(
write "Arg.";
write (Symbol.toValue terminal);
write " => (";
write (Int.toString (D.lookup terminalOrdinals terminal));
write ", Value.nonterminal)\n"
)
| SOME tp =>
(
write "Arg.";
write (Symbol.toValue terminal);
write " x => (";
write (Int.toString (D.lookup terminalOrdinals terminal));
write ", Value.";
write (Symbol.toValue tp);
write " x)\n"
));
false
))
true
terminals;
write ")\n)\nin\nval parse = ParseEngine.parse (\nParseEngine.next";
write minorSize;
write "x";
write (Int.toString majorSize);
write " \"";
app
(fn (d, _, _) =>
let
val arr =
(* initialize with 0, which represents error *)
Array.array (minorLimit', 0)
in
D.app
(fn (terminal, (Shift n :: _, _)) =>
Array.update (arr, D.lookup terminalOrdinals terminal, n+1)
| (terminal, (Reduce n :: _, _)) =>
(* When n = ~1, this is the accept action. *)
Array.update (arr, D.lookup terminalOrdinals terminal, ~(n+2))
| _ =>
raise (Fail "invariant"))
d;
Array.app (writeTableEntry write majorSize adjust) arr
end)
states;
write "\",\nParseEngine.next";
write minorSize;
write "x";
write (Int.toString majorSize);
write " \"";
app
(fn (_, d, _) =>
let
val arr =
Array.array (minorLimit', 0)
in
D.app
(fn (nonterminal, n) =>
Array.update (arr, D.lookup nonterminalOrdinals nonterminal, n))
d;
Array.app (writeTableEntry write majorSize adjust) arr
end)
states;
write "\",\nVector.fromList [";
Vector.foldl
(fn ((rulenum, _, lhs, rhs, args, solearg, action, _, _), first) =>
(
if first then
()
else
write ",\n";
write "(";
write (Int.toString (D.lookup nonterminalOrdinals lhs));
write ",";
write (Int.toString (length rhs));
write ",(fn ";
ListPair.foldrEq
(fn (_, NONE, n) =>
(
write "_::";
n
)
| (symbol, SOME label, n) =>
let
val tp =
(case D.find nonterminals symbol of
SOME (_, tp, _) => tp
| NONE =>
valOf (#1 (D.lookup terminals symbol)))
in
write "Value.";
write (Symbol.toValue tp);
write "(arg";
write (Int.toString n);
write ")::";
n+1
end)
0
(rhs, args);
write "rest => Value.";
write (Symbol.toValue (#2 (D.lookup nonterminals lhs)));
write "(Arg.";
write (Symbol.toValue action);
write " ";
(* If solearg, we suppress generating a record pattern. *)
if solearg then
write "arg0"
else
(
write "{";
foldr
(fn (NONE, n) => n
| (SOME label, n) =>
(
if n = 0 then
()
else
write ",";
write (labelToString label);
write "=arg";
write (Int.toString n);
n+1
))
0
args;
write "}"
);
write ")::rest";
if List.null rhs then
()
else
write "|_=>raise (Fail \"bad parser\")";
write "))";
false
))
true
rules;
write "],\n(fn Value.";
write (Symbol.toValue (#2 (D.lookup nonterminals start)));
write " x => x | _ => raise (Fail \"bad parser\")), Arg.error)\nend\nend\n";
TextIO.closeOut outs
end
end