Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/nowhere/nowhere.sml

raw
1 structure NoWhere =
2 struct
3
4 local
5
6 val i2s = Int.toString
7
8 val basis =
9 "datatype 'a list = nil | :: of 'a * 'a list "^
10 "datatype 'a option = NONE | SOME of 'a "^
11 "datatype order = LESS | EQUAL | GREATER "
12
13 val version = "1.2.2"
14
15 fun WARNING file =
16 "(* WARNING: this is generated by running 'nowhere "^file^"'.\n"^
17 " * Do not edit this file directly.\n"^
18 " * Version "^version^"\n"^
19 " *)\n"
20
21 structure Ast = MDLAst
22 structure AstUtil = MDLAstUtil(MDLAst)
23 structure AstPP = MDLAstPrettyPrinter(AstUtil)
24 structure AstRewriter = MDLAstRewriter(MDLAst)
25 structure MG = MatchGen(structure AstPP = AstPP
26 structure AstUtil = AstUtil
27 structure AstRewriter = AstRewriter)
28 structure LitMap = MG.LitMap
29 structure Parser = MDLParserDriver
30 (structure AstPP = AstPP val MDLmode = false val extraCells = [])
31 structure MC = MG.MC
32
33 open Ast MDLError AstUtil
34 val NO = AstRewriter.noRewrite
35 val rw = AstRewriter.rewrite
36 val ++ = PP.++
37 infix ++
38 in
39
40
41 fun gen filename =
42 let (* parse file *)
43 val program = Parser.load filename
44
45 val () = MG.init()
46
47 (* By default, we take after ML *)
48 fun failure() = RAISEexp(ID "Match")
49
50 val literals = ref MG.LitMap.empty
51
52 fun trans[LOCALdecl(defs, body)] =
53 let val basis = Parser.parseString basis
54 val dts = MG.compileTypes(basis @ defs)
55
56 (* Translate a case statement *)
57 fun compileCase(root, clauses) =
58 let val dfa = MG.compile dts clauses
59 val _ = MG.report{warning=warning, error=error,
60 log=log, dfa=dfa, rules=clauses}
61 (* val _ = print(MG.MC.toString dfa) *)
62 in MG.codeGen{root=root, dfa=dfa, fail=failure,
63 literals=literals}
64 end handle MC.MatchCompiler msg =>
65 (error msg; CASEexp(root,clauses)) (* just continue *)
66
67 fun exp _ (e as CASEexp(r,cs)) = (* case expr *)
68 if MG.isComplex cs then compileCase(r, cs) else e
69 | exp _ e = e
70
71 fun fbind (fb as FUNbind(f,cs as c::_)) =
72 if MG.isComplex cs then (* expand function *)
73 let val CLAUSE(args,_,_) = c
74 val arity = length args
75 val vars = List.tabulate(arity, fn i => "p_"^i2s i)
76 val root = TUPLEexp(map ID vars)
77 val cs' = map (fn CLAUSE(ps, g, e) =>
78 CLAUSE([TUPLEpat ps], g, e)) cs
79 val body = compileCase(root, cs')
80 in FUNbind(f, [CLAUSE(map IDpat vars, NONE, body)])
81 end
82 else fb
83 | fbind fb = fb
84
85 fun decl _ (FUNdecl(fbs)) = FUNdecl(map fbind fbs)
86 | decl _ d = d
87
88 val prog =
89 #decl(rw{exp=exp,ty=NO,pat=NO,decl=decl,sexp=NO})
90 (SEQdecl body)
91
92 fun lit _ (VALdecl[VALbind(WILDpat,
93 LITexp(STRINGlit "literals"))]) =
94 VALdecl(LitMap.foldri (fn (l,v,d) =>
95 VALbind(IDpat v,LITexp l)::d) []
96 (!literals)) before literals := LitMap.empty
97 | lit _ d = d
98
99 val prog = #decl(rw{exp=NO,ty=NO,pat=NO,decl=lit,sexp=NO}) prog
100 in if LitMap.numItems(!literals) > 0 then
101 fail "missing declaration val _ = \"literals\""
102 else ();
103 prog
104 end
105 | trans[SEQdecl d] = trans d
106 | trans[MARKdecl(_,d)] = trans [d]
107 | trans _ = fail "program must be wrapped with local"
108
109 val program = trans program
110 val text = PP.text(PP.setmode "code" ++
111 PP.textWidth 160 ++
112 AstPP.decl program)
113 in WARNING filename^text
114 end
115
116 fun main x =
117 if GenFile.gen {program="nowhere", fileSuffix="sml", trans=gen } x = 0
118 then OS.Process.success
119 else OS.Process.failure
120
121 end
122
123 end