]>
Commit | Line | Data |
---|---|---|
3b52fd1a JF |
1 | #include "Parser.hpp" |
2 | ||
3b52fd1a JF |
3 | #include <iomanip> |
4 | ||
4de0686f | 5 | #include "Replace.hpp" |
3b52fd1a JF |
6 | |
7 | CYExpression *CYAddressOf::Replace(CYContext &context) { | |
8 | CYPrefix::Replace(context); | |
9 | return $C0($M(rhs_, $S("$cya"))); | |
10 | } | |
11 | ||
12 | void CYArgument::Replace(CYContext &context) { $T() | |
13 | context.Replace(value_); | |
14 | next_->Replace(context); | |
15 | } | |
16 | ||
17 | CYExpression *CYArray::Replace(CYContext &context) { | |
18 | elements_->Replace(context); | |
19 | return NULL; | |
20 | } | |
21 | ||
22 | CYExpression *CYArrayComprehension::Replace(CYContext &context) { | |
23 | CYVariable *cyv($V("$cyv")); | |
24 | ||
25 | return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->* | |
26 | $E($ CYAssign(cyv, $ CYArray()))->* | |
27 | comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->* | |
28 | $ CYReturn(cyv) | |
29 | )); | |
30 | } | |
31 | ||
32 | CYExpression *CYAssignment::Replace(CYContext &context) { | |
33 | context.Replace(lhs_); | |
34 | context.Replace(rhs_); | |
35 | return NULL; | |
36 | } | |
37 | ||
38 | CYStatement *CYBlock::Replace(CYContext &context) { | |
39 | statements_ = statements_->ReplaceAll(context); | |
40 | return NULL; | |
41 | } | |
42 | ||
43 | CYStatement *CYBreak::Replace(CYContext &context) { | |
44 | return NULL; | |
45 | } | |
46 | ||
47 | CYExpression *CYCall::Replace(CYContext &context) { | |
48 | context.Replace(function_); | |
49 | arguments_->Replace(context); | |
50 | return NULL; | |
51 | } | |
52 | ||
53 | void CYCatch::Replace(CYContext &context) { $T() | |
54 | code_.Replace(context); | |
55 | } | |
56 | ||
3b52fd1a JF |
57 | void CYClause::Replace(CYContext &context) { $T() |
58 | context.Replace(case_); | |
59 | statements_ = statements_->ReplaceAll(context); | |
60 | next_->Replace(context); | |
61 | } | |
62 | ||
63 | CYExpression *CYCompound::Replace(CYContext &context) { | |
64 | expressions_ = expressions_->ReplaceAll(context); | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL) | |
69 | CYFunctionParameter *next(next_->Parameters(context)); | |
70 | if (CYFunctionParameter *parameter = Parameter(context)) { | |
71 | parameter->SetNext(next); | |
72 | return parameter; | |
73 | } else | |
74 | return next; | |
75 | } | |
76 | ||
77 | CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
78 | return next_ == NULL ? statement : next_->Replace(context, statement); | |
79 | } | |
80 | ||
81 | CYExpression *CYCondition::Replace(CYContext &context) { | |
82 | context.Replace(test_); | |
83 | context.Replace(true_); | |
84 | context.Replace(false_); | |
85 | return NULL; | |
86 | } | |
87 | ||
88 | CYStatement *CYContinue::Replace(CYContext &context) { | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | CYExpression *CYDeclaration::ForEachIn(CYContext &context) { | |
93 | return $ CYVariable(identifier_); | |
94 | } | |
95 | ||
96 | void CYDeclaration::Replace(CYContext &context) { | |
97 | context.Replace(initialiser_); | |
98 | } | |
99 | ||
100 | void CYDeclarations::Replace(CYContext &context) { $T() | |
101 | declaration_->Replace(context); | |
102 | next_->Replace(context); | |
103 | } | |
104 | ||
105 | CYExpression *CYDirectMember::Replace(CYContext &context) { | |
106 | Replace_(context); | |
107 | return NULL; | |
108 | } | |
109 | ||
110 | CYStatement *CYDoWhile::Replace(CYContext &context) { | |
111 | context.Replace(test_); | |
112 | context.Replace(code_); | |
113 | return NULL; | |
114 | } | |
115 | ||
116 | void CYElement::Replace(CYContext &context) { $T() | |
117 | context.Replace(value_); | |
118 | next_->Replace(context); | |
119 | } | |
120 | ||
121 | CYStatement *CYEmpty::Replace(CYContext &context) { | |
122 | return NULL; | |
123 | } | |
124 | ||
125 | CYStatement *CYExpress::Replace(CYContext &context) { | |
126 | context.Replace(expression_); | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | CYExpression *CYExpression::ClassName(CYContext &context, bool object) { | |
131 | return this; | |
132 | } | |
133 | ||
134 | CYExpression *CYExpression::ForEachIn(CYContext &context) { | |
135 | return this; | |
136 | } | |
137 | ||
138 | CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL) | |
139 | CYExpression *replace(this); | |
140 | context.Replace(replace); | |
141 | ||
142 | if (CYExpression *next = next_->ReplaceAll(context)) | |
143 | replace->SetNext(next); | |
144 | else | |
145 | replace->SetNext(next_); | |
146 | ||
147 | return replace; | |
148 | } | |
149 | ||
3b52fd1a JF |
150 | void CYFinally::Replace(CYContext &context) { $T() |
151 | code_.Replace(context); | |
152 | } | |
153 | ||
154 | CYStatement *CYFor::Replace(CYContext &context) { | |
155 | // XXX: initialiser_ | |
156 | context.Replace(test_); | |
157 | context.Replace(increment_); | |
158 | context.Replace(code_); | |
159 | return NULL; | |
160 | } | |
161 | ||
162 | CYStatement *CYForIn::Replace(CYContext &context) { | |
163 | // XXX: initialiser_ | |
164 | context.Replace(set_); | |
165 | context.Replace(code_); | |
166 | return NULL; | |
167 | } | |
168 | ||
169 | CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const { | |
170 | return $ CYFunctionParameter(name_); | |
171 | } | |
172 | ||
173 | CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
174 | return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement)); | |
175 | } | |
176 | ||
177 | CYStatement *CYForEachIn::Replace(CYContext &context) { | |
178 | CYVariable *cys($V("$cys")), *cyt($V("$cyt")); | |
179 | ||
180 | return $ CYWith($ CYObject($ CYProperty($S("$cys"), $D(0), $ CYProperty($S("$cyt"), $D(0)))), $ CYBlock($$->* | |
181 | $E($ CYAssign(cys, set_))->* | |
182 | $ CYForIn(cyt, cys, $ CYBlock($$->* | |
183 | $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->* | |
184 | code_ | |
185 | )) | |
186 | )); | |
187 | } | |
188 | ||
189 | CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const { | |
190 | return $ CYFunctionParameter(name_); | |
191 | } | |
192 | ||
193 | CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
194 | CYVariable *cys($V("$cys")), *name($ CYVariable(name_)); | |
195 | ||
196 | return $E($C0($F(NULL, $P1("$cys"), $$->* | |
197 | $E($ CYAssign(cys, set_))->* | |
198 | $ CYForIn(name, cys, $ CYBlock($$->* | |
199 | $E($ CYAssign(name, $M(cys, name)))->* | |
200 | CYComprehension::Replace(context, statement) | |
201 | )) | |
202 | ))); | |
203 | } | |
204 | ||
205 | void CYFunction::Replace_(CYContext &context) { | |
206 | code_.Replace(context); | |
207 | } | |
208 | ||
209 | CYExpression *CYFunctionExpression::Replace(CYContext &context) { | |
210 | Replace_(context); | |
211 | return NULL; | |
212 | } | |
213 | ||
214 | CYStatement *CYFunctionStatement::Replace(CYContext &context) { | |
215 | Replace_(context); | |
216 | return NULL; | |
217 | } | |
218 | ||
219 | CYStatement *CYIf::Replace(CYContext &context) { | |
220 | context.Replace(test_); | |
221 | context.Replace(true_); | |
222 | context.Replace(false_); | |
223 | return NULL; | |
224 | } | |
225 | ||
226 | CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const { | |
227 | return NULL; | |
228 | } | |
229 | ||
230 | CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
231 | return $ CYIf(test_, CYComprehension::Replace(context, statement)); | |
232 | } | |
233 | ||
234 | CYExpression *CYIndirect::Replace(CYContext &context) { | |
235 | CYPrefix::Replace(context); | |
236 | return $M(rhs_, $S("$cyi")); | |
237 | } | |
238 | ||
239 | CYExpression *CYIndirectMember::Replace(CYContext &context) { | |
240 | Replace_(context); | |
241 | return $M($ CYIndirect(object_), property_); | |
242 | } | |
243 | ||
244 | CYExpression *CYInfix::Replace(CYContext &context) { | |
245 | context.Replace(lhs_); | |
246 | context.Replace(rhs_); | |
247 | return NULL; | |
248 | } | |
249 | ||
250 | CYStatement *CYLabel::Replace(CYContext &context) { | |
251 | context.Replace(statement_); | |
252 | return NULL; | |
253 | } | |
254 | ||
255 | void CYMember::Replace_(CYContext &context) { | |
256 | context.Replace(object_); | |
257 | context.Replace(property_); | |
258 | } | |
259 | ||
3b52fd1a JF |
260 | CYExpression *CYNew::Replace(CYContext &context) { |
261 | context.Replace(constructor_); | |
262 | arguments_->Replace(context); | |
263 | return NULL; | |
264 | } | |
265 | ||
266 | CYExpression *CYObject::Replace(CYContext &context) { | |
267 | properties_->Replace(context); | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | CYExpression *CYPostfix::Replace(CYContext &context) { | |
272 | context.Replace(lhs_); | |
273 | return NULL; | |
274 | } | |
275 | ||
276 | CYExpression *CYPrefix::Replace(CYContext &context) { | |
277 | context.Replace(rhs_); | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | void CYProgram::Replace(CYContext &context) { | |
282 | statements_ = statements_->ReplaceAll(context); | |
283 | } | |
284 | ||
285 | void CYProperty::Replace(CYContext &context) { $T() | |
286 | context.Replace(value_); | |
287 | next_->Replace(context); | |
288 | } | |
289 | ||
290 | CYStatement *CYReturn::Replace(CYContext &context) { | |
291 | context.Replace(value_); | |
292 | return NULL; | |
293 | } | |
294 | ||
3b52fd1a JF |
295 | CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL) |
296 | CYStatement *replace(this); | |
297 | context.Replace(replace); | |
298 | ||
299 | if (CYStatement *next = next_->ReplaceAll(context)) | |
300 | replace->SetNext(next); | |
301 | else | |
302 | replace->SetNext(next_); | |
303 | ||
304 | return replace; | |
305 | } | |
306 | ||
307 | CYStatement *CYSwitch::Replace(CYContext &context) { | |
308 | context.Replace(value_); | |
309 | clauses_->Replace(context); | |
310 | return NULL; | |
311 | } | |
312 | ||
313 | CYExpression *CYThis::Replace(CYContext &context) { | |
314 | return NULL; | |
315 | } | |
316 | ||
317 | CYStatement *CYThrow::Replace(CYContext &context) { | |
318 | context.Replace(value_); | |
319 | return NULL; | |
320 | } | |
321 | ||
322 | CYExpression *CYTrivial::Replace(CYContext &context) { | |
323 | return NULL; | |
324 | } | |
325 | ||
326 | CYStatement *CYTry::Replace(CYContext &context) { | |
327 | code_.Replace(context); | |
328 | catch_->Replace(context); | |
329 | finally_->Replace(context); | |
330 | return NULL; | |
331 | } | |
332 | ||
333 | CYStatement *CYVar::Replace(CYContext &context) { | |
334 | declarations_->Replace(context); | |
335 | return NULL; | |
336 | } | |
337 | ||
338 | CYExpression *CYVariable::Replace(CYContext &context) { | |
339 | return NULL; | |
340 | } | |
341 | ||
342 | CYStatement *CYWhile::Replace(CYContext &context) { | |
343 | context.Replace(test_); | |
344 | context.Replace(code_); | |
345 | return NULL; | |
346 | } | |
347 | ||
348 | CYStatement *CYWith::Replace(CYContext &context) { | |
349 | context.Replace(scope_); | |
350 | context.Replace(code_); | |
351 | return NULL; | |
352 | } | |
353 | ||
354 | CYExpression *CYWord::ClassName(CYContext &context, bool object) { | |
355 | CYString *name($S(this)); | |
356 | if (object) | |
357 | return $C1($V("objc_getClass"), name); | |
358 | else | |
359 | return name; | |
360 | } |