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