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