]> git.saurik.com Git - cycript.git/blob - Replace.cpp
New syntax: let declarations, initialised arguments.
[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 CYFunctionExpression *CYNonLocalize(CYContext &context, CYFunctionExpression *function) {
28 function->nonlocal_ = context.nextlocal_;
29 return function;
30 }
31
32 CYExpression *CYAdd::Replace(CYContext &context) {
33 CYInfix::Replace(context);
34
35 CYExpression *lhp(lhs_->Primitive(context));
36 CYExpression *rhp(rhs_->Primitive(context));
37
38 CYString *lhs(dynamic_cast<CYString *>(lhp));
39 CYString *rhs(dynamic_cast<CYString *>(rhp));
40
41 if (lhs != NULL || rhs != NULL) {
42 if (lhs == NULL) {
43 lhs = lhp->String(context);
44 if (lhs == NULL)
45 return this;
46 } else if (rhs == NULL) {
47 rhs = rhp->String(context);
48 if (rhs == NULL)
49 return this;
50 }
51
52 return lhs->Concat(context, rhs);
53 }
54
55 if (CYNumber *lhn = lhp->Number(context))
56 if (CYNumber *rhn = rhp->Number(context))
57 return $D(lhn->Value() + rhn->Value());
58
59 return this;
60 }
61
62 CYExpression *CYAddressOf::Replace(CYContext &context) {
63 return $C0($M(rhs_, $S("$cya")));
64 }
65
66 CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
67 context.Replace(value_);
68 next_ = next_->Replace(context);
69
70 if (value_ == NULL) {
71 if (next_ == NULL)
72 return NULL;
73 else
74 value_ = $U;
75 }
76
77 return this;
78 }
79
80 CYExpression *CYArray::Replace(CYContext &context) {
81 elements_->Replace(context);
82 return this;
83 }
84
85 CYExpression *CYArrayComprehension::Replace(CYContext &context) {
86 CYVariable *cyv($V("$cyv"));
87
88 return $C0($F(NULL, $P1($L("$cyv"), comprehensions_->Parameters(context)), $$->*
89 $E($ CYAssign(cyv, $ CYArray()))->*
90 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
91 $ CYReturn(cyv)
92 ));
93 }
94
95 CYExpression *CYAssignment::Replace(CYContext &context) {
96 context.Replace(lhs_);
97 context.Replace(rhs_);
98 return this;
99 }
100
101 CYStatement *CYBlock::Replace(CYContext &context) {
102 context.ReplaceAll(statements_);
103 if (statements_ == NULL)
104 return $ CYEmpty();
105 return this;
106 }
107
108 CYStatement *CYBreak::Replace(CYContext &context) {
109 return this;
110 }
111
112 CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
113 CYArgument **argument(&arguments_);
114 while (*argument != NULL)
115 argument = &(*argument)->next_;
116 *argument = $ CYArgument(value);
117 return this;
118 }
119
120 CYExpression *CYCall::Replace(CYContext &context) {
121 context.Replace(function_);
122 arguments_->Replace(context);
123 return this;
124 }
125
126 namespace cy {
127 namespace Syntax {
128
129 void Catch::Replace(CYContext &context) { $T()
130 CYScope scope(true, context, code_.statements_);
131
132 context.Replace(name_);
133 context.scope_->Declare(context, name_, CYIdentifierCatch);
134
135 code_.Replace(context);
136 scope.Close();
137 }
138
139 } }
140
141 void CYClause::Replace(CYContext &context) { $T()
142 context.Replace(case_);
143 context.ReplaceAll(statements_);
144 next_->Replace(context);
145 }
146
147 CYStatement *CYComment::Replace(CYContext &context) {
148 return this;
149 }
150
151 CYExpression *CYCompound::Replace(CYContext &context) {
152 context.ReplaceAll(expressions_);
153 if (expressions_ == NULL)
154 return NULL;
155 return this;
156 }
157
158 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
159 CYFunctionParameter *next(next_->Parameters(context));
160 if (CYFunctionParameter *parameter = Parameter(context)) {
161 parameter->SetNext(next);
162 return parameter;
163 } else
164 return next;
165 }
166
167 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
168 return next_ == NULL ? statement : next_->Replace(context, statement);
169 }
170
171 CYExpression *CYCondition::Replace(CYContext &context) {
172 context.Replace(test_);
173 context.Replace(true_);
174 context.Replace(false_);
175 return this;
176 }
177
178 void CYContext::NonLocal(CYStatement *&statements) {
179 CYContext &context(*this);
180
181 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
182 CYIdentifier *cye($I("$cye")->Replace(context));
183 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
184
185 CYStatement *declare(
186 $ CYVar($L1($ CYDeclaration(unique, $ CYObject()))));
187
188 cy::Syntax::Catch *rescue(
189 $ cy::Syntax::Catch(cye, $$->*
190 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
191 $ CYReturn($M($V(cye), $S("$cyv"))))->*
192 $ cy::Syntax::Throw($V(cye))));
193
194 context.Replace(declare);
195 rescue->Replace(context);
196
197 statements = $$->*
198 declare->*
199 $ cy::Syntax::Try(statements, rescue, NULL);
200 }
201 }
202
203 CYIdentifier *CYContext::Unique() {
204 return $ CYIdentifier(apr_psprintf($pool, "$cy%u", unique_++));
205 }
206
207 CYStatement *CYContinue::Replace(CYContext &context) {
208 return this;
209 }
210
211 CYStatement *CYDebugger::Replace(CYContext &context) {
212 return this;
213 }
214
215 CYAssignment *CYDeclaration::Assignment(CYContext &context) {
216 if (initialiser_ == NULL)
217 return NULL;
218
219 CYAssignment *value($ CYAssign(Variable(context), initialiser_));
220 initialiser_ = NULL;
221 return value;
222 }
223
224 CYVariable *CYDeclaration::Variable(CYContext &context) {
225 return $V(identifier_);
226 }
227
228 CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
229 return $ CYVar($L1($ CYDeclaration(identifier_, value)));
230 }
231
232 CYExpression *CYDeclaration::Replace(CYContext &context) {
233 context.Replace(identifier_);
234 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
235 return Variable(context);
236 }
237
238 void CYDeclarations::Replace(CYContext &context) { $T()
239 declaration_->Replace(context);
240 next_->Replace(context);
241 }
242
243 CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
244 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_, next_->Property(context));
245 }
246
247 CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
248 return $ CYFunctionParameter($ CYDeclaration(declaration_->identifier_), next_->Parameter(context));
249 }
250
251 CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
252 return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
253 }
254
255 CYCompound *CYDeclarations::Compound(CYContext &context) { $T(NULL)
256 CYCompound *compound(next_->Compound(context) ?: $ CYCompound());
257 if (CYAssignment *assignment = declaration_->Assignment(context))
258 compound->AddPrev(assignment);
259 return compound;
260 }
261
262 CYExpression *CYDirectMember::Replace(CYContext &context) {
263 context.Replace(object_);
264 context.Replace(property_);
265 return this;
266 }
267
268 CYStatement *CYDoWhile::Replace(CYContext &context) {
269 context.Replace(test_);
270 context.Replace(code_);
271 return this;
272 }
273
274 void CYElement::Replace(CYContext &context) { $T()
275 context.Replace(value_);
276 next_->Replace(context);
277 }
278
279 CYStatement *CYEmpty::Replace(CYContext &context) {
280 return NULL;
281 }
282
283 CYStatement *CYExpress::Replace(CYContext &context) {
284 while (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
285 CYCompound *compound(dynamic_cast<CYCompound *>(express->expression_));
286 if (compound == NULL)
287 compound = $ CYCompound(express->expression_);
288 compound->AddPrev(expression_);
289 expression_ = compound;
290 SetNext(express->next_);
291 }
292
293 context.Replace(expression_);
294 if (expression_ == NULL)
295 return $ CYEmpty();
296
297 return this;
298 }
299
300 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
301 return $C1(this, value);
302 }
303
304 CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
305 return this;
306 }
307
308 CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
309 return $E($ CYAssign(this, value));
310 }
311
312 CYAssignment *CYExpression::Assignment(CYContext &context) {
313 return NULL;
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 CYCompound *CYForDeclarations::Replace(CYContext &context) {
337 declarations_->Replace(context);
338 return declarations_->Compound(context);
339 }
340
341 // XXX: this still feels highly suboptimal
342 CYStatement *CYForIn::Replace(CYContext &context) {
343 if (CYAssignment *assignment = initialiser_->Assignment(context))
344 return $ CYBlock($$->*
345 $E(assignment)->*
346 this
347 );
348
349 context.Replace(initialiser_);
350 context.Replace(set_);
351 context.Replace(code_);
352 return this;
353 }
354
355 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
356 return $ CYFunctionParameter($ CYDeclaration(name_));
357 }
358
359 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
360 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
361 }
362
363 CYStatement *CYForEachIn::Replace(CYContext &context) {
364 if (CYAssignment *assignment = initialiser_->Assignment(context))
365 return $ CYBlock($$->*
366 $E(assignment)->*
367 this
368 );
369
370 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
371
372 return $ CYLetStatement($L2($ CYDeclaration(cys, set_), $ CYDeclaration(cyt)), $$->*
373 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
374 initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))->*
375 code_
376 ))
377 );
378 }
379
380 CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
381 return $ CYFunctionParameter($ CYDeclaration(name_));
382 }
383
384 CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
385 CYIdentifier *cys($I("cys"));
386
387 return $E($C0($F(NULL, $P1($L("$cys")), $$->*
388 $E($ CYAssign($V(cys), set_))->*
389 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
390 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
391 CYComprehension::Replace(context, statement)
392 ))
393 )));
394 }
395
396 void CYFunction::Inject(CYContext &context) {
397 context.Replace(name_);
398 context.scope_->Declare(context, name_, CYIdentifierOther);
399 }
400
401 void CYFunction::Replace_(CYContext &context, bool outer) {
402 if (outer)
403 Inject(context);
404
405 CYNonLocal *nonlocal(context.nonlocal_);
406 CYNonLocal *nextlocal(context.nextlocal_);
407
408 bool localize;
409 if (nonlocal_ != NULL) {
410 localize = false;
411 context.nonlocal_ = nonlocal_;
412 } else {
413 localize = true;
414 nonlocal_ = $ CYNonLocal();
415 context.nextlocal_ = nonlocal_;
416 }
417
418 CYScope scope(!localize, context, code_.statements_);
419
420 if (!outer && name_ != NULL)
421 Inject(context);
422
423 parameters_->Replace(context, code_);
424 code_.Replace(context);
425
426 if (localize)
427 context.NonLocal(code_.statements_);
428
429 context.nextlocal_ = nextlocal;
430 context.nonlocal_ = nonlocal;
431
432 scope.Close();
433 }
434
435 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
436 Replace_(context, false);
437 return this;
438 }
439
440 void CYFunctionParameter::Replace(CYContext &context, CYBlock &code) { $T()
441 CYAssignment *assignment(initialiser_->Assignment(context));
442 context.Replace(initialiser_);
443
444 next_->Replace(context, code);
445
446 if (assignment != NULL)
447 // XXX: this cast is quite incorrect
448 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(dynamic_cast<CYExpression *>(initialiser_)), $S("undefined")), $$->*
449 $E(assignment)
450 ));
451 }
452
453 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
454 Replace_(context, true);
455 return this;
456 }
457
458 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
459 if (replace_ != NULL && replace_ != this)
460 return replace_->Replace(context);
461 replace_ = context.scope_->Lookup(context, this);
462 return replace_;
463 }
464
465 CYStatement *CYIf::Replace(CYContext &context) {
466 context.Replace(test_);
467 context.Replace(true_);
468 context.Replace(false_);
469 return this;
470 }
471
472 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
473 return NULL;
474 }
475
476 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
477 return $ CYIf(test_, CYComprehension::Replace(context, statement));
478 }
479
480 CYExpression *CYIndirect::Replace(CYContext &context) {
481 return $M(rhs_, $S("$cyi"));
482 }
483
484 CYExpression *CYIndirectMember::Replace(CYContext &context) {
485 return $M($ CYIndirect(object_), property_);
486 }
487
488 CYExpression *CYInfix::Replace(CYContext &context) {
489 context.Replace(lhs_);
490 context.Replace(rhs_);
491 return this;
492 }
493
494 CYStatement *CYLabel::Replace(CYContext &context) {
495 context.Replace(statement_);
496 return this;
497 }
498
499 CYStatement *CYLetStatement::Replace(CYContext &context) {
500 return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context)));
501 }
502
503 namespace cy {
504 namespace Syntax {
505
506 CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
507 CYSetLast(arguments_, $ CYArgument(value));
508 return this;
509 }
510
511 CYExpression *New::Replace(CYContext &context) {
512 context.Replace(constructor_);
513 arguments_->Replace(context);
514 return this;
515 }
516
517 } }
518
519 CYNumber *CYNull::Number(CYContext &context) {
520 return $D(0);
521 }
522
523 CYString *CYNull::String(CYContext &context) {
524 return $S("null");
525 }
526
527 CYNumber *CYNumber::Number(CYContext &context) {
528 return this;
529 }
530
531 CYString *CYNumber::String(CYContext &context) {
532 // XXX: there is a precise algorithm for this
533 return $S(apr_psprintf($pool, "%.17g", Value()));
534 }
535
536 CYExpression *CYObject::Replace(CYContext &context) {
537 properties_->Replace(context);
538 return this;
539 }
540
541 CYExpression *CYPostfix::Replace(CYContext &context) {
542 context.Replace(lhs_);
543 return this;
544 }
545
546 CYExpression *CYPrefix::Replace(CYContext &context) {
547 context.Replace(rhs_);
548 return this;
549 }
550
551 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
552 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
553 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
554
555 namespace {
556 struct IdentifierUsageLess :
557 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
558 {
559 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
560 if (lhs->usage_ != rhs->usage_)
561 return lhs->usage_ > rhs->usage_;
562 return lhs < rhs;
563 }
564 };
565
566 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
567 }
568
569 void CYProgram::Replace(CYContext &context) {
570 CYScope scope(true, context, statements_);
571
572 context.nextlocal_ = $ CYNonLocal();
573 context.ReplaceAll(statements_);
574 context.NonLocal(statements_);
575
576 scope.Close();
577
578 size_t offset(0);
579
580 CYCStringSet external;
581 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
582 external.insert((*i)->Word());
583
584 IdentifierUsages usages;
585
586 if (offset < context.rename_.size())
587 CYForEach (i, context.rename_[offset].identifier_)
588 usages.insert(i);
589
590 // XXX: totalling the probable occurrences and sorting by them would improve the result
591 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
592 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
593
594 const char *name;
595
596 if (context.options_.verbose_)
597 name = apr_psprintf($pool, "$%"APR_SIZE_T_FMT"", offset);
598 else {
599 char id[8];
600 id[7] = '\0';
601
602 id:
603 unsigned position(7), local(offset + 1);
604
605 do {
606 unsigned index(local % (sizeof(MappingSet) - 1));
607 local /= sizeof(MappingSet) - 1;
608 id[--position] = MappingSet[index];
609 } while (local != 0);
610
611 if (external.find(id + position) != external.end()) {
612 ++offset;
613 goto id;
614 }
615
616 name = apr_pstrmemdup($pool, id + position, 7 - position);
617 // XXX: at some point, this could become a keyword
618 }
619
620 CYForEach (identifier, i->identifier_)
621 identifier->Set(name);
622 }
623 }
624
625 void CYProperty::Replace(CYContext &context) { $T()
626 context.Replace(value_);
627 next_->Replace(context);
628 if (value_ == NULL)
629 value_ = $U;
630 }
631
632 CYStatement *CYReturn::Replace(CYContext &context) {
633 if (context.nonlocal_ != NULL) {
634 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
635 return $ cy::Syntax::Throw($ CYObject(
636 $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
637 ));
638 }
639
640 context.Replace(value_);
641 return this;
642 }
643
644 CYExpression *CYRubyBlock::Replace(CYContext &context) {
645 // XXX: this needs to do something much more epic to handle return
646 return call_->AddArgument(context, proc_->Replace(context));
647 }
648
649 CYExpression *CYRubyProc::Replace(CYContext &context) {
650 return CYNonLocalize(context, $ CYFunctionExpression(NULL, parameters_, code_));
651 }
652
653 CYScope::CYScope(bool transparent, CYContext &context, CYStatement *&statements) :
654 transparent_(transparent),
655 context_(context),
656 statements_(statements),
657 parent_(context.scope_)
658 {
659 context_.scope_ = this;
660 }
661
662 CYScope::~CYScope() {
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 (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch)
672 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
673 else if (parent_ != NULL)
674 parent_->Declare(context, 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 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
776 size_t size(size_ + rhs->size_);
777 char *value($ char[size + 1]);
778 memcpy(value, value_, size_);
779 memcpy(value + size_, rhs->value_, rhs->size_);
780 value[size] = '\0';
781 return $S(value, size);
782 }
783
784 CYNumber *CYString::Number(CYContext &context) {
785 // XXX: there is a precise algorithm for this
786 return NULL;
787 }
788
789 CYString *CYString::String(CYContext &context) {
790 return this;
791 }
792
793 CYStatement *CYSwitch::Replace(CYContext &context) {
794 context.Replace(value_);
795 clauses_->Replace(context);
796 return this;
797 }
798
799 CYExpression *CYThis::Replace(CYContext &context) {
800 return this;
801 }
802
803 namespace cy {
804 namespace Syntax {
805
806 CYStatement *Throw::Replace(CYContext &context) {
807 context.Replace(value_);
808 return this;
809 }
810
811 } }
812
813 CYExpression *CYTrivial::Replace(CYContext &context) {
814 return this;
815 }
816
817 CYNumber *CYTrue::Number(CYContext &context) {
818 return $D(1);
819 }
820
821 CYString *CYTrue::String(CYContext &context) {
822 return $S("true");
823 }
824
825 namespace cy {
826 namespace Syntax {
827
828 CYStatement *Try::Replace(CYContext &context) {
829 code_.Replace(context);
830 catch_->Replace(context);
831 finally_->Replace(context);
832 return this;
833 }
834
835 } }
836
837 CYStatement *CYVar::Replace(CYContext &context) {
838 declarations_->Replace(context);
839 return $E(declarations_->Compound(context));
840 }
841
842 CYExpression *CYVariable::Replace(CYContext &context) {
843 context.Replace(name_);
844 return this;
845 }
846
847 CYStatement *CYWhile::Replace(CYContext &context) {
848 context.Replace(test_);
849 context.Replace(code_);
850 return this;
851 }
852
853 CYStatement *CYWith::Replace(CYContext &context) {
854 context.Replace(scope_);
855 context.Replace(code_);
856 return this;
857 }
858
859 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
860 CYString *name($S(this));
861 if (object)
862 return $C1($V("objc_getClass"), name);
863 else
864 return name;
865 }