]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Remove Execution Server and Disassembler | |
2 | * Copyright (C) 2009 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* Modified BSD License {{{ */ | |
6 | /* | |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the | |
12 | * above copyright notice, this list of conditions | |
13 | * and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the | |
15 | * above copyright notice, this list of conditions | |
16 | * and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. The name of the author may not be used to endorse | |
20 | * or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
25 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
36 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
38 | /* }}} */ | |
39 | ||
40 | #include "Parser.hpp" | |
41 | ||
42 | #include <iomanip> | |
43 | ||
44 | #include "Replace.hpp" | |
45 | ||
46 | CYExpression *CYAdd::Replace(CYContext &context) { | |
47 | CYInfix::Replace(context); | |
48 | ||
49 | CYExpression *lhp(lhs_->Primitive(context)); | |
50 | CYExpression *rhp(rhs_->Primitive(context)); | |
51 | ||
52 | CYString *lhs(dynamic_cast<CYString *>(lhp)); | |
53 | CYString *rhs(dynamic_cast<CYString *>(rhp)); | |
54 | ||
55 | if (lhs != NULL || rhs != NULL) { | |
56 | if (lhs == NULL) { | |
57 | lhs = lhp->String(context); | |
58 | if (lhs == NULL) | |
59 | return NULL; | |
60 | } else if (rhs == NULL) { | |
61 | rhs = rhp->String(context); | |
62 | if (rhs == NULL) | |
63 | return NULL; | |
64 | } | |
65 | ||
66 | return lhs->Concat(context, rhs); | |
67 | } | |
68 | ||
69 | if (CYNumber *lhn = lhp->Number(context)) | |
70 | if (CYNumber *rhn = rhp->Number(context)) | |
71 | return $D(lhn->Value() + rhn->Value()); | |
72 | ||
73 | return NULL; | |
74 | } | |
75 | ||
76 | CYExpression *CYAddressOf::Replace(CYContext &context) { | |
77 | CYPrefix::Replace(context); | |
78 | return $C0($M(rhs_, $S("$cya"))); | |
79 | } | |
80 | ||
81 | void CYArgument::Replace(CYContext &context) { $T() | |
82 | context.Replace(value_); | |
83 | next_->Replace(context); | |
84 | } | |
85 | ||
86 | CYExpression *CYArray::Replace(CYContext &context) { | |
87 | elements_->Replace(context); | |
88 | return NULL; | |
89 | } | |
90 | ||
91 | CYExpression *CYArrayComprehension::Replace(CYContext &context) { | |
92 | CYVariable *cyv($V("$cyv")); | |
93 | ||
94 | return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->* | |
95 | $E($ CYAssign(cyv, $ CYArray()))->* | |
96 | comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->* | |
97 | $ CYReturn(cyv) | |
98 | )); | |
99 | } | |
100 | ||
101 | CYExpression *CYAssignment::Replace(CYContext &context) { | |
102 | context.Replace(lhs_); | |
103 | context.Replace(rhs_); | |
104 | return NULL; | |
105 | } | |
106 | ||
107 | CYStatement *CYBlock::Replace(CYContext &context) { | |
108 | statements_ = statements_->ReplaceAll(context); | |
109 | return NULL; | |
110 | } | |
111 | ||
112 | CYStatement *CYBreak::Replace(CYContext &context) { | |
113 | return NULL; | |
114 | } | |
115 | ||
116 | CYExpression *CYCall::Replace(CYContext &context) { | |
117 | context.Replace(function_); | |
118 | arguments_->Replace(context); | |
119 | return NULL; | |
120 | } | |
121 | ||
122 | namespace cy { | |
123 | namespace Syntax { | |
124 | ||
125 | void Catch::Replace(CYContext &context) { $T() | |
126 | code_.Replace(context); | |
127 | } | |
128 | ||
129 | } } | |
130 | ||
131 | void CYClause::Replace(CYContext &context) { $T() | |
132 | context.Replace(case_); | |
133 | statements_ = statements_->ReplaceAll(context); | |
134 | next_->Replace(context); | |
135 | } | |
136 | ||
137 | CYExpression *CYCompound::Replace(CYContext &context) { | |
138 | expressions_ = expressions_->ReplaceAll(context); | |
139 | return NULL; | |
140 | } | |
141 | ||
142 | CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL) | |
143 | CYFunctionParameter *next(next_->Parameters(context)); | |
144 | if (CYFunctionParameter *parameter = Parameter(context)) { | |
145 | parameter->SetNext(next); | |
146 | return parameter; | |
147 | } else | |
148 | return next; | |
149 | } | |
150 | ||
151 | CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
152 | return next_ == NULL ? statement : next_->Replace(context, statement); | |
153 | } | |
154 | ||
155 | CYExpression *CYCondition::Replace(CYContext &context) { | |
156 | context.Replace(test_); | |
157 | context.Replace(true_); | |
158 | context.Replace(false_); | |
159 | return NULL; | |
160 | } | |
161 | ||
162 | CYStatement *CYContinue::Replace(CYContext &context) { | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | CYExpression *CYDeclaration::ForEachIn(CYContext &context) { | |
167 | return $ CYVariable(identifier_); | |
168 | } | |
169 | ||
170 | void CYDeclaration::Replace(CYContext &context) { | |
171 | context.Replace(initialiser_); | |
172 | } | |
173 | ||
174 | CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL) | |
175 | return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context)); | |
176 | } | |
177 | ||
178 | void CYDeclarations::Replace(CYContext &context) { $T() | |
179 | declaration_->Replace(context); | |
180 | next_->Replace(context); | |
181 | } | |
182 | ||
183 | CYExpression *CYDirectMember::Replace(CYContext &context) { | |
184 | Replace_(context); | |
185 | return NULL; | |
186 | } | |
187 | ||
188 | CYStatement *CYDoWhile::Replace(CYContext &context) { | |
189 | context.Replace(test_); | |
190 | context.Replace(code_); | |
191 | return NULL; | |
192 | } | |
193 | ||
194 | void CYElement::Replace(CYContext &context) { $T() | |
195 | context.Replace(value_); | |
196 | next_->Replace(context); | |
197 | } | |
198 | ||
199 | CYStatement *CYEmpty::Replace(CYContext &context) { | |
200 | return NULL; | |
201 | } | |
202 | ||
203 | CYStatement *CYExpress::Replace(CYContext &context) { | |
204 | context.Replace(expression_); | |
205 | return NULL; | |
206 | } | |
207 | ||
208 | CYExpression *CYExpression::ClassName(CYContext &context, bool object) { | |
209 | return this; | |
210 | } | |
211 | ||
212 | CYExpression *CYExpression::ForEachIn(CYContext &context) { | |
213 | return this; | |
214 | } | |
215 | ||
216 | CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL) | |
217 | CYExpression *replace(this); | |
218 | context.Replace(replace); | |
219 | ||
220 | if (CYExpression *next = next_->ReplaceAll(context)) | |
221 | replace->SetNext(next); | |
222 | else | |
223 | replace->SetNext(next_); | |
224 | ||
225 | return replace; | |
226 | } | |
227 | ||
228 | CYNumber *CYFalse::Number(CYContext &context) { | |
229 | return $D(0); | |
230 | } | |
231 | ||
232 | CYString *CYFalse::String(CYContext &context) { | |
233 | return $S("false"); | |
234 | } | |
235 | ||
236 | void CYFinally::Replace(CYContext &context) { $T() | |
237 | code_.Replace(context); | |
238 | } | |
239 | ||
240 | CYStatement *CYFor::Replace(CYContext &context) { | |
241 | // XXX: initialiser_ | |
242 | context.Replace(test_); | |
243 | context.Replace(increment_); | |
244 | context.Replace(code_); | |
245 | return NULL; | |
246 | } | |
247 | ||
248 | CYStatement *CYForIn::Replace(CYContext &context) { | |
249 | // XXX: initialiser_ | |
250 | context.Replace(set_); | |
251 | context.Replace(code_); | |
252 | return NULL; | |
253 | } | |
254 | ||
255 | CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const { | |
256 | return $ CYFunctionParameter(name_); | |
257 | } | |
258 | ||
259 | CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
260 | return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement)); | |
261 | } | |
262 | ||
263 | CYStatement *CYForEachIn::Replace(CYContext &context) { | |
264 | CYVariable *cys($V("$cys")), *cyt($V("$cyt")); | |
265 | ||
266 | return $ CYLet($L2($L($I("$cys"), set_), $L($I("$cyt"))), $$->* | |
267 | $ CYForIn(cyt, cys, $ CYBlock($$->* | |
268 | $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->* | |
269 | code_ | |
270 | )) | |
271 | ); | |
272 | } | |
273 | ||
274 | CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const { | |
275 | return $ CYFunctionParameter(name_); | |
276 | } | |
277 | ||
278 | CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
279 | CYVariable *cys($V("$cys")), *name($ CYVariable(name_)); | |
280 | ||
281 | return $E($C0($F(NULL, $P1("$cys"), $$->* | |
282 | $E($ CYAssign(cys, set_))->* | |
283 | $ CYForIn(name, cys, $ CYBlock($$->* | |
284 | $E($ CYAssign(name, $M(cys, name)))->* | |
285 | CYComprehension::Replace(context, statement) | |
286 | )) | |
287 | ))); | |
288 | } | |
289 | ||
290 | void CYFunction::Replace_(CYContext &context) { | |
291 | code_.Replace(context); | |
292 | } | |
293 | ||
294 | CYExpression *CYFunctionExpression::Replace(CYContext &context) { | |
295 | Replace_(context); | |
296 | return NULL; | |
297 | } | |
298 | ||
299 | CYStatement *CYFunctionStatement::Replace(CYContext &context) { | |
300 | Replace_(context); | |
301 | return NULL; | |
302 | } | |
303 | ||
304 | CYStatement *CYIf::Replace(CYContext &context) { | |
305 | context.Replace(test_); | |
306 | context.Replace(true_); | |
307 | context.Replace(false_); | |
308 | return NULL; | |
309 | } | |
310 | ||
311 | CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const { | |
312 | return NULL; | |
313 | } | |
314 | ||
315 | CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
316 | return $ CYIf(test_, CYComprehension::Replace(context, statement)); | |
317 | } | |
318 | ||
319 | CYExpression *CYIndirect::Replace(CYContext &context) { | |
320 | CYPrefix::Replace(context); | |
321 | return $M(rhs_, $S("$cyi")); | |
322 | } | |
323 | ||
324 | CYExpression *CYIndirectMember::Replace(CYContext &context) { | |
325 | Replace_(context); | |
326 | return $M($ CYIndirect(object_), property_); | |
327 | } | |
328 | ||
329 | CYExpression *CYInfix::Replace(CYContext &context) { | |
330 | context.Replace(lhs_); | |
331 | context.Replace(rhs_); | |
332 | return NULL; | |
333 | } | |
334 | ||
335 | CYStatement *CYLabel::Replace(CYContext &context) { | |
336 | context.Replace(statement_); | |
337 | return NULL; | |
338 | } | |
339 | ||
340 | CYStatement *CYLet::Replace(CYContext &context) { | |
341 | return $ CYWith($ CYObject(declarations_->Property(context)), &code_); | |
342 | } | |
343 | ||
344 | void CYMember::Replace_(CYContext &context) { | |
345 | context.Replace(object_); | |
346 | context.Replace(property_); | |
347 | } | |
348 | ||
349 | CYExpression *CYNew::Replace(CYContext &context) { | |
350 | context.Replace(constructor_); | |
351 | arguments_->Replace(context); | |
352 | return NULL; | |
353 | } | |
354 | ||
355 | CYNumber *CYNull::Number(CYContext &context) { | |
356 | return $D(0); | |
357 | } | |
358 | ||
359 | CYString *CYNull::String(CYContext &context) { | |
360 | return $S("null"); | |
361 | } | |
362 | ||
363 | CYNumber *CYNumber::Number(CYContext &context) { | |
364 | return this; | |
365 | } | |
366 | ||
367 | CYString *CYNumber::String(CYContext &context) { | |
368 | // XXX: there is a precise algorithm for this | |
369 | return $S(apr_psprintf(context.pool_, "%.17g", Value())); | |
370 | } | |
371 | ||
372 | CYExpression *CYObject::Replace(CYContext &context) { | |
373 | properties_->Replace(context); | |
374 | return NULL; | |
375 | } | |
376 | ||
377 | CYExpression *CYPostfix::Replace(CYContext &context) { | |
378 | context.Replace(lhs_); | |
379 | return NULL; | |
380 | } | |
381 | ||
382 | CYExpression *CYPrefix::Replace(CYContext &context) { | |
383 | context.Replace(rhs_); | |
384 | return NULL; | |
385 | } | |
386 | ||
387 | void CYProgram::Replace(CYContext &context) { | |
388 | statements_ = statements_->ReplaceAll(context); | |
389 | } | |
390 | ||
391 | void CYProperty::Replace(CYContext &context) { $T() | |
392 | context.Replace(value_); | |
393 | next_->Replace(context); | |
394 | } | |
395 | ||
396 | CYStatement *CYReturn::Replace(CYContext &context) { | |
397 | context.Replace(value_); | |
398 | return NULL; | |
399 | } | |
400 | ||
401 | CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL) | |
402 | CYStatement *replace(this); | |
403 | context.Replace(replace); | |
404 | ||
405 | if (CYStatement *next = next_->ReplaceAll(context)) | |
406 | replace->SetNext(next); | |
407 | else | |
408 | replace->SetNext(next_); | |
409 | ||
410 | return replace; | |
411 | } | |
412 | ||
413 | CYString *CYString::Concat(CYContext &context, CYString *rhs) const { | |
414 | size_t size(size_ + rhs->size_); | |
415 | char *value(new(context.pool_) char[size + 1]); | |
416 | memcpy(value, value_, size_); | |
417 | memcpy(value + size_, rhs->value_, rhs->size_); | |
418 | value[size] = '\0'; | |
419 | return $S(value, size); | |
420 | } | |
421 | ||
422 | CYNumber *CYString::Number(CYContext &context) { | |
423 | // XXX: there is a precise algorithm for this | |
424 | return NULL; | |
425 | } | |
426 | ||
427 | CYString *CYString::String(CYContext &context) { | |
428 | return this; | |
429 | } | |
430 | ||
431 | CYStatement *CYSwitch::Replace(CYContext &context) { | |
432 | context.Replace(value_); | |
433 | clauses_->Replace(context); | |
434 | return NULL; | |
435 | } | |
436 | ||
437 | CYExpression *CYThis::Replace(CYContext &context) { | |
438 | return NULL; | |
439 | } | |
440 | ||
441 | namespace cy { | |
442 | namespace Syntax { | |
443 | ||
444 | CYStatement *Throw::Replace(CYContext &context) { | |
445 | context.Replace(value_); | |
446 | return NULL; | |
447 | } | |
448 | ||
449 | } } | |
450 | ||
451 | CYExpression *CYTrivial::Replace(CYContext &context) { | |
452 | return NULL; | |
453 | } | |
454 | ||
455 | CYNumber *CYTrue::Number(CYContext &context) { | |
456 | return $D(1); | |
457 | } | |
458 | ||
459 | CYString *CYTrue::String(CYContext &context) { | |
460 | return $S("true"); | |
461 | } | |
462 | ||
463 | namespace cy { | |
464 | namespace Syntax { | |
465 | ||
466 | CYStatement *Try::Replace(CYContext &context) { | |
467 | code_.Replace(context); | |
468 | catch_->Replace(context); | |
469 | finally_->Replace(context); | |
470 | return NULL; | |
471 | } | |
472 | ||
473 | } } | |
474 | ||
475 | CYStatement *CYVar::Replace(CYContext &context) { | |
476 | declarations_->Replace(context); | |
477 | return NULL; | |
478 | } | |
479 | ||
480 | CYExpression *CYVariable::Replace(CYContext &context) { | |
481 | return NULL; | |
482 | } | |
483 | ||
484 | CYStatement *CYWhile::Replace(CYContext &context) { | |
485 | context.Replace(test_); | |
486 | context.Replace(code_); | |
487 | return NULL; | |
488 | } | |
489 | ||
490 | CYStatement *CYWith::Replace(CYContext &context) { | |
491 | context.Replace(scope_); | |
492 | context.Replace(code_); | |
493 | return NULL; | |
494 | } | |
495 | ||
496 | CYExpression *CYWord::ClassName(CYContext &context, bool object) { | |
497 | CYString *name($S(this)); | |
498 | if (object) | |
499 | return $C1($V("objc_getClass"), name); | |
500 | else | |
501 | return name; | |
502 | } |