Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-ulex/BackEnds/SML/ml.sml

raw
1 (* ml.sml
2 *
3 * COPYRIGHT (c) 1999 Bell Labs, Lucent Technologies
4 * (Used and modified with permission)
5 * Aaron Turon (http://www.cs.uchicago.edu/~adrassi)
6 *
7 * ML core language representation and pretty-printing
8 *)
9
10 structure ML =
11 struct
12
13 datatype raw_ml = Raw of ml_token list
14
15 and ml_token = Tok of string
16
17 datatype cmp_op = LT | GT | EQ | LEQ | GEQ
18 datatype bool_op = AND | OR
19
20 (* a subset of ML expressions and patterns that we use to represent the
21 * match DFA
22 *)
23 datatype ml_exp
24 = ML_Var of string
25 | ML_Cmp of (cmp_op * ml_exp * ml_exp)
26 | ML_Bool of (bool_op * ml_exp * ml_exp)
27 | ML_Case of ml_exp * (ml_pat * ml_exp) list
28 | ML_If of ml_exp * ml_exp * ml_exp
29 | ML_App of (string * ml_exp list)
30 | ML_Let of (string * ml_exp * ml_exp)
31 | ML_Fun of (string * string list * ml_exp * ml_exp)
32 | ML_Seq of ml_exp list
33 | ML_Tuple of ml_exp list
34 | ML_List of ml_exp list
35 | ML_RefGet of ml_exp
36 | ML_RefPut of ml_exp * ml_exp
37 | ML_Raw of ml_token list
38 | ML_NewGroup of ml_exp
39
40 and ml_pat
41 = ML_Wild
42 | ML_VarPat of string
43 | ML_IntPat of RegExp.Sym.point
44 | ML_ConPat of string * ml_pat list
45
46 local
47 structure PP = TextIOPP
48 in
49 fun ppML (ppStrm, e) = let
50 fun str s = PP.string ppStrm s
51 fun sp () = PP.space ppStrm 1
52 fun nl () = PP.newline ppStrm
53 fun hbox () = PP.openHBox ppStrm
54 fun vbox () = PP.openVBox ppStrm (PP.Abs 2)
55 fun close () = PP.closeBox ppStrm
56 fun letBody (true, pp) = (
57 nl();
58 str "in";
59 vbox(); nl(); pp(); close();
60 nl();
61 str "end")
62 | letBody (false, pp) = pp()
63 fun ppExp (inLet, prevFn, e) = (case e
64 of (ML_Var x) => letBody(inLet, fn () => str x)
65 | (ML_Cmp (cop, e1, e2)) => letBody(inLet, fn () => (
66 ppExp' e1;
67 sp();
68 str (case cop
69 of LT => "<"
70 | GT => ">"
71 | EQ => "="
72 | LEQ => "<="
73 | GEQ => ">=");
74 sp();
75 ppExp' e2))
76 | (ML_Bool (bop, e1, e2)) => letBody(inLet, fn () => (
77 ppExp' e1;
78 sp();
79 str (case bop
80 of AND => "andalso"
81 | OR => "orelse");
82 sp();
83 ppExp' e2))
84 | (ML_Case(arg, pl)) => let
85 fun doCases (_, []) = ()
86 | doCases (isFirst, (p, e)::r) = (
87 nl();
88 (* NOTE: the following seems to trigger a bug in the PP library (bad indent) *)
89 PP.openHOVBox ppStrm (PP.Abs 6);
90 hbox();
91 if isFirst
92 then (sp(); str "of")
93 else (PP.space ppStrm 2; str "|");
94 sp();
95 ppPat p; sp(); str "=>";
96 close();
97 sp();
98 hbox();
99 PP.openVBox ppStrm (PP.Abs 0);
100 ppExp' e;
101 close();
102 close();
103 close();
104 doCases (false, r))
105 in
106 letBody(inLet, fn () => (
107 hbox();
108 str "(case"; sp(); str "("; ppExp' arg; str ")";
109 close();
110 doCases (true, pl);
111 nl(); str "(* end case *))"))
112 end
113 | (ML_App(f, args)) => letBody(inLet, fn () => (
114 hbox();
115 str f; str "(";
116 case args
117 of [] => ()
118 | [e] => ppExp' e
119 | (e::r) => (
120 ppExp' e; app (fn e => (str ","; sp(); ppExp' e)) r)
121 (* end case *);
122 str ")";
123 close()))
124 | (ML_If(e1, e2, e3 as ML_If _)) => letBody(inLet, fn () => (
125 PP.openVBox ppStrm (PP.Abs 0);
126 vbox();
127 hbox(); str "if"; sp(); ppExp' e1; close(); nl();
128 hbox(); str "then"; sp();
129 vbox(); ppExp' e2; close();
130 close();
131 close(); nl();
132 hbox(); str "else"; sp();
133 ppExp' e3;
134 close();
135 close()))
136 | (ML_If(e1, e2, e3)) => letBody(inLet, fn () => (
137 vbox();
138 hbox(); str "if"; sp(); ppExp' e1; close(); nl();
139 hbox(); str "then"; sp();
140 vbox(); ppExp' e2; close();
141 close(); nl();
142 hbox(); str "else"; sp();
143 vbox(); ppExp' e3; close();
144 close();
145 close()))
146 | (ML_Let(x, e1, e2)) => let
147 fun pp () = (
148 nl();
149 hbox();
150 str "val"; sp(); str x; sp(); str "="; sp();
151 ppExp' e1;
152 close();
153 ppExp (true, false, e2))
154 in
155 if inLet
156 then pp()
157 else (
158 str "let";
159 PP.openVBox ppStrm (PP.Abs 0);
160 pp();
161 close())
162 end
163 | (ML_Fun(f, params, body, e)) => let
164 fun pp prefix = (
165 nl();
166 hbox();
167 str prefix; sp(); str f; sp();
168 str "(";
169 case params
170 of [] => ()
171 | [x] => str x
172 | (x::r) => (
173 str x; app (fn x => (str ","; sp(); str x)) r)
174 (* end case *);
175 str ")"; sp(); str "="; sp();
176 PP.openVBox ppStrm (PP.Abs 6);
177 ppExp' body;
178 close();
179 close();
180 ppExp (true, true, e))
181 in
182 if inLet
183 then if prevFn then pp "and" else pp "fun"
184 else (
185 PP.openVBox ppStrm (PP.Abs 0);
186 str "let";
187 pp "fun";
188 close())
189 end
190 | (ML_Seq[]) => letBody(inLet, fn () => str "()")
191 | (ML_Seq[e]) => ppExp(inLet, prevFn, e)
192 | (ML_Seq(e::r)) => let
193 fun pp () = (
194 ppExp' e;
195 app (fn e => (str ";"; sp(); ppExp' e)) r)
196 in
197 if inLet
198 then (
199 nl(); str "in";
200 PP.openBox ppStrm (PP.Abs 2);
201 nl(); pp();
202 close();
203 nl();
204 str "end")
205 else (
206 PP.openBox ppStrm (PP.Abs 0);
207 str "("; pp(); str ")";
208 close())
209 end
210 | (ML_Tuple[]) => letBody(inLet, fn () => str "()")
211 | (ML_Tuple(e::r)) => letBody (inLet, fn () => (
212 PP.openBox ppStrm (PP.Abs 2);
213 str "(";
214 ppExp' e;
215 app (fn e => (str ","; sp(); ppExp' e)) r;
216 str ")";
217 close()))
218 | (ML_List[]) => letBody(inLet, fn () => str "[]")
219 | (ML_List(e::r)) => letBody (inLet, fn () => (
220 PP.openBox ppStrm (PP.Abs 2);
221 str "[";
222 ppExp' e;
223 app (fn e => (str ","; sp(); ppExp' e)) r;
224 str "]";
225 close()))
226 | (ML_RefGet e) => letBody(inLet, fn () => (
227 str "!(";
228 ppExp' e;
229 str ")"))
230 | (ML_RefPut (e1, e2)) => letBody(inLet, fn () => (
231 ppExp' e1;
232 str " := ";
233 ppExp' e2))
234 | (ML_Raw toks) => letBody(inLet, fn () => (
235 hbox(); app (fn (Tok s) => str s) toks; close()))
236 | (ML_NewGroup e) => ppExp(inLet, false, e)
237 (* end case *))
238 and ppExp' e = ppExp(false, false, e)
239 and ppPat p = let
240 fun pp (ML_Wild) = str "_"
241 | pp (ML_VarPat x) = str x
242 | pp (ML_IntPat n) = str(RegExp.symToString n)
243 | pp (ML_ConPat(c, [])) = str c
244 | pp (ML_ConPat(c, [p])) = (
245 str c; str "("; pp p; str ")")
246 | pp (ML_ConPat(c, p::r)) = (
247 str c; str "("; pp p;
248 app (fn p => (str ","; pp p)) r;
249 str ")")
250 in
251 hbox(); pp p; close()
252 end
253 in
254 ppExp (false, false, e)
255 end
256 end (* local *)
257
258 end