| 1 |
functor RewriteGen |
| 2 |
(structure AstRewriter : MDL_AST_REWRITER |
| 3 |
structure AstPP : MDL_AST_PRETTY_PRINTER |
| 4 |
structure AstTrans : MDL_AST_TRANSLATION |
| 5 |
structure Parser : MDL_PARSER_DRIVER |
| 6 |
structure PolyGen : POLY_GEN |
| 7 |
sharing AstRewriter.Ast = AstPP.Ast = AstTrans.Ast = |
| 8 |
Parser.Ast = PolyGen.Ast |
| 9 |
) : REWRITE_GEN = |
| 10 |
struct |
| 11 |
structure Ast = AstRewriter.Ast |
| 12 |
structure A = Ast |
| 13 |
structure T = AstTrans |
| 14 |
|
| 15 |
val NO = AstRewriter.noRewrite |
| 16 |
val RW = AstRewriter.rewrite |
| 17 |
|
| 18 |
exception RewriteGen |
| 19 |
fun bug msg = MLRiscErrorMsg.error("RewriteGen",msg) |
| 20 |
fun error msg = (TextIO.output(TextIO.stdErr,msg^"\n"); raise RewriteGen) |
| 21 |
|
| 22 |
fun ID x = A.IDexp(A.IDENT([],x)) |
| 23 |
|
| 24 |
(* |
| 25 |
* Collect datatype and function declaractions |
| 26 |
*) |
| 27 |
fun processDecls hook (decls,exps) = |
| 28 |
let val datatypeBinds = ref [] |
| 29 |
val funBinds = ref [] |
| 30 |
val miscDecls = ref [] |
| 31 |
|
| 32 |
fun enterDb(db as A.DATATYPEbind _) = |
| 33 |
datatypeBinds := db :: !datatypeBinds |
| 34 |
| enterDb _ = () |
| 35 |
fun enterFb fbs = funBinds := fbs :: !funBinds |
| 36 |
fun enterMisc d = miscDecls := d :: !miscDecls |
| 37 |
|
| 38 |
fun decl _ (d as A.DATATYPEdecl(dbs, _)) = (app enterDb dbs; d) |
| 39 |
| decl _ (d as A.FUNdecl fbs) = (enterFb fbs; d) |
| 40 |
| decl _ (d as A.VALdecl vbs) = (enterMisc d; d) |
| 41 |
| decl _ (d as A.OPENdecl vbs) = (enterMisc d; d) |
| 42 |
| decl _ d = d (* ignore the rest *) |
| 43 |
|
| 44 |
(* Collect info *) |
| 45 |
val _ = map (#decl (RW{sexp=NO,ty=NO,decl=decl,exp=NO,pat=NO})) decls |
| 46 |
|
| 47 |
(* Collect rules *) |
| 48 |
fun findDb name = |
| 49 |
let fun find((db as A.DATATYPEbind{id, ...})::dbs) = |
| 50 |
if name = id then db else find dbs |
| 51 |
| find(_::dbs) = find dbs |
| 52 |
| find [] = error("unknown datatype "^name) |
| 53 |
in find(!datatypeBinds) |
| 54 |
end |
| 55 |
|
| 56 |
fun processRules(A.FUNbind(name, clauses)) = (findDb(name), clauses) |
| 57 |
val rules = map (map processRules) (rev(!funBinds)) |
| 58 |
val nonTerms = foldr (fn (fbs,ids) => |
| 59 |
foldr (fn (A.FUNbind(id,_),ids) => id::ids) |
| 60 |
ids fbs) [] (!funBinds) |
| 61 |
fun isNonTerm id = List.exists(fn id' => id=id') nonTerms |
| 62 |
val generated = map (PolyGen.gen hook isNonTerm) rules |
| 63 |
val miscs = rev(!miscDecls) |
| 64 |
in A.LETexp(miscs @ generated, exps) |
| 65 |
end |
| 66 |
|
| 67 |
(* |
| 68 |
* Hooks for various things |
| 69 |
*) |
| 70 |
val rewriteHook = |
| 71 |
PolyGen.HOOK |
| 72 |
{ name ="rewrite", |
| 73 |
factor=true, |
| 74 |
args =["redex"], |
| 75 |
ret ="redex", |
| 76 |
unit =fn x => x, |
| 77 |
gen =fn(trans,cons) => |
| 78 |
T.mapConsToExp |
| 79 |
{id=fn{newName,ty,...} => trans(ty,ID newName), |
| 80 |
prefix=[] |
| 81 |
} cons |
| 82 |
} |
| 83 |
|
| 84 |
val appHook = |
| 85 |
PolyGen.HOOK |
| 86 |
{ name ="app", |
| 87 |
factor=false, |
| 88 |
args =["redex"], |
| 89 |
ret ="_", |
| 90 |
unit =fn _ => A.TUPLEexp [], |
| 91 |
gen =fn (trans,cons) => |
| 92 |
let fun f({origName,newName,ty},es) = trans(ty,ID newName)::es |
| 93 |
in A.SEQexp (rev(T.foldCons f [] cons)) |
| 94 |
end |
| 95 |
} |
| 96 |
|
| 97 |
val foldHook = |
| 98 |
PolyGen.HOOK |
| 99 |
{ name ="fold", |
| 100 |
factor=false, |
| 101 |
args =["redex","foldArg"], |
| 102 |
ret ="foldArg", |
| 103 |
unit = fn _ => ID "foldArg", |
| 104 |
gen = fn (trans,cons) => |
| 105 |
T.foldCons |
| 106 |
(fn({origName,newName,ty},e) => |
| 107 |
trans(ty,A.TUPLEexp[ID newName,e])) |
| 108 |
(ID "foldArg") cons |
| 109 |
} |
| 110 |
|
| 111 |
fun compile decl = |
| 112 |
let fun exp _ (A.APPexp(A.IDexp(A.IDENT(["Generic"],"rewrite")), |
| 113 |
A.LETexp(decls,exp))) = |
| 114 |
processDecls rewriteHook (decls,exp) |
| 115 |
| exp _ (A.APPexp(A.IDexp(A.IDENT(["Generic"],"app")), |
| 116 |
A.LETexp(decls,exp))) = |
| 117 |
processDecls appHook (decls,exp) |
| 118 |
| exp _ (A.APPexp(A.IDexp(A.IDENT(["Generic"],"fold")), |
| 119 |
A.LETexp(decls,exp))) = |
| 120 |
processDecls foldHook (decls,exp) |
| 121 |
| exp _ e = e |
| 122 |
in #decl(RW{sexp=NO,ty=NO,decl=NO,exp=exp,pat=NO}) decl |
| 123 |
end |
| 124 |
|
| 125 |
fun gen filename = |
| 126 |
let val decl = A.SEQdecl(Parser.load filename) |
| 127 |
val decl = |
| 128 |
A.SEQdecl |
| 129 |
[A.$["(* WARNING: This file is generated using 'rwgen "^ |
| 130 |
filename^"' *)"], |
| 131 |
compile decl |
| 132 |
] |
| 133 |
in PP.text(AstPP.decl decl) |
| 134 |
end |
| 135 |
|
| 136 |
fun main(_, [filename]) = |
| 137 |
((print(gen filename); 0) |
| 138 |
handle e => (print("Uncaught exception "^exnName e^"\n"); 1)) |
| 139 |
| main(_, _) = (print("Usage: rwgen <filename>\n"); 1) |
| 140 |
|
| 141 |
end |