Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/MatchCompiler/test-match-gen.sml

raw
1 structure TestMatchGen =
2 struct
3
4 local
5 structure AstUtil = MDLAstUtil(MDLAst)
6 structure AstPP = MDLAstPrettyPrinter(AstUtil)
7 structure AstRewriter = MDLAstRewriter(MDLAst)
8 structure MG =
9 MatchGen(structure AstPP = AstPP
10 structure AstUtil = AstUtil
11 structure AstRewriter = AstRewriter
12 )
13 structure MC = MG.MC
14
15 open AstPP AstUtil AstPP.Ast
16
17 fun newDatatype(id,cbs) = DATATYPE(id,[],cbs)
18 fun ty id = IDty(IDENT([],id))
19
20 val footy = ty "foo"
21
22 val defs =
23 [newDatatype("foo",[CONS("A",SOME(TUPLEty[footy,footy])),
24 CONS("B",NONE),
25 CONS("C",NONE),
26 CONS("D",SOME(RECORDty[("x",footy),("y",footy)]))
27 ]
28 )
29 ]
30 val info = MG.compileTypes defs
31
32 fun test root rules =
33 let val clauses = map (fn (p, g, x) => CLAUSE([p],g,INTexp(x))) rules
34 val _ = print(PP.text(AstPP.exp(CASEexp(root,clauses)))^"\n")
35 val dfa = MG.compile info clauses
36 (* val _ = print(MC.toString dfa) *)
37 fun fail() = RAISEexp(ID "Match")
38 val code = MG.codeGen {root=root, dfa=dfa, fail=fail}
39 in print(PP.text(AstPP.exp code)^"\n")
40 end handle MC.MatchCompiler msg => print msg
41
42 fun CONS(x,[]) = CONSpat(IDENT([],x),NONE)
43 | CONS(x,[a]) = CONSpat(IDENT([],x),SOME a)
44 | CONS(x,xs) = CONSpat(IDENT([],x),SOME(TUPLEpat xs))
45
46 val WILD = WILDpat
47
48 in
49
50 fun rule1() =
51 test
52 (ID "B")
53 [ (CONS("A",[WILD,WILD]), NONE, 0)
54 ]
55
56 fun rule2() =
57 test
58 (ID "B")
59 [ (CONS("A",[WILD,WILD]), NONE, 0),
60 (CONS("B",[]), NONE, 1)
61 ]
62
63 fun rule3() =
64 test
65 (ID "B")
66 [ (CONS("A",[WILD, CONS("B",[])]), NONE, 0),
67 (CONS("A",[CONS("B",[]), WILD]), NONE, 1)
68 ]
69
70 fun rule4() =
71 test
72 (ID "B")
73 [ (CONS("A",[CONS("B",[]), CONS("B",[])]), NONE, 0),
74 (CONS("A",[IDpat "a", IDpat "b"]), NONE, 1)
75 ]
76
77 fun rule5() =
78 test
79 (ID "B")
80 [ (CONS("A",[CONS("B",[]), CONS("B",[])]), NONE, 0),
81 (CONS("A",[IDpat "c", CONS("B",[])]), NONE, 1),
82 (CONS("A",[IDpat "a", IDpat "b"]), NONE, 2),
83 (ASpat("u",CONS("B",[])), NONE, 3)
84 ]
85
86 fun rule6() =
87 test
88 (TUPLEexp[ID "B",ID "C"])
89 [ (TUPLEpat[CONS("A",[WILD, WILD]), CONS("B",[])], NONE, 0),
90 (TUPLEpat[WILD, WILD], NONE, 1)
91 ]
92
93 fun rule7() =
94 test
95 (ID "B")
96 [ (CONS("D",[RECORDpat([("x",IDpat "x"),
97 ("y",CONS("B",[]))],false)]), NONE, 0)
98 ]
99
100 fun rule8() =
101 test
102 (ID "B")
103 [ (CONS("D",[RECORDpat([("x",IDpat "x"),("y",CONS("B",[]))],false)]),
104 SOME(APP("=",TUPLEexp[ID "x", ID "C"])), 0)
105 ]
106
107 fun rule9() =
108 test
109 (ID "B")
110 [ (CONS("A",[IDpat "x", CONS("B",[])]),
111 SOME(APP("=",TUPLEexp[ID "x", ID "C"])), 0),
112 (CONS("A",[CONS("B",[]), ASpat("z", CONS("C",[]))]),
113 SOME(APP("=",TUPLEexp[ID "z", ID "C"])), 1),
114 (CONS("A",[CONS("B",[]), CONS("C",[])]), NONE, 2),
115 (CONS("A",[CONS("B",[]), CONS("B",[])]), NONE, 3),
116 (IDpat "z", NONE, 4)
117 ]
118
119 end
120 end