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