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