]> git.saurik.com Git - cycript.git/blob - Replace.cpp
Port Objective-C/Replace to C++.
[cycript.git] / Replace.cpp
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #include "Parser.hpp"
41 #include "Replace.hpp"
42
43 #include <iomanip>
44
45 CYExpression *CYAdd::Replace(CYContext &context) {
46 CYInfix::Replace(context);
47
48 CYExpression *lhp(lhs_->Primitive(context));
49 CYExpression *rhp(rhs_->Primitive(context));
50
51 CYString *lhs(dynamic_cast<CYString *>(lhp));
52 CYString *rhs(dynamic_cast<CYString *>(rhp));
53
54 if (lhs != NULL || rhs != NULL) {
55 if (lhs == NULL) {
56 lhs = lhp->String(context);
57 if (lhs == NULL)
58 return this;
59 } else if (rhs == NULL) {
60 rhs = rhp->String(context);
61 if (rhs == NULL)
62 return this;
63 }
64
65 return lhs->Concat(context, rhs);
66 }
67
68 if (CYNumber *lhn = lhp->Number(context))
69 if (CYNumber *rhn = rhp->Number(context))
70 return $D(lhn->Value() + rhn->Value());
71
72 return this;
73 }
74
75 CYExpression *CYAddressOf::Replace(CYContext &context) {
76 CYPrefix::Replace(context);
77 return $C0($M(rhs_, $S("$cya")));
78 }
79
80 void CYArgument::Replace(CYContext &context) { $T()
81 context.Replace(value_);
82 next_->Replace(context);
83 }
84
85 CYExpression *CYArray::Replace(CYContext &context) {
86 elements_->Replace(context);
87 return this;
88 }
89
90 CYExpression *CYArrayComprehension::Replace(CYContext &context) {
91 CYVariable *cyv($V("$cyv"));
92
93 return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->*
94 $E($ CYAssign(cyv, $ CYArray()))->*
95 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
96 $ CYReturn(cyv)
97 ));
98 }
99
100 CYExpression *CYAssignment::Replace(CYContext &context) {
101 context.Replace(lhs_);
102 context.Replace(rhs_);
103 return this;
104 }
105
106 CYStatement *CYBlock::Replace(CYContext &context) {
107 statements_ = statements_->ReplaceAll(context);
108 if (statements_ == NULL)
109 return $ CYEmpty();
110 return this;
111 }
112
113 CYStatement *CYBreak::Replace(CYContext &context) {
114 return this;
115 }
116
117 CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
118 CYArgument **argument(&arguments_);
119 while (*argument != NULL)
120 argument = &(*argument)->next_;
121 *argument = $ CYArgument(value);
122 return this;
123 }
124
125 CYExpression *CYCall::Replace(CYContext &context) {
126 context.Replace(function_);
127 arguments_->Replace(context);
128 return this;
129 }
130
131 namespace cy {
132 namespace Syntax {
133
134 void Catch::Replace(CYContext &context) { $T()
135 CYScope scope(CYScopeCatch, context, code_.statements_);
136
137 context.Replace(name_);
138 context.scope_->Declare(context, name_, CYIdentifierCatch);
139
140 code_.Replace(context);
141 scope.Close();
142 }
143
144 } }
145
146 void CYClause::Replace(CYContext &context) { $T()
147 context.Replace(case_);
148 statements_ = statements_->ReplaceAll(context);
149 next_->Replace(context);
150 }
151
152 CYStatement *CYComment::Replace(CYContext &context) {
153 return this;
154 }
155
156 CYExpression *CYCompound::Replace(CYContext &context) {
157 expressions_ = expressions_->ReplaceAll(context);
158 return expressions_ == NULL ? NULL : this;
159 }
160
161 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
162 CYFunctionParameter *next(next_->Parameters(context));
163 if (CYFunctionParameter *parameter = Parameter(context)) {
164 parameter->SetNext(next);
165 return parameter;
166 } else
167 return next;
168 }
169
170 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
171 return next_ == NULL ? statement : next_->Replace(context, statement);
172 }
173
174 CYExpression *CYCondition::Replace(CYContext &context) {
175 context.Replace(test_);
176 context.Replace(true_);
177 context.Replace(false_);
178 return this;
179 }
180
181 void CYContext::NonLocal(CYStatement *&statements) {
182 CYContext &context(*this);
183
184 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
185 CYIdentifier *cye($I("$cye")->Replace(context));
186 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
187
188 CYStatement *declare(
189 $ CYVar($L1($L(unique, $ CYObject()))));
190
191 cy::Syntax::Catch *rescue(
192 $ cy::Syntax::Catch(cye, $$->*
193 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
194 $ CYReturn($M($V(cye), $S("$cyv"))))->*
195 $ cy::Syntax::Throw($V(cye))));
196
197 context.Replace(declare);
198 rescue->Replace(context);
199
200 statements = $$->*
201 declare->*
202 $ cy::Syntax::Try(statements, rescue, NULL);
203 }
204 }
205
206 CYIdentifier *CYContext::Unique() {
207 return $ CYIdentifier(apr_psprintf($pool, "$cy%u", unique_++));
208 }
209
210 CYStatement *CYContinue::Replace(CYContext &context) {
211 return this;
212 }
213
214 CYAssignment *CYDeclaration::Assignment(CYContext &context) {
215 CYExpression *variable(Replace(context));
216 return initialiser_ == NULL ? NULL : $ CYAssign(variable, initialiser_);
217 }
218
219 CYExpression *CYDeclaration::ForEachIn(CYContext &context) {
220 return $V(identifier_);
221 }
222
223 CYExpression *CYDeclaration::Replace(CYContext &context) {
224 context.Replace(identifier_);
225 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
226 return $V(identifier_);
227 }
228
229 CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
230 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context));
231 }
232
233 CYCompound *CYDeclarations::Replace(CYContext &context) {
234 CYCompound *compound;
235 if (next_ == NULL) compound:
236 compound = $ CYCompound();
237 else {
238 compound = next_->Replace(context);
239 if (compound == NULL)
240 goto compound;
241 }
242
243 if (CYAssignment *assignment = declaration_->Assignment(context))
244 compound->AddPrev(assignment);
245 return compound;
246 }
247
248 CYExpression *CYDirectMember::Replace(CYContext &context) {
249 Replace_(context);
250 return this;
251 }
252
253 CYStatement *CYDoWhile::Replace(CYContext &context) {
254 context.Replace(test_);
255 context.Replace(code_);
256 return this;
257 }
258
259 void CYElement::Replace(CYContext &context) { $T()
260 context.Replace(value_);
261 next_->Replace(context);
262 }
263
264 CYStatement *CYEmpty::Collapse(CYContext &context) {
265 return next_;
266 }
267
268 CYStatement *CYEmpty::Replace(CYContext &context) {
269 return this;
270 }
271
272 CYStatement *CYExpress::Collapse(CYContext &context) {
273 if (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
274 CYCompound *next(dynamic_cast<CYCompound *>(express->expression_));
275 if (next == NULL)
276 next = $ CYCompound(express->expression_);
277 next->AddPrev(expression_);
278 expression_ = next;
279 SetNext(express->next_);
280 }
281
282 return this;
283 }
284
285 CYStatement *CYExpress::Replace(CYContext &context) {
286 context.Replace(expression_);
287 if (expression_ == NULL)
288 return $ CYEmpty();
289 return this;
290 }
291
292 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
293 return $C1(this, value);
294 }
295
296 CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
297 return this;
298 }
299
300 CYExpression *CYExpression::ForEachIn(CYContext &context) {
301 return this;
302 }
303
304 CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL)
305 CYExpression *replace(this);
306 context.Replace(replace);
307
308 if (CYExpression *next = next_->ReplaceAll(context))
309 replace->SetNext(next);
310 else
311 replace->SetNext(next_);
312
313 return replace;
314 }
315
316 CYNumber *CYFalse::Number(CYContext &context) {
317 return $D(0);
318 }
319
320 CYString *CYFalse::String(CYContext &context) {
321 return $S("false");
322 }
323
324 void CYFinally::Replace(CYContext &context) { $T()
325 code_.Replace(context);
326 }
327
328 CYStatement *CYFor::Replace(CYContext &context) {
329 context.Replace(initialiser_);
330 context.Replace(test_);
331 context.Replace(increment_);
332 context.Replace(code_);
333 return this;
334 }
335
336 CYStatement *CYForIn::Replace(CYContext &context) {
337 // XXX: this actually might need a prefix statement
338 context.Replace(initialiser_);
339 context.Replace(set_);
340 context.Replace(code_);
341 return this;
342 }
343
344 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
345 return $ CYFunctionParameter(name_);
346 }
347
348 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
349 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
350 }
351
352 CYStatement *CYForEachIn::Replace(CYContext &context) {
353 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
354
355 return $ CYLet($L2($L(cys, set_), $L(cyt)), $$->*
356 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
357 $E($ CYAssign(initialiser_->ForEachIn(context), $M($V(cys), $V(cyt))))->*
358 code_
359 ))
360 );
361 }
362
363 CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
364 return $ CYFunctionParameter(name_);
365 }
366
367 CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
368 CYIdentifier *cys($I("cys"));
369
370 return $E($C0($F(NULL, $P1("$cys"), $$->*
371 $E($ CYAssign($V(cys), set_))->*
372 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
373 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
374 CYComprehension::Replace(context, statement)
375 ))
376 )));
377 }
378
379 void CYFunction::Inject(CYContext &context) {
380 context.Replace(name_);
381 context.scope_->Declare(context, name_, CYIdentifierOther);
382 }
383
384 void CYFunction::Replace_(CYContext &context, bool outer) {
385 if (outer)
386 Inject(context);
387
388 CYScope scope(CYScopeFunction, context, code_.statements_);
389
390 CYNonLocal *nonlocal(context.nonlocal_);
391 CYNonLocal *nextlocal(context.nextlocal_);
392
393 bool localize;
394 if (nonlocal_ != NULL) {
395 localize = false;
396 context.nonlocal_ = nonlocal_;
397 } else {
398 localize = true;
399 nonlocal_ = $ CYNonLocal();
400 context.nextlocal_ = nonlocal_;
401 }
402
403 if (!outer && name_ != NULL)
404 Inject(context);
405
406 if (parameters_ != NULL)
407 parameters_ = parameters_->Replace(context, code_);
408
409 code_.Replace(context);
410
411 if (localize)
412 context.NonLocal(code_.statements_);
413
414 context.nextlocal_ = nextlocal;
415 context.nonlocal_ = nonlocal;
416
417 scope.Close();
418 }
419
420 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
421 Replace_(context, false);
422 return this;
423 }
424
425 CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
426 context.Replace(name_);
427 context.scope_->Declare(context, name_, CYIdentifierArgument);
428 if (next_ != NULL)
429 next_ = next_->Replace(context, code);
430 return this;
431 }
432
433 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
434 Replace_(context, true);
435 return this;
436 }
437
438 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
439 if (replace_ != NULL && replace_ != this)
440 return replace_->Replace(context);
441 replace_ = context.scope_->Lookup(context, this);
442 return replace_;
443 }
444
445 CYStatement *CYIf::Replace(CYContext &context) {
446 context.Replace(test_);
447 context.Replace(true_);
448 context.Replace(false_);
449 return this;
450 }
451
452 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
453 return NULL;
454 }
455
456 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
457 return $ CYIf(test_, CYComprehension::Replace(context, statement));
458 }
459
460 CYExpression *CYIndirect::Replace(CYContext &context) {
461 CYPrefix::Replace(context);
462 return $M(rhs_, $S("$cyi"));
463 }
464
465 CYExpression *CYIndirectMember::Replace(CYContext &context) {
466 Replace_(context);
467 return $M($ CYIndirect(object_), property_);
468 }
469
470 CYExpression *CYInfix::Replace(CYContext &context) {
471 context.Replace(lhs_);
472 context.Replace(rhs_);
473 return this;
474 }
475
476 CYStatement *CYLabel::Replace(CYContext &context) {
477 context.Replace(statement_);
478 return this;
479 }
480
481 CYStatement *CYLet::Replace(CYContext &context) {
482 return $ CYWith($ CYObject(declarations_->Property(context)), &code_);
483 }
484
485 void CYMember::Replace_(CYContext &context) {
486 context.Replace(object_);
487 context.Replace(property_);
488 }
489
490 namespace cy {
491 namespace Syntax {
492
493 CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
494 CYArgument **argument(&arguments_);
495 while (*argument != NULL)
496 argument = &(*argument)->next_;
497 *argument = $ CYArgument(value);
498 return this;
499 }
500
501 CYExpression *New::Replace(CYContext &context) {
502 context.Replace(constructor_);
503 arguments_->Replace(context);
504 return this;
505 }
506
507 } }
508
509 CYNumber *CYNull::Number(CYContext &context) {
510 return $D(0);
511 }
512
513 CYString *CYNull::String(CYContext &context) {
514 return $S("null");
515 }
516
517 CYNumber *CYNumber::Number(CYContext &context) {
518 return this;
519 }
520
521 CYString *CYNumber::String(CYContext &context) {
522 // XXX: there is a precise algorithm for this
523 return $S(apr_psprintf($pool, "%.17g", Value()));
524 }
525
526 CYExpression *CYObject::Replace(CYContext &context) {
527 properties_->Replace(context);
528 return this;
529 }
530
531 CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
532 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
533 parameter = parameter->Replace(context, code);
534 context.Replace(initializer_);
535
536 CYVariable *name($V(name_));
537 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
538 $E($ CYAssign(name, initializer_))
539 ));
540
541 return parameter;
542 }
543
544 CYExpression *CYPostfix::Replace(CYContext &context) {
545 context.Replace(lhs_);
546 return this;
547 }
548
549 CYExpression *CYPrefix::Replace(CYContext &context) {
550 context.Replace(rhs_);
551 return this;
552 }
553
554 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
555 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
556 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
557
558 namespace {
559 struct IdentifierUsageLess :
560 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
561 {
562 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
563 if (lhs->usage_ != rhs->usage_)
564 return lhs->usage_ > rhs->usage_;
565 return lhs < rhs;
566 }
567 };
568
569 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
570 }
571
572 void CYProgram::Replace(CYContext &context) {
573 CYScope scope(CYScopeProgram, context, statements_);
574
575 context.nextlocal_ = $ CYNonLocal();
576 statements_ = statements_->ReplaceAll(context);
577 context.NonLocal(statements_);
578
579 scope.Close();
580
581 size_t offset(0);
582
583 CYCStringSet external;
584 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
585 external.insert((*i)->Word());
586
587 IdentifierUsages usages;
588
589 if (offset < context.rename_.size())
590 for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_)
591 usages.insert(i);
592
593 // XXX: totalling the probable occurrences and sorting by them would improve the result
594 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
595 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
596
597 const char *name;
598
599 if (context.options_.verbose_)
600 name = apr_psprintf($pool, "$%"APR_SIZE_T_FMT"", offset);
601 else {
602 char id[8];
603 id[7] = '\0';
604
605 id:
606 unsigned position(7), local(offset + 1);
607
608 do {
609 unsigned index(local % (sizeof(MappingSet) - 1));
610 local /= sizeof(MappingSet) - 1;
611 id[--position] = MappingSet[index];
612 } while (local != 0);
613
614 if (external.find(id + position) != external.end()) {
615 ++offset;
616 goto id;
617 }
618
619 name = apr_pstrmemdup($pool, id + position, 7 - position);
620 // XXX: at some point, this could become a keyword
621 }
622
623 for (CYIdentifier *identifier(i->identifier_); identifier != NULL; identifier = identifier->next_)
624 identifier->Set(name);
625 }
626 }
627
628 void CYProperty::Replace(CYContext &context) { $T()
629 context.Replace(value_);
630 next_->Replace(context);
631 }
632
633 CYStatement *CYReturn::Replace(CYContext &context) {
634 if (context.nonlocal_ != NULL) {
635 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
636 return $ cy::Syntax::Throw($ CYObject(
637 $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
638 ));
639 }
640
641 context.Replace(value_);
642 return this;
643 }
644
645 CYExpression *CYRubyBlock::Replace(CYContext &context) {
646 // XXX: this needs to do something much more epic to handle return
647 return call_->AddArgument(context, proc_->Replace(context));
648 }
649
650 CYExpression *CYRubyProc::Replace(CYContext &context) {
651 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
652 function->nonlocal_ = context.nextlocal_;
653 return function;
654 }
655
656 CYScope::CYScope(CYScopeType type, CYContext &context, CYStatement *&statements) :
657 type_(type),
658 context_(context),
659 statements_(statements),
660 parent_(context.scope_)
661 {
662 context_.scope_ = this;
663 }
664
665 void CYScope::Close() {
666 context_.scope_ = parent_;
667 Scope(context_, statements_);
668 }
669
670 void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
671 if (type_ == CYScopeCatch && flags != CYIdentifierCatch)
672 parent_->Declare(context, identifier, flags);
673 else
674 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
675 }
676
677 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
678 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
679 return *insert.first;
680 }
681
682 void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
683 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
684 if (!insert.second) {
685 if ((*insert.first)->offset_ < identifier->offset_)
686 (*insert.first)->offset_ = identifier->offset_;
687 identifier->replace_ = *insert.first;
688 (*insert.first)->usage_ += identifier->usage_ + 1;
689 }
690 }
691
692 namespace {
693 struct IdentifierOffset {
694 size_t offset_;
695 CYIdentifierFlags flags_;
696 size_t usage_;
697 CYIdentifier *identifier_;
698
699 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
700 offset_(identifier->offset_),
701 flags_(flags),
702 usage_(identifier->usage_),
703 identifier_(identifier)
704 {
705 }
706 };
707
708 struct IdentifierOffsetLess :
709 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
710 {
711 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
712 if (lhs.offset_ != rhs.offset_)
713 return lhs.offset_ < rhs.offset_;
714 if (lhs.flags_ != rhs.flags_)
715 return lhs.flags_ < rhs.flags_;
716 /*if (lhs.usage_ != rhs.usage_)
717 return lhs.usage_ < rhs.usage_;*/
718 return lhs.identifier_ < rhs.identifier_;
719 }
720 };
721
722 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
723 }
724
725 void CYScope::Scope(CYContext &context, CYStatement *&statements) {
726 if (parent_ == NULL)
727 return;
728
729 CYDeclarations *last(NULL), *curr(NULL);
730
731 IdentifierOffsets offsets;
732
733 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
734 if (i->second != CYIdentifierMagic)
735 offsets.insert(IdentifierOffset(i->first, i->second));
736
737 size_t offset(0);
738
739 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
740 if (i->flags_ == CYIdentifierVariable) {
741 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
742 if (last == NULL)
743 last = next;
744 if (curr != NULL)
745 curr->SetNext(next);
746 curr = next;
747 }
748
749 if (offset < i->offset_)
750 offset = i->offset_;
751 if (context.rename_.size() <= offset)
752 context.rename_.resize(offset + 1);
753
754 CYIdentifierUsage &rename(context.rename_[offset++]);
755 i->identifier_->SetNext(rename.identifier_);
756 rename.identifier_ = i->identifier_;
757 rename.usage_ += i->identifier_->usage_ + 1;
758 }
759
760 if (last != NULL) {
761 CYVar *var($ CYVar(last));
762 var->SetNext(statements);
763 statements = var;
764 }
765
766 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
767 if (internal_.find(*i) == internal_.end()) {
768 //std::cout << *i << '=' << offset << std::endl;
769 if ((*i)->offset_ < offset)
770 (*i)->offset_ = offset;
771 parent_->Merge(context, *i);
772 }
773 }
774
775 CYStatement *CYStatement::Collapse(CYContext &context) {
776 return this;
777 }
778
779 CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL)
780 CYStatement *replace(this);
781 context.Replace(replace);
782 replace->SetNext(next_->ReplaceAll(context));
783 return replace->Collapse(context);
784 }
785
786 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
787 size_t size(size_ + rhs->size_);
788 char *value($ char[size + 1]);
789 memcpy(value, value_, size_);
790 memcpy(value + size_, rhs->value_, rhs->size_);
791 value[size] = '\0';
792 return $S(value, size);
793 }
794
795 CYNumber *CYString::Number(CYContext &context) {
796 // XXX: there is a precise algorithm for this
797 return NULL;
798 }
799
800 CYString *CYString::String(CYContext &context) {
801 return this;
802 }
803
804 CYStatement *CYSwitch::Replace(CYContext &context) {
805 context.Replace(value_);
806 clauses_->Replace(context);
807 return this;
808 }
809
810 CYExpression *CYThis::Replace(CYContext &context) {
811 return this;
812 }
813
814 namespace cy {
815 namespace Syntax {
816
817 CYStatement *Throw::Replace(CYContext &context) {
818 context.Replace(value_);
819 return this;
820 }
821
822 } }
823
824 CYExpression *CYTrivial::Replace(CYContext &context) {
825 return this;
826 }
827
828 CYNumber *CYTrue::Number(CYContext &context) {
829 return $D(1);
830 }
831
832 CYString *CYTrue::String(CYContext &context) {
833 return $S("true");
834 }
835
836 namespace cy {
837 namespace Syntax {
838
839 CYStatement *Try::Replace(CYContext &context) {
840 code_.Replace(context);
841 catch_->Replace(context);
842 finally_->Replace(context);
843 return this;
844 }
845
846 } }
847
848 CYStatement *CYVar::Replace(CYContext &context) {
849 return $E(declarations_->Replace(context));
850 }
851
852 CYExpression *CYVariable::Replace(CYContext &context) {
853 context.Replace(name_);
854 return this;
855 }
856
857 CYStatement *CYWhile::Replace(CYContext &context) {
858 context.Replace(test_);
859 context.Replace(code_);
860 return this;
861 }
862
863 CYStatement *CYWith::Replace(CYContext &context) {
864 context.Replace(scope_);
865 context.Replace(code_);
866 return this;
867 }
868
869 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
870 CYString *name($S(this));
871 if (object)
872 return $C1($V("objc_getClass"), name);
873 else
874 return name;
875 }