Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-antlr/BackEnds/SML/pp.sml

raw
1 (* pp.sml
2 *
3 * COPYRIGHT (c) 2018 The Fellowship of SML/NJ (http://www.smlnj.org)
4 * All rights reserved.
5 *)
6
7 structure MLPP =
8 struct
9 structure PP = TextIOPP
10 structure ABS = MLABS
11
12 fun ppAtom (ppStrm, a) = PP.string ppStrm (Atom.toString a)
13 fun ppList {lb, rb, sep, item} (ppStrm, l) = let
14 fun pp [] = ()
15 | pp [x] = item(ppStrm, x)
16 | pp (x::r) = (item(ppStrm, x); sep ppStrm; pp r)
17 in
18 lb ppStrm; pp l; rb ppStrm
19 end
20
21 fun ppAtomList (ppStrm, als) = ppList {
22 lb = fn ppStrm => (PP.openVBox ppStrm (PP.Abs 0);
23 PP.string ppStrm "("),
24 rb = fn ppStrm => (PP.closeBox ppStrm; PP.string ppStrm ")"),
25 sep = fn ppStrm => (PP.string ppStrm ", "),
26 item = fn (ppStrm, a) => ppAtom (ppStrm, a)
27 } (ppStrm, als)
28
29 fun ppNullaryConsList(ppStrm, als) = ppList {
30 lb = fn ppStrm => (PP.openVBox ppStrm (PP.Abs 0)),
31 rb = fn ppStrm => (PP.closeBox ppStrm),
32 sep = fn ppStrm => (PP.string ppStrm ", "),
33 item = fn (ppStrm, a) => ppAtom (ppStrm, a)
34 } (ppStrm, als)
35
36 fun ppGuid (ppStrm, guid) = PP.string ppStrm (concat["(\"", guid, "\")"])
37
38 fun ppStringLit (ppStrm, s) = PP.string ppStrm (concat [
39 "(\"", String.toCString s, "\")"
40 ])
41
42 fun ppTyFormals(ppStrm, als) = ppList {
43 lb = fn ppStrm => (PP.openVBox ppStrm (PP.Abs 0);
44 PP.string ppStrm "["),
45 rb = fn ppStrm => (PP.closeBox ppStrm; PP.string ppStrm "] "),
46 sep = fn ppStrm => (PP.string ppStrm ", "),
47 item = fn (ppStrm, a) => ppAtom (ppStrm, a)
48 } (ppStrm, als)
49
50 fun ppLongUid (ppStrm, uids) = ppList {
51 lb = fn ppStrm => (PP.openVBox ppStrm (PP.Abs 0)),
52 rb = fn ppStrm => (PP.closeBox ppStrm),
53 sep = fn ppStrm => (PP.string ppStrm "."),
54 item = fn (ppStrm, uid) => ppAtom (ppStrm, uid)
55 } (ppStrm, uids)
56
57
58 fun ppAnd f (ppStrm, (a::alist))
59 = ppList {lb= fn ppStrm => (),
60 rb= fn ppStrm => (),
61 sep= fn ppStrm => (PP.string ppStrm "and "),
62 item = f} (ppStrm, a::alist)
63 | ppAnd _ _ = raise Fail "empty list"
64
65 fun ppBar f (ppStrm, (a::alist))
66 = ppList {lb= fn ppStrm => (),
67 rb= fn ppStrm => (),
68 sep= fn ppStrm => (PP.string ppStrm "| "),
69 item = f} (ppStrm, a::alist)
70 | ppBar _ _ = raise Fail "empty list"
71 (*
72 fun ppConst (ppStrm, const)
73 = case const of
74 ABS.C_INT i => PP.string ppStrm (Int.toString i)
75 | ABS.C_REAL r => PP.string ppStrm (Real.toString r)
76 | ABS.C_STRING s => PP.string ppStrm s
77
78 fun ppBoolop (ppStrm, ABS.B_AND)
79 = PP.string ppStrm "andalso"
80 | ppBoolop (ppStrm, ABS.B_OR)
81 = PP.string ppStrm "orelse"
82 *)
83 fun ppType (ppStrm, ty)
84 = case ty of
85 ABS.T_VAR var => ppAtom (ppStrm, var)
86 | ABS.T_TUPLE tys_nonempty
87 => ppList {lb= fn ppStrm => (),
88 rb= fn ppStrm => (),
89 sep= fn ppStrm => (PP.string ppStrm "*"),
90 item = ppType} (ppStrm, tys_nonempty)
91 | ABS.T_FUN (t1, t2) => (ppType (ppStrm, t1);
92 PP.string ppStrm "->";
93 ppType (ppStrm, t2))
94 | ABS.T_RECORD fields
95 => let fun ppField (ppStrm, (labl, ty))
96 = (ppAtom (ppStrm, labl);
97 PP.string ppStrm ":";
98 ppType (ppStrm, ty))
99 in
100 ppList{lb= fn ppStrm => (PP.string ppStrm "{"),
101 rb= fn ppStrm => (PP.string ppStrm "}"),
102 sep= fn ppStrm => (PP.string ppStrm ", "),
103 item = ppField
104 } (ppStrm, fields)
105 end
106 | ABS.T_PAREN t => (PP.string ppStrm "(";
107 ppType (ppStrm, t);
108 PP.string ppStrm ")")
109
110 fun ppTypeBinding (ppStrm, ABS.TypeBind andlist)
111 = let fun try (ppStrm, (varlist, Type))
112 = (ppAtomList (ppStrm, varlist);
113 PP.string ppStrm "=";
114 ppType (ppStrm, Type))
115 in
116 ppAnd try (ppStrm, andlist)
117 end
118
119 fun ppDatatypeBinding (ppStrm, ABS.DatatypeBind andlist)
120 = let fun trybar (ppStrm, (id, ty))
121 = (ppAtom (ppStrm, id);
122 case ty
123 of NONE => ()
124 | SOME ty => (PP.string ppStrm " of ";
125 ppType (ppStrm, ty)))
126
127 fun try (ppStrm, (varlist, barlist))
128 = (ppAtomList (ppStrm, varlist);
129 PP.string ppStrm "=";
130 ppBar trybar (ppStrm, barlist))
131 in
132 ppAnd try (ppStrm, andlist)
133 end
134
135 fun ppSimpleDecl (ppStrm, simple_decl)
136 = case simple_decl
137 of ABS.D_VAL val_andlist
138 => let
139 fun ppVal (ppStrm, (pattern, exp))
140 = (ppPat (ppStrm, pattern);
141 PP.string ppStrm "=";
142 ppExp (ppStrm, exp))
143 in
144 PP.string ppStrm "val ";
145 ppAnd ppVal (ppStrm, val_andlist)
146 end
147 | ABS.D_FUN fun_andlist
148 => (PP.string ppStrm "fun ";
149 ppAnd ppFunDecl (ppStrm, fun_andlist))
150 | ABS.D_TYPE typebinding
151 => (PP.string ppStrm "type ";
152 ppTypeBinding (ppStrm, typebinding))
153 | ABS.D_ABS_DATATYPE (dataTBind, TBindOp)
154 => (PP.string ppStrm "datatype ";
155 ppDatatypeBinding (ppStrm, dataTBind);
156 case TBindOp
157 of NONE => ()
158 | SOME b => (PP.string ppStrm "withtype ";
159 ppTypeBinding (ppStrm, b)))
160 | ABS.D_ABS_TYPE (dataTBind, TBindOp, decl)
161 => (PP.string ppStrm "abstype ";
162 ppDatatypeBinding (ppStrm, dataTBind);
163 case TBindOp
164 of NONE => ()
165 | SOME b => (PP.string ppStrm "withtype ";
166 ppTypeBinding (ppStrm, b));
167 PP.string ppStrm "with ";
168 ppDecl (ppStrm, decl);
169 PP.string ppStrm "end")
170
171 and ppDecl (ppStrm, dlist_nonempty)
172 = ppList {lb= fn ppStrm => (),
173 rb= fn ppStrm => (),
174 sep= fn ppStrm => (),
175 item = ppSimpleDecl} (ppStrm, dlist_nonempty)
176
177
178 and ppMatch (ppStrm, matchlist)
179 = let fun try (ppStrm, (pattern, exp))
180 = (ppPat (ppStrm, pattern);
181 PP.string ppStrm "=> ";
182 ppExp (ppStrm, exp))
183 in ppBar try (ppStrm, matchlist)
184 end
185
186 and ppFunDecl (ppStrm, ABS.FUN barlist)
187 = let fun trybar (ppStrm, (fun_heading, tyop, exp))
188 = (ppFunHeading (ppStrm, fun_heading);
189 case tyop of
190 NONE => ()
191 | SOME ty => (PP.string ppStrm ": ";
192 ppType (ppStrm, ty));
193 ppExp (ppStrm, exp))
194 in
195 PP.string ppStrm "fun ";
196 ppBar trybar (ppStrm, barlist)
197 end
198
199 and ppFunHeading (ppStrm, ABS.FUNHEAD (s, patlist_nonempty))
200 = (ppAtom(ppStrm, s);
201 ppList {lb= fn ppStrm => (),
202 rb= fn ppStrm => (),
203 sep= fn ppStrm => (PP.string ppStrm " "),
204 item = ppPat} (ppStrm, patlist_nonempty))
205
206 and ppSigna (ppStrm, ABS.SG s)
207 (* fix this *)
208 = case s of
209 ABS.SP_EMPTY => ()
210
211 and ppStruc (ppStrm, ABS.ST_STRUCT object_decl)
212 = (PP.string ppStrm "struct";
213 ppObject (ppStrm, object_decl);
214 PP.string ppStrm "end")
215
216 and ppObject (ppStrm, object_decl)
217 = case object_decl
218 of ABS.DECL r => ppDecl (ppStrm, r)
219 | ABS.STRUCTURE (SOME sg, st)
220 => (PP.string ppStrm "structure :sig";
221 ppSigna (ppStrm, sg);
222 PP.string ppStrm "end = ";
223 ppStruc (ppStrm, st))
224 | ABS.STRUCTURE (NONE, st)
225 => (PP.string ppStrm "structure ";
226 ppStruc (ppStrm, st))
227 | ABS.LOCAL (o1, o2)
228 => (PP.string ppStrm "local";
229 ppObject (ppStrm, o1);
230 PP.string ppStrm "in";
231 ppObject (ppStrm, o2);
232 PP.string ppStrm "end")
233 (* end case *)
234
235
236 and ppFile (ppStrm, ABS.OBJECT r) = (
237 PP.openHBox ppStrm;
238 ppObject (ppStrm, r);
239 PP.closeBox ppStrm)
240
241 and ppExp (ppStrm, e)
242 = let
243 fun str s = PP.string ppStrm s
244 fun sp () = PP.space ppStrm 1
245 fun nl () = PP.newline ppStrm
246 fun hbox () = PP.openHBox ppStrm
247 fun vbox () = PP.openVBox ppStrm (PP.Abs 2)
248 fun close () = PP.closeBox ppStrm
249 fun letBody (true, pp)
250 = (nl();
251 str "in";
252 vbox(); nl(); pp(); close();
253 nl();
254 str "end")
255 | letBody (false, pp) = pp()
256 fun ppE (inLet, prevFn, e)
257 = (case e
258 of (ABS.ML_Var x) => letBody(inLet, fn () => str x)
259 | (ABS.ML_Int n) => letBody(inLet, fn () => str(IntInf.toString n))
260 | (ABS.ML_Cmp (cop, e1, e2))
261 => letBody(inLet,
262 fn ()
263 => (
264 ppExp' e1;
265 sp();
266 str (case cop
267 of ABS.LT => "<"
268 | ABS.GT => ">"
269 | ABS.EQ => "="
270 | ABS.LEQ => "<="
271 | ABS.GEQ => ">=");
272 sp();
273 ppExp' e2))
274 | (ABS.ML_Bool (bop, e1, e2))
275 => letBody(inLet,
276 fn () => (
277 ppExp' e1;
278 sp();
279 str (case bop
280 of ABS.AND => "andalso"
281 | ABS.OR => "orelse");
282 sp();
283 ppExp' e2))
284 | (ABS.ML_Case(arg, pl)) =>
285 letBody(inLet,
286 fn () => (
287 hbox();
288 str "(case"; sp(); str "("; ppExp' arg; str ")";
289 close();
290 doCases (false, true, pl);
291 nl(); str "(* end case *))"))
292 | (ABS.ML_App(f, args)) =>
293 letBody(inLet,
294 fn () => (
295 hbox();
296 str f; str "(";
297 case args
298 of [] => ()
299 | [e] => ppExp' e
300 | (e::r) => (
301 ppExp' e; app (fn e => (str ","; sp(); ppExp' e)) r)
302 (* end case *);
303 str ")";
304 close()))
305 | (ABS.ML_If(e1, e2, e3 as ABS.ML_If _)) =>
306 letBody(inLet,
307 fn () => (
308 PP.openVBox ppStrm (PP.Abs 0);
309 vbox();
310 hbox(); str "if"; sp(); ppExp' e1; close(); nl();
311 hbox(); str "then"; sp();
312 vbox(); ppExp' e2; close();
313 close();
314 close(); nl();
315 hbox(); str "else"; sp();
316 ppExp' e3;
317 close();
318 close()))
319 | (ABS.ML_If(e1, e2, e3)) =>
320 letBody(inLet,
321 fn () => (
322 vbox();
323 hbox(); str "if"; sp(); ppExp' e1; close(); nl();
324 hbox(); str "then"; sp();
325 vbox(); ppExp' e2; close();
326 close(); nl();
327 hbox(); str "else"; sp();
328 vbox(); ppExp' e3; close();
329 close();
330 close()))
331 | (ABS.ML_Let(x, e1, e2)) => let
332 fun pp () = (
333 nl();
334 hbox();
335 str "val"; sp(); str x; sp(); str "="; sp();
336 ppExp' e1;
337 close();
338 ppE (true, false, e2))
339 in
340 if inLet
341 then pp()
342 else (
343 str "let";
344 PP.openVBox ppStrm (PP.Abs 0);
345 pp();
346 close())
347 end
348 | (ABS.ML_Funs([], e)) =>
349 ppE (inLet, false, e)
350 | (ABS.ML_Funs((f, params, body)::fs, e)) => let
351 fun pp prefix = (
352 nl();
353 hbox();
354 str prefix; sp(); str f; sp();
355 str "(";
356 case params
357 of [] => ()
358 | [x] => str x
359 | (x::r) => (
360 str x; app (fn x => (str ","; sp(); str x)) r)
361 (* end case *);
362 str ")"; sp(); str "="; sp();
363 PP.openVBox ppStrm (PP.Abs 6);
364 ppExp' body;
365 close();
366 close();
367 ppE (true, true, ABS.ML_Funs(fs, e)))
368 in
369 if inLet
370 then if prevFn then pp "and" else pp "fun"
371 else (
372 PP.openVBox ppStrm (PP.Abs 0);
373 str "let";
374 pp "fun";
375 close())
376 end
377 | (ABS.ML_Seq[]) => letBody(inLet, fn () => str "()")
378 | (ABS.ML_Seq[e]) => ppE(inLet, prevFn, e)
379 | (ABS.ML_Seq(e::r)) => let
380 fun pp () = (
381 ppExp' e;
382 app (fn e => (str ";"; sp(); ppExp' e)) r)
383 in
384 if inLet
385 then (
386 nl(); str "in";
387 PP.openBox ppStrm (PP.Abs 2);
388 nl(); pp();
389 close();
390 nl();
391 str "end")
392 else (
393 PP.openBox ppStrm (PP.Abs 0);
394 str "("; pp(); str ")";
395 close())
396 end
397 | (ABS.ML_Tuple[]) => letBody(inLet, fn () => str "()")
398 | (ABS.ML_Tuple(e::r)) =>
399 letBody (inLet, fn () => (
400 PP.openBox ppStrm (PP.Abs 2);
401 str "(";
402 ppExp' e;
403 app (fn e => (str ","; sp(); ppExp' e)) r;
404 str ")";
405 close()))
406 | (ABS.ML_List[]) => letBody(inLet, fn () => str "[]")
407 | (ABS.ML_List(e::r)) =>
408 letBody (inLet, fn () => (
409 PP.openBox ppStrm (PP.Abs 2);
410 str "[";
411 ppExp' e;
412 app (fn e => (str ","; sp(); ppExp' e)) r;
413 str "]";
414 close()))
415 | (ABS.ML_RefGet e) => letBody(inLet, fn () => (
416 str "!(";
417 ppExp' e;
418 str ")"))
419 | (ABS.ML_RefPut (e1, e2)) =>
420 letBody(inLet, fn () => (
421 ppExp' e1;
422 str " := ";
423 ppExp' e2))
424 | (ABS.ML_Raw toks) =>
425 letBody(inLet,
426 fn () => (hbox(); app (fn (ABS.Tok s) => str s) toks; close()))
427 | (ABS.ML_Handle (exp, cases)) =>
428 (
429 ppE (inLet, prevFn, exp);
430 nl(); str "handle";
431 doCases (true, true, cases))
432 (* end case *))
433 and ppExp' e = ppE (false, false, e)
434 and doCases (_,_, []) = ()
435 | doCases (isExn, isFirst, (p, e)::r) =
436 (
437 nl();
438 (* NOTE: the following seems to trigger a bug in the PP library (bad indent) *)
439 PP.openHOVBox ppStrm (PP.Abs 6);
440 hbox();
441 if isFirst
442 then if isExn
443 then (sp(); sp(); sp())
444 else (sp(); str "of")
445 else (PP.space ppStrm 2; str "|");
446 sp();
447 ppPat (ppStrm, p); sp(); str "=>";
448 close();
449 sp();
450 hbox();
451 PP.openVBox ppStrm (PP.Abs 0);
452 ppExp' e;
453 close();
454 close();
455 close();
456 doCases (isExn, false, r))
457 in
458 ppE(false, false, e)
459 end
460
461 and ppPat (ppStrm, p) = let
462 fun str s = PP.string ppStrm s
463 fun sp () = PP.space ppStrm 1
464 fun nl () = PP.newline ppStrm
465 fun hbox () = PP.openHBox ppStrm
466 fun vbox () = PP.openVBox ppStrm (PP.Abs 2)
467 fun close () = PP.closeBox ppStrm
468 fun pp (ABS.ML_Wild) = str "_"
469 | pp (ABS.ML_VarPat x) = str x
470 | pp (ABS.ML_IntPat n) = str(IntInf.toString n)
471 | pp (ABS.ML_ConPat(c, [])) = str c
472 | pp (ABS.ML_ConPat(c, [p])) = (
473 str c; str "("; pp p; str ")")
474 | pp (ABS.ML_ConPat(c, p::r)) = (
475 str c; str "("; pp p;
476 app (fn p => (str ","; sp(); pp p)) r;
477 str ")")
478 | pp (ABS.ML_TupPat []) = str "()"
479 | pp (ABS.ML_TupPat (p::r)) = (
480 str "("; pp p;
481 app (fn p => (str ","; sp(); pp p)) r;
482 str ")")
483 | pp (ABS.ML_ListPat []) = str "[]"
484 | pp (ABS.ML_ListPat (p::r))
485 = (str "["; pp p;
486 app (fn p => (str ","; sp(); pp p)) r;
487 str "]")
488 | pp (ABS.ML_TypePat (p, ty))
489 = (pp p;
490 str ":";
491 ppType (ppStrm, ty))
492 in
493 hbox(); pp p; close()
494 end
495
496 end