Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/FakeSMLAst/ast-rewrite.sml

raw
1 (*
2 * Various translation functions
3 *)
4 functor MDLAstRewriter(Ast : MDL_AST) : MDL_AST_REWRITER =
5 struct
6
7 structure Ast = Ast
8 structure Error = MDLError
9
10 type 'a rewriter = ('a -> 'a) -> ('a -> 'a)
11
12 type clients = {exp : Ast.exp rewriter,
13 decl : Ast.decl rewriter,
14 sexp : Ast.structexp rewriter,
15 pat : Ast.pat rewriter,
16 ty : Ast.ty rewriter
17 }
18 type trans = {exp : Ast.exp -> Ast.exp,
19 decl : Ast.decl -> Ast.decl,
20 sexp : Ast.structexp -> Ast.structexp,
21 pat : Ast.pat -> Ast.pat,
22 ty : Ast.ty -> Ast.ty
23 }
24
25 open Ast
26
27 fun noRewrite f e = e
28
29 fun opt f NONE = NONE
30 | opt f (SOME e) = SOME(f e)
31
32 fun rewrite{exp=rwExp, decl=rwDecl, pat=rwPat, sexp=rwSexp, ty=rwTy} =
33 let fun exp e =
34 let val e = case e of
35 CONSexp(id,SOME e) => CONSexp(id,SOME(exp e))
36 | LISTexp(es,e) => LISTexp(map exp es,opt exp e)
37 | TUPLEexp es => TUPLEexp(map exp es)
38 | VECTORexp es => VECTORexp(map exp es)
39 | RECORDexp es => RECORDexp(map (fn (l,e) => (l,exp e)) es)
40 | SEQexp es => SEQexp(map exp es)
41 | APPexp(f,x) => APPexp(exp f, exp x)
42 | IFexp(x,y,z) => IFexp(exp x, exp y, exp z)
43 | RAISEexp e => RAISEexp(exp e)
44 | HANDLEexp(e,c) => HANDLEexp(exp e,map clause c)
45 | CASEexp(e,c) => CASEexp(exp e,map clause c)
46 | LAMBDAexp c => LAMBDAexp(map clause c)
47 | LETexp(d,e) => LETexp(map decl d,map exp e)
48 | TYPEDexp(e,t) => TYPEDexp(exp e,ty t)
49 | MARKexp(l,e) => (Error.setLoc l; MARKexp(l,exp e))
50 | LOCexp(id,e,region) => LOCexp(id,exp e,region)
51 | BITSLICEexp(e,slices) => BITSLICEexp(exp e,slices)
52 | TYPEexp t => TYPEexp(ty t)
53 | CONTexp(e,x) => CONTexp(exp e,x)
54 | e => e
55 in rwExp exp e end
56
57 and decl d =
58 let val d = case d of
59 DATATYPEdecl(dbs,tbs) => DATATYPEdecl(map dbind dbs,map tbind tbs)
60 | FUNdecl(fbs) => FUNdecl(map fbind fbs)
61 | RTLdecl(p,e,l) => RTLdecl(pat p,exp e,l)
62 | RTLSIGdecl(id,t) => RTLSIGdecl(id,ty t)
63 | VALdecl(vbs) => VALdecl(map vbind vbs)
64 | VALSIGdecl(id,t) => VALSIGdecl(id,ty t)
65 | TYPESIGdecl(id,tvs) => TYPESIGdecl(id,tvs)
66 | LOCALdecl(d1,d2) => LOCALdecl(map decl d1,map decl d2)
67 | SEQdecl ds => SEQdecl(map decl ds)
68 | STRUCTUREdecl(id,ds,s,se) =>
69 STRUCTUREdecl(id,map decl ds,sigconopt s,sexp se)
70 | FUNCTORdecl(id,ds,s,se) =>
71 FUNCTORdecl(id, map decl ds,sigconopt s, sexp se)
72 | INCLUDESIGdecl s => INCLUDESIGdecl(sigexp s)
73 | SIGNATUREdecl(id,s) => SIGNATUREdecl(id, sigexp s)
74 | STRUCTURESIGdecl(id,s) => STRUCTURESIGdecl(id, sigexp s)
75 | OPENdecl ids => OPENdecl ids
76 | FUNCTORARGdecl(id,se) => FUNCTORARGdecl(id,sigcon se)
77 | EXCEPTIONdecl ebs => EXCEPTIONdecl(map ebind ebs)
78 | MARKdecl(l,d) => (Error.setLoc l; MARKdecl(l,decl d))
79 | d => d
80 in rwDecl decl d end
81
82 and sigcon{abstract,sigexp=se} = {abstract=abstract,sigexp=sigexp se}
83
84 and sigconopt s = Option.map sigcon s
85
86 and ebind(b as EXCEPTIONbind(id,NONE)) = b
87 | ebind(EXCEPTIONbind(id,SOME t)) = EXCEPTIONbind(id,SOME(ty t))
88 | ebind(b as EXCEPTIONEQbind _) = b
89
90 and sigexp se =
91 let val se = case se of
92 IDsig _ => se
93 | WHEREsig(se,ident,s) =>
94 WHEREsig(sigexp se,ident,sexp s)
95 | WHERETYPEsig(se,ident,t) =>
96 WHERETYPEsig(sigexp se,ident,ty t)
97 | DECLsig ds => DECLsig(map decl ds)
98 in se end
99
100 and sexp se =
101 let val se = case se of
102 APPsexp(a,se) => APPsexp(sexp a,sexp se)
103 | DECLsexp ds => DECLsexp(map decl ds)
104 | CONSTRAINEDsexp(s, si) => CONSTRAINEDsexp(sexp s,sigexp si)
105 | IDsexp _ => se
106 in rwSexp sexp se end
107
108 and ty t =
109 let val t = case t of
110 IDty _ => t
111 | TYVARty _ => t
112 | INTVARty _ => t
113 | VARty(_,_,_,ref(SOME t)) => ty t
114 | VARty(_,_,_,ref NONE) => t
115 | APPty(f, ts) => APPty(f, map ty ts)
116 | FUNty(a,b) => FUNty(ty a, ty b)
117 | TUPLEty ts => TUPLEty(map ty ts)
118 | RECORDty lts => RECORDty(map (fn (l,t) => (l,ty t)) lts)
119 | POLYty(ts,t) => POLYty(map ty ts, ty t)
120 | LAMBDAty(ts, t) => LAMBDAty(map ty ts, ty t)
121 | CELLty _ => t
122 in rwTy ty t end
123
124 and pat p =
125 let val p = case p of
126 IDpat id => p
127 | WILDpat => p
128 | ASpat(id,p) => ASpat(id, pat p)
129 | LITpat l => p
130 | LISTpat(ps,p) => LISTpat(map pat ps,opt pat p)
131 | TUPLEpat ps => TUPLEpat(map pat ps)
132 | VECTORpat ps => VECTORpat(map pat ps)
133 | RECORDpat(lps,flex) =>
134 RECORDpat(map (fn (l,p) => (l,pat p)) lps, flex)
135 | TYPEDpat(p,t) => TYPEDpat(pat p,ty t)
136 | CONSpat(id,NONE) => p
137 | CONSpat(id,SOME p) => CONSpat(id,SOME(pat p))
138 | ORpat ps => ORpat(map pat ps)
139 | ANDpat ps => ANDpat(map pat ps)
140 | NOTpat p => NOTpat(pat p)
141 | WHEREpat(p,e) => WHEREpat(pat p,exp e)
142 | NESTEDpat(p,e,p') => NESTEDpat(pat p,exp e,pat p')
143 in rwPat pat p end
144
145 and fbind(FUNbind(id,c)) = FUNbind(id,map clause c)
146
147 and clause(CLAUSE(ps,g,e)) = CLAUSE(map pat ps,guard g,exp e)
148
149 and guard NONE = NONE
150 | guard (SOME e) = SOME(exp e)
151
152 and vbind(VALbind(p,e)) = VALbind(pat p,exp e)
153
154 and dbind db = db
155
156 and tbind(TYPEbind(x,tvs,t)) = TYPEbind(x,tvs,ty t)
157 in { pat=pat,
158 exp=exp,
159 decl=decl,
160 sexp=sexp,
161 ty=ty
162 }
163 end
164 end