forked from RobertHarper/cmyacc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen-hs.sml
621 lines (557 loc) · 20.5 KB
/
codegen-hs.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
structure CodegenHs
:> CODEGEN
=
struct
structure S = SymbolSet
structure D = SymbolDict
open Automaton
fun appSeparated f g l =
(case l of
[] =>
()
| h :: t =>
(
f h;
app (fn x => (g (); f x)) t
))
(* 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 list of chars representing n
|l| = stateSize
l' = map Char.ord l
and
return l'
*)
fun intToChars size n =
let
val l =
map
(fn w => Word.toInt w)
(wordToBytelist (Word.fromInt n) [])
in
duplicateOnto (size - length l) 0 l
end
fun writeTableEntry write stateSize adjust entry =
app
(fn i => (write "\\"; write (Int.toString i)))
(intToChars stateSize (entry + adjust))
exception Error
fun writeProgram outfile (options, types, terminals, nonterminals : (int list * Symbol.symbol * bool ref) D.dict, actions, automaton as (stateCount, states, rules, start)) =
let
val moduleName =
(case D.find options (Symbol.fromValue "name") of
SOME name => name
| NONE =>
(
print "Error: no module name specified.\n";
raise Error
))
val terminalName =
(case D.find options (Symbol.fromValue "data") of
SOME name => name
| NONE =>
(
print "Error: no terminal type name specified.\n";
raise Error
))
val monadic = D.member options (Symbol.fromValue "monadic")
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 terminalTypes =
let
val set =
D.foldl
(fn (_, (tpo, _, _), set) =>
(case tpo of
SOME tp =>
S.insert set tp
| NONE =>
set))
S.empty
terminals
in
Mergesort.sort String.compare (map Symbol.toValue (S.toList set))
end
val allTypes =
Mergesort.sort String.compare (map Symbol.toValue (S.toList types))
val outs = TextIO.openOut outfile
fun write str = TextIO.output (outs, str)
in
write "{-# LANGUAGE OverloadedStrings";
if monadic then
()
else
write ", FlexibleContexts";
write " #-}\n\n";
write "{-\n\n";
write "module ";
write moduleName;
write " exports:\n\ndata ";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write " =\n";
D.foldl
(fn (symbol, (tpo, _, _), first) =>
(
if first then
write " "
else
write " | ";
write (Symbol.toValue symbol);
(case tpo of
NONE => ()
| SOME tp =>
(
write " ";
write (Symbol.toValue tp)
));
write "\n";
false
))
true
terminals;
write "\n";
write "data Arg stream";
if monadic then
write " monad"
else
();
app (fn tp => (write " "; write tp)) allTypes;
write " =\n Arg { error :: stream (";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write ") ->";
if monadic then
write " monad"
else
();
write " Control.Exception.SomeException,\n\n {- type arguments -}\n";
appSeparated
(fn typeName =>
(
write " ";
write typeName;
write " :: ";
write typeName
))
(fn () => write ",\n")
allTypes;
write "\n\n {- action arguments -}\n";
appSeparated
(fn (actionName, dom, cod) =>
let
val dom' =
Mergesort.sort
(fn ((Syntax.NumericLabel n, _), (Syntax.NumericLabel n', _)) => Int.compare (n, n')
| _ =>
(* Ident labels aren't permitted for Haskell. *)
raise (Fail "invariant"))
dom
in
write " ";
write (Symbol.toValue actionName);
write " :: ";
(* By construction, we have a complete, no-duplicate sequence from 1 to some n. *)
app
(fn (_, tp) =>
(
write (Symbol.toValue tp);
write " -> "
))
dom';
write (Symbol.toValue cod)
end)
(fn () => write ",\n")
actions;
write " }\n\n";
write "parse :: ParseEngine.Streamable stream ";
if monadic then
write "monad "
else
write "Control.Monad.Identity.Identity ";
write "\n => ";
write moduleName;
write ".Arg stream";
if monadic then
write " monad"
else
();
app (fn tp => (write " "; write tp)) allTypes;
write " -> stream (";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write ") -> ";
if monadic then
write "monad "
else
();
write "(";
write (Symbol.toValue (#2 (D.lookup nonterminals start)));
write ", stream (";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write "))";
write "\n\n\n";
WriteAutomaton.writeAutomaton outs automaton;
write "\n-}\n\n";
write "module ";
write moduleName;
write " (";
write moduleName;
write ".";
write terminalName;
write "(..), Arg(..), parse) where {\nimport qualified Array;\nimport qualified Control.Exception;\n";
if monadic then
()
else
write "import qualified Control.Monad.Identity;\n";
write "import qualified Data.ByteString;\nimport qualified Data.ByteString.Char8;\nimport qualified Util.ParseEngine as ParseEngine;\n";
write "data ";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write " =\n";
D.foldl
(fn (symbol, (tpo, _, _), first) =>
(
if first then
()
else
write "| ";
write (Symbol.toValue symbol);
(case tpo of
NONE => ()
| SOME tp =>
(
write " ";
write (Symbol.toValue tp)
));
write "\n";
false
))
true
terminals;
write ";\ndata Arg stream";
if monadic then
write " monad"
else
();
app (fn tp => (write " "; write tp)) allTypes;
write " = Arg { error :: stream (";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write ") ->";
if monadic then
write " monad"
else
();
write " Control.Exception.SomeException";
app
(fn typeName =>
(
write ",\n";
write typeName;
write " :: ";
write typeName
))
allTypes;
app
(fn (actionName, dom, cod) =>
let
val dom' =
Mergesort.sort
(fn ((Syntax.NumericLabel n, _), (Syntax.NumericLabel n', _)) => Int.compare (n, n')
| _ =>
(* Ident labels aren't permitted for Haskell. *)
raise (Fail "invariant"))
dom
in
write ",\n";
write (Symbol.toValue actionName);
write " :: ";
(* By construction, we have a complete, no-duplicate sequence from 1 to some n. *)
app
(fn (_, tp) =>
(
write (Symbol.toValue tp);
write " -> "
))
dom';
write (Symbol.toValue cod)
end)
actions;
write " }\n;\n";
write "data VVV";
app (fn tp => (write " "; write tp)) allTypes;
write " =\nVVV\n";
S.app
(fn tp =>
(
write "| VVV";
write (Symbol.toValue tp);
write " ";
write (Symbol.toValue tp);
write "\n"
))
types;
write ";\nterminal :: (";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write ") -> (Int, VVV";
app (fn tp => (write " "; write tp)) allTypes;
write ");\n";
write "terminal t = case t of {";
D.foldl
(fn (terminal, (tpo, _, _), first) =>
(
if first then
write "\n"
else
write ";\n";
(case tpo of
NONE =>
(
write (Symbol.toValue terminal);
write " -> (";
write (Int.toString (D.lookup terminalOrdinals terminal));
write ", VVV)"
)
| SOME tp =>
(
write (Symbol.toValue terminal);
write " x -> (";
write (Int.toString (D.lookup terminalOrdinals terminal));
write ", VVV";
write (Symbol.toValue tp);
write " x)"
));
false
))
true
terminals;
write "\n}\n;\n";
write "parse :: ParseEngine.Streamable stream ";
if monadic then
write "monad "
else
write "Control.Monad.Identity.Identity ";
write "=> ";
write moduleName;
write ".Arg stream";
if monadic then
write " monad"
else
();
app (fn tp => (write " "; write tp)) allTypes;
write " -> stream (";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write ") -> ";
if monadic then
write "monad "
else
();
write "(";
write (Symbol.toValue (#2 (D.lookup nonterminals start)));
write ", stream (";
write terminalName;
app (fn tp => (write " "; write tp)) terminalTypes;
write "));\n";
write "parse arg s = ";
if monadic then
()
else
write "Control.Monad.Identity.runIdentity $ ";
write "ParseEngine.parse (terminal, ParseEngine.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 "\",\nArray.listArray (0, ";
write (Int.toString (Vector.length rules - 1));
write ") [";
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 ",(\\ (";
let
val len =
ListPair.foldrEq
(fn (_, NONE, n) =>
(
write "_:";
n
)
| (symbol, SOME _, n) =>
let
val tp =
(case D.find nonterminals symbol of
SOME (_, tp, _) => tp
| NONE =>
valOf (#1 (D.lookup terminals symbol)))
in
write "VVV";
write (Symbol.toValue tp);
write "(arg";
write (Int.toString n);
write "):";
n+1
end)
0
(rhs, args)
val permutation = Array.array (len, ~1)
in
write "rest) -> VVV";
write (Symbol.toValue (#2 (D.lookup nonterminals lhs)));
write "(Test.";
write (Symbol.toValue action);
write " arg";
(* By construction, args is a complete, no-duplicate sequence from 1 to some n. *)
foldr
(fn (NONE, n) => n
| (SOME (Syntax.NumericLabel i), n) =>
(
Array.update (permutation, i-1, n);
n+1
)
| (SOME (Syntax.IdentLabel _), _) =>
(* Not allowed in Haskell. *)
raise (Fail "invariant"))
0
args;
Array.app
(fn n =>
(
write " arg";
write (Int.toString n)
))
permutation
end;
write "):rest))";
false
))
true
rules;
write "],\n(\\ (VVV";
write (Symbol.toValue (#2 (D.lookup nonterminals start)));
write " x) -> x), ";
if monadic then
write "Test.error arg"
else
write "return . Test.error arg";
write ") s;\n";
write "}\n";
TextIO.closeOut outs
end
end