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