]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Moved -fPIC to base configuration.
[cycript.git] / Replace.cpp
CommitLineData
3b52fd1a
JF
1#include "Parser.hpp"
2
3b52fd1a
JF
3#include <iomanip>
4
4de0686f 5#include "Replace.hpp"
3b52fd1a
JF
6
7CYExpression *CYAddressOf::Replace(CYContext &context) {
8 CYPrefix::Replace(context);
9 return $C0($M(rhs_, $S("$cya")));
10}
11
12void CYArgument::Replace(CYContext &context) { $T()
13 context.Replace(value_);
14 next_->Replace(context);
15}
16
17CYExpression *CYArray::Replace(CYContext &context) {
18 elements_->Replace(context);
19 return NULL;
20}
21
22CYExpression *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
32CYExpression *CYAssignment::Replace(CYContext &context) {
33 context.Replace(lhs_);
34 context.Replace(rhs_);
35 return NULL;
36}
37
38CYStatement *CYBlock::Replace(CYContext &context) {
39 statements_ = statements_->ReplaceAll(context);
40 return NULL;
41}
42
43CYStatement *CYBreak::Replace(CYContext &context) {
44 return NULL;
45}
46
47CYExpression *CYCall::Replace(CYContext &context) {
48 context.Replace(function_);
49 arguments_->Replace(context);
50 return NULL;
51}
52
53void CYCatch::Replace(CYContext &context) { $T()
54 code_.Replace(context);
55}
56
3b52fd1a
JF
57void CYClause::Replace(CYContext &context) { $T()
58 context.Replace(case_);
59 statements_ = statements_->ReplaceAll(context);
60 next_->Replace(context);
61}
62
63CYExpression *CYCompound::Replace(CYContext &context) {
64 expressions_ = expressions_->ReplaceAll(context);
65 return NULL;
66}
67
68CYFunctionParameter *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
77CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
78 return next_ == NULL ? statement : next_->Replace(context, statement);
79}
80
81CYExpression *CYCondition::Replace(CYContext &context) {
82 context.Replace(test_);
83 context.Replace(true_);
84 context.Replace(false_);
85 return NULL;
86}
87
88CYStatement *CYContinue::Replace(CYContext &context) {
89 return NULL;
90}
91
92CYExpression *CYDeclaration::ForEachIn(CYContext &context) {
93 return $ CYVariable(identifier_);
94}
95
96void CYDeclaration::Replace(CYContext &context) {
97 context.Replace(initialiser_);
98}
99
100void CYDeclarations::Replace(CYContext &context) { $T()
101 declaration_->Replace(context);
102 next_->Replace(context);
103}
104
105CYExpression *CYDirectMember::Replace(CYContext &context) {
106 Replace_(context);
107 return NULL;
108}
109
110CYStatement *CYDoWhile::Replace(CYContext &context) {
111 context.Replace(test_);
112 context.Replace(code_);
113 return NULL;
114}
115
116void CYElement::Replace(CYContext &context) { $T()
117 context.Replace(value_);
118 next_->Replace(context);
119}
120
121CYStatement *CYEmpty::Replace(CYContext &context) {
122 return NULL;
123}
124
125CYStatement *CYExpress::Replace(CYContext &context) {
126 context.Replace(expression_);
127 return NULL;
128}
129
130CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
131 return this;
132}
133
134CYExpression *CYExpression::ForEachIn(CYContext &context) {
135 return this;
136}
137
138CYExpression *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
150void CYFinally::Replace(CYContext &context) { $T()
151 code_.Replace(context);
152}
153
154CYStatement *CYFor::Replace(CYContext &context) {
155 // XXX: initialiser_
156 context.Replace(test_);
157 context.Replace(increment_);
158 context.Replace(code_);
159 return NULL;
160}
161
162CYStatement *CYForIn::Replace(CYContext &context) {
163 // XXX: initialiser_
164 context.Replace(set_);
165 context.Replace(code_);
166 return NULL;
167}
168
169CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
170 return $ CYFunctionParameter(name_);
171}
172
173CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
174 return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement));
175}
176
177CYStatement *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
189CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
190 return $ CYFunctionParameter(name_);
191}
192
193CYStatement *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
205void CYFunction::Replace_(CYContext &context) {
206 code_.Replace(context);
207}
208
209CYExpression *CYFunctionExpression::Replace(CYContext &context) {
210 Replace_(context);
211 return NULL;
212}
213
214CYStatement *CYFunctionStatement::Replace(CYContext &context) {
215 Replace_(context);
216 return NULL;
217}
218
219CYStatement *CYIf::Replace(CYContext &context) {
220 context.Replace(test_);
221 context.Replace(true_);
222 context.Replace(false_);
223 return NULL;
224}
225
226CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
227 return NULL;
228}
229
230CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
231 return $ CYIf(test_, CYComprehension::Replace(context, statement));
232}
233
234CYExpression *CYIndirect::Replace(CYContext &context) {
235 CYPrefix::Replace(context);
236 return $M(rhs_, $S("$cyi"));
237}
238
239CYExpression *CYIndirectMember::Replace(CYContext &context) {
240 Replace_(context);
241 return $M($ CYIndirect(object_), property_);
242}
243
244CYExpression *CYInfix::Replace(CYContext &context) {
245 context.Replace(lhs_);
246 context.Replace(rhs_);
247 return NULL;
248}
249
250CYStatement *CYLabel::Replace(CYContext &context) {
251 context.Replace(statement_);
252 return NULL;
253}
254
255void CYMember::Replace_(CYContext &context) {
256 context.Replace(object_);
257 context.Replace(property_);
258}
259
3b52fd1a
JF
260CYExpression *CYNew::Replace(CYContext &context) {
261 context.Replace(constructor_);
262 arguments_->Replace(context);
263 return NULL;
264}
265
266CYExpression *CYObject::Replace(CYContext &context) {
267 properties_->Replace(context);
268 return NULL;
269}
270
271CYExpression *CYPostfix::Replace(CYContext &context) {
272 context.Replace(lhs_);
273 return NULL;
274}
275
276CYExpression *CYPrefix::Replace(CYContext &context) {
277 context.Replace(rhs_);
278 return NULL;
279}
280
281void CYProgram::Replace(CYContext &context) {
282 statements_ = statements_->ReplaceAll(context);
283}
284
285void CYProperty::Replace(CYContext &context) { $T()
286 context.Replace(value_);
287 next_->Replace(context);
288}
289
290CYStatement *CYReturn::Replace(CYContext &context) {
291 context.Replace(value_);
292 return NULL;
293}
294
3b52fd1a
JF
295CYStatement *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
307CYStatement *CYSwitch::Replace(CYContext &context) {
308 context.Replace(value_);
309 clauses_->Replace(context);
310 return NULL;
311}
312
313CYExpression *CYThis::Replace(CYContext &context) {
314 return NULL;
315}
316
317CYStatement *CYThrow::Replace(CYContext &context) {
318 context.Replace(value_);
319 return NULL;
320}
321
322CYExpression *CYTrivial::Replace(CYContext &context) {
323 return NULL;
324}
325
326CYStatement *CYTry::Replace(CYContext &context) {
327 code_.Replace(context);
328 catch_->Replace(context);
329 finally_->Replace(context);
330 return NULL;
331}
332
333CYStatement *CYVar::Replace(CYContext &context) {
334 declarations_->Replace(context);
335 return NULL;
336}
337
338CYExpression *CYVariable::Replace(CYContext &context) {
339 return NULL;
340}
341
342CYStatement *CYWhile::Replace(CYContext &context) {
343 context.Replace(test_);
344 context.Replace(code_);
345 return NULL;
346}
347
348CYStatement *CYWith::Replace(CYContext &context) {
349 context.Replace(scope_);
350 context.Replace(code_);
351 return NULL;
352}
353
354CYExpression *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}