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