]> git.saurik.com Git - cycript.git/blob - Replace.cpp
Reboot variable renaming for lexical name scoping.
[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 <iomanip>
23 #include <map>
24
25 #include "Replace.hpp"
26 #include "Syntax.hpp"
27
28 CYFunctionExpression *CYNonLocalize(CYContext &context, CYFunctionExpression *function) {
29 function->nonlocal_ = context.nextlocal_;
30 return function;
31 }
32
33 CYFunctionExpression *CYSuperize(CYContext &context, CYFunctionExpression *function) {
34 function->super_ = context.super_;
35 return function;
36 }
37
38 CYStatement *CYDefineProperty(CYExpression *object, CYExpression *name, bool configurable, bool enumerable, CYProperty *descriptor) {
39 return $E($C3($M($V("Object"), $S("defineProperty")), object, name, $ CYObject(CYList<CYProperty>()
40 ->* (configurable ? $ CYPropertyValue($S("configurable"), $ CYTrue()) : NULL)
41 ->* (enumerable ? $ CYPropertyValue($S("enumerable"), $ CYTrue()) : NULL)
42 ->* descriptor)));
43 }
44
45 static void CYImplicitReturn(CYStatement *&code) {
46 if (CYStatement *&last = CYGetLast(code))
47 last = last->Return();
48 }
49
50 CYExpression *CYAdd::Replace(CYContext &context) {
51 CYInfix::Replace(context);
52
53 CYString *lhs(dynamic_cast<CYString *>(lhs_));
54 CYString *rhs(dynamic_cast<CYString *>(rhs_));
55
56 if (lhs != NULL || rhs != NULL) {
57 if (lhs == NULL) {
58 lhs = lhs_->String(context);
59 if (lhs == NULL)
60 return this;
61 } else if (rhs == NULL) {
62 rhs = rhs_->String(context);
63 if (rhs == NULL)
64 return this;
65 }
66
67 return lhs->Concat(context, rhs);
68 }
69
70 if (CYNumber *lhn = lhs_->Number(context))
71 if (CYNumber *rhn = rhs_->Number(context))
72 return $D(lhn->Value() + rhn->Value());
73
74 return this;
75 }
76
77 CYExpression *CYAddressOf::Replace(CYContext &context) {
78 return $C0($M(rhs_, $S("$cya")));
79 }
80
81 CYTarget *CYApply::AddArgument(CYContext &context, CYExpression *value) {
82 CYArgument **argument(&arguments_);
83 while (*argument != NULL)
84 argument = &(*argument)->next_;
85 *argument = $ CYArgument(value);
86 return this;
87 }
88
89 CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
90 context.Replace(value_);
91 next_ = next_->Replace(context);
92
93 if (value_ == NULL) {
94 if (next_ == NULL)
95 return NULL;
96 else
97 value_ = $U;
98 }
99
100 return this;
101 }
102
103 CYTarget *CYArray::Replace(CYContext &context) {
104 if (elements_ != NULL)
105 elements_->Replace(context);
106 return this;
107 }
108
109 CYTarget *CYArrayComprehension::Replace(CYContext &context) {
110 CYVariable *cyv($V("$cyv"));
111
112 return $C0($F(NULL, $P1($L($I("$cyv")), comprehensions_->Parameters(context)), $$->*
113 $E($ CYAssign(cyv, $ CYArray()))->*
114 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
115 $ CYReturn(cyv)
116 ));
117 }
118
119 CYExpression *CYAssignment::Replace(CYContext &context) {
120 context.Replace(lhs_);
121 context.Replace(rhs_);
122 return this;
123 }
124
125 CYStatement *CYBlock::Return() {
126 CYImplicitReturn(code_);
127 return this;
128 }
129
130 CYStatement *CYBlock::Replace(CYContext &context) {
131 CYScope scope(true, context);
132 context.ReplaceAll(code_);
133 scope.Close(context);
134
135 if (code_ == NULL)
136 return $ CYEmpty();
137 return this;
138 }
139
140 CYStatement *CYBreak::Replace(CYContext &context) {
141 return this;
142 }
143
144 CYTarget *CYCall::Replace(CYContext &context) {
145 context.Replace(function_);
146 arguments_->Replace(context);
147 return this;
148 }
149
150 namespace cy {
151 namespace Syntax {
152
153 void Catch::Replace(CYContext &context) { $T()
154 CYScope scope(true, context);
155
156 name_ = name_->Replace(context, CYIdentifierCatch);
157
158 context.ReplaceAll(code_);
159 scope.Close(context);
160 }
161
162 } }
163
164 CYTarget *CYClassExpression::Replace(CYContext &context) {
165 CYBuilder builder;
166
167 CYIdentifier *super(context.Unique());
168
169 CYIdentifier *old(context.super_);
170 context.super_ = super;
171
172 CYIdentifier *constructor(context.Unique());
173 CYForEach (member, tail_->static_)
174 member->Replace(context, builder, $V(constructor), true);
175
176 CYIdentifier *prototype(context.Unique());
177 CYForEach (member, tail_->instance_)
178 member->Replace(context, builder, $V(prototype), true);
179
180 if (tail_->constructor_ == NULL)
181 tail_->constructor_ = $ CYFunctionExpression(NULL, NULL, NULL);
182 tail_->constructor_ = CYSuperize(context, tail_->constructor_);
183
184 context.super_ = old;
185
186 return $C1($ CYFunctionExpression(NULL, $P($L(super)), $$
187 ->* $ CYVar($L1($L(constructor, tail_->constructor_)))
188 ->* $ CYVar($L1($L(prototype, $ CYFunctionExpression(NULL, NULL, NULL))))
189 ->* $E($ CYAssign($M($V(prototype), $S("prototype")), $M($V(super), $S("prototype"))))
190 ->* $E($ CYAssign($V(prototype), $N($V(prototype))))
191 ->* CYDefineProperty($V(prototype), $S("constructor"), false, false, $ CYPropertyValue($S("value"), $V(constructor)))
192 ->* $ CYVar(builder.declarations_)
193 ->* builder.statements_
194 ->* CYDefineProperty($V(constructor), $S("prototype"), false, false, $ CYPropertyValue($S("value"), $V(prototype)))
195 ->* $ CYReturn($V(constructor))
196 ), tail_->extends_ ?: $V($I("Object")));
197 }
198
199 CYStatement *CYClassStatement::Replace(CYContext &context) {
200 return $ CYVar($L1($L(name_, $ CYClassExpression(name_, tail_))));
201 }
202
203 void CYClause::Replace(CYContext &context) { $T()
204 context.Replace(case_);
205 context.ReplaceAll(code_);
206 next_->Replace(context);
207 }
208
209 CYExpression *CYCompound::Replace(CYContext &context) {
210 context.Replace(expression_);
211 context.Replace(next_);
212
213 if (CYCompound *compound = dynamic_cast<CYCompound *>(expression_)) {
214 expression_ = compound->expression_;
215 compound->expression_ = compound->next_;
216 compound->next_ = next_;
217 next_ = compound;
218 }
219
220 return this;
221 }
222
223 CYFunctionParameter *CYCompound::Parameter() const {
224 CYFunctionParameter *next(next_->Parameter());
225 if (next == NULL)
226 return NULL;
227
228 CYFunctionParameter *parameter(expression_->Parameter());
229 if (parameter == NULL)
230 return NULL;
231
232 parameter->SetNext(next);
233 return parameter;
234 }
235
236 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
237 CYFunctionParameter *next(next_->Parameters(context));
238 if (CYFunctionParameter *parameter = Parameter(context)) {
239 parameter->SetNext(next);
240 return parameter;
241 } else
242 return next;
243 }
244
245 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
246 return next_ == NULL ? statement : next_->Replace(context, statement);
247 }
248
249 CYExpression *CYComputed::PropertyName(CYContext &context) {
250 return expression_;
251 }
252
253 CYExpression *CYCondition::Replace(CYContext &context) {
254 context.Replace(test_);
255 context.Replace(true_);
256 context.Replace(false_);
257 return this;
258 }
259
260 void CYContext::NonLocal(CYStatement *&statements) {
261 CYContext &context(*this);
262
263 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
264 CYIdentifier *cye($I("$cye")->Replace(context, CYIdentifierGlobal));
265 CYIdentifier *unique(nextlocal_->identifier_->Replace(context, CYIdentifierGlobal));
266
267 CYStatement *declare(
268 $ CYVar($L1($L(unique, $ CYObject()))));
269
270 cy::Syntax::Catch *rescue(
271 $ cy::Syntax::Catch(cye, $$->*
272 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
273 $ CYReturn($M($V(cye), $S("$cyv"))))->*
274 $ cy::Syntax::Throw($V(cye))));
275
276 context.Replace(declare);
277 rescue->Replace(context);
278
279 statements = $$->*
280 declare->*
281 $ cy::Syntax::Try(statements, rescue, NULL);
282 }
283 }
284
285 CYIdentifier *CYContext::Unique() {
286 return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL));
287 }
288
289 CYStatement *CYContinue::Replace(CYContext &context) {
290 return this;
291 }
292
293 CYStatement *CYDebugger::Replace(CYContext &context) {
294 return this;
295 }
296
297 CYTarget *CYDeclaration::Target(CYContext &context) {
298 return $V(identifier_);
299 }
300
301 CYAssignment *CYDeclaration::Replace(CYContext &context, CYIdentifierKind kind) {
302 identifier_ = identifier_->Replace(context, kind);
303
304 if (initialiser_ == NULL)
305 return NULL;
306
307 CYAssignment *value($ CYAssign(Target(context), initialiser_));
308 initialiser_ = NULL;
309 return value;
310 }
311
312 CYExpression *CYDeclarations::Replace(CYContext &context, CYIdentifierKind kind) { $T(NULL)
313 CYAssignment *assignment(declaration_->Replace(context, kind));
314 CYExpression *compound(next_->Replace(context, kind));
315
316 if (assignment != NULL)
317 if (compound == NULL)
318 compound = assignment;
319 else
320 compound = $ CYCompound(assignment, compound);
321 return compound;
322 }
323
324 CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
325 return $ CYFunctionParameter($ CYDeclaration(declaration_->identifier_), next_->Parameter(context));
326 }
327
328 CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
329 return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
330 }
331
332 CYTarget *CYDirectMember::Replace(CYContext &context) {
333 context.Replace(object_);
334 context.Replace(property_);
335 return this;
336 }
337
338 CYStatement *CYDoWhile::Replace(CYContext &context) {
339 context.Replace(test_);
340 context.ReplaceAll(code_);
341 return this;
342 }
343
344 void CYElementSpread::Replace(CYContext &context) {
345 context.Replace(value_);
346 }
347
348 void CYElementValue::Replace(CYContext &context) {
349 context.Replace(value_);
350 if (next_ != NULL)
351 next_->Replace(context);
352 }
353
354 CYStatement *CYEmpty::Replace(CYContext &context) {
355 return NULL;
356 }
357
358 CYTarget *CYEncodedType::Replace(CYContext &context) {
359 return typed_->Replace(context);
360 }
361
362 CYTarget *CYEval::Replace(CYContext &context) {
363 context.scope_->Damage();
364 if (arguments_ != NULL)
365 arguments_->value_ = $C1($M($V("Cycript"), $S("compile")), arguments_->value_);
366 return $C($V("eval"), arguments_);
367 }
368
369 CYStatement *CYExpress::Return() {
370 return $ CYReturn(expression_);
371 }
372
373 CYStatement *CYExpress::Replace(CYContext &context) {
374 context.Replace(expression_);
375 return this;
376 }
377
378 CYTarget *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
379 return $C1(this, value);
380 }
381
382 CYFunctionParameter *CYExpression::Parameter() const {
383 return NULL;
384 }
385
386 CYStatement *CYExternal::Replace(CYContext &context) {
387 return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word())))));
388 }
389
390 CYNumber *CYFalse::Number(CYContext &context) {
391 return $D(0);
392 }
393
394 CYString *CYFalse::String(CYContext &context) {
395 return $S("false");
396 }
397
398 CYExpression *CYFatArrow::Replace(CYContext &context) {
399 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
400 function->this_.SetNext(context.this_);
401 return function;
402 }
403
404 void CYFinally::Replace(CYContext &context) { $T()
405 CYScope scope(true, context);
406 context.ReplaceAll(code_);
407 scope.Close(context);
408 }
409
410 CYStatement *CYFor::Replace(CYContext &context) {
411 CYScope outer(true, context);
412 context.Replace(initialiser_);
413
414 context.Replace(test_);
415
416 {
417 CYScope inner(true, context);
418 context.ReplaceAll(code_);
419 inner.Close(context);
420 }
421
422 context.Replace(increment_);
423
424 outer.Close(context);
425 return this;
426 }
427
428 CYExpression *CYForDeclarations::Replace(CYContext &context) {
429 return declarations_->Replace(context, CYIdentifierVariable);
430 }
431
432 CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
433 if (value == NULL) {
434 if (declaration_->initialiser_ == NULL)
435 return NULL;
436 value = declaration_->initialiser_;
437 }
438
439 return $ CYLet(constant_, $L1($ CYDeclaration(declaration_->identifier_, value)));
440 }
441
442 CYTarget *CYForLexical::Replace(CYContext &context) {
443 _assert(declaration_->Replace(context, CYIdentifierLexical) == NULL);
444 return declaration_->Target(context);
445 }
446
447 CYStatement *CYForIn::Replace(CYContext &context) {
448 CYScope scope(true, context);
449 context.Replace(initialiser_);
450 context.Replace(set_);
451 context.ReplaceAll(code_);
452 scope.Close(context);
453 return this;
454 }
455
456 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
457 return $ CYFunctionParameter(declaration_);
458 }
459
460 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
461 return $ CYForIn(declaration_->Target(context), set_, CYComprehension::Replace(context, statement));
462 }
463
464 CYStatement *CYForOf::Replace(CYContext &context) {
465 CYIdentifier *item(context.Unique()), *list(context.Unique());
466
467 return $ CYBlock($$
468 ->* initialiser_->Initialize(context, NULL)
469 ->* $ CYLet(false, $L2($L(list, set_), $L(item)))
470 ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
471 ->* initialiser_->Initialize(context, $M($V(list), $V(item)))
472 ->* code_
473 )));
474 }
475
476 CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
477 return $ CYFunctionParameter(declaration_);
478 }
479
480 CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
481 CYIdentifier *cys($I("$cys"));
482
483 return $E($C0($F(NULL, $P1($L($I("$cys"))), $$->*
484 $E($ CYAssign($V(cys), set_))->*
485 $ CYForIn(declaration_->Target(context), $V(cys), $ CYBlock($$->*
486 $E($ CYAssign(declaration_->Target(context), $M($V(cys), declaration_->Target(context))))->*
487 CYComprehension::Replace(context, statement)
488 ))
489 )));
490 }
491
492 CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
493 if (value == NULL) {
494 if (declaration_->initialiser_ == NULL)
495 return NULL;
496 value = declaration_->initialiser_;
497 }
498
499 return $ CYVar($L1($ CYDeclaration(declaration_->identifier_, value)));
500 }
501
502 CYTarget *CYForVariable::Replace(CYContext &context) {
503 _assert(declaration_->Replace(context, CYIdentifierVariable) == NULL);
504 return declaration_->Target(context);
505 }
506
507 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
508 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
509 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
510
511 void CYFunction::Replace(CYContext &context) {
512 CYThisScope *_this(context.this_);
513 context.this_ = &this_;
514 context.this_ = CYGetLast(context.this_);
515
516 CYIdentifier *super(context.super_);
517 context.super_ = super_;
518
519 CYNonLocal *nonlocal(context.nonlocal_);
520 CYNonLocal *nextlocal(context.nextlocal_);
521
522 bool localize;
523 if (nonlocal_ != NULL) {
524 localize = false;
525 context.nonlocal_ = nonlocal_;
526 } else {
527 localize = true;
528 nonlocal_ = $ CYNonLocal();
529 context.nextlocal_ = nonlocal_;
530 }
531
532 CYScope scope(!localize, context);
533
534 parameters_->Replace(context, code_);
535
536 context.ReplaceAll(code_);
537
538 if (implicit_)
539 CYImplicitReturn(code_);
540
541 if (CYIdentifier *identifier = this_.identifier_) {
542 context.scope_->Declare(context, identifier, CYIdentifierVariable);
543 code_ = $$
544 ->*$E($ CYAssign($V(identifier), $ CYThis()))
545 ->*code_
546 ;
547 }
548
549 if (localize)
550 context.NonLocal(code_);
551
552 context.nextlocal_ = nextlocal;
553 context.nonlocal_ = nonlocal;
554
555 context.super_ = super;
556 context.this_ = _this;
557
558 scope.Close(context, code_);
559 }
560
561 CYTarget *CYFunctionExpression::Replace(CYContext &context) {
562 CYScope scope(false, context);
563 if (name_ != NULL)
564 name_ = name_->Replace(context, CYIdentifierOther);
565
566 CYFunction::Replace(context);
567 scope.Close(context);
568 return this;
569 }
570
571 void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
572 CYAssignment *assignment(initialiser_->Replace(context, CYIdentifierArgument));
573
574 next_->Replace(context, statements);
575
576 if (assignment != NULL)
577 statements = $$->*
578 $ CYIf($ CYIdentical($ CYTypeOf(initialiser_->Target(context)), $S("undefined")), $$->*
579 $E(assignment)
580 )->*
581 statements;
582 }
583
584 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
585 name_ = name_->Replace(context, CYIdentifierOther);
586 CYFunction::Replace(context);
587 return this;
588 }
589
590 CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
591 if (next_ == this)
592 return this;
593 if (next_ != NULL)
594 return next_->Replace(context, kind);
595 next_ = context.scope_->Declare(context, this, kind)->identifier_;
596 return next_;
597 }
598
599 CYStatement *CYIf::Return() {
600 CYImplicitReturn(true_);
601 CYImplicitReturn(false_);
602 return this;
603 }
604
605 CYStatement *CYIf::Replace(CYContext &context) {
606 context.Replace(test_);
607 context.ReplaceAll(true_);
608 context.ReplaceAll(false_);
609 return this;
610 }
611
612 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
613 return NULL;
614 }
615
616 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
617 return $ CYIf(test_, CYComprehension::Replace(context, statement));
618 }
619
620 CYStatement *CYImport::Replace(CYContext &context) {
621 return $ CYVar($L1($L($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
622 }
623
624 CYTarget *CYIndirect::Replace(CYContext &context) {
625 return $M(rhs_, $S("$cyi"));
626 }
627
628 CYTarget *CYIndirectMember::Replace(CYContext &context) {
629 return $M($ CYIndirect(object_), property_);
630 }
631
632 CYExpression *CYInfix::Replace(CYContext &context) {
633 context.Replace(lhs_);
634 context.Replace(rhs_);
635 return this;
636 }
637
638 CYStatement *CYLabel::Replace(CYContext &context) {
639 context.Replace(statement_);
640 return this;
641 }
642
643 CYTarget *CYLambda::Replace(CYContext &context) {
644 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
645 }
646
647 CYStatement *CYLet::Replace(CYContext &context) {
648 if (CYExpression *expression = declarations_->Replace(context, CYIdentifierLexical))
649 return $E(expression);
650 return $ CYEmpty();
651 }
652
653 CYFunctionExpression *CYMethod::Constructor() {
654 return NULL;
655 }
656
657 void CYMethod::Replace(CYContext &context) {
658 CYFunction::Replace(context);
659 }
660
661 CYString *CYModule::Replace(CYContext &context, const char *separator) const {
662 if (next_ == NULL)
663 return $ CYString(part_);
664 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
665 }
666
667 CYExpression *CYMultiply::Replace(CYContext &context) {
668 CYInfix::Replace(context);
669
670 if (CYNumber *lhn = lhs_->Number(context))
671 if (CYNumber *rhn = rhs_->Number(context))
672 return $D(lhn->Value() * rhn->Value());
673
674 return this;
675 }
676
677 namespace cy {
678 namespace Syntax {
679
680 CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
681 CYSetLast(arguments_) = $ CYArgument(value);
682 return this;
683 }
684
685 CYTarget *New::Replace(CYContext &context) {
686 context.Replace(constructor_);
687 arguments_->Replace(context);
688 return this;
689 }
690
691 } }
692
693 CYNumber *CYNull::Number(CYContext &context) {
694 return $D(0);
695 }
696
697 CYString *CYNull::String(CYContext &context) {
698 return $S("null");
699 }
700
701 CYNumber *CYNumber::Number(CYContext &context) {
702 return this;
703 }
704
705 CYString *CYNumber::String(CYContext &context) {
706 // XXX: there is a precise algorithm for this
707 return $S($pool.sprintf(24, "%.17g", Value()));
708 }
709
710 CYExpression *CYNumber::PropertyName(CYContext &context) {
711 return String(context);
712 }
713
714 CYTarget *CYObject::Replace(CYContext &context) {
715 CYBuilder builder;
716 if (properties_ != NULL)
717 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
718
719 if (builder) {
720 return $C1($M($ CYFunctionExpression(NULL, builder.declarations_->Parameter(context),
721 builder.statements_->*
722 $ CYReturn($ CYThis())
723 ), $S("call")), this, builder.declarations_->Argument(context));
724 }
725
726 CYForEach (property, properties_)
727 property->Replace(context);
728 return this;
729 }
730
731 CYTarget *CYParenthetical::Replace(CYContext &context) {
732 // XXX: return expression_;
733 context.Replace(expression_);
734 return this;
735 }
736
737 CYExpression *CYPostfix::Replace(CYContext &context) {
738 context.Replace(lhs_);
739 return this;
740 }
741
742 CYExpression *CYPrefix::Replace(CYContext &context) {
743 context.Replace(rhs_);
744 return this;
745 }
746
747 CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
748 update |= name_->Computed();
749 if (update)
750 Replace(context, builder, self, false);
751 if (next_ != NULL)
752 next_ = next_->ReplaceAll(context, builder, self, update);
753 return update ? next_ : this;
754 }
755
756 void CYProperty::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect) {
757 CYExpression *name(name_->PropertyName(context));
758 if (name_->Computed()) {
759 CYIdentifier *unique(context.Unique());
760 builder.declarations_->*$L1($L(unique, name));
761 name = $V(unique);
762 }
763
764 Replace(context, builder, self, name, protect);
765 }
766
767 void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
768 CYIdentifier *unique(context.Unique());
769 builder.declarations_
770 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
771 builder.statements_
772 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
773 }
774
775 CYFunctionExpression *CYPropertyMethod::Constructor() {
776 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
777 }
778
779 void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
780 CYIdentifier *unique(context.Unique());
781 builder.declarations_
782 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
783 builder.statements_
784 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
785 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
786 }
787
788 void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
789 CYIdentifier *unique(context.Unique());
790 builder.declarations_
791 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
792 builder.statements_
793 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
794 }
795
796 void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
797 _assert(!protect);
798 CYIdentifier *unique(context.Unique());
799 builder.declarations_
800 ->* $L1($L(unique, value_));
801 builder.statements_
802 ->* $E($ CYAssign($M(self, name), $V(unique)));
803 }
804
805 void CYPropertyValue::Replace(CYContext &context) {
806 context.Replace(value_);
807 }
808
809 void CYScript::Replace(CYContext &context) {
810 CYScope scope(false, context);
811 context.scope_->Damage();
812
813 context.nextlocal_ = $ CYNonLocal();
814 context.ReplaceAll(code_);
815 context.NonLocal(code_);
816
817 scope.Close(context, code_);
818
819 unsigned offset(0);
820
821 for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
822 const char *name;
823 if (context.options_.verbose_)
824 name = $pool.strcat("$", $pool.itoa(offset++), NULL);
825 else {
826 char id[8];
827 id[7] = '\0';
828
829 id:
830 unsigned position(7), local(offset++ + 1);
831
832 do {
833 unsigned index(local % (sizeof(MappingSet) - 1));
834 local /= sizeof(MappingSet) - 1;
835 id[--position] = MappingSet[index];
836 } while (local != 0);
837
838 if (scope.Lookup(context, id + position) != NULL)
839 goto id;
840 // XXX: at some point, this could become a keyword
841
842 name = $pool.strmemdup(id + position, 7 - position);
843 }
844
845 CYIdentifier *identifier(*i);
846 _assert(identifier->next_ == identifier);
847 identifier->next_ = $I(name);
848 }
849 }
850
851 CYStatement *CYReturn::Replace(CYContext &context) {
852 if (context.nonlocal_ != NULL) {
853 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
854 return $ cy::Syntax::Throw($ CYObject(
855 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
856 ));
857 }
858
859 context.Replace(value_);
860 return this;
861 }
862
863 CYTarget *CYRubyBlock::Replace(CYContext &context) {
864 return call_->AddArgument(context, proc_->Replace(context));
865 }
866
867 CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
868 return Replace(context)->AddArgument(context, value);
869 }
870
871 CYTarget *CYRubyProc::Replace(CYContext &context) {
872 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
873 function = CYNonLocalize(context, function);
874 function->implicit_ = true;
875 return function;
876 }
877
878 CYScope::CYScope(bool transparent, CYContext &context) :
879 transparent_(transparent),
880 parent_(context.scope_),
881 damaged_(false),
882 shadow_(NULL),
883 internal_(NULL)
884 {
885 _assert(!transparent_ || parent_ != NULL);
886 context.scope_ = this;
887 }
888
889 void CYScope::Damage() {
890 damaged_ = true;
891 if (parent_ != NULL)
892 parent_->Damage();
893 }
894
895 CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
896 CYForEach (i, internal_)
897 if (strcmp(i->identifier_->Word(), word) == 0)
898 return i;
899 return NULL;
900 }
901
902 CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
903 return Lookup(context, identifier->Word());
904 }
905
906 CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
907 _assert(identifier->next_ == NULL || identifier->next_ == identifier);
908
909 CYIdentifierFlags *existing(Lookup(context, identifier));
910 if (existing == NULL)
911 internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
912 ++internal_->count_;
913 if (existing == NULL)
914 return internal_;
915
916 switch (kind) {
917 case CYIdentifierArgument:
918 case CYIdentifierCatch:
919 case CYIdentifierMagic:
920 _assert(false);
921 default:
922 break;
923 }
924
925 if (existing->kind_ == CYIdentifierGlobal)
926 existing->kind_ = kind;
927 else if (kind == CYIdentifierGlobal)
928 ;
929 else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
930 _assert(false); // XXX: throw new SyntaxError()
931
932 return existing;
933 }
934
935 void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
936 _assert(flags->identifier_->next_ == flags->identifier_);
937 CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
938 flags->identifier_->next_ = existing->identifier_;
939
940 existing->count_ += flags->count_;
941 if (existing->offset_ < flags->offset_)
942 existing->offset_ = flags->offset_;
943 }
944
945 void CYScope::Close(CYContext &context, CYStatement *&statements) {
946 Close(context);
947
948 CYList<CYDeclarations> declarations;
949
950 CYForEach (i, internal_)
951 if (i->kind_ == CYIdentifierVariable)
952 declarations ->* $ CYDeclarations($ CYDeclaration(i->identifier_));
953
954 if (declarations) {
955 CYVar *var($ CYVar(declarations));
956 var->SetNext(statements);
957 statements = var;
958 }
959 }
960
961 void CYScope::Close(CYContext &context) {
962 context.scope_ = parent_;
963
964 CYForEach (i, internal_) {
965 _assert(i->identifier_->next_ == i->identifier_);
966 switch (i->kind_) {
967 case CYIdentifierArgument: {
968 _assert(!transparent_);
969 } break;
970
971 case CYIdentifierLexical: {
972 if (!damaged_) {
973 CYIdentifier *replace(context.Unique());
974 replace->next_ = replace;
975 i->identifier_->next_ = replace;
976 i->identifier_ = replace;
977 }
978
979 if (!transparent_)
980 i->kind_ = CYIdentifierVariable;
981 else
982 parent_->Declare(context, i->identifier_, CYIdentifierVariable);
983 } break;
984
985 case CYIdentifierVariable: {
986 if (transparent_) {
987 parent_->Declare(context, i->identifier_, i->kind_);
988 i->kind_ = CYIdentifierGlobal;
989 }
990 } break;
991 default:; } }
992
993 if (damaged_)
994 return;
995
996 typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
997 CYIdentifierOffsetMap offsets;
998
999 CYForEach (i, internal_) {
1000 _assert(i->identifier_->next_ == i->identifier_);
1001 switch (i->kind_) {
1002 case CYIdentifierArgument:
1003 case CYIdentifierVariable:
1004 offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
1005 break;
1006 default:; } }
1007
1008 unsigned offset(0);
1009
1010 for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
1011 if (offset < i->first)
1012 offset = i->first;
1013 CYIdentifier *identifier(i->second);
1014
1015 if (offset >= context.replace_.size())
1016 context.replace_.resize(offset + 1, NULL);
1017 CYIdentifier *&replace(context.replace_[offset++]);
1018
1019 if (replace == NULL)
1020 replace = identifier;
1021 else {
1022 _assert(replace->next_ == replace);
1023 identifier->next_ = replace;
1024 }
1025 }
1026
1027 if (parent_ == NULL)
1028 return;
1029
1030 CYForEach (i, internal_) {
1031 switch (i->kind_) {
1032 case CYIdentifierGlobal: {
1033 if (i->offset_ < offset)
1034 i->offset_ = offset;
1035 parent_->Merge(context, i);
1036 } break;
1037 default:; } }
1038 }
1039
1040 CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
1041 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
1042 }
1043
1044 CYStatement *CYStatement::Return() {
1045 return this;
1046 }
1047
1048 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1049 size_t size(size_ + rhs->size_);
1050 char *value($ char[size + 1]);
1051 memcpy(value, value_, size_);
1052 memcpy(value + size_, rhs->value_, rhs->size_);
1053 value[size] = '\0';
1054 return $S(value, size);
1055 }
1056
1057 CYNumber *CYString::Number(CYContext &context) {
1058 // XXX: there is a precise algorithm for this
1059 return NULL;
1060 }
1061
1062 CYExpression *CYString::PropertyName(CYContext &context) {
1063 return this;
1064 }
1065
1066 CYString *CYString::String(CYContext &context) {
1067 return this;
1068 }
1069
1070 CYTarget *CYSuperAccess::Replace(CYContext &context) {
1071 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1072 }
1073
1074 CYTarget *CYSuperCall::Replace(CYContext &context) {
1075 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1076 }
1077
1078 CYStatement *CYSwitch::Replace(CYContext &context) {
1079 context.Replace(value_);
1080 clauses_->Replace(context);
1081 return this;
1082 }
1083
1084 CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
1085 if (value == NULL)
1086 return NULL;
1087 return $E($ CYAssign(this, value));
1088 }
1089
1090 CYTarget *CYTemplate::Replace(CYContext &context) {
1091 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
1092 }
1093
1094 CYTarget *CYThis::Replace(CYContext &context) {
1095 if (context.this_ != NULL)
1096 return $V(context.this_->Identifier(context));
1097 return this;
1098 }
1099
1100 namespace cy {
1101 namespace Syntax {
1102
1103 CYStatement *Throw::Replace(CYContext &context) {
1104 context.Replace(value_);
1105 return this;
1106 }
1107
1108 } }
1109
1110 CYTarget *CYTrivial::Replace(CYContext &context) {
1111 return this;
1112 }
1113
1114 CYNumber *CYTrue::Number(CYContext &context) {
1115 return $D(1);
1116 }
1117
1118 CYString *CYTrue::String(CYContext &context) {
1119 return $S("true");
1120 }
1121
1122 namespace cy {
1123 namespace Syntax {
1124
1125 CYStatement *Try::Replace(CYContext &context) {
1126 CYScope scope(true, context);
1127 context.ReplaceAll(code_);
1128 scope.Close(context);
1129
1130 catch_->Replace(context);
1131 finally_->Replace(context);
1132 return this;
1133 }
1134
1135 } }
1136
1137 CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
1138 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
1139 }
1140
1141 CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
1142 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1143 }
1144
1145 CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
1146 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
1147 }
1148
1149 CYStatement *CYTypeDefinition::Replace(CYContext &context) {
1150 return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
1151 }
1152
1153 CYTarget *CYTypeError::Replace(CYContext &context) {
1154 _assert(false);
1155 return NULL;
1156 }
1157
1158 CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
1159 return Replace_(context, type);
1160 }
1161
1162 CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
1163 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
1164 }
1165
1166 CYTarget *CYTypeLong::Replace(CYContext &context) {
1167 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
1168 }
1169
1170 CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
1171 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
1172 }
1173
1174 CYTarget *CYTypeShort::Replace(CYContext &context) {
1175 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
1176 }
1177
1178 CYTarget *CYTypeSigned::Replace(CYContext &context) {
1179 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
1180 }
1181
1182 CYTarget *CYTypeUnsigned::Replace(CYContext &context) {
1183 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
1184 }
1185
1186 CYTarget *CYTypeVariable::Replace(CYContext &context) {
1187 return $V(name_);
1188 }
1189
1190 CYTarget *CYTypeVoid::Replace(CYContext &context) {
1191 return $N1($V("Type"), $ CYString("v"));
1192 }
1193
1194 CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
1195 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
1196 }
1197
1198 CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
1199 return modifier_->Replace(context, specifier_->Replace(context));
1200 }
1201
1202 CYTypeFunctionWith *CYTypedIdentifier::Function() {
1203 CYTypeModifier **modifier(&modifier_);
1204 if (*modifier == NULL)
1205 return NULL;
1206 while ((*modifier)->next_ != NULL)
1207 modifier = &(*modifier)->next_;
1208 CYTypeFunctionWith *function((*modifier)->Function());
1209 if (function == NULL)
1210 return NULL;
1211 *modifier = NULL;
1212 return function;
1213 }
1214
1215 CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
1216 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
1217 }
1218
1219 CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1220 return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
1221 }
1222
1223 CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
1224 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
1225 }
1226
1227 CYStatement *CYVar::Replace(CYContext &context) {
1228 if (CYExpression *expression = declarations_->Replace(context, CYIdentifierVariable))
1229 return $E(expression);
1230 return $ CYEmpty();
1231 }
1232
1233 CYTarget *CYVariable::Replace(CYContext &context) {
1234 name_ = name_->Replace(context, CYIdentifierGlobal);
1235 return this;
1236 }
1237
1238 CYFunctionParameter *CYVariable::Parameter() const {
1239 return $ CYFunctionParameter($ CYDeclaration(name_));
1240 }
1241
1242 CYStatement *CYWhile::Replace(CYContext &context) {
1243 context.Replace(test_);
1244 context.ReplaceAll(code_);
1245 return this;
1246 }
1247
1248 CYStatement *CYWith::Replace(CYContext &context) {
1249 context.Replace(scope_);
1250 context.ReplaceAll(code_);
1251 return this;
1252 }
1253
1254 CYExpression *CYWord::PropertyName(CYContext &context) {
1255 return $S(this);
1256 }