1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
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
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.
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.
41 #include "Replace.hpp"
45 CYExpression
*CYAdd::Replace(CYContext
&context
) {
46 CYInfix::Replace(context
);
48 CYExpression
*lhp(lhs_
->Primitive(context
));
49 CYExpression
*rhp(rhs_
->Primitive(context
));
51 CYString
*lhs(dynamic_cast<CYString
*>(lhp
));
52 CYString
*rhs(dynamic_cast<CYString
*>(rhp
));
54 if (lhs
!= NULL
|| rhs
!= NULL
) {
56 lhs
= lhp
->String(context
);
59 } else if (rhs
== NULL
) {
60 rhs
= rhp
->String(context
);
65 return lhs
->Concat(context
, rhs
);
68 if (CYNumber
*lhn
= lhp
->Number(context
))
69 if (CYNumber
*rhn
= rhp
->Number(context
))
70 return $
D(lhn
->Value() + rhn
->Value());
75 CYExpression
*CYAddressOf::Replace(CYContext
&context
) {
76 CYPrefix::Replace(context
);
77 return $
C0($
M(rhs_
, $
S("$cya")));
80 void CYArgument::Replace(CYContext
&context
) { $
T()
81 context
.Replace(value_
);
82 next_
->Replace(context
);
85 CYExpression
*CYArray::Replace(CYContext
&context
) {
86 elements_
->Replace(context
);
90 CYExpression
*CYArrayComprehension::Replace(CYContext
&context
) {
91 CYVariable
*cyv($
V("$cyv"));
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_
)))->*
100 CYExpression
*CYAssignment::Replace(CYContext
&context
) {
101 context
.Replace(lhs_
);
102 context
.Replace(rhs_
);
106 CYStatement
*CYBlock::Replace(CYContext
&context
) {
107 statements_
= statements_
->ReplaceAll(context
);
108 if (statements_
== NULL
)
113 CYStatement
*CYBreak::Replace(CYContext
&context
) {
117 CYExpression
*CYCall::AddArgument(CYContext
&context
, CYExpression
*value
) {
118 CYArgument
**argument(&arguments_
);
119 while (*argument
!= NULL
)
120 argument
= &(*argument
)->next_
;
121 *argument
= $
CYArgument(value
);
125 CYExpression
*CYCall::Replace(CYContext
&context
) {
126 context
.Replace(function_
);
127 arguments_
->Replace(context
);
134 void Catch::Replace(CYContext
&context
) { $
T()
135 code_
.Replace(context
);
140 void CYClause::Replace(CYContext
&context
) { $
T()
141 context
.Replace(case_
);
142 statements_
= statements_
->ReplaceAll(context
);
143 next_
->Replace(context
);
146 CYStatement
*CYComment::Replace(CYContext
&context
) {
150 CYExpression
*CYCompound::Replace(CYContext
&context
) {
151 expressions_
= expressions_
->ReplaceAll(context
);
152 return expressions_
== NULL
? NULL
: this;
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
);
164 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
165 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
168 CYExpression
*CYCondition::Replace(CYContext
&context
) {
169 context
.Replace(test_
);
170 context
.Replace(true_
);
171 context
.Replace(false_
);
175 void CYContext::NonLocal(CYStatement
*&statements
) {
176 CYContext
&context(*this);
178 if (nonlocal_
->identifier_
!= NULL
) {
179 CYVariable
*cye($
V("$cye"));
180 CYVariable
*unique($
CYVariable(nonlocal_
->identifier_
));
183 $
E($
CYAssign(unique
, $
CYObject()))->*
184 $
cy::Syntax::Try(statements
, $
cy::Syntax::Catch(cye
->name_
, $$
->*
185 $
CYIf($
CYIdentical($
M(cye
, $
S("$cyk")), unique
), $$
->*
186 $
CYReturn($
M(cye
, $
S("$cyv"))))->*
187 $
cy::Syntax::Throw(cye
)
192 CYIdentifier
*CYContext::Unique() {
193 CYContext
&context(*this);
194 return $
CYIdentifier(apr_psprintf(pool_
, "$cy%u", unique_
++));
197 CYStatement
*CYContinue::Replace(CYContext
&context
) {
201 CYAssignment
*CYDeclaration::Assignment(CYContext
&context
) {
202 CYExpression
*variable(Replace(context
));
203 return initialiser_
== NULL
? NULL
: $
CYAssign(variable
, initialiser_
);
206 CYExpression
*CYDeclaration::ForEachIn(CYContext
&context
) {
207 return $
CYVariable(identifier_
);
210 CYExpression
*CYDeclaration::Replace(CYContext
&context
) {
211 context
.Replace(identifier_
);
212 context
.scope_
->Declare(context
, identifier_
, CYIdentifierVariable
);
213 return $
CYVariable(identifier_
);
216 CYProperty
*CYDeclarations::Property(CYContext
&context
) { $
T(NULL
)
217 return $
CYProperty(declaration_
->identifier_
, declaration_
->initialiser_
?: $U
, next_
->Property(context
));
220 CYCompound
*CYDeclarations::Replace(CYContext
&context
) {
221 CYCompound
*compound
;
222 if (next_
== NULL
) compound
:
223 compound
= $
CYCompound();
225 compound
= next_
->Replace(context
);
226 if (compound
== NULL
)
230 if (CYAssignment
*assignment
= declaration_
->Assignment(context
))
231 compound
->AddPrev(assignment
);
235 CYExpression
*CYDirectMember::Replace(CYContext
&context
) {
240 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
241 context
.Replace(test_
);
242 context
.Replace(code_
);
246 void CYElement::Replace(CYContext
&context
) { $
T()
247 context
.Replace(value_
);
248 next_
->Replace(context
);
251 CYStatement
*CYEmpty::Collapse(CYContext
&context
) {
255 CYStatement
*CYEmpty::Replace(CYContext
&context
) {
259 CYStatement
*CYExpress::Collapse(CYContext
&context
) {
260 if (CYExpress
*express
= dynamic_cast<CYExpress
*>(next_
)) {
261 CYCompound
*next(dynamic_cast<CYCompound
*>(express
->expression_
));
263 next
= $
CYCompound(express
->expression_
);
264 next
->AddPrev(expression_
);
266 SetNext(express
->next_
);
272 CYStatement
*CYExpress::Replace(CYContext
&context
) {
273 context
.Replace(expression_
);
274 if (expression_
== NULL
)
279 CYExpression
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
280 return $
C1(this, value
);
283 CYExpression
*CYExpression::ClassName(CYContext
&context
, bool object
) {
287 CYExpression
*CYExpression::ForEachIn(CYContext
&context
) {
291 CYExpression
*CYExpression::ReplaceAll(CYContext
&context
) { $
T(NULL
)
292 CYExpression
*replace(this);
293 context
.Replace(replace
);
295 if (CYExpression
*next
= next_
->ReplaceAll(context
))
296 replace
->SetNext(next
);
298 replace
->SetNext(next_
);
303 CYNumber
*CYFalse::Number(CYContext
&context
) {
307 CYString
*CYFalse::String(CYContext
&context
) {
311 void CYFinally::Replace(CYContext
&context
) { $
T()
312 code_
.Replace(context
);
315 CYStatement
*CYFor::Replace(CYContext
&context
) {
316 context
.Replace(initialiser_
);
317 context
.Replace(test_
);
318 context
.Replace(increment_
);
319 context
.Replace(code_
);
323 CYStatement
*CYForIn::Replace(CYContext
&context
) {
324 // XXX: this actually might need a prefix statement
325 context
.Replace(initialiser_
);
326 context
.Replace(set_
);
327 context
.Replace(code_
);
331 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
332 return $
CYFunctionParameter(name_
);
335 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
336 return $
CYForIn($
CYVariable(name_
), set_
, CYComprehension::Replace(context
, statement
));
339 CYStatement
*CYForEachIn::Replace(CYContext
&context
) {
340 CYVariable
*cys($
V("$cys")), *cyt($
V("$cyt"));
342 return $
CYLet($
L2($
L($
I("$cys"), set_
), $
L($
I("$cyt"))), $$
->*
343 $
CYForIn(cyt
, cys
, $
CYBlock($$
->*
344 $
E($
CYAssign(initialiser_
->ForEachIn(context
), $
M(cys
, cyt
)))->*
350 CYFunctionParameter
*CYForEachInComprehension::Parameter(CYContext
&context
) const {
351 return $
CYFunctionParameter(name_
);
354 CYStatement
*CYForEachInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
355 CYVariable
*cys($
V("$cys")), *name($
CYVariable(name_
));
357 return $
E($
C0($
F(NULL
, $
P1("$cys"), $$
->*
358 $
E($
CYAssign(cys
, set_
))->*
359 $
CYForIn(name
, cys
, $
CYBlock($$
->*
360 $
E($
CYAssign(name
, $
M(cys
, name
)))->*
361 CYComprehension::Replace(context
, statement
)
366 void CYFunction::Inject(CYContext
&context
) {
367 context
.Replace(name_
);
368 context
.scope_
->Declare(context
, name_
, CYIdentifierOther
);
371 void CYFunction::Replace_(CYContext
&context
, bool outer
) {
376 scope
.parent_
= context
.scope_
;
377 context
.scope_
= &scope
;
380 if (nonlocal_
!= NULL
)
384 nonlocal_
= $
CYNonLocal();
387 CYNonLocal
*nonlocal(context
.nonlocal_
);
388 context
.nonlocal_
= nonlocal_
;
390 if (!outer
&& name_
!= NULL
)
393 if (parameters_
!= NULL
)
394 parameters_
= parameters_
->Replace(context
, code_
);
395 code_
.Replace(context
);
398 context
.NonLocal(code_
.statements_
);
399 context
.nonlocal_
= nonlocal
;
401 context
.scope_
= scope
.parent_
;
402 scope
.Scope(context
, code_
.statements_
);
405 CYExpression
*CYFunctionExpression::Replace(CYContext
&context
) {
406 Replace_(context
, false);
410 CYFunctionParameter
*CYFunctionParameter::Replace(CYContext
&context
, CYBlock
&code
) {
411 name_
= name_
->Replace(context
);
412 context
.scope_
->Declare(context
, name_
, CYIdentifierArgument
);
414 next_
= next_
->Replace(context
, code
);
418 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
419 Replace_(context
, true);
423 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
) {
424 if (replace_
!= NULL
&& replace_
!= this)
425 return replace_
->Replace(context
);
426 replace_
= context
.scope_
->Lookup(context
, this);
430 CYStatement
*CYIf::Replace(CYContext
&context
) {
431 context
.Replace(test_
);
432 context
.Replace(true_
);
433 context
.Replace(false_
);
437 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
441 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
442 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
445 CYExpression
*CYIndirect::Replace(CYContext
&context
) {
446 CYPrefix::Replace(context
);
447 return $
M(rhs_
, $
S("$cyi"));
450 CYExpression
*CYIndirectMember::Replace(CYContext
&context
) {
452 return $
M($
CYIndirect(object_
), property_
);
455 CYExpression
*CYInfix::Replace(CYContext
&context
) {
456 context
.Replace(lhs_
);
457 context
.Replace(rhs_
);
461 CYStatement
*CYLabel::Replace(CYContext
&context
) {
462 context
.Replace(statement_
);
466 CYStatement
*CYLet::Replace(CYContext
&context
) {
467 return $
CYWith($
CYObject(declarations_
->Property(context
)), &code_
);
470 void CYMember::Replace_(CYContext
&context
) {
471 context
.Replace(object_
);
472 context
.Replace(property_
);
475 CYExpression
*CYNew::AddArgument(CYContext
&context
, CYExpression
*value
) {
476 CYArgument
**argument(&arguments_
);
477 while (*argument
!= NULL
)
478 argument
= &(*argument
)->next_
;
479 *argument
= $
CYArgument(value
);
483 CYExpression
*CYNew::Replace(CYContext
&context
) {
484 context
.Replace(constructor_
);
485 arguments_
->Replace(context
);
489 CYNumber
*CYNull::Number(CYContext
&context
) {
493 CYString
*CYNull::String(CYContext
&context
) {
497 CYNumber
*CYNumber::Number(CYContext
&context
) {
501 CYString
*CYNumber::String(CYContext
&context
) {
502 // XXX: there is a precise algorithm for this
503 return $
S(apr_psprintf(context
.pool_
, "%.17g", Value()));
506 CYExpression
*CYObject::Replace(CYContext
&context
) {
507 properties_
->Replace(context
);
511 CYFunctionParameter
*CYOptionalFunctionParameter::Replace(CYContext
&context
, CYBlock
&code
) {
512 CYFunctionParameter
*parameter($
CYFunctionParameter(name_
, next_
));
513 parameter
= parameter
->Replace(context
, code
);
515 CYVariable
*name($
CYVariable(name_
));
516 code
.AddPrev($
CYIf($
CYIdentical($
CYTypeOf(name
), $
S("undefined")), $$
->*
517 $
E($
CYAssign(name
, initializer_
))
523 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
524 context
.Replace(lhs_
);
528 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
529 context
.Replace(rhs_
);
533 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
534 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
535 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
538 struct IdentifierUsageLess
:
539 std::binary_function
<CYIdentifier
*, CYIdentifier
*, bool>
541 _finline
bool operator ()(CYIdentifier
*lhs
, CYIdentifier
*rhs
) const {
542 if (lhs
->usage_
!= rhs
->usage_
)
543 return lhs
->usage_
> rhs
->usage_
;
548 typedef std::set
<CYIdentifier
*, IdentifierUsageLess
> IdentifierUsages
;
551 void CYProgram::Replace(CYContext
&context
) {
553 scope
.parent_
= context
.scope_
;
554 context
.scope_
= &scope
;
556 context
.nonlocal_
= $
CYNonLocal();
557 statements_
= statements_
->ReplaceAll(context
);
558 context
.NonLocal(statements_
);
560 context
.scope_
= scope
.parent_
;
561 scope
.Scope(context
, statements_
);
565 CYCStringSet external
;
566 for (CYIdentifierValueSet::const_iterator
i(scope
.identifiers_
.begin()); i
!= scope
.identifiers_
.end(); ++i
)
567 external
.insert((*i
)->Word());
569 IdentifierUsages usages
;
571 if (offset
< context
.rename_
.size())
572 for (CYIdentifier
*i(context
.rename_
[offset
].identifier_
); i
!= NULL
; i
= i
->next_
)
575 // XXX: totalling the probable occurrences and sorting by them would improve the result
576 for (CYIdentifierUsageVector::const_iterator
i(context
.rename_
.begin()); i
!= context
.rename_
.end(); ++i
, ++offset
) {
577 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
581 if (context
.options_
.verbose_
)
582 name
= apr_psprintf(context
.pool_
, "$%"APR_SIZE_T_FMT
"", offset
);
588 unsigned position(7), local(offset
+ 1);
591 unsigned index(local
% (sizeof(MappingSet
) - 1));
592 local
/= sizeof(MappingSet
) - 1;
593 id
[--position
] = MappingSet
[index
];
594 } while (local
!= 0);
596 if (external
.find(id
+ position
) != external
.end()) {
601 name
= apr_pstrmemdup(context
.pool_
, id
+ position
, 7 - position
);
602 // XXX: at some point, this could become a keyword
605 for (CYIdentifier
*identifier(i
->identifier_
); identifier
!= NULL
; identifier
= identifier
->next_
)
606 identifier
->Set(name
);
610 void CYProperty::Replace(CYContext
&context
) { $
T()
611 context
.Replace(value_
);
612 next_
->Replace(context
);
615 CYStatement
*CYReturn::Replace(CYContext
&context
) {
616 if (context
.nonlocal_
!= NULL
) {
617 CYProperty
*value(value_
== NULL
? NULL
: $
CYProperty($
S("$cyv"), value_
));
618 return $
cy::Syntax::Throw($
CYObject(
619 $
CYProperty($
S("$cyk"), $
CYVariable(context
.nonlocal_
->Target(context
)), value
)
623 context
.Replace(value_
);
627 CYExpression
*CYRubyBlock::Replace(CYContext
&context
) {
628 // XXX: this needs to do something much more epic to handle return
629 return call_
->AddArgument(context
, proc_
->Replace(context
));
632 CYExpression
*CYRubyProc::Replace(CYContext
&context
) {
633 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
634 function
->nonlocal_
= context
.nonlocal_
;
638 void CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierFlags flags
) {
639 internal_
.insert(CYIdentifierAddressFlagsMap::value_type(identifier
, flags
));
642 CYIdentifier
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
643 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
644 return *insert
.first
;
647 void CYScope::Merge(CYContext
&context
, CYIdentifier
*identifier
) {
648 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
649 if (!insert
.second
) {
650 if ((*insert
.first
)->offset_
< identifier
->offset_
)
651 (*insert
.first
)->offset_
= identifier
->offset_
;
652 identifier
->replace_
= *insert
.first
;
653 (*insert
.first
)->usage_
+= identifier
->usage_
+ 1;
658 struct IdentifierOffset
{
660 CYIdentifierFlags flags_
;
662 CYIdentifier
*identifier_
;
664 IdentifierOffset(CYIdentifier
*identifier
, CYIdentifierFlags flags
) :
665 offset_(identifier
->offset_
),
667 usage_(identifier
->usage_
),
668 identifier_(identifier
)
673 struct IdentifierOffsetLess
:
674 std::binary_function
<const IdentifierOffset
&, const IdentifierOffset
&, bool>
676 _finline
bool operator ()(const IdentifierOffset
&lhs
, const IdentifierOffset
&rhs
) const {
677 if (lhs
.offset_
!= rhs
.offset_
)
678 return lhs
.offset_
< rhs
.offset_
;
679 if (lhs
.flags_
!= rhs
.flags_
)
680 return lhs
.flags_
< rhs
.flags_
;
681 /*if (lhs.usage_ != rhs.usage_)
682 return lhs.usage_ < rhs.usage_;*/
683 return lhs
.identifier_
< rhs
.identifier_
;
687 typedef std::set
<IdentifierOffset
, IdentifierOffsetLess
> IdentifierOffsets
;
690 void CYScope::Scope(CYContext
&context
, CYStatement
*&statements
) {
694 CYDeclarations
*last(NULL
), *curr(NULL
);
696 IdentifierOffsets offsets
;
698 for (CYIdentifierAddressFlagsMap::const_iterator
i(internal_
.begin()); i
!= internal_
.end(); ++i
)
699 if (i
->second
!= CYIdentifierMagic
)
700 offsets
.insert(IdentifierOffset(i
->first
, i
->second
));
704 for (IdentifierOffsets::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
705 if (i
->flags_
== CYIdentifierVariable
) {
706 CYDeclarations
*next($
CYDeclarations($
CYDeclaration(i
->identifier_
)));
714 if (offset
< i
->offset_
)
716 if (context
.rename_
.size() <= offset
)
717 context
.rename_
.resize(offset
+ 1);
719 CYIdentifierUsage
&rename(context
.rename_
[offset
++]);
720 i
->identifier_
->SetNext(rename
.identifier_
);
721 rename
.identifier_
= i
->identifier_
;
722 rename
.usage_
+= i
->identifier_
->usage_
+ 1;
726 CYVar
*var($
CYVar(last
));
727 var
->SetNext(statements
);
731 for (CYIdentifierValueSet::const_iterator
i(identifiers_
.begin()); i
!= identifiers_
.end(); ++i
)
732 if (internal_
.find(*i
) == internal_
.end()) {
733 //std::cout << *i << '=' << offset << std::endl;
734 if ((*i
)->offset_
< offset
)
735 (*i
)->offset_
= offset
;
736 parent_
->Merge(context
, *i
);
740 CYStatement
*CYStatement::Collapse(CYContext
&context
) {
744 CYStatement
*CYStatement::ReplaceAll(CYContext
&context
) { $
T(NULL
)
745 CYStatement
*replace(this);
746 context
.Replace(replace
);
747 replace
->SetNext(next_
->ReplaceAll(context
));
748 return replace
->Collapse(context
);
751 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
752 size_t size(size_
+ rhs
->size_
);
753 char *value(new(context
.pool_
) char[size
+ 1]);
754 memcpy(value
, value_
, size_
);
755 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
757 return $
S(value
, size
);
760 CYNumber
*CYString::Number(CYContext
&context
) {
761 // XXX: there is a precise algorithm for this
765 CYString
*CYString::String(CYContext
&context
) {
769 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
770 context
.Replace(value_
);
771 clauses_
->Replace(context
);
775 CYExpression
*CYThis::Replace(CYContext
&context
) {
782 CYStatement
*Throw::Replace(CYContext
&context
) {
783 context
.Replace(value_
);
789 CYExpression
*CYTrivial::Replace(CYContext
&context
) {
793 CYNumber
*CYTrue::Number(CYContext
&context
) {
797 CYString
*CYTrue::String(CYContext
&context
) {
804 CYStatement
*Try::Replace(CYContext
&context
) {
805 code_
.Replace(context
);
806 catch_
->Replace(context
);
807 finally_
->Replace(context
);
813 CYStatement
*CYVar::Replace(CYContext
&context
) {
814 return $
E(declarations_
->Replace(context
));
817 CYExpression
*CYVariable::Replace(CYContext
&context
) {
818 name_
= name_
->Replace(context
);
822 CYStatement
*CYWhile::Replace(CYContext
&context
) {
823 context
.Replace(test_
);
824 context
.Replace(code_
);
828 CYStatement
*CYWith::Replace(CYContext
&context
) {
829 context
.Replace(scope_
);
830 context
.Replace(code_
);
834 CYExpression
*CYWord::ClassName(CYContext
&context
, bool object
) {
835 CYString
*name($
S(this));
837 return $
C1($
V("objc_getClass"), name
);