| 1 |
(* |
| 2 |
* Pretty printer for the AST |
| 3 |
* |
| 4 |
* Allen Leung ([email protected]) |
| 5 |
*) |
| 6 |
functor MDLAstPrettyPrinter(AstUtil : MDL_AST_UTIL) : MDL_AST_PRETTY_PRINTER = |
| 7 |
struct |
| 8 |
|
| 9 |
structure Ast = AstUtil.Ast |
| 10 |
|
| 11 |
open PP Ast MDLError |
| 12 |
infix ++ |
| 13 |
|
| 14 |
fun error msg = MDLError.error("error while processing "^msg) |
| 15 |
|
| 16 |
val goodBreak = nl'(5,3) (* if over column 75, tab and indent by 3 *) |
| 17 |
val goodFunBreak = nl'(5,3) (* if over column 75, tab and indent by 3 *) |
| 18 |
|
| 19 |
val comma = !! ", " |
| 20 |
val semi = !! "; " |
| 21 |
val cons = !! "::" |
| 22 |
val dot = !! "." |
| 23 |
val list = seq(!! "[",comma++goodBreak,!! "]") |
| 24 |
val tuple = seq(!! "(",comma++goodBreak,!! ")") |
| 25 |
val vector = seq(!! "#[",comma++goodBreak,!! "]") |
| 26 |
val record = seq(!! "{",comma++goodBreak,!! "}") |
| 27 |
val bars = seq(settab,nl'(5,0) ++ tab' ~2 ++ ! "|" ++ tab,unindent) |
| 28 |
val ands = seq(settab,tab' ~4 ++ ! "and" ++ tab,unindent) |
| 29 |
|
| 30 |
fun isAlpha "" = true |
| 31 |
| isAlpha s = Char.isAlpha(String.sub(s,0)) |
| 32 |
|
| 33 |
fun isMLSym #"'" = false |
| 34 |
| isMLSym #"_" = false |
| 35 |
| isMLSym #"." = false |
| 36 |
| isMLSym c = Char.isPunct c |
| 37 |
|
| 38 |
fun isComplex s = |
| 39 |
let fun loop(~1, alpha, sym) = alpha andalso sym |
| 40 |
| loop(i, alpha, sym) = |
| 41 |
let val c = String.sub(s,i) |
| 42 |
in loop(i-1, alpha orelse Char.isAlphaNum c, |
| 43 |
sym orelse isMLSym c) |
| 44 |
end |
| 45 |
in loop(String.size s - 1, false, false) end |
| 46 |
|
| 47 |
fun encodeChar c = if isMLSym c then "_"^Int.toString(Char.ord c) |
| 48 |
else Char.toString c |
| 49 |
|
| 50 |
fun encodeName s = String.translate encodeChar s |
| 51 |
|
| 52 |
fun name id = if isComplex id then encodeName id else id |
| 53 |
|
| 54 |
fun ident(IDENT([],id)) = if isSym id then !"op" ++ ! id |
| 55 |
else if isAlpha id then !(name id) |
| 56 |
else sp ++ !id |
| 57 |
| ident(IDENT(p,id)) = seq(nop,dot,nop) (map ! (p @[name id])) |
| 58 |
|
| 59 |
and literal(WORDlit w) = word w |
| 60 |
| literal(WORD32lit w) = word32 w |
| 61 |
| literal(INTINFlit i) = |
| 62 |
select |
| 63 |
(fn "code" => |
| 64 |
(!"(IntInf.fromInt" ++ int(IntInf.toInt i) ++ !!")" |
| 65 |
handle Overflow => |
| 66 |
!"(Option.valOf(IntInt.fromString"++string(IntInf.toString i)++ !!"))" |
| 67 |
) |
| 68 |
| _ => intinf i |
| 69 |
) |
| 70 |
| literal(INTlit i) = int i |
| 71 |
| literal(INT32lit i) = int32 i |
| 72 |
| literal(STRINGlit s) = string s |
| 73 |
| literal(CHARlit c) = char c |
| 74 |
| literal(BOOLlit b) = bool b |
| 75 |
| literal(REALlit r) = !r |
| 76 |
|
| 77 |
and exp(LITexp l) = literal l |
| 78 |
| exp(IDexp id) = ident id |
| 79 |
| exp(CONSexp(id,e)) = ident id ++ sp ++ exp' e |
| 80 |
| exp(LISTexp(es,NONE)) = if length es >= 10 then longlistexp es |
| 81 |
else list (map appexp es) |
| 82 |
| exp(LISTexp([],SOME e)) = exp e |
| 83 |
| exp(LISTexp(es,SOME e)) = seq(nop,cons,cons) (map exp es) ++ exp e |
| 84 |
| exp(TUPLEexp [e]) = exp e |
| 85 |
| exp(TUPLEexp es) = tuple (map appexp es) |
| 86 |
| exp(VECTORexp es) = vector (map appexp es) |
| 87 |
| exp(RECORDexp es) = record(map labexp es) |
| 88 |
| exp(SEQexp []) = ! "()" |
| 89 |
| exp(SEQexp [e]) = exp e |
| 90 |
| exp(SEQexp es) = nl ++ tab ++ |
| 91 |
seq(! "(" ++ sp ++ |
| 92 |
settab,semi++nl++tab,unindent ++ tab ++ ! ")") |
| 93 |
(map appexp es) |
| 94 |
| exp(APPexp(e as IDexp(IDENT([],f)),e' as TUPLEexp[x,y])) = |
| 95 |
if isSym f then |
| 96 |
paren(exp x ++ sp ++ ! f ++ sp ++ exp y) |
| 97 |
else |
| 98 |
paren(exp e ++ !! " " ++ exp e') |
| 99 |
| exp(APPexp(f,x)) = paren(appexp f ++ !! " " ++ exp x) |
| 100 |
| exp(IFexp(x,y,z)) = paren(line(! "if" ++ sp ++ exp x) ++ |
| 101 |
block(line(! "then" ++ sp ++ exp y) ++ |
| 102 |
tab ++ ! "else" ++ sp ++ exp z)) |
| 103 |
| exp(RAISEexp e) = ! "raise" ++ exp e |
| 104 |
| exp(HANDLEexp(e,c)) = paren(exp e ++ sp ++ ! "handle" ++ sp ++ clauses c) |
| 105 |
| exp(CASEexp(e,c)) = |
| 106 |
nl ++ line(! "(case" ++ sp ++ appexp e ++ sp ++ ! "of") |
| 107 |
++ tab' 2 ++ settab ++ block(clauses c) ++ unindent ++ tab ++ !!")" |
| 108 |
| exp(LAMBDAexp c) = group ("(",")") (! "fn" ++ sp ++ clauses c) |
| 109 |
| exp(LETexp([],e)) = expseq e |
| 110 |
| exp(LETexp(d,e)) = nl ++ tab ++ ! "let" ++ sp ++ settab ++ |
| 111 |
decls d ++ unindent ++ |
| 112 |
line(! "in" ++ sp ++ expseq e) ++ tab ++ ! "end" |
| 113 |
| exp(TYPEDexp(e,t)) = paren(exp e ++ sp ++ !!":" ++ sp ++ ty t) |
| 114 |
| exp(MARKexp(_,e)) = exp e |
| 115 |
| exp(LOCexp(id,e,region)) = locexp(id,e,region) |
| 116 |
| exp(BITSLICEexp(e,slices)) = |
| 117 |
select(fn "code" => exp(AstUtil.BITSLICE(e,slices)) |
| 118 |
| "pretty" => exp e ++ sp ++ ! "at" ++ |
| 119 |
list(map (fn (i,j) => int i ++ !! ".." ++ int j) slices) |
| 120 |
| mode => (error mode; nop) |
| 121 |
) |
| 122 |
| exp(TYPEexp t) = ty t |
| 123 |
| exp(ASMexp a) = (error "PP.ASMexp"; nop) |
| 124 |
| exp(RTLexp r) = |
| 125 |
select(fn "pretty" => rtl r |
| 126 |
| mode => (error mode; nop) |
| 127 |
) |
| 128 |
| exp(CONTexp(e,x)) = exp e |
| 129 |
|
| 130 |
and rtl r = seq(!"[[",sp,!"]]") (map rtlterm r) |
| 131 |
|
| 132 |
and rtlterm(LITrtl s) = string s |
| 133 |
| rtlterm(IDrtl x) = ! x |
| 134 |
|
| 135 |
and longlistexp es = |
| 136 |
select(fn "pretty" => list(map appexp es) |
| 137 |
| "code" => codelonglistexp es) |
| 138 |
|
| 139 |
and prettylonglistexp es = |
| 140 |
nl ++ tab ++ seq(! "[",comma++nl++tab,! "]") (map appexp es) |
| 141 |
and codelonglistexp es = |
| 142 |
nl ++ |
| 143 |
line(!"let infix $$ fun x $$ y = y::x") ++ |
| 144 |
line(!"in nil") ++ |
| 145 |
block(concat(map (fn e => line(!"$$" ++ appexp e)) (rev es))) ++ |
| 146 |
line(!"end") |
| 147 |
|
| 148 |
and appexp(APPexp(e as IDexp(IDENT([],f)),e' as TUPLEexp[x,y])) = |
| 149 |
if isSym f then exp x ++ sp ++ ! f ++ sp ++ exp y |
| 150 |
else exp e ++ !! " " ++ exp e' |
| 151 |
| appexp(APPexp(f,x)) = (appexp f ++ !! " " ++ exp x) |
| 152 |
| appexp(SEQexp[e]) = appexp e |
| 153 |
| appexp(TUPLEexp[e]) = appexp e |
| 154 |
| appexp e = exp e |
| 155 |
|
| 156 |
and exp' NONE = nop |
| 157 |
| exp'(SOME e) = if isParenedExp e then exp e else paren(exp e) |
| 158 |
|
| 159 |
and isParenedExp(IDexp _) = true |
| 160 |
| isParenedExp(TUPLEexp []) = true |
| 161 |
| isParenedExp(TUPLEexp [x]) = isParenedExp x |
| 162 |
| isParenedExp(TUPLEexp _) = true |
| 163 |
| isParenedExp(RECORDexp _) = true |
| 164 |
| isParenedExp(LISTexp _) = true |
| 165 |
| isParenedExp(VECTORexp _) = true |
| 166 |
| isParenedExp _ = false |
| 167 |
|
| 168 |
and isSym "+" = true |
| 169 |
| isSym "-" = true |
| 170 |
| isSym "*" = true |
| 171 |
| isSym "mod" = true |
| 172 |
| isSym "div" = true |
| 173 |
| isSym "=" = true |
| 174 |
| isSym "<>" = true |
| 175 |
| isSym "<" = true |
| 176 |
| isSym ">" = true |
| 177 |
| isSym ">=" = true |
| 178 |
| isSym "<=" = true |
| 179 |
| isSym "<<" = true |
| 180 |
| isSym ">>" = true |
| 181 |
| isSym "~>>" = true |
| 182 |
| isSym "||" = true |
| 183 |
| isSym "&&" = true |
| 184 |
| isSym "^" = true |
| 185 |
| isSym ":=" = true |
| 186 |
| isSym "::" = true |
| 187 |
| isSym "@" = true |
| 188 |
| isSym "andalso" = true |
| 189 |
| isSym "orelse" = true |
| 190 |
| isSym "o" = true |
| 191 |
| isSym _ = false |
| 192 |
|
| 193 |
and locexp(id,e,region) = |
| 194 |
select(fn "pretty" => |
| 195 |
!!"$" ++ ! id ++ !!"[" ++ exp e ++ |
| 196 |
(case region of |
| 197 |
SOME r => ! ":" ++ ! r |
| 198 |
| NONE => nop |
| 199 |
) ++ |
| 200 |
!!"]" |
| 201 |
| "code" => paren(exp e ++ ! "+" ++ !("offset"^id)) |
| 202 |
| mode => (error mode; nop) |
| 203 |
) |
| 204 |
|
| 205 |
and decl(DATATYPEdecl(dbs,tbs)) = datatypedecl(dbs,tbs) |
| 206 |
| decl(FUNdecl(fbs)) = fundecl fbs |
| 207 |
| decl(RTLdecl(p,e,_)) = |
| 208 |
line(! "rtl " ++ pat p ++ ! "=" ++ exp e) |
| 209 |
| decl(VALdecl(vbs)) = valdecl vbs |
| 210 |
| decl(VALSIGdecl(ids,ty)) = valsig("val",ids,ty) |
| 211 |
| decl(RTLSIGdecl(ids,ty)) = valsig("rtl",ids,ty) |
| 212 |
| decl(TYPESIGdecl(id,tvs)) = typesig(id,tvs) |
| 213 |
| decl(LOCALdecl([],d2)) = decls d2 |
| 214 |
| decl(LOCALdecl(d1,d2)) = |
| 215 |
line(! "local") ++ block(decls d1) ++ line(! "in ") ++ |
| 216 |
block(decls d2) ++ line(! "end") |
| 217 |
| decl(SEQdecl ds) = decls ds |
| 218 |
| decl($ ds) = concat(map line (map !! ds)) |
| 219 |
| decl(STRUCTUREdecl(id,[],s,se)) = |
| 220 |
line(! "structure" ++ ! id ++ sigconOpt(s) ++ ! "=" ++ sexp se) |
| 221 |
| decl(STRUCTURESIGdecl(id,se)) = |
| 222 |
line(! "structure" ++ ! id ++ !":" ++ sigexp se) |
| 223 |
| decl(STRUCTUREdecl(id,ds,s,se)) = |
| 224 |
line(! "functor" ++ ! id ++ settab ++ !! "(" ++ settab ++ |
| 225 |
decls ds ++ unindent ++ |
| 226 |
tab ++ !! ")" ++ unindent ++ sigconOpt(s) ++ |
| 227 |
! "=" ++ nl ++ sexp se) |
| 228 |
| decl(FUNCTORdecl(id,[],s,se)) = |
| 229 |
line(! "functor" ++ ! id ++ sigconOpt(s) ++ ! "=" ++ nl ++ sexp se) |
| 230 |
| decl(FUNCTORdecl(id,ds,s,se)) = |
| 231 |
line(! "functor" ++ ! id ++ settab ++ !! "(" ++ settab ++ |
| 232 |
decls ds ++ unindent ++ |
| 233 |
tab ++ !! ")" ++ unindent ++ sigconOpt(s) ++ |
| 234 |
! "=" ++ nl ++ sexp se) |
| 235 |
| decl(SIGNATUREdecl(id,se)) = |
| 236 |
line(! "signature" ++ ! id ++ ! "=" ++ sigexp se) |
| 237 |
| decl(OPENdecl ids) = |
| 238 |
line(! "open" ++ seq(nop,sp,nop)(map ident ids)) |
| 239 |
| decl(INCLUDESIGdecl s) = line(! "include" ++ sigexp s) |
| 240 |
| decl(FUNCTORARGdecl(id,se)) = ! id ++ sigcon se |
| 241 |
| decl(EXCEPTIONdecl ebs) = |
| 242 |
line(!"exception" ++ ands(map exceptionbind ebs)) |
| 243 |
| decl(SHARINGdecl s) = line(! "sharing" ++ ands(map share s)) |
| 244 |
| decl(MARKdecl(l,d)) = |
| 245 |
nl++ !(SourceMapping.directive l) ++nl ++ decl d |
| 246 |
| decl(INFIXdecl(i,ids)) = line(! "infix" ++ int i ++ concat(map ! ids)) |
| 247 |
| decl(INFIXRdecl(i,ids)) = line(! "infixr" ++ int i ++ concat(map ! ids)) |
| 248 |
| decl(NONFIXdecl ids) = line(! "nonfix" ++ concat(map ! ids)) |
| 249 |
| decl(ARCHdecl(id,ds)) = |
| 250 |
line(! "architecture" ++ ! id ++ !"=" ++ decls ds) |
| 251 |
| decl(BITSORDERINGdecl _) = line(! "bitsordering...") |
| 252 |
| decl(FORMATdecl _) = line(!"instruction formats ...") |
| 253 |
| decl(ARCHKINDdecl SUPERSCALAR) = line(!"superscalar") |
| 254 |
| decl(ARCHKINDdecl VLIW) = line(!"vliw") |
| 255 |
| decl(ENDIANESSdecl LITTLE) = line(!"little endian") |
| 256 |
| decl(ENDIANESSdecl BIG) = line(!"big endian") |
| 257 |
| decl(STORAGEdecl _) = line(!"storage ...") |
| 258 |
| decl(LOCATIONSdecl _) = line(!"locations ...") |
| 259 |
| decl(NAMEdecl _) = line(!"name ...") |
| 260 |
| decl(VERSIONdecl _) = line(!"version ...") |
| 261 |
| decl(ASSEMBLYCASEdecl _) = line(!"assembly ...") |
| 262 |
| decl(INSTRUCTIONdecl cbs) = line(!"instruction" ++ |
| 263 |
tab' ~6 ++ consbinds cbs) |
| 264 |
| decl(DEBUGdecl _) = line(!"debug ...") |
| 265 |
| decl(RESOURCEdecl _) = line(!"resource ...") |
| 266 |
| decl(CPUdecl _) = line(!"cpu ...") |
| 267 |
| decl(PIPELINEdecl _) = line(!"pipeline ...") |
| 268 |
| decl(LATENCYdecl _) = line(!"latency ...") |
| 269 |
|
| 270 |
and exceptionbind(EXCEPTIONbind(id,NONE)) = ! id |
| 271 |
| exceptionbind(EXCEPTIONbind(id,SOME t)) = !id ++ !"of" ++ ty t |
| 272 |
| exceptionbind(EXCEPTIONEQbind(id,id')) = !id ++ !"=" ++ ident id' |
| 273 |
|
| 274 |
and share(TYPEshare ids) = !"type" ++ seq(nop,! "=",nop) (map ident ids) |
| 275 |
| share(STRUCTshare ids) = seq(nop,! "=",nop) (map ident ids) |
| 276 |
|
| 277 |
and sigexp(IDsig id) = ident id |
| 278 |
| sigexp(WHEREsig(se,x,s)) = |
| 279 |
sigexp se ++ !"where" ++ ident x ++ !! "=" ++ sexp s |
| 280 |
| sigexp(WHERETYPEsig(se,x,t)) = |
| 281 |
sigexp se ++ !"where type" ++ ident x ++ !! "=" ++ ty t |
| 282 |
| sigexp(DECLsig ds) = line(!"sig") ++ block(decls ds) ++ line(!"end") |
| 283 |
|
| 284 |
and sigconOpt(NONE) = nop |
| 285 |
| sigconOpt(SOME s) = sigcon s |
| 286 |
|
| 287 |
and sigcon{abstract=false,sigexp=s} = !":" ++ sigexp s |
| 288 |
| sigcon{abstract=true,sigexp=s} = !":>" ++ sigexp s |
| 289 |
|
| 290 |
and sexp (IDsexp id) = ident id |
| 291 |
| sexp (APPsexp(a,DECLsexp ds)) = sexp a ++ nl ++ |
| 292 |
block(line(group("(",")") (decls ds))) |
| 293 |
| sexp (APPsexp(a,IDsexp id)) = sexp a ++ paren(ident id) |
| 294 |
| sexp (APPsexp(a,b)) = sexp a ++ nl ++ paren(sexp b) |
| 295 |
| sexp (DECLsexp ds) = line(!"struct") ++ block(decls ds) ++ line(!"end") |
| 296 |
| sexp (CONSTRAINEDsexp(s,si)) = sexp s ++ !":" ++ sigexp si |
| 297 |
|
| 298 |
and decls ds = concat (map decl ds) |
| 299 |
|
| 300 |
and valsig (keyword,[],t) = nop |
| 301 |
| valsig (keyword,id::ids,t) = |
| 302 |
line(! keyword ++ ! id ++ ! ":" ++ sp ++ ty t) ++ |
| 303 |
valsig(keyword,ids,t) |
| 304 |
|
| 305 |
and typesig (id,tvs) = line(! "type" ++ tyvars tvs ++ ! id) |
| 306 |
|
| 307 |
and expseq es = block(seq(nop,semi++nl++tab,nop) (map appexp es)) |
| 308 |
|
| 309 |
and labexp(id,e) = ! id ++ !! "=" ++ appexp e |
| 310 |
|
| 311 |
and ty(IDty id) = ident id |
| 312 |
| ty(TYVARty tv) = tyvar tv |
| 313 |
| ty(APPty(id,[t])) = pty t ++ sp ++ ident id |
| 314 |
| ty(APPty(id,tys)) = tuple(map ty tys) ++ sp ++ ident id |
| 315 |
| ty(FUNty(x,y)) = ty x ++ !! " -> " ++ fty y |
| 316 |
| ty(TUPLEty []) = ! "unit" |
| 317 |
| ty(TUPLEty [t]) = ty t |
| 318 |
| ty(TUPLEty tys) = seq(nop,!! " * ",nop) (map pty tys) |
| 319 |
| ty(RECORDty labtys) = record(map labty labtys) |
| 320 |
| ty(CELLty id) = |
| 321 |
select( fn "pretty" => !!"$" ++ !id |
| 322 |
| "code" => !(if id = "cellset" then "C.cellset" |
| 323 |
else "CellsBasis.cell") |
| 324 |
| mode => (error mode; nop) |
| 325 |
) |
| 326 |
| ty(VARty(TYPEkind,i,_,ref NONE)) = !("'X"^Int.toString i) |
| 327 |
| ty(VARty(INTkind,i,_,ref NONE)) = |
| 328 |
select (fn "pretty" => !("#X"^Int.toString i) |
| 329 |
| "code" => !("T"^Int.toString i)) |
| 330 |
| ty(VARty(_,_,_,ref(SOME t))) = ty t |
| 331 |
| ty(POLYty(vars,t)) = ty t |
| 332 |
| ty(INTVARty i) = select (fn "pretty" => !!"#" ++ int i |
| 333 |
| "code" => int i) |
| 334 |
| ty(LAMBDAty(vars,t)) = !!"\\" ++ tuple(map ty vars) ++ !!"." ++ ty t |
| 335 |
|
| 336 |
and fty(t as FUNty _) = ty t |
| 337 |
| fty t = pty t |
| 338 |
|
| 339 |
and pty(t as FUNty _) = paren(ty t) |
| 340 |
| pty(TUPLEty[t]) = pty t |
| 341 |
| pty(t as TUPLEty []) = ty t |
| 342 |
| pty(t as TUPLEty _) = paren(ty t) |
| 343 |
| pty(t as RECORDty _) = ty t |
| 344 |
| pty(t as IDty _) = ty t |
| 345 |
| pty(t as APPty _) = ty t |
| 346 |
| pty(t as VARty _) = ty t |
| 347 |
| pty(t as TYVARty _) = ty t |
| 348 |
| pty t = paren(ty t) |
| 349 |
|
| 350 |
and labty (id,t) = ! id ++ !! ":" ++ ty t |
| 351 |
|
| 352 |
and pat(IDpat id) = if isSym id then !"op" ++ !id else !(name id) |
| 353 |
| pat(WILDpat) = ! "_" |
| 354 |
| pat(ASpat(id,p)) = paren(!id ++ !"as" ++ sp ++ pat p) |
| 355 |
| pat(LITpat l) = literal l |
| 356 |
| pat(LISTpat(ps,NONE)) = list(map pat ps) |
| 357 |
| pat(LISTpat([],SOME p)) = pat p |
| 358 |
| pat(LISTpat(ps,SOME p)) = seq(nop,cons,cons) (map pat ps) ++ pat p |
| 359 |
| pat(TUPLEpat [p]) = pat p |
| 360 |
| pat(TUPLEpat ps) = tuple(map pat ps) |
| 361 |
| pat(VECTORpat ps) = vector(map pat ps) |
| 362 |
| pat(RECORDpat(lps,flex)) = |
| 363 |
record(map labpat lps @ (if flex then [! "..."] else [])) |
| 364 |
| pat(TYPEDpat(p,t)) = paren(pat p ++ !! ":" ++ ty t) |
| 365 |
| pat(CONSpat(id,NONE)) = ident id |
| 366 |
| pat(CONSpat(IDENT([],"::"),SOME(TUPLEpat[x,y]))) = |
| 367 |
paren(pat x ++ sp ++ !!"::" ++ sp ++ pat y) |
| 368 |
| pat(CONSpat(id,SOME p)) = ident id ++ ppat p |
| 369 |
| pat(ORpat [p]) = pat p |
| 370 |
| pat(ORpat ps) = |
| 371 |
if length ps > 10 |
| 372 |
then nl ++ tab ++ seq(! "(",! "|"++nl++tab,! ")") (map pat ps) |
| 373 |
else seq(!! "(", ! "|"++sp, !! ")") (map pat ps) |
| 374 |
| pat(ANDpat [p]) = pat p |
| 375 |
| pat(ANDpat ps) = seq(!! "(",sp ++ !"and" ++ sp, !!")") (map pat ps) |
| 376 |
| pat(NOTpat p) = !"not" ++ sp ++ pat p |
| 377 |
| pat(WHEREpat(p,e)) = pat p ++ sp ++ !"where" ++ sp ++ exp e |
| 378 |
| pat(NESTEDpat(p,e,p')) = pat p ++ sp ++ !"where" ++ sp ++ exp e ++ |
| 379 |
sp ++ !"in" ++ sp ++ pat p' |
| 380 |
|
| 381 |
and ppat(p as (CONSpat _ | ASpat _)) = paren(pat p) |
| 382 |
| ppat p = pat p |
| 383 |
|
| 384 |
and pats ps = concat(map pat ps) |
| 385 |
|
| 386 |
and ppats ps = concat(map (fn p => ppat p ++ sp) ps) |
| 387 |
|
| 388 |
and labpat(id,p as IDpat id') = |
| 389 |
if id = id' then ! id |
| 390 |
else ! id ++ !! "=" ++ pat p |
| 391 |
| labpat(id,p) = ! id ++ !! "=" ++ pat p |
| 392 |
|
| 393 |
and funbind(FUNbind(id,c)) = bars (map (funclause id) c) |
| 394 |
|
| 395 |
and funbinds fbs = ands (map funbind fbs) |
| 396 |
|
| 397 |
and funclause id (CLAUSE(ps,g,e)) = |
| 398 |
line(!(name id) ++ sp ++ ppats ps ++ sp ++ guard g ++ ! "=" ++ |
| 399 |
sp ++ goodFunBreak ++ appexp e) |
| 400 |
|
| 401 |
and guard NONE = nop |
| 402 |
| guard (SOME e) = ! "where" ++ sp ++ appexp e ++ sp |
| 403 |
|
| 404 |
and clause (CLAUSE([p],g,e)) = |
| 405 |
line(settab ++ pat p ++ sp ++ guard g ++ |
| 406 |
! "=>" ++ sp ++ goodFunBreak ++ appexp e ++ unindent) |
| 407 |
| clause (CLAUSE(ps,g,e)) = |
| 408 |
line(settab ++ ppats ps ++ sp ++ guard g ++ |
| 409 |
! "=>" ++ sp ++ appexp e ++ unindent) |
| 410 |
|
| 411 |
and clauses c = block(bars (map clause c)) |
| 412 |
|
| 413 |
and fundecl [] = nop |
| 414 |
| fundecl fbs = (* nl ++ *) tab ++ ! "fun" ++ sp ++ settab ++ |
| 415 |
funbinds fbs ++ unindent |
| 416 |
|
| 417 |
and valbind (VALbind(p,e)) = |
| 418 |
line(settab ++ pat p ++ sp ++ ! "=" ++ sp ++ appexp e ++ unindent) |
| 419 |
|
| 420 |
and valbinds vbs = block(ands (map valbind vbs)) |
| 421 |
|
| 422 |
and valdecl [] = nop |
| 423 |
| valdecl vbs = tab ++ ! "val" ++ sp ++ valbinds vbs |
| 424 |
|
| 425 |
and datatypebind(DATATYPEbind{id,tyvars=ts,cbs,...}) = |
| 426 |
line(tyvars ts ++ ! id ++ ! "=") ++ |
| 427 |
tab' ~6 ++ consbinds cbs |
| 428 |
| datatypebind(DATATYPEEQbind{id,tyvars=ts,ty=t,...}) = |
| 429 |
line(tyvars ts ++ ! id ++ ! "=" ++ !"datatype" ++ ty t) |
| 430 |
|
| 431 |
and datatypebinds ds = block(ands(map datatypebind ds)) |
| 432 |
|
| 433 |
and consbinds cbs = bars(map consbind cbs) |
| 434 |
|
| 435 |
and consbind(CONSbind{id,ty=NONE,...}) = line(! id) |
| 436 |
| consbind(CONSbind{id,ty=SOME t,...}) = line(! id ++ ! "of" ++ sp ++ ty t) |
| 437 |
|
| 438 |
and typebind(TYPEbind(id,ts,t)) = |
| 439 |
line (tyvars ts ++ !id ++ ! "=" ++ sp ++ ty t) |
| 440 |
|
| 441 |
and typebinds tbs = block(ands (map typebind tbs)) |
| 442 |
|
| 443 |
and tyvars [] = nop |
| 444 |
| tyvars [t] = tyvar t |
| 445 |
| tyvars tvs = tuple(map tyvar tvs) |
| 446 |
|
| 447 |
and tyvar (VARtv tv) = ! tv |
| 448 |
| tyvar (INTtv tv) = sp ++ !! "#" ++ ! tv |
| 449 |
|
| 450 |
and range(x,y) = paren(int x ++ comma ++ int y) |
| 451 |
|
| 452 |
and datatypedecl([],t) = tab ++ ! "type" ++ block(ands (map typebind t)) |
| 453 |
| datatypedecl(d,t) = |
| 454 |
tab ++ ! "datatype" ++ |
| 455 |
datatypebinds d ++ |
| 456 |
(case t of |
| 457 |
[] => nop |
| 458 |
| _ => tab ++ ! "withtype" ++ typebinds t |
| 459 |
) |
| 460 |
|
| 461 |
end |