]> git.saurik.com Git - cycript.git/blob - Replace.cpp
1285e8fb567a16b048954c72d0e028e0deb44bb8
[cycript.git] / Replace.cpp
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
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 #include "Replace.hpp"
42
43 #include <iomanip>
44
45 CYExpression *CYAdd::Replace(CYContext &context) {
46 CYInfix::Replace(context);
47
48 CYExpression *lhp(lhs_->Primitive(context));
49 CYExpression *rhp(rhs_->Primitive(context));
50
51 CYString *lhs(dynamic_cast<CYString *>(lhp));
52 CYString *rhs(dynamic_cast<CYString *>(rhp));
53
54 if (lhs != NULL || rhs != NULL) {
55 if (lhs == NULL) {
56 lhs = lhp->String(context);
57 if (lhs == NULL)
58 return this;
59 } else if (rhs == NULL) {
60 rhs = rhp->String(context);
61 if (rhs == NULL)
62 return this;
63 }
64
65 return lhs->Concat(context, rhs);
66 }
67
68 if (CYNumber *lhn = lhp->Number(context))
69 if (CYNumber *rhn = rhp->Number(context))
70 return $D(lhn->Value() + rhn->Value());
71
72 return this;
73 }
74
75 CYExpression *CYAddressOf::Replace(CYContext &context) {
76 CYPrefix::Replace(context);
77 return $C0($M(rhs_, $S("$cya")));
78 }
79
80 void CYArgument::Replace(CYContext &context) { $T()
81 context.Replace(value_);
82 next_->Replace(context);
83 }
84
85 CYExpression *CYArray::Replace(CYContext &context) {
86 elements_->Replace(context);
87 return this;
88 }
89
90 CYExpression *CYArrayComprehension::Replace(CYContext &context) {
91 CYVariable *cyv($V("$cyv"));
92
93 return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->*
94 $E($ CYAssign(cyv, $ CYArray()))->*
95 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
96 $ CYReturn(cyv)
97 ));
98 }
99
100 CYExpression *CYAssignment::Replace(CYContext &context) {
101 context.Replace(lhs_);
102 context.Replace(rhs_);
103 return this;
104 }
105
106 CYStatement *CYBlock::Replace(CYContext &context) {
107 statements_ = statements_->ReplaceAll(context);
108 if (statements_ == NULL)
109 return $ CYEmpty();
110 return this;
111 }
112
113 CYStatement *CYBreak::Replace(CYContext &context) {
114 return this;
115 }
116
117 CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
118 CYArgument **argument(&arguments_);
119 while (*argument != NULL)
120 argument = &(*argument)->next_;
121 *argument = $ CYArgument(value);
122 return this;
123 }
124
125 CYExpression *CYCall::Replace(CYContext &context) {
126 context.Replace(function_);
127 arguments_->Replace(context);
128 return this;
129 }
130
131 namespace cy {
132 namespace Syntax {
133
134 void Catch::Replace(CYContext &context) { $T()
135 code_.Replace(context);
136 }
137
138 } }
139
140 void CYClause::Replace(CYContext &context) { $T()
141 context.Replace(case_);
142 statements_ = statements_->ReplaceAll(context);
143 next_->Replace(context);
144 }
145
146 CYStatement *CYComment::Replace(CYContext &context) {
147 return this;
148 }
149
150 CYExpression *CYCompound::Replace(CYContext &context) {
151 expressions_ = expressions_->ReplaceAll(context);
152 return expressions_ == NULL ? NULL : this;
153 }
154
155 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
156 CYFunctionParameter *next(next_->Parameters(context));
157 if (CYFunctionParameter *parameter = Parameter(context)) {
158 parameter->SetNext(next);
159 return parameter;
160 } else
161 return next;
162 }
163
164 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
165 return next_ == NULL ? statement : next_->Replace(context, statement);
166 }
167
168 CYExpression *CYCondition::Replace(CYContext &context) {
169 context.Replace(test_);
170 context.Replace(true_);
171 context.Replace(false_);
172 return this;
173 }
174
175 CYStatement *CYContinue::Replace(CYContext &context) {
176 return this;
177 }
178
179 CYAssignment *CYDeclaration::Assignment(CYContext &context) {
180 CYExpression *variable(Replace(context));
181 return initialiser_ == NULL ? NULL : $ CYAssign(variable, initialiser_);
182 }
183
184 CYExpression *CYDeclaration::ForEachIn(CYContext &context) {
185 return $ CYVariable(identifier_);
186 }
187
188 CYExpression *CYDeclaration::Replace(CYContext &context) {
189 context.Replace(identifier_);
190 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
191 return $ CYVariable(identifier_);
192 }
193
194 CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
195 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context));
196 }
197
198 CYCompound *CYDeclarations::Replace(CYContext &context) {
199 CYCompound *compound;
200 if (next_ == NULL) compound:
201 compound = $ CYCompound();
202 else {
203 compound = next_->Replace(context);
204 if (compound == NULL)
205 goto compound;
206 }
207
208 if (CYAssignment *assignment = declaration_->Assignment(context))
209 compound->AddPrev(assignment);
210 return compound;
211 }
212
213 CYExpression *CYDirectMember::Replace(CYContext &context) {
214 Replace_(context);
215 return this;
216 }
217
218 CYStatement *CYDoWhile::Replace(CYContext &context) {
219 context.Replace(test_);
220 context.Replace(code_);
221 return this;
222 }
223
224 void CYElement::Replace(CYContext &context) { $T()
225 context.Replace(value_);
226 next_->Replace(context);
227 }
228
229 CYStatement *CYEmpty::Collapse(CYContext &context) {
230 return next_;
231 }
232
233 CYStatement *CYEmpty::Replace(CYContext &context) {
234 return this;
235 }
236
237 CYStatement *CYExpress::Collapse(CYContext &context) {
238 if (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
239 CYCompound *next(dynamic_cast<CYCompound *>(express->expression_));
240 if (next == NULL)
241 next = $ CYCompound(express->expression_);
242 next->AddPrev(expression_);
243 expression_ = next;
244 SetNext(express->next_);
245 }
246
247 return this;
248 }
249
250 CYStatement *CYExpress::Replace(CYContext &context) {
251 context.Replace(expression_);
252 if (expression_ == NULL)
253 return $ CYEmpty();
254 return this;
255 }
256
257 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
258 return $C1(this, value);
259 }
260
261 CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
262 return this;
263 }
264
265 CYExpression *CYExpression::ForEachIn(CYContext &context) {
266 return this;
267 }
268
269 CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL)
270 CYExpression *replace(this);
271 context.Replace(replace);
272
273 if (CYExpression *next = next_->ReplaceAll(context))
274 replace->SetNext(next);
275 else
276 replace->SetNext(next_);
277
278 return replace;
279 }
280
281 CYNumber *CYFalse::Number(CYContext &context) {
282 return $D(0);
283 }
284
285 CYString *CYFalse::String(CYContext &context) {
286 return $S("false");
287 }
288
289 void CYFinally::Replace(CYContext &context) { $T()
290 code_.Replace(context);
291 }
292
293 CYStatement *CYFor::Replace(CYContext &context) {
294 context.Replace(initialiser_);
295 context.Replace(test_);
296 context.Replace(increment_);
297 context.Replace(code_);
298 return this;
299 }
300
301 CYStatement *CYForIn::Replace(CYContext &context) {
302 // XXX: this actually might need a prefix statement
303 context.Replace(initialiser_);
304 context.Replace(set_);
305 context.Replace(code_);
306 return this;
307 }
308
309 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
310 return $ CYFunctionParameter(name_);
311 }
312
313 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
314 return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement));
315 }
316
317 CYStatement *CYForEachIn::Replace(CYContext &context) {
318 CYVariable *cys($V("$cys")), *cyt($V("$cyt"));
319
320 return $ CYLet($L2($L($I("$cys"), set_), $L($I("$cyt"))), $$->*
321 $ CYForIn(cyt, cys, $ CYBlock($$->*
322 $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->*
323 code_
324 ))
325 );
326 }
327
328 CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
329 return $ CYFunctionParameter(name_);
330 }
331
332 CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
333 CYVariable *cys($V("$cys")), *name($ CYVariable(name_));
334
335 return $E($C0($F(NULL, $P1("$cys"), $$->*
336 $E($ CYAssign(cys, set_))->*
337 $ CYForIn(name, cys, $ CYBlock($$->*
338 $E($ CYAssign(name, $M(cys, name)))->*
339 CYComprehension::Replace(context, statement)
340 ))
341 )));
342 }
343
344 void CYFunction::Inject(CYContext &context) {
345 context.Replace(name_);
346 context.scope_->Declare(context, name_, CYIdentifierOther);
347 }
348
349 void CYFunction::Replace_(CYContext &context, bool outer) {
350 if (outer)
351 Inject(context);
352
353 CYScope scope;
354 scope.parent_ = context.scope_;
355 context.scope_ = &scope;
356
357 if (!outer && name_ != NULL)
358 Inject(context);
359
360 if (parameters_ != NULL)
361 parameters_ = parameters_->Replace(context, code_);
362 code_.Replace(context);
363
364 context.scope_ = scope.parent_;
365 scope.Scope(context, code_.statements_);
366 }
367
368 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
369 Replace_(context, false);
370 return this;
371 }
372
373 CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
374 name_ = name_->Replace(context);
375 context.scope_->Declare(context, name_, CYIdentifierArgument);
376 if (next_ != NULL)
377 next_ = next_->Replace(context, code);
378 return this;
379 }
380
381 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
382 Replace_(context, true);
383 return this;
384 }
385
386 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
387 if (replace_ != NULL && replace_ != this)
388 return replace_->Replace(context);
389 replace_ = context.scope_->Lookup(context, this);
390 return replace_;
391 }
392
393 CYStatement *CYIf::Replace(CYContext &context) {
394 context.Replace(test_);
395 context.Replace(true_);
396 context.Replace(false_);
397 return this;
398 }
399
400 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
401 return NULL;
402 }
403
404 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
405 return $ CYIf(test_, CYComprehension::Replace(context, statement));
406 }
407
408 CYExpression *CYIndirect::Replace(CYContext &context) {
409 CYPrefix::Replace(context);
410 return $M(rhs_, $S("$cyi"));
411 }
412
413 CYExpression *CYIndirectMember::Replace(CYContext &context) {
414 Replace_(context);
415 return $M($ CYIndirect(object_), property_);
416 }
417
418 CYExpression *CYInfix::Replace(CYContext &context) {
419 context.Replace(lhs_);
420 context.Replace(rhs_);
421 return this;
422 }
423
424 CYStatement *CYLabel::Replace(CYContext &context) {
425 context.Replace(statement_);
426 return this;
427 }
428
429 CYStatement *CYLet::Replace(CYContext &context) {
430 return $ CYWith($ CYObject(declarations_->Property(context)), &code_);
431 }
432
433 void CYMember::Replace_(CYContext &context) {
434 context.Replace(object_);
435 context.Replace(property_);
436 }
437
438 CYExpression *CYNew::AddArgument(CYContext &context, CYExpression *value) {
439 CYArgument **argument(&arguments_);
440 while (*argument != NULL)
441 argument = &(*argument)->next_;
442 *argument = $ CYArgument(value);
443 return this;
444 }
445
446 CYExpression *CYNew::Replace(CYContext &context) {
447 context.Replace(constructor_);
448 arguments_->Replace(context);
449 return this;
450 }
451
452 CYNumber *CYNull::Number(CYContext &context) {
453 return $D(0);
454 }
455
456 CYString *CYNull::String(CYContext &context) {
457 return $S("null");
458 }
459
460 CYNumber *CYNumber::Number(CYContext &context) {
461 return this;
462 }
463
464 CYString *CYNumber::String(CYContext &context) {
465 // XXX: there is a precise algorithm for this
466 return $S(apr_psprintf(context.pool_, "%.17g", Value()));
467 }
468
469 CYExpression *CYObject::Replace(CYContext &context) {
470 properties_->Replace(context);
471 return this;
472 }
473
474 CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
475 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
476 parameter = parameter->Replace(context, code);
477
478 CYVariable *name($ CYVariable(name_));
479 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
480 $E($ CYAssign(name, initializer_))
481 ));
482
483 return parameter;
484 }
485
486 CYExpression *CYPostfix::Replace(CYContext &context) {
487 context.Replace(lhs_);
488 return this;
489 }
490
491 CYExpression *CYPrefix::Replace(CYContext &context) {
492 context.Replace(rhs_);
493 return this;
494 }
495
496 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
497 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
498 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
499
500 namespace {
501 struct IdentifierUsageLess :
502 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
503 {
504 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
505 if (lhs->usage_ != rhs->usage_)
506 return lhs->usage_ > rhs->usage_;
507 return lhs < rhs;
508 }
509 };
510
511 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
512 }
513
514 void CYProgram::Replace(CYContext &context) {
515 CYScope scope;
516 scope.parent_ = context.scope_;
517 context.scope_ = &scope;
518 statements_ = statements_->ReplaceAll(context);
519 context.scope_ = scope.parent_;
520 scope.Scope(context, statements_);
521
522 size_t offset(0);
523
524 CYCStringSet external;
525 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
526 external.insert((*i)->Word());
527
528 IdentifierUsages usages;
529
530 if (offset < context.rename_.size())
531 for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_)
532 usages.insert(i);
533
534 // XXX: totalling the probable occurrences and sorting by them would improve the result
535 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
536 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
537
538 const char *name;
539
540 if (context.options_.verbose_)
541 name = apr_psprintf(context.pool_, "$%"APR_SIZE_T_FMT"", offset);
542 else {
543 char id[8];
544 id[7] = '\0';
545
546 id:
547 unsigned position(7), local(offset + 1);
548
549 do {
550 unsigned index(local % (sizeof(MappingSet) - 1));
551 local /= sizeof(MappingSet) - 1;
552 id[--position] = MappingSet[index];
553 } while (local != 0);
554
555 if (external.find(id + position) != external.end()) {
556 ++offset;
557 goto id;
558 }
559
560 name = apr_pstrmemdup(context.pool_, id + position, 7 - position);
561 // XXX: at some point, this could become a keyword
562 }
563
564 for (CYIdentifier *identifier(i->identifier_); identifier != NULL; identifier = identifier->next_)
565 identifier->Set(name);
566 }
567 }
568
569 void CYProperty::Replace(CYContext &context) { $T()
570 context.Replace(value_);
571 next_->Replace(context);
572 }
573
574 CYStatement *CYReturn::Replace(CYContext &context) {
575 context.Replace(value_);
576 return this;
577 }
578
579 CYExpression *CYRubyBlock::Replace(CYContext &context) {
580 // XXX: this needs to do something much more epic to handle return
581 return call_->AddArgument(context, proc_->Replace(context));
582 }
583
584 CYExpression *CYRubyProc::Replace(CYContext &context) {
585 return $ CYFunctionExpression(NULL, parameters_, code_);
586 }
587
588 void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
589 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
590 }
591
592 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
593 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
594 return *insert.first;
595 }
596
597 void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
598 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
599 if (!insert.second) {
600 if ((*insert.first)->offset_ < identifier->offset_)
601 (*insert.first)->offset_ = identifier->offset_;
602 identifier->replace_ = *insert.first;
603 (*insert.first)->usage_ += identifier->usage_ + 1;
604 }
605 }
606
607 namespace {
608 struct IdentifierOffset {
609 size_t offset_;
610 CYIdentifierFlags flags_;
611 size_t usage_;
612 CYIdentifier *identifier_;
613
614 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
615 offset_(identifier->offset_),
616 flags_(flags),
617 usage_(identifier->usage_),
618 identifier_(identifier)
619 {
620 }
621 };
622
623 struct IdentifierOffsetLess :
624 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
625 {
626 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
627 if (lhs.offset_ != rhs.offset_)
628 return lhs.offset_ < rhs.offset_;
629 if (lhs.flags_ != rhs.flags_)
630 return lhs.flags_ < rhs.flags_;
631 /*if (lhs.usage_ != rhs.usage_)
632 return lhs.usage_ < rhs.usage_;*/
633 return lhs.identifier_ < rhs.identifier_;
634 }
635 };
636
637 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
638 }
639
640 void CYScope::Scope(CYContext &context, CYStatement *&statements) {
641 if (parent_ == NULL)
642 return;
643
644 CYDeclarations *last(NULL), *curr(NULL);
645
646 IdentifierOffsets offsets;
647
648 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
649 if (i->second != CYIdentifierMagic)
650 offsets.insert(IdentifierOffset(i->first, i->second));
651
652 size_t offset(0);
653
654 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
655 if (i->flags_ == CYIdentifierVariable) {
656 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
657 if (last == NULL)
658 last = next;
659 if (curr != NULL)
660 curr->SetNext(next);
661 curr = next;
662 }
663
664 if (offset < i->offset_)
665 offset = i->offset_;
666 if (context.rename_.size() <= offset)
667 context.rename_.resize(offset + 1);
668
669 CYIdentifierUsage &rename(context.rename_[offset++]);
670 i->identifier_->SetNext(rename.identifier_);
671 rename.identifier_ = i->identifier_;
672 rename.usage_ += i->identifier_->usage_ + 1;
673 }
674
675 if (last != NULL) {
676 CYVar *var($ CYVar(last));
677 var->SetNext(statements);
678 statements = var;
679 }
680
681 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
682 if (internal_.find(*i) == internal_.end()) {
683 //std::cout << *i << '=' << offset << std::endl;
684 if ((*i)->offset_ < offset)
685 (*i)->offset_ = offset;
686 parent_->Merge(context, *i);
687 }
688 }
689
690 CYStatement *CYStatement::Collapse(CYContext &context) {
691 return this;
692 }
693
694 CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL)
695 CYStatement *replace(this);
696 context.Replace(replace);
697 replace->SetNext(next_->ReplaceAll(context));
698 return replace->Collapse(context);
699 }
700
701 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
702 size_t size(size_ + rhs->size_);
703 char *value(new(context.pool_) char[size + 1]);
704 memcpy(value, value_, size_);
705 memcpy(value + size_, rhs->value_, rhs->size_);
706 value[size] = '\0';
707 return $S(value, size);
708 }
709
710 CYNumber *CYString::Number(CYContext &context) {
711 // XXX: there is a precise algorithm for this
712 return NULL;
713 }
714
715 CYString *CYString::String(CYContext &context) {
716 return this;
717 }
718
719 CYStatement *CYSwitch::Replace(CYContext &context) {
720 context.Replace(value_);
721 clauses_->Replace(context);
722 return this;
723 }
724
725 CYExpression *CYThis::Replace(CYContext &context) {
726 return this;
727 }
728
729 namespace cy {
730 namespace Syntax {
731
732 CYStatement *Throw::Replace(CYContext &context) {
733 context.Replace(value_);
734 return this;
735 }
736
737 } }
738
739 CYExpression *CYTrivial::Replace(CYContext &context) {
740 return this;
741 }
742
743 CYNumber *CYTrue::Number(CYContext &context) {
744 return $D(1);
745 }
746
747 CYString *CYTrue::String(CYContext &context) {
748 return $S("true");
749 }
750
751 namespace cy {
752 namespace Syntax {
753
754 CYStatement *Try::Replace(CYContext &context) {
755 code_.Replace(context);
756 catch_->Replace(context);
757 finally_->Replace(context);
758 return this;
759 }
760
761 } }
762
763 CYStatement *CYVar::Replace(CYContext &context) {
764 return $E(declarations_->Replace(context));
765 }
766
767 CYExpression *CYVariable::Replace(CYContext &context) {
768 name_ = name_->Replace(context);
769 return this;
770 }
771
772 CYStatement *CYWhile::Replace(CYContext &context) {
773 context.Replace(test_);
774 context.Replace(code_);
775 return this;
776 }
777
778 CYStatement *CYWith::Replace(CYContext &context) {
779 context.Replace(scope_);
780 context.Replace(code_);
781 return this;
782 }
783
784 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
785 CYString *name($S(this));
786 if (object)
787 return $C1($V("objc_getClass"), name);
788 else
789 return name;
790 }