]> git.saurik.com Git - cycript.git/blob - Replace.cpp
Fix lexer hack correctness check when more spaces.
[cycript.git] / Replace.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. 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 static void CYImplicitReturn(CYStatement *&code) {
33 if (CYStatement *&last = CYGetLast(code))
34 last = last->Return();
35 }
36
37 CYExpression *CYAdd::Replace(CYContext &context) {
38 CYInfix::Replace(context);
39
40 CYString *lhs(dynamic_cast<CYString *>(lhs_));
41 CYString *rhs(dynamic_cast<CYString *>(rhs_));
42
43 if (lhs != NULL || rhs != NULL) {
44 if (lhs == NULL) {
45 lhs = lhs_->String(context);
46 if (lhs == NULL)
47 return this;
48 } else if (rhs == NULL) {
49 rhs = rhs_->String(context);
50 if (rhs == NULL)
51 return this;
52 }
53
54 return lhs->Concat(context, rhs);
55 }
56
57 if (CYNumber *lhn = lhs_->Number(context))
58 if (CYNumber *rhn = rhs_->Number(context))
59 return $D(lhn->Value() + rhn->Value());
60
61 return this;
62 }
63
64 CYExpression *CYAddressOf::Replace(CYContext &context) {
65 return $C0($M(rhs_, $S("$cya")));
66 }
67
68 CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
69 context.Replace(value_);
70 next_ = next_->Replace(context);
71
72 if (value_ == NULL) {
73 if (next_ == NULL)
74 return NULL;
75 else
76 value_ = $U;
77 }
78
79 return this;
80 }
81
82 CYExpression *CYArray::Replace(CYContext &context) {
83 if (elements_ != NULL)
84 elements_->Replace(context);
85 return this;
86 }
87
88 CYExpression *CYArrayComprehension::Replace(CYContext &context) {
89 CYVariable *cyv($V("$cyv"));
90
91 return $C0($F(NULL, $P1($L("$cyv"), comprehensions_->Parameters(context)), $$->*
92 $E($ CYAssign(cyv, $ CYArray()))->*
93 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
94 $ CYReturn(cyv)
95 ));
96 }
97
98 CYExpression *CYAssignment::Replace(CYContext &context) {
99 context.Replace(lhs_);
100 context.Replace(rhs_);
101 return this;
102 }
103
104 CYStatement *CYBlock::Return() {
105 CYImplicitReturn(code_);
106 return this;
107 }
108
109 CYStatement *CYBlock::Replace(CYContext &context) {
110 context.ReplaceAll(code_);
111 if (code_ == NULL)
112 return $ CYEmpty();
113 return this;
114 }
115
116 CYStatement *CYBreak::Replace(CYContext &context) {
117 return this;
118 }
119
120 CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
121 CYArgument **argument(&arguments_);
122 while (*argument != NULL)
123 argument = &(*argument)->next_;
124 *argument = $ CYArgument(value);
125 return this;
126 }
127
128 CYExpression *CYCall::Replace(CYContext &context) {
129 context.Replace(function_);
130 arguments_->Replace(context);
131 return this;
132 }
133
134 namespace cy {
135 namespace Syntax {
136
137 void Catch::Replace(CYContext &context) { $T()
138 CYScope scope(true, context);
139
140 context.Replace(name_);
141 context.scope_->Declare(context, name_, CYIdentifierCatch);
142
143 context.ReplaceAll(code_);
144 scope.Close(context, code_);
145 }
146
147 } }
148
149 void CYClause::Replace(CYContext &context) { $T()
150 context.Replace(case_);
151 context.ReplaceAll(code_);
152 next_->Replace(context);
153 }
154
155 CYExpression *CYCompound::Replace(CYContext &context) {
156 context.Replace(expression_);
157 context.Replace(next_);
158
159 if (CYCompound *compound = dynamic_cast<CYCompound *>(expression_)) {
160 expression_ = compound->expression_;
161 compound->expression_ = compound->next_;
162 compound->next_ = next_;
163 next_ = compound;
164 }
165
166 return this;
167 }
168
169 CYFunctionParameter *CYCompound::Parameter() const {
170 CYFunctionParameter *next(next_->Parameter());
171 if (next == NULL)
172 return NULL;
173
174 CYFunctionParameter *parameter(expression_->Parameter());
175 if (parameter == NULL)
176 return NULL;
177
178 parameter->SetNext(next);
179 return parameter;
180 }
181
182 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
183 CYFunctionParameter *next(next_->Parameters(context));
184 if (CYFunctionParameter *parameter = Parameter(context)) {
185 parameter->SetNext(next);
186 return parameter;
187 } else
188 return next;
189 }
190
191 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
192 return next_ == NULL ? statement : next_->Replace(context, statement);
193 }
194
195 CYExpression *CYCondition::Replace(CYContext &context) {
196 context.Replace(test_);
197 context.Replace(true_);
198 context.Replace(false_);
199 return this;
200 }
201
202 void CYContext::NonLocal(CYStatement *&statements) {
203 CYContext &context(*this);
204
205 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
206 CYIdentifier *cye($I("$cye")->Replace(context));
207 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
208
209 CYStatement *declare(
210 $ CYVar($L1($ CYDeclaration(unique, $ CYObject()))));
211
212 cy::Syntax::Catch *rescue(
213 $ cy::Syntax::Catch(cye, $$->*
214 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
215 $ CYReturn($M($V(cye), $S("$cyv"))))->*
216 $ cy::Syntax::Throw($V(cye))));
217
218 // XXX: I don't understand any of this
219 context.Replace(declare);
220 rescue->Replace(context);
221
222 statements = $$->*
223 declare->*
224 $ cy::Syntax::Try(statements, rescue, NULL);
225 }
226 }
227
228 CYIdentifier *CYContext::Unique() {
229 return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL));
230 }
231
232 CYStatement *CYContinue::Replace(CYContext &context) {
233 return this;
234 }
235
236 CYStatement *CYDebugger::Replace(CYContext &context) {
237 return this;
238 }
239
240 CYAssignment *CYDeclaration::Assignment(CYContext &context) {
241 if (initialiser_ == NULL)
242 return NULL;
243
244 CYAssignment *value($ CYAssign(Variable(context), initialiser_));
245 initialiser_ = NULL;
246 return value;
247 }
248
249 CYVariable *CYDeclaration::Variable(CYContext &context) {
250 return $V(identifier_);
251 }
252
253 CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
254 return $ CYVar($L1($ CYDeclaration(identifier_, value)));
255 }
256
257 CYExpression *CYDeclaration::Replace(CYContext &context) {
258 context.Replace(identifier_);
259 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
260 return Variable(context);
261 }
262
263 void CYDeclarations::Replace(CYContext &context) { $T()
264 declaration_->Replace(context);
265 next_->Replace(context);
266 }
267
268 CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
269 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_, next_->Property(context));
270 }
271
272 CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
273 return $ CYFunctionParameter($ CYDeclaration(declaration_->identifier_), next_->Parameter(context));
274 }
275
276 CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
277 return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
278 }
279
280 CYExpression *CYDeclarations::Expression(CYContext &context) { $T(NULL)
281 CYExpression *compound(next_->Expression(context));
282 if (CYAssignment *assignment = declaration_->Assignment(context))
283 if (compound == NULL)
284 compound = assignment;
285 else
286 compound = $ CYCompound(assignment, compound);
287 return compound;
288 }
289
290 CYExpression *CYDirectMember::Replace(CYContext &context) {
291 context.Replace(object_);
292 context.Replace(property_);
293 return this;
294 }
295
296 CYStatement *CYDoWhile::Replace(CYContext &context) {
297 context.Replace(test_);
298 context.ReplaceAll(code_);
299 return this;
300 }
301
302 void CYElementSpread::Replace(CYContext &context) {
303 context.Replace(value_);
304 }
305
306 void CYElementValue::Replace(CYContext &context) {
307 context.Replace(value_);
308 if (next_ != NULL)
309 next_->Replace(context);
310 }
311
312 CYStatement *CYEmpty::Replace(CYContext &context) {
313 return NULL;
314 }
315
316 CYExpression *CYEncodedType::Replace(CYContext &context) {
317 return typed_->Replace(context);
318 }
319
320 CYStatement *CYExpress::Return() {
321 return $ CYReturn(expression_);
322 }
323
324 CYStatement *CYExpress::Replace(CYContext &context) {
325 context.Replace(expression_);
326 return this;
327 }
328
329 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
330 return $C1(this, value);
331 }
332
333 CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
334 return this;
335 }
336
337 CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
338 return $E($ CYAssign(this, value));
339 }
340
341 CYAssignment *CYExpression::Assignment(CYContext &context) {
342 return NULL;
343 }
344
345 CYFunctionParameter *CYExpression::Parameter() const {
346 return NULL;
347 }
348
349 CYStatement *CYExternal::Replace(CYContext &context) {
350 return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word())))));
351 }
352
353 CYNumber *CYFalse::Number(CYContext &context) {
354 return $D(0);
355 }
356
357 CYString *CYFalse::String(CYContext &context) {
358 return $S("false");
359 }
360
361 CYExpression *CYFatArrow::Replace(CYContext &context) {
362 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
363 function->this_.SetNext(context.this_);
364 return function;
365 }
366
367 void CYFinally::Replace(CYContext &context) { $T()
368 context.ReplaceAll(code_);
369 }
370
371 CYStatement *CYFor::Replace(CYContext &context) {
372 context.Replace(initialiser_);
373 context.Replace(test_);
374 context.Replace(increment_);
375 context.ReplaceAll(code_);
376 return this;
377 }
378
379 CYExpression *CYForDeclarations::Replace(CYContext &context) {
380 declarations_->Replace(context);
381 return declarations_->Expression(context);
382 }
383
384 // XXX: this still feels highly suboptimal
385 CYStatement *CYForIn::Replace(CYContext &context) {
386 if (CYAssignment *assignment = initialiser_->Assignment(context))
387 return $ CYBlock($$->*
388 $E(assignment)->*
389 this
390 );
391
392 context.Replace(initialiser_);
393 context.Replace(set_);
394 context.ReplaceAll(code_);
395 return this;
396 }
397
398 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
399 return $ CYFunctionParameter($ CYDeclaration(name_));
400 }
401
402 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
403 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
404 }
405
406 CYStatement *CYForOf::Replace(CYContext &context) {
407 if (CYAssignment *assignment = initialiser_->Assignment(context))
408 return $ CYBlock($$->*
409 $E(assignment)->*
410 this
411 );
412
413 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
414
415 return $ CYLetStatement($L2($ CYDeclaration(cys, set_), $ CYDeclaration(cyt)), $$->*
416 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
417 initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))->*
418 code_
419 ))
420 );
421 }
422
423 CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
424 return $ CYFunctionParameter($ CYDeclaration(name_));
425 }
426
427 CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
428 CYIdentifier *cys($I("$cys"));
429
430 return $E($C0($F(NULL, $P1($L("$cys")), $$->*
431 $E($ CYAssign($V(cys), set_))->*
432 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
433 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
434 CYComprehension::Replace(context, statement)
435 ))
436 )));
437 }
438
439 void CYFunction::Inject(CYContext &context) {
440 context.Replace(name_);
441 context.scope_->Declare(context, name_, CYIdentifierOther);
442 }
443
444 void CYFunction::Replace_(CYContext &context, bool outer) {
445 if (outer)
446 Inject(context);
447
448 CYThisScope *_this(context.this_);
449 context.this_ = &this_;
450 context.this_ = CYGetLast(context.this_);
451
452 CYNonLocal *nonlocal(context.nonlocal_);
453 CYNonLocal *nextlocal(context.nextlocal_);
454
455 bool localize;
456 if (nonlocal_ != NULL) {
457 localize = false;
458 context.nonlocal_ = nonlocal_;
459 } else {
460 localize = true;
461 nonlocal_ = $ CYNonLocal();
462 context.nextlocal_ = nonlocal_;
463 }
464
465 CYScope scope(!localize, context);
466
467 if (!outer && name_ != NULL)
468 Inject(context);
469
470 parameters_->Replace(context, code_);
471 context.ReplaceAll(code_);
472
473 if (implicit_)
474 CYImplicitReturn(code_);
475
476 if (CYIdentifier *identifier = this_.identifier_)
477 code_ = $$->*
478 $ CYVar($L1($ CYDeclaration(identifier, $ CYThis())))->*
479 code_;
480
481 if (localize)
482 context.NonLocal(code_);
483
484 context.nextlocal_ = nextlocal;
485 context.nonlocal_ = nonlocal;
486
487 context.this_ = _this;
488
489 scope.Close(context, code_);
490 }
491
492 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
493 Replace_(context, false);
494 return this;
495 }
496
497 void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
498 CYAssignment *assignment(initialiser_->Assignment(context));
499 context.Replace(initialiser_);
500
501 next_->Replace(context, statements);
502
503 if (assignment != NULL)
504 statements = $$->*
505 // XXX: this cast is quite incorrect
506 $ CYIf($ CYIdentical($ CYTypeOf(dynamic_cast<CYExpression *>(initialiser_)), $S("undefined")), $$->*
507 $E(assignment)
508 )->*
509 statements;
510 }
511
512 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
513 Replace_(context, true);
514 return this;
515 }
516
517 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
518 if (replace_ != NULL && replace_ != this)
519 return replace_->Replace(context);
520 replace_ = context.scope_->Lookup(context, this);
521 return replace_;
522 }
523
524 CYStatement *CYIf::Return() {
525 CYImplicitReturn(true_);
526 CYImplicitReturn(false_);
527 return this;
528 }
529
530 CYStatement *CYIf::Replace(CYContext &context) {
531 context.Replace(test_);
532 context.ReplaceAll(true_);
533 context.ReplaceAll(false_);
534 return this;
535 }
536
537 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
538 return NULL;
539 }
540
541 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
542 return $ CYIf(test_, CYComprehension::Replace(context, statement));
543 }
544
545 CYStatement *CYImport::Replace(CYContext &context) {
546 return $ CYVar($L1($L(module_->part_->Word(), $C1($V("require"), module_->Replace(context, "/")))));
547 }
548
549 CYExpression *CYIndirect::Replace(CYContext &context) {
550 return $M(rhs_, $S("$cyi"));
551 }
552
553 CYExpression *CYIndirectMember::Replace(CYContext &context) {
554 return $M($ CYIndirect(object_), property_);
555 }
556
557 CYExpression *CYInfix::Replace(CYContext &context) {
558 context.Replace(lhs_);
559 context.Replace(rhs_);
560 return this;
561 }
562
563 CYStatement *CYLabel::Replace(CYContext &context) {
564 context.Replace(statement_);
565 return this;
566 }
567
568 CYExpression *CYLambda::Replace(CYContext &context) {
569 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
570 }
571
572 CYStatement *CYLetStatement::Replace(CYContext &context) {
573 return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context)));
574 }
575
576 CYString *CYModule::Replace(CYContext &context, const char *separator) const {
577 if (next_ == NULL)
578 return $ CYString(part_);
579 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
580 }
581
582 CYExpression *CYMultiply::Replace(CYContext &context) {
583 CYInfix::Replace(context);
584
585 if (CYNumber *lhn = lhs_->Number(context))
586 if (CYNumber *rhn = rhs_->Number(context))
587 return $D(lhn->Value() * rhn->Value());
588
589 return this;
590 }
591
592 namespace cy {
593 namespace Syntax {
594
595 CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
596 CYSetLast(arguments_) = $ CYArgument(value);
597 return this;
598 }
599
600 CYExpression *New::Replace(CYContext &context) {
601 context.Replace(constructor_);
602 arguments_->Replace(context);
603 return this;
604 }
605
606 } }
607
608 CYNumber *CYNull::Number(CYContext &context) {
609 return $D(0);
610 }
611
612 CYString *CYNull::String(CYContext &context) {
613 return $S("null");
614 }
615
616 CYNumber *CYNumber::Number(CYContext &context) {
617 return this;
618 }
619
620 CYString *CYNumber::String(CYContext &context) {
621 // XXX: there is a precise algorithm for this
622 return $S($pool.sprintf(24, "%.17g", Value()));
623 }
624
625 CYExpression *CYObject::Replace(CYContext &context) {
626 properties_->Replace(context);
627 return this;
628 }
629
630 CYExpression *CYParenthetical::Replace(CYContext &context) {
631 return expression_;
632 }
633
634 CYExpression *CYPostfix::Replace(CYContext &context) {
635 context.Replace(lhs_);
636 return this;
637 }
638
639 CYExpression *CYPrefix::Replace(CYContext &context) {
640 context.Replace(rhs_);
641 return this;
642 }
643
644 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
645 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
646 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
647
648 namespace {
649 struct IdentifierUsageLess :
650 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
651 {
652 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
653 if (lhs->usage_ != rhs->usage_)
654 return lhs->usage_ > rhs->usage_;
655 return lhs < rhs;
656 }
657 };
658
659 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
660 }
661
662 void CYScript::Replace(CYContext &context) {
663 CYScope scope(true, context);
664
665 context.nextlocal_ = $ CYNonLocal();
666 context.ReplaceAll(code_);
667 context.NonLocal(code_);
668
669 scope.Close(context, code_);
670
671 size_t offset(0);
672
673 CYCStringSet external;
674 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
675 external.insert((*i)->Word());
676
677 IdentifierUsages usages;
678
679 if (offset < context.rename_.size())
680 CYForEach (i, context.rename_[offset].identifier_)
681 usages.insert(i);
682
683 // XXX: totalling the probable occurrences and sorting by them would improve the result
684 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
685 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
686
687 const char *name;
688
689 if (context.options_.verbose_)
690 name = $pool.strcat("$", $pool.itoa(offset), NULL);
691 else {
692 char id[8];
693 id[7] = '\0';
694
695 id:
696 unsigned position(7), local(offset + 1);
697
698 do {
699 unsigned index(local % (sizeof(MappingSet) - 1));
700 local /= sizeof(MappingSet) - 1;
701 id[--position] = MappingSet[index];
702 } while (local != 0);
703
704 if (external.find(id + position) != external.end()) {
705 ++offset;
706 goto id;
707 }
708
709 name = $pool.strmemdup(id + position, 7 - position);
710 // XXX: at some point, this could become a keyword
711 }
712
713 CYForEach (identifier, i->identifier_)
714 identifier->Set(name);
715 }
716 }
717
718 void CYProperty::Replace(CYContext &context) { $T()
719 context.Replace(value_);
720 next_->Replace(context);
721 if (value_ == NULL)
722 value_ = $U;
723 }
724
725 CYStatement *CYReturn::Replace(CYContext &context) {
726 if (context.nonlocal_ != NULL) {
727 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
728 return $ cy::Syntax::Throw($ CYObject(
729 $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
730 ));
731 }
732
733 context.Replace(value_);
734 return this;
735 }
736
737 CYExpression *CYRubyBlock::Replace(CYContext &context) {
738 return call_->AddArgument(context, proc_->Replace(context));
739 }
740
741 CYExpression *CYRubyProc::Replace(CYContext &context) {
742 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
743 function = CYNonLocalize(context, function);
744 function->implicit_ = true;
745 return function;
746 }
747
748 CYScope::CYScope(bool transparent, CYContext &context) :
749 transparent_(transparent),
750 parent_(context.scope_)
751 {
752 context.scope_ = this;
753 }
754
755 void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
756 if (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch)
757 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
758 else if (parent_ != NULL)
759 parent_->Declare(context, identifier, flags);
760 }
761
762 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
763 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
764 return *insert.first;
765 }
766
767 void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
768 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
769 if (!insert.second) {
770 if ((*insert.first)->offset_ < identifier->offset_)
771 (*insert.first)->offset_ = identifier->offset_;
772 identifier->replace_ = *insert.first;
773 (*insert.first)->usage_ += identifier->usage_ + 1;
774 }
775 }
776
777 namespace {
778 struct IdentifierOffset {
779 size_t offset_;
780 CYIdentifierFlags flags_;
781 size_t usage_;
782 CYIdentifier *identifier_;
783
784 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
785 offset_(identifier->offset_),
786 flags_(flags),
787 usage_(identifier->usage_),
788 identifier_(identifier)
789 {
790 }
791 };
792
793 struct IdentifierOffsetLess :
794 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
795 {
796 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
797 if (lhs.offset_ != rhs.offset_)
798 return lhs.offset_ < rhs.offset_;
799 if (lhs.flags_ != rhs.flags_)
800 return lhs.flags_ < rhs.flags_;
801 /*if (lhs.usage_ != rhs.usage_)
802 return lhs.usage_ < rhs.usage_;*/
803 return lhs.identifier_ < rhs.identifier_;
804 }
805 };
806
807 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
808 }
809
810 void CYScope::Close(CYContext &context, CYStatement *&statements) {
811 context.scope_ = parent_;
812
813 if (parent_ == NULL)
814 return;
815
816 CYDeclarations *last(NULL), *curr(NULL);
817
818 IdentifierOffsets offsets;
819
820 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
821 if (i->second != CYIdentifierMagic)
822 offsets.insert(IdentifierOffset(i->first, i->second));
823
824 size_t offset(0);
825
826 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
827 if (i->flags_ == CYIdentifierVariable) {
828 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
829 if (last == NULL)
830 last = next;
831 if (curr != NULL)
832 curr->SetNext(next);
833 curr = next;
834 }
835
836 if (offset < i->offset_)
837 offset = i->offset_;
838 if (context.rename_.size() <= offset)
839 context.rename_.resize(offset + 1);
840
841 CYIdentifierUsage &rename(context.rename_[offset++]);
842 i->identifier_->SetNext(rename.identifier_);
843 rename.identifier_ = i->identifier_;
844 rename.usage_ += i->identifier_->usage_ + 1;
845 }
846
847 if (last != NULL) {
848 CYVar *var($ CYVar(last));
849 var->SetNext(statements);
850 statements = var;
851 }
852
853 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
854 if (internal_.find(*i) == internal_.end()) {
855 //std::cout << *i << '=' << offset << std::endl;
856 if ((*i)->offset_ < offset)
857 (*i)->offset_ = offset;
858 parent_->Merge(context, *i);
859 }
860 }
861
862 CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
863 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
864 }
865
866 CYStatement *CYStatement::Return() {
867 return this;
868 }
869
870 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
871 size_t size(size_ + rhs->size_);
872 char *value($ char[size + 1]);
873 memcpy(value, value_, size_);
874 memcpy(value + size_, rhs->value_, rhs->size_);
875 value[size] = '\0';
876 return $S(value, size);
877 }
878
879 CYNumber *CYString::Number(CYContext &context) {
880 // XXX: there is a precise algorithm for this
881 return NULL;
882 }
883
884 CYString *CYString::String(CYContext &context) {
885 return this;
886 }
887
888 CYStatement *CYSwitch::Replace(CYContext &context) {
889 context.Replace(value_);
890 clauses_->Replace(context);
891 return this;
892 }
893
894 CYExpression *CYTemplate::Replace(CYContext &context) {
895 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
896 }
897
898 CYExpression *CYThis::Replace(CYContext &context) {
899 if (context.this_ != NULL)
900 return $V(context.this_->Identifier(context));
901 return this;
902 }
903
904 namespace cy {
905 namespace Syntax {
906
907 CYStatement *Throw::Replace(CYContext &context) {
908 context.Replace(value_);
909 return this;
910 }
911
912 } }
913
914 CYExpression *CYTrivial::Replace(CYContext &context) {
915 return this;
916 }
917
918 CYNumber *CYTrue::Number(CYContext &context) {
919 return $D(1);
920 }
921
922 CYString *CYTrue::String(CYContext &context) {
923 return $S("true");
924 }
925
926 namespace cy {
927 namespace Syntax {
928
929 CYStatement *Try::Replace(CYContext &context) {
930 context.ReplaceAll(code_);
931 catch_->Replace(context);
932 finally_->Replace(context);
933 return this;
934 }
935
936 } }
937
938 CYExpression *CYTypeArrayOf::Replace_(CYContext &context, CYExpression *type) {
939 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
940 }
941
942 CYExpression *CYTypeBlockWith::Replace_(CYContext &context, CYExpression *type) {
943 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
944 }
945
946 CYExpression *CYTypeConstant::Replace_(CYContext &context, CYExpression *type) {
947 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
948 }
949
950 CYStatement *CYTypeDefinition::Replace(CYContext &context) {
951 return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
952 }
953
954 CYExpression *CYTypeError::Replace(CYContext &context) {
955 _assert(false);
956 return NULL;
957 }
958
959 CYExpression *CYTypeModifier::Replace(CYContext &context, CYExpression *type) { $T(type)
960 return Replace_(context, type);
961 }
962
963 CYExpression *CYTypeFunctionWith::Replace_(CYContext &context, CYExpression *type) {
964 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
965 }
966
967 CYExpression *CYTypeLong::Replace(CYContext &context) {
968 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
969 }
970
971 CYExpression *CYTypePointerTo::Replace_(CYContext &context, CYExpression *type) {
972 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
973 }
974
975 CYExpression *CYTypeShort::Replace(CYContext &context) {
976 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
977 }
978
979 CYExpression *CYTypeSigned::Replace(CYContext &context) {
980 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
981 }
982
983 CYExpression *CYTypeUnsigned::Replace(CYContext &context) {
984 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
985 }
986
987 CYExpression *CYTypeVariable::Replace(CYContext &context) {
988 return $V(name_);
989 }
990
991 CYExpression *CYTypeVoid::Replace(CYContext &context) {
992 return $N1($V("Type"), $ CYString("v"));
993 }
994
995 CYExpression *CYTypeVolatile::Replace_(CYContext &context, CYExpression *type) {
996 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
997 }
998
999 CYExpression *CYTypedIdentifier::Replace(CYContext &context) {
1000 return modifier_->Replace(context, specifier_->Replace(context));
1001 }
1002
1003 CYTypeFunctionWith *CYTypedIdentifier::Function() {
1004 CYTypeModifier **modifier(&modifier_);
1005 if (*modifier == NULL)
1006 return NULL;
1007 while ((*modifier)->next_ != NULL)
1008 modifier = &(*modifier)->next_;
1009 CYTypeFunctionWith *function((*modifier)->Function());
1010 if (function == NULL)
1011 return NULL;
1012 *modifier = NULL;
1013 return function;
1014 }
1015
1016 CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
1017 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
1018 }
1019
1020 CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1021 return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
1022 }
1023
1024 CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
1025 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
1026 }
1027
1028 CYStatement *CYVar::Replace(CYContext &context) {
1029 declarations_->Replace(context);
1030 if (CYExpression *expression = declarations_->Expression(context))
1031 return $E(expression);
1032 return $ CYEmpty();
1033 }
1034
1035 CYExpression *CYVariable::Replace(CYContext &context) {
1036 context.Replace(name_);
1037 return this;
1038 }
1039
1040 CYFunctionParameter *CYVariable::Parameter() const {
1041 return $ CYFunctionParameter($ CYDeclaration(name_));
1042 }
1043
1044 CYStatement *CYWhile::Replace(CYContext &context) {
1045 context.Replace(test_);
1046 context.ReplaceAll(code_);
1047 return this;
1048 }
1049
1050 CYStatement *CYWith::Replace(CYContext &context) {
1051 context.Replace(scope_);
1052 context.ReplaceAll(code_);
1053 return this;
1054 }
1055
1056 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
1057 CYString *name($S(this));
1058 if (object)
1059 return $C1($V("objc_getClass"), name);
1060 else
1061 return name;
1062 }