]> git.saurik.com Git - cycript.git/blob - Replace.cpp
2caa728e32e4ed39af2d64a12b763357dcb7ddf4
[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 if (initialiser_ == NULL)
200 return NULL;
201 return $ CYAssign(Variable(context), initialiser_);
202 }
203
204 CYVariable *CYDeclaration::Variable(CYContext &context) {
205 return $V(identifier_);
206 }
207
208 CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
209 return $ CYVar($L1($L(identifier_, value)));
210 }
211
212 CYExpression *CYDeclaration::Replace(CYContext &context) {
213 context.Replace(identifier_);
214 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
215 return Variable(context);
216 }
217
218 void CYDeclarations::Replace(CYContext &context) { $T()
219 declaration_->Replace(context);
220 next_->Replace(context);
221 }
222
223 CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
224 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context));
225 }
226
227 CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
228 return $ CYFunctionParameter(declaration_->identifier_, next_->Parameter(context));
229 }
230
231 CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
232 return $ CYArgument(declaration_->initialiser_ ?: $U, next_->Argument(context));
233 }
234
235 CYCompound *CYDeclarations::Compound(CYContext &context) { $T(NULL)
236 CYCompound *compound(next_->Compound(context) ?: $ CYCompound());
237 if (CYAssignment *assignment = declaration_->Assignment(context))
238 compound->AddPrev(assignment);
239 return compound;
240 }
241
242 CYExpression *CYDirectMember::Replace(CYContext &context) {
243 Replace_(context);
244 return this;
245 }
246
247 CYStatement *CYDoWhile::Replace(CYContext &context) {
248 context.Replace(test_);
249 context.Replace(code_);
250 return this;
251 }
252
253 void CYElement::Replace(CYContext &context) { $T()
254 context.Replace(value_);
255 next_->Replace(context);
256 }
257
258 CYStatement *CYEmpty::Replace(CYContext &context) {
259 return NULL;
260 }
261
262 CYStatement *CYExpress::Replace(CYContext &context) {
263 while (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
264 CYCompound *compound(dynamic_cast<CYCompound *>(express->expression_));
265 if (compound == NULL)
266 compound = $ CYCompound(express->expression_);
267 compound->AddPrev(expression_);
268 expression_ = compound;
269 SetNext(express->next_);
270 }
271
272 context.Replace(expression_);
273 if (expression_ == NULL)
274 return $ CYEmpty();
275
276 return this;
277 }
278
279 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
280 return $C1(this, value);
281 }
282
283 CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
284 return this;
285 }
286
287 CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
288 return $E($ CYAssign(this, value));
289 }
290
291 CYAssignment *CYExpression::Assignment(CYContext &context) {
292 return NULL;
293 }
294
295 CYNumber *CYFalse::Number(CYContext &context) {
296 return $D(0);
297 }
298
299 CYString *CYFalse::String(CYContext &context) {
300 return $S("false");
301 }
302
303 void CYFinally::Replace(CYContext &context) { $T()
304 code_.Replace(context);
305 }
306
307 CYStatement *CYFor::Replace(CYContext &context) {
308 context.Replace(initialiser_);
309 context.Replace(test_);
310 context.Replace(increment_);
311 context.Replace(code_);
312 return this;
313 }
314
315 CYCompound *CYForDeclarations::Replace(CYContext &context) {
316 declarations_->Replace(context);
317 return declarations_->Compound(context);
318 }
319
320 // XXX: this still feels highly suboptimal
321 CYStatement *CYForIn::Replace(CYContext &context) {
322 CYForInInitialiser *initialiser(initialiser_);
323
324 context.Replace(initialiser_);
325 context.Replace(set_);
326 context.Replace(code_);
327
328 if (CYAssignment *assignment = initialiser->Assignment(context))
329 return $ CYBlock($$->*
330 $E(assignment)->*
331 this
332 );
333
334 return this;
335 }
336
337 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
338 return $ CYFunctionParameter(name_);
339 }
340
341 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
342 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
343 }
344
345 CYStatement *CYForEachIn::Replace(CYContext &context) {
346 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
347
348 return $ CYLet($L2($L(cys, set_), $L(cyt)), $$->*
349 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
350 initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))->*
351 code_
352 ))
353 );
354 }
355
356 CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
357 return $ CYFunctionParameter(name_);
358 }
359
360 CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
361 CYIdentifier *cys($I("cys"));
362
363 return $E($C0($F(NULL, $P1("$cys"), $$->*
364 $E($ CYAssign($V(cys), set_))->*
365 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
366 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
367 CYComprehension::Replace(context, statement)
368 ))
369 )));
370 }
371
372 void CYFunction::Inject(CYContext &context) {
373 context.Replace(name_);
374 context.scope_->Declare(context, name_, CYIdentifierOther);
375 }
376
377 void CYFunction::Replace_(CYContext &context, bool outer) {
378 if (outer)
379 Inject(context);
380
381 CYScope scope(CYScopeFunction, context, code_.statements_);
382
383 CYNonLocal *nonlocal(context.nonlocal_);
384 CYNonLocal *nextlocal(context.nextlocal_);
385
386 bool localize;
387 if (nonlocal_ != NULL) {
388 localize = false;
389 context.nonlocal_ = nonlocal_;
390 } else {
391 localize = true;
392 nonlocal_ = $ CYNonLocal();
393 context.nextlocal_ = nonlocal_;
394 }
395
396 if (!outer && name_ != NULL)
397 Inject(context);
398
399 if (parameters_ != NULL)
400 parameters_ = parameters_->Replace(context, code_);
401
402 code_.Replace(context);
403
404 if (localize)
405 context.NonLocal(code_.statements_);
406
407 context.nextlocal_ = nextlocal;
408 context.nonlocal_ = nonlocal;
409
410 scope.Close();
411 }
412
413 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
414 Replace_(context, false);
415 return this;
416 }
417
418 CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
419 context.Replace(name_);
420 context.scope_->Declare(context, name_, CYIdentifierArgument);
421 if (next_ != NULL)
422 next_ = next_->Replace(context, code);
423 return this;
424 }
425
426 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
427 Replace_(context, true);
428 return this;
429 }
430
431 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
432 if (replace_ != NULL && replace_ != this)
433 return replace_->Replace(context);
434 replace_ = context.scope_->Lookup(context, this);
435 return replace_;
436 }
437
438 CYStatement *CYIf::Replace(CYContext &context) {
439 context.Replace(test_);
440 context.Replace(true_);
441 context.Replace(false_);
442 return this;
443 }
444
445 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
446 return NULL;
447 }
448
449 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
450 return $ CYIf(test_, CYComprehension::Replace(context, statement));
451 }
452
453 CYExpression *CYIndirect::Replace(CYContext &context) {
454 CYPrefix::Replace(context);
455 return $M(rhs_, $S("$cyi"));
456 }
457
458 CYExpression *CYIndirectMember::Replace(CYContext &context) {
459 Replace_(context);
460 return $M($ CYIndirect(object_), property_);
461 }
462
463 CYExpression *CYInfix::Replace(CYContext &context) {
464 context.Replace(lhs_);
465 context.Replace(rhs_);
466 return this;
467 }
468
469 CYStatement *CYLabel::Replace(CYContext &context) {
470 context.Replace(statement_);
471 return this;
472 }
473
474 CYStatement *CYLet::Replace(CYContext &context) {
475 declarations_->Replace(context);
476 return $ CYWith($ CYObject(declarations_->Property(context)), code_);
477 }
478
479 void CYMember::Replace_(CYContext &context) {
480 context.Replace(object_);
481 context.Replace(property_);
482 }
483
484 namespace cy {
485 namespace Syntax {
486
487 CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
488 CYSetLast(arguments_, $ CYArgument(value));
489 return this;
490 }
491
492 CYExpression *New::Replace(CYContext &context) {
493 context.Replace(constructor_);
494 arguments_->Replace(context);
495 return this;
496 }
497
498 } }
499
500 CYNumber *CYNull::Number(CYContext &context) {
501 return $D(0);
502 }
503
504 CYString *CYNull::String(CYContext &context) {
505 return $S("null");
506 }
507
508 CYNumber *CYNumber::Number(CYContext &context) {
509 return this;
510 }
511
512 CYString *CYNumber::String(CYContext &context) {
513 // XXX: there is a precise algorithm for this
514 return $S(apr_psprintf($pool, "%.17g", Value()));
515 }
516
517 CYExpression *CYObject::Replace(CYContext &context) {
518 properties_->Replace(context);
519 return this;
520 }
521
522 CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
523 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
524 parameter = parameter->Replace(context, code);
525 context.Replace(initializer_);
526
527 CYVariable *name($V(name_));
528 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
529 $E($ CYAssign(name, initializer_))
530 ));
531
532 return parameter;
533 }
534
535 CYExpression *CYPostfix::Replace(CYContext &context) {
536 context.Replace(lhs_);
537 return this;
538 }
539
540 CYExpression *CYPrefix::Replace(CYContext &context) {
541 context.Replace(rhs_);
542 return this;
543 }
544
545 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
546 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
547 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
548
549 namespace {
550 struct IdentifierUsageLess :
551 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
552 {
553 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
554 if (lhs->usage_ != rhs->usage_)
555 return lhs->usage_ > rhs->usage_;
556 return lhs < rhs;
557 }
558 };
559
560 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
561 }
562
563 void CYProgram::Replace(CYContext &context) {
564 CYScope scope(CYScopeProgram, context, statements_);
565
566 context.nextlocal_ = $ CYNonLocal();
567 context.ReplaceAll(statements_);
568 context.NonLocal(statements_);
569
570 scope.Close();
571
572 size_t offset(0);
573
574 CYCStringSet external;
575 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
576 external.insert((*i)->Word());
577
578 IdentifierUsages usages;
579
580 if (offset < context.rename_.size())
581 CYForEach (i, context.rename_[offset].identifier_)
582 usages.insert(i);
583
584 // XXX: totalling the probable occurrences and sorting by them would improve the result
585 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
586 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
587
588 const char *name;
589
590 if (context.options_.verbose_)
591 name = apr_psprintf($pool, "$%"APR_SIZE_T_FMT"", offset);
592 else {
593 char id[8];
594 id[7] = '\0';
595
596 id:
597 unsigned position(7), local(offset + 1);
598
599 do {
600 unsigned index(local % (sizeof(MappingSet) - 1));
601 local /= sizeof(MappingSet) - 1;
602 id[--position] = MappingSet[index];
603 } while (local != 0);
604
605 if (external.find(id + position) != external.end()) {
606 ++offset;
607 goto id;
608 }
609
610 name = apr_pstrmemdup($pool, id + position, 7 - position);
611 // XXX: at some point, this could become a keyword
612 }
613
614 CYForEach (identifier, i->identifier_)
615 identifier->Set(name);
616 }
617 }
618
619 void CYProperty::Replace(CYContext &context) { $T()
620 context.Replace(value_);
621 next_->Replace(context);
622 }
623
624 CYStatement *CYReturn::Replace(CYContext &context) {
625 if (context.nonlocal_ != NULL) {
626 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
627 return $ cy::Syntax::Throw($ CYObject(
628 $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
629 ));
630 }
631
632 context.Replace(value_);
633 return this;
634 }
635
636 CYExpression *CYRubyBlock::Replace(CYContext &context) {
637 // XXX: this needs to do something much more epic to handle return
638 return call_->AddArgument(context, proc_->Replace(context));
639 }
640
641 CYExpression *CYRubyProc::Replace(CYContext &context) {
642 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
643 function->nonlocal_ = context.nextlocal_;
644 return function;
645 }
646
647 CYScope::CYScope(CYScopeType type, CYContext &context, CYStatement *&statements) :
648 type_(type),
649 context_(context),
650 statements_(statements),
651 parent_(context.scope_)
652 {
653 context_.scope_ = this;
654 }
655
656 CYScope::~CYScope() {
657 }
658
659 void CYScope::Close() {
660 context_.scope_ = parent_;
661 Scope(context_, statements_);
662 }
663
664 void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
665 if (type_ == CYScopeCatch && flags != CYIdentifierCatch)
666 parent_->Declare(context, identifier, flags);
667 else
668 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
669 }
670
671 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
672 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
673 return *insert.first;
674 }
675
676 void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
677 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
678 if (!insert.second) {
679 if ((*insert.first)->offset_ < identifier->offset_)
680 (*insert.first)->offset_ = identifier->offset_;
681 identifier->replace_ = *insert.first;
682 (*insert.first)->usage_ += identifier->usage_ + 1;
683 }
684 }
685
686 namespace {
687 struct IdentifierOffset {
688 size_t offset_;
689 CYIdentifierFlags flags_;
690 size_t usage_;
691 CYIdentifier *identifier_;
692
693 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
694 offset_(identifier->offset_),
695 flags_(flags),
696 usage_(identifier->usage_),
697 identifier_(identifier)
698 {
699 }
700 };
701
702 struct IdentifierOffsetLess :
703 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
704 {
705 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
706 if (lhs.offset_ != rhs.offset_)
707 return lhs.offset_ < rhs.offset_;
708 if (lhs.flags_ != rhs.flags_)
709 return lhs.flags_ < rhs.flags_;
710 /*if (lhs.usage_ != rhs.usage_)
711 return lhs.usage_ < rhs.usage_;*/
712 return lhs.identifier_ < rhs.identifier_;
713 }
714 };
715
716 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
717 }
718
719 void CYScope::Scope(CYContext &context, CYStatement *&statements) {
720 if (parent_ == NULL)
721 return;
722
723 CYDeclarations *last(NULL), *curr(NULL);
724
725 IdentifierOffsets offsets;
726
727 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
728 if (i->second != CYIdentifierMagic)
729 offsets.insert(IdentifierOffset(i->first, i->second));
730
731 size_t offset(0);
732
733 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
734 if (i->flags_ == CYIdentifierVariable) {
735 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
736 if (last == NULL)
737 last = next;
738 if (curr != NULL)
739 curr->SetNext(next);
740 curr = next;
741 }
742
743 if (offset < i->offset_)
744 offset = i->offset_;
745 if (context.rename_.size() <= offset)
746 context.rename_.resize(offset + 1);
747
748 CYIdentifierUsage &rename(context.rename_[offset++]);
749 i->identifier_->SetNext(rename.identifier_);
750 rename.identifier_ = i->identifier_;
751 rename.usage_ += i->identifier_->usage_ + 1;
752 }
753
754 if (last != NULL) {
755 CYVar *var($ CYVar(last));
756 var->SetNext(statements);
757 statements = var;
758 }
759
760 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
761 if (internal_.find(*i) == internal_.end()) {
762 //std::cout << *i << '=' << offset << std::endl;
763 if ((*i)->offset_ < offset)
764 (*i)->offset_ = offset;
765 parent_->Merge(context, *i);
766 }
767 }
768
769 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
770 size_t size(size_ + rhs->size_);
771 char *value($ char[size + 1]);
772 memcpy(value, value_, size_);
773 memcpy(value + size_, rhs->value_, rhs->size_);
774 value[size] = '\0';
775 return $S(value, size);
776 }
777
778 CYNumber *CYString::Number(CYContext &context) {
779 // XXX: there is a precise algorithm for this
780 return NULL;
781 }
782
783 CYString *CYString::String(CYContext &context) {
784 return this;
785 }
786
787 CYStatement *CYSwitch::Replace(CYContext &context) {
788 context.Replace(value_);
789 clauses_->Replace(context);
790 return this;
791 }
792
793 CYExpression *CYThis::Replace(CYContext &context) {
794 return this;
795 }
796
797 namespace cy {
798 namespace Syntax {
799
800 CYStatement *Throw::Replace(CYContext &context) {
801 context.Replace(value_);
802 return this;
803 }
804
805 } }
806
807 CYExpression *CYTrivial::Replace(CYContext &context) {
808 return this;
809 }
810
811 CYNumber *CYTrue::Number(CYContext &context) {
812 return $D(1);
813 }
814
815 CYString *CYTrue::String(CYContext &context) {
816 return $S("true");
817 }
818
819 namespace cy {
820 namespace Syntax {
821
822 CYStatement *Try::Replace(CYContext &context) {
823 code_.Replace(context);
824 catch_->Replace(context);
825 finally_->Replace(context);
826 return this;
827 }
828
829 } }
830
831 CYStatement *CYVar::Replace(CYContext &context) {
832 declarations_->Replace(context);
833 return $E(declarations_->Compound(context));
834 }
835
836 CYExpression *CYVariable::Replace(CYContext &context) {
837 context.Replace(name_);
838 return this;
839 }
840
841 CYStatement *CYWhile::Replace(CYContext &context) {
842 context.Replace(test_);
843 context.Replace(code_);
844 return this;
845 }
846
847 CYStatement *CYWith::Replace(CYContext &context) {
848 context.Replace(scope_);
849 context.Replace(code_);
850 return this;
851 }
852
853 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
854 CYString *name($S(this));
855 if (object)
856 return $C1($V("objc_getClass"), name);
857 else
858 return name;
859 }