1 | // $Id: rfp_asail2asail.rf 2488 2007-02-27 18:34:33Z orlov $ |
---|
2 | |
---|
3 | $use Apply Arithm Box Class Compare List StdIO Table; |
---|
4 | |
---|
5 | $func Simplify-Infix t.asail-term = t.asail-term; |
---|
6 | $func Simplify-Arithm e = e; |
---|
7 | |
---|
8 | $func? Simplify-Cmp s.op (expr1) (expr2) = e; |
---|
9 | |
---|
10 | $func Simplify e.asail = e.asail; |
---|
11 | |
---|
12 | $func Remove-Unreachable e.asail = e.asail; |
---|
13 | |
---|
14 | $func Remove-Dupl-Decl (e.decls) (e.subst) e.asail = e.asail; |
---|
15 | |
---|
16 | $func Process-Var (e.decls) (e.subst) s.type t.var = e.maybe-decl (e.decls) (e.subst); |
---|
17 | |
---|
18 | $func Free-Idx = s.idx; |
---|
19 | |
---|
20 | $box Idx; |
---|
21 | |
---|
22 | |
---|
23 | Simplify-ASAIL (MODULE t.ModuleName e.asail) = |
---|
24 | <Store &Idx>, |
---|
25 | (MODULE t.ModuleName <Remove-Dupl-Decl () () <Remove-Unreachable <Simplify e.asail>>>); |
---|
26 | |
---|
27 | Simplify { |
---|
28 | t.first e.rest, t.first : { |
---|
29 | (IF-INT-CMP s.op (e.arg1) (e.arg2) e.body) = { |
---|
30 | <Simplify-Cmp s.op (<Simplify-Arithm (e.arg1)>) (<Simplify-Arithm (e.arg2)>)> : { |
---|
31 | (e1) (e2) = (IF-INT-CMP s.op (e1) (e2) <Simplify e.body>); |
---|
32 | /*empty*/ = <Simplify e.body>; |
---|
33 | };; |
---|
34 | }; |
---|
35 | (IF t.cond e.body) = (IF t.cond <Simplify e.body>); |
---|
36 | (ASSIGN (INT t.var) e.expr) = (ASSIGN (INT t.var) <Simplify-Arithm (e.expr)>); |
---|
37 | (e1) = (<Simplify e1>); |
---|
38 | s1 = s1; |
---|
39 | } :: e.first = |
---|
40 | e.first <Simplify e.rest>;; |
---|
41 | }; |
---|
42 | |
---|
43 | $box Blocks; |
---|
44 | $table Breaks; |
---|
45 | $box Last-Breaks; |
---|
46 | |
---|
47 | $func? GetR s.box = t.right-term; |
---|
48 | GetR s.box = |
---|
49 | <? s.box> : e1 t2, |
---|
50 | <Store s.box e1>, |
---|
51 | t2; |
---|
52 | |
---|
53 | Remove-Unreachable { |
---|
54 | t1 e2, t1 : \{ |
---|
55 | (FUNC t.name t.in t.out e.body) = |
---|
56 | <Put &Blocks (FUNC t.name t.in t.out)>, |
---|
57 | <Clear-Table &Breaks>, |
---|
58 | e.body; |
---|
59 | (FUNC? t.name t.in t.out e.body) = |
---|
60 | <Put &Blocks (FUNC? t.name t.in t.out)>, |
---|
61 | <Clear-Table &Breaks>, |
---|
62 | e.body; |
---|
63 | (FOR (e.cont) (e.break) (e.cond) (e.step) e.body) = |
---|
64 | <Put &Blocks (FOR (e.cont) (e.break) (e.cond) (e.step))>, e.body; |
---|
65 | (LABEL (t.label) e.body) = |
---|
66 | <Put &Blocks (LABEL (t.label))>, e.body; |
---|
67 | (IF t.cond e.body) = |
---|
68 | <Put &Blocks (IF t.cond)>, e.body; |
---|
69 | (IF-INT-CMP s.op t.arg1 t.arg2 e.body) = |
---|
70 | <Put &Blocks (IF-INT-CMP s.op t.arg1 t.arg2)>, e.body; |
---|
71 | (TRY e.body) = |
---|
72 | <Put &Blocks (TRY)>, e.body; |
---|
73 | (CATCH-ERROR e.body) = |
---|
74 | <Put &Blocks (CATCH-ERROR)>, e.body; |
---|
75 | } :: e.body = |
---|
76 | <Put &Last-Breaks ()>, |
---|
77 | <Remove-Unreachable e.body> :: e.body, |
---|
78 | <GetR &Blocks> : { |
---|
79 | (LABEL (t.label)) = { |
---|
80 | <In-Table? &Breaks t.label> = (LABEL (t.label) e.body) (e2); |
---|
81 | { |
---|
82 | <? &Blocks> : e (LABEL (t.other-label)), |
---|
83 | <Lookup &Breaks t.other-label> : /*empty*/ = |
---|
84 | <Unbind &Breaks t.other-label>;; |
---|
85 | } = |
---|
86 | /*empty*/ (e.body e2); |
---|
87 | }; |
---|
88 | (FOR (e.cont) (e.break) (/*empty-cond*/) (e.step)) = { |
---|
89 | <In-Table? &Breaks e.break> = |
---|
90 | (FOR (e.cont) (e.break) (/*empty-cond*/) (e.step) e.body) (e2); |
---|
91 | (FOR (e.cont) (e.break) (/*empty-cond*/) (e.step) e.body) (); |
---|
92 | }; |
---|
93 | (TRY) = { |
---|
94 | <? &Last-Breaks> : e (e Normal-Exit) = |
---|
95 | (TRY e.body) (e2); |
---|
96 | e2 : (CATCH-ERROR e.catch-body) e3, |
---|
97 | (TRY e.body) ((CATCH-ERROR e.catch-body e3)); |
---|
98 | }; |
---|
99 | (e.item) = (e.item e.body) (e2); |
---|
100 | } :: e.t1 (e2), |
---|
101 | <GetR &Last-Breaks> :: t.breaks, |
---|
102 | { |
---|
103 | e.t1 : v, |
---|
104 | t.breaks : (e t.l e), |
---|
105 | <Bind &Breaks (t.l) (Persistent)>, |
---|
106 | $fail;; |
---|
107 | }, |
---|
108 | e.t1 <Remove-Unreachable e2>; |
---|
109 | t1 e2, t1 : \{ |
---|
110 | (BREAK t.label) = |
---|
111 | <GetR &Last-Breaks> : (e.breaks), |
---|
112 | <Put &Last-Breaks (e.breaks t.label)>, |
---|
113 | { |
---|
114 | <? &Blocks> : e (LABEL (t.label)); |
---|
115 | <Bind &Breaks (t.label) ()>, t1; |
---|
116 | }; |
---|
117 | (CONTINUE t.label) = { |
---|
118 | <? &Blocks> : e (FOR (t.label) (e.break) (e.cond) (e.step)); |
---|
119 | t1; |
---|
120 | }; |
---|
121 | RETFAIL = t1; |
---|
122 | (ERROR e) = t1; |
---|
123 | FATAL = t1; |
---|
124 | }; |
---|
125 | t1 e2 = t1 <Remove-Unreachable e2>; |
---|
126 | /*empty*/ = |
---|
127 | { |
---|
128 | <GetR &Last-Breaks> : (e.breaks), |
---|
129 | <Put &Last-Breaks (e.breaks Normal-Exit)>;; |
---|
130 | }; |
---|
131 | }; |
---|
132 | |
---|
133 | |
---|
134 | Remove-Dupl-Decl (e.decls) (e.subst) e.items, e.items : { |
---|
135 | t1 e.rest = { |
---|
136 | t1 : (DECL s.type t.var) = |
---|
137 | <Process-Var (e.decls) (e.subst) s.type t.var> :: e.d (e.decls) (e.subst), |
---|
138 | e.d <Remove-Dupl-Decl (e.decls) (e.subst) e.rest>; |
---|
139 | t1 : (s.split expr (e.len) t.var1 t.var2), s.split : \{ LSPLIT; RSPLIT; } = |
---|
140 | <Remove-Dupl-Decl (e.decls) (e.subst) expr (e.len)> :: expr, |
---|
141 | <Process-Var (e.decls) (e.subst) Split t.var1> :: e.decl1 (e.decls) (e.subst), |
---|
142 | { |
---|
143 | e.decl1 : (DECL s.type t.new-var) = t.new-var; |
---|
144 | t.var1; |
---|
145 | } :: t.var1, |
---|
146 | <Process-Var (e.decls) (e.subst) Split t.var2> :: e.decl2 (e.decls) (e.subst), |
---|
147 | { |
---|
148 | e.decl2 : (DECL s.type t.new-var) = t.new-var; |
---|
149 | t.var2; |
---|
150 | } :: t.var2, |
---|
151 | (s.split expr t.var1 t.var2) <Remove-Dupl-Decl (e.decls) (e.subst) e.rest>; |
---|
152 | e.subst : e (t1 t.new-var) e = |
---|
153 | <Remove-Dupl-Decl (e.decls) (e.subst) t.new-var e.rest>; |
---|
154 | t1 : (e2) = |
---|
155 | (<Remove-Dupl-Decl (e.decls) (e.subst) e2>) <Remove-Dupl-Decl (e.decls) (e.subst) e.rest>; |
---|
156 | t1 <Remove-Dupl-Decl (e.decls) (e.subst) e.rest>; |
---|
157 | }; |
---|
158 | /*empty*/ = /*empty*/; |
---|
159 | }; |
---|
160 | |
---|
161 | Process-Var (e.decls) (e.subst) s.type t.var, { |
---|
162 | e.decls : $r e (DECL s.type-old t.var) e = { |
---|
163 | s.type : s.type-old = /*empty*/ (e.decls) (e.subst); |
---|
164 | t.var : (s.tag e (e.name)), |
---|
165 | (s.tag 'dd' (e.name <Free-Idx>)) :: t.new-var, |
---|
166 | (DECL s.type t.new-var) :: t.decl, |
---|
167 | t.decl (e.decls t.decl) (e.subst (t.var t.new-var)); |
---|
168 | }; |
---|
169 | (DECL s.type t.var) (e.decls (DECL s.type t.var)) (e.subst); |
---|
170 | }; |
---|
171 | |
---|
172 | |
---|
173 | $func "Eval *" e = e; |
---|
174 | $func "Eval /" e = e; |
---|
175 | $func "Eval %" e = e; |
---|
176 | $func "Eval +" e = e; |
---|
177 | $func "Eval -" e = e; |
---|
178 | |
---|
179 | Simplify-Infix /*txxx = <WriteLN SSS txxx>, txxx :*/ { |
---|
180 | (INFIX s.op e.args), s.op : { |
---|
181 | "*" = <Foldr1 &"Eval *" (<Map &Simplify-Arithm (e.args)>)>; |
---|
182 | "/" = <Foldr1 &"Eval /" (<Map &Simplify-Arithm (e.args)>)>; |
---|
183 | "%" = <Foldr1 &"Eval %" (<Map &Simplify-Arithm (e.args)>)>; |
---|
184 | "+" = <Foldr1 &"Eval +" (<Map &Simplify-Arithm (e.args)>)>; |
---|
185 | "-" = <Foldr1 &"Eval -" (<Map &Simplify-Arithm (e.args)>)>; |
---|
186 | s = <Map &Simplify-Arithm (e.args)>; |
---|
187 | } : { |
---|
188 | t1 = t1; |
---|
189 | e1 = (INFIX s.op <Paren e1>); |
---|
190 | }; |
---|
191 | (e1) = (<Map &Simplify-Infix (e1)>); |
---|
192 | s1 = s1; |
---|
193 | }; |
---|
194 | |
---|
195 | Simplify-Arithm (e.args) = |
---|
196 | <Foldr1 &"Eval +" (<Map &Simplify-Infix (e.args)>)> : { |
---|
197 | t1 = t1; |
---|
198 | e1 = (INFIX "+" <Paren e1>); |
---|
199 | }; |
---|
200 | |
---|
201 | "Eval *" { |
---|
202 | 0 e = 0; |
---|
203 | e 0 = 0; |
---|
204 | 1 e2 = e2; |
---|
205 | e1 1 = e1; |
---|
206 | s1 e2 s3 = e2 <"*" s1 s3>; |
---|
207 | s1 e2 = e2 s1; |
---|
208 | e2 = e2; |
---|
209 | }; |
---|
210 | |
---|
211 | "Eval +" { |
---|
212 | 0 e2 = e2; |
---|
213 | e1 0 = e1; |
---|
214 | s1 e2 s3 = e2 <"+" s1 s3>; |
---|
215 | s1 e2 = e2 s1; |
---|
216 | e2 = e2; |
---|
217 | }; |
---|
218 | |
---|
219 | "Eval /" { |
---|
220 | e1 1 = e1; |
---|
221 | e1 = e1; |
---|
222 | }; |
---|
223 | |
---|
224 | "Eval %" { |
---|
225 | e1 1 = 0; |
---|
226 | e1 = e1; |
---|
227 | }; |
---|
228 | |
---|
229 | "Eval -" { |
---|
230 | e1 0 = e1; |
---|
231 | e1 = e1; |
---|
232 | }; |
---|
233 | |
---|
234 | $func? Cmp s.fn (expr1) (expr2) = e; |
---|
235 | |
---|
236 | Simplify-Cmp s.op (expr1) (expr2) = |
---|
237 | s.op : { "!=" = &"/="; ">" = &">"; "<" = &"<"; } :: s.op, |
---|
238 | <Cmp s.op (expr1) (expr2)>; |
---|
239 | |
---|
240 | Cmp { |
---|
241 | s.fn (e11 tx e12) (e21 tx e22) = |
---|
242 | <Cmp s.fn (e11 e12) (e21 e22)>; |
---|
243 | s.fn (e1) (e2), <All &Int? (e1)>, <All &Int? (e2)> = |
---|
244 | <Apply s.fn (<Foldr1 &"+" (e1)>) (<Foldr1 &"+" (e2)>)>; |
---|
245 | s.fn (e1) (e2) = (e1) (e2); |
---|
246 | }; |
---|
247 | |
---|
248 | |
---|
249 | Free-Idx = |
---|
250 | { |
---|
251 | <? &Idx> : s1 = <"+" s1 1>; |
---|
252 | 1; |
---|
253 | } :: s1, |
---|
254 | <Store &Idx s1>, |
---|
255 | s1; |
---|
256 | |
---|