]> git.saurik.com Git - cycript.git/blob - Replace.cpp
337e00ed8ceeb2d069b50081f5e78d3d2cad66a5
[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 CYForInitializer *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 CYForInitializer *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 CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
429 if (value == NULL) {
430 if (declaration_->initialiser_ == NULL)
431 return NULL;
432 value = declaration_->initialiser_;
433 }
434
435 return $ CYLet(constant_, $L1($ CYDeclaration(declaration_->identifier_, value)));
436 }
437
438 CYTarget *CYForLexical::Replace(CYContext &context) {
439 _assert(declaration_->Replace(context, CYIdentifierLexical) == NULL);
440 return declaration_->Target(context);
441 }
442
443 CYStatement *CYForIn::Replace(CYContext &context) {
444 CYScope scope(true, context);
445 context.Replace(initialiser_);
446 context.Replace(set_);
447 context.ReplaceAll(code_);
448 scope.Close(context);
449 return this;
450 }
451
452 CYStatement *CYForInitialized::Replace(CYContext &context) {
453 CYAssignment *assignment(declaration_->Replace(context, CYIdentifierVariable));
454 return $ CYBlock($$
455 ->* (assignment == NULL ? NULL : $ CYExpress(assignment))
456 ->* $ CYForIn(declaration_->Target(context), set_, code_));
457 }
458
459 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
460 return $ CYFunctionParameter(declaration_);
461 }
462
463 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
464 return $ CYForIn(declaration_->Target(context), set_, CYComprehension::Replace(context, statement));
465 }
466
467 CYStatement *CYForOf::Replace(CYContext &context) {
468 CYIdentifier *item(context.Unique()), *list(context.Unique());
469
470 return $ CYBlock($$
471 ->* initialiser_->Initialize(context, NULL)
472 ->* $ CYLet(false, $L2($L(list, set_), $L(item)))
473 ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
474 ->* initialiser_->Initialize(context, $M($V(list), $V(item)))
475 ->* code_
476 )));
477 }
478
479 CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
480 return $ CYFunctionParameter(declaration_);
481 }
482
483 CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
484 CYIdentifier *cys($I("$cys"));
485
486 return $E($C0($F(NULL, $P1($L($I("$cys"))), $$
487 ->* $E($ CYAssign($V(cys), set_))
488 ->* $ CYForIn(declaration_->Target(context), $V(cys), $ CYBlock($$
489 ->* $E($ CYAssign(declaration_->Target(context), $M($V(cys), declaration_->Target(context))))
490 ->* CYComprehension::Replace(context, statement)
491 ))
492 )));
493 }
494
495 CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
496 if (value == NULL) {
497 if (declaration_->initialiser_ == NULL)
498 return NULL;
499 value = declaration_->initialiser_;
500 }
501
502 return $ CYVar($L1($ CYDeclaration(declaration_->identifier_, value)));
503 }
504
505 CYTarget *CYForVariable::Replace(CYContext &context) {
506 _assert(declaration_->Replace(context, CYIdentifierVariable) == NULL);
507 return declaration_->Target(context);
508 }
509
510 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
511 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
512 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
513
514 void CYFunction::Replace(CYContext &context) {
515 CYThisScope *_this(context.this_);
516 context.this_ = &this_;
517 context.this_ = CYGetLast(context.this_);
518
519 CYIdentifier *super(context.super_);
520 context.super_ = super_;
521
522 CYNonLocal *nonlocal(context.nonlocal_);
523 CYNonLocal *nextlocal(context.nextlocal_);
524
525 bool localize;
526 if (nonlocal_ != NULL) {
527 localize = false;
528 context.nonlocal_ = nonlocal_;
529 } else {
530 localize = true;
531 nonlocal_ = $ CYNonLocal();
532 context.nextlocal_ = nonlocal_;
533 }
534
535 CYScope scope(!localize, context);
536
537 parameters_->Replace(context, code_);
538
539 context.ReplaceAll(code_);
540
541 if (implicit_)
542 CYImplicitReturn(code_);
543
544 if (CYIdentifier *identifier = this_.identifier_) {
545 context.scope_->Declare(context, identifier, CYIdentifierVariable);
546 code_ = $$
547 ->* $E($ CYAssign($V(identifier), $ CYThis()))
548 ->* code_;
549 }
550
551 if (localize)
552 context.NonLocal(code_);
553
554 context.nextlocal_ = nextlocal;
555 context.nonlocal_ = nonlocal;
556
557 context.super_ = super;
558 context.this_ = _this;
559
560 scope.Close(context, code_);
561 }
562
563 CYTarget *CYFunctionExpression::Replace(CYContext &context) {
564 CYScope scope(false, context);
565 if (name_ != NULL)
566 name_ = name_->Replace(context, CYIdentifierOther);
567
568 CYFunction::Replace(context);
569 scope.Close(context);
570 return this;
571 }
572
573 void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
574 CYAssignment *assignment(initialiser_->Replace(context, CYIdentifierArgument));
575
576 next_->Replace(context, statements);
577
578 if (assignment != NULL)
579 statements = $$
580 ->* $ CYIf($ CYIdentical($ CYTypeOf(initialiser_->Target(context)), $S("undefined")), $$
581 ->* $E(assignment))
582 ->* statements;
583 }
584
585 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
586 name_ = name_->Replace(context, CYIdentifierOther);
587 CYFunction::Replace(context);
588 return this;
589 }
590
591 CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
592 if (next_ == this)
593 return this;
594 if (next_ != NULL)
595 return next_->Replace(context, kind);
596 next_ = context.scope_->Declare(context, this, kind)->identifier_;
597 return next_;
598 }
599
600 CYStatement *CYIf::Return() {
601 CYImplicitReturn(true_);
602 CYImplicitReturn(false_);
603 return this;
604 }
605
606 CYStatement *CYIf::Replace(CYContext &context) {
607 context.Replace(test_);
608 context.ReplaceAll(true_);
609 context.ReplaceAll(false_);
610 return this;
611 }
612
613 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
614 return NULL;
615 }
616
617 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
618 return $ CYIf(test_, CYComprehension::Replace(context, statement));
619 }
620
621 CYStatement *CYImport::Replace(CYContext &context) {
622 return $ CYVar($L1($L($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
623 }
624
625 CYTarget *CYIndirect::Replace(CYContext &context) {
626 return $M(rhs_, $S("$cyi"));
627 }
628
629 CYTarget *CYIndirectMember::Replace(CYContext &context) {
630 return $M($ CYIndirect(object_), property_);
631 }
632
633 CYExpression *CYInfix::Replace(CYContext &context) {
634 context.Replace(lhs_);
635 context.Replace(rhs_);
636 return this;
637 }
638
639 CYStatement *CYLabel::Replace(CYContext &context) {
640 context.Replace(statement_);
641 return this;
642 }
643
644 CYTarget *CYLambda::Replace(CYContext &context) {
645 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
646 }
647
648 CYForInitializer *CYLet::Replace(CYContext &context) {
649 if (CYExpression *expression = declarations_->Replace(context, CYIdentifierLexical))
650 return $E(expression);
651 return $ CYEmpty();
652 }
653
654 CYFunctionExpression *CYMethod::Constructor() {
655 return NULL;
656 }
657
658 void CYMethod::Replace(CYContext &context) {
659 CYFunction::Replace(context);
660 }
661
662 CYString *CYModule::Replace(CYContext &context, const char *separator) const {
663 if (next_ == NULL)
664 return $ CYString(part_);
665 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
666 }
667
668 CYExpression *CYMultiply::Replace(CYContext &context) {
669 CYInfix::Replace(context);
670
671 if (CYNumber *lhn = lhs_->Number(context))
672 if (CYNumber *rhn = rhs_->Number(context))
673 return $D(lhn->Value() * rhn->Value());
674
675 return this;
676 }
677
678 namespace cy {
679 namespace Syntax {
680
681 CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
682 CYSetLast(arguments_) = $ CYArgument(value);
683 return this;
684 }
685
686 CYTarget *New::Replace(CYContext &context) {
687 context.Replace(constructor_);
688 arguments_->Replace(context);
689 return this;
690 }
691
692 } }
693
694 CYNumber *CYNull::Number(CYContext &context) {
695 return $D(0);
696 }
697
698 CYString *CYNull::String(CYContext &context) {
699 return $S("null");
700 }
701
702 CYNumber *CYNumber::Number(CYContext &context) {
703 return this;
704 }
705
706 CYString *CYNumber::String(CYContext &context) {
707 // XXX: there is a precise algorithm for this
708 return $S($pool.sprintf(24, "%.17g", Value()));
709 }
710
711 CYExpression *CYNumber::PropertyName(CYContext &context) {
712 return String(context);
713 }
714
715 CYTarget *CYObject::Replace(CYContext &context) {
716 CYBuilder builder;
717 if (properties_ != NULL)
718 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
719
720 if (builder) {
721 return $C1($M($ CYFunctionExpression(NULL, builder.declarations_->Parameter(context),
722 builder.statements_
723 ->* $ CYReturn($ CYThis())
724 ), $S("call")), this, builder.declarations_->Argument(context));
725 }
726
727 CYForEach (property, properties_)
728 property->Replace(context);
729 return this;
730 }
731
732 CYTarget *CYParenthetical::Replace(CYContext &context) {
733 // XXX: return expression_;
734 context.Replace(expression_);
735 return this;
736 }
737
738 CYExpression *CYPostfix::Replace(CYContext &context) {
739 context.Replace(lhs_);
740 return this;
741 }
742
743 CYExpression *CYPrefix::Replace(CYContext &context) {
744 context.Replace(rhs_);
745 return this;
746 }
747
748 CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
749 update |= name_->Computed();
750 if (update)
751 Replace(context, builder, self, false);
752 if (next_ != NULL)
753 next_ = next_->ReplaceAll(context, builder, self, update);
754 return update ? next_ : this;
755 }
756
757 void CYProperty::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect) {
758 CYExpression *name(name_->PropertyName(context));
759 if (name_->Computed()) {
760 CYIdentifier *unique(context.Unique());
761 builder.declarations_
762 ->* $L1($L(unique, name));
763 name = $V(unique);
764 }
765
766 Replace(context, builder, self, name, protect);
767 }
768
769 void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
770 CYIdentifier *unique(context.Unique());
771 builder.declarations_
772 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
773 builder.statements_
774 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
775 }
776
777 CYFunctionExpression *CYPropertyMethod::Constructor() {
778 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
779 }
780
781 void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
782 CYIdentifier *unique(context.Unique());
783 builder.declarations_
784 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
785 builder.statements_
786 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
787 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
788 }
789
790 void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
791 CYIdentifier *unique(context.Unique());
792 builder.declarations_
793 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
794 builder.statements_
795 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
796 }
797
798 void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
799 _assert(!protect);
800 CYIdentifier *unique(context.Unique());
801 builder.declarations_
802 ->* $L1($L(unique, value_));
803 builder.statements_
804 ->* $E($ CYAssign($M(self, name), $V(unique)));
805 }
806
807 void CYPropertyValue::Replace(CYContext &context) {
808 context.Replace(value_);
809 }
810
811 void CYScript::Replace(CYContext &context) {
812 CYScope scope(false, context);
813 context.scope_->Damage();
814
815 context.nextlocal_ = $ CYNonLocal();
816 context.ReplaceAll(code_);
817 context.NonLocal(code_);
818
819 scope.Close(context, code_);
820
821 unsigned offset(0);
822
823 for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
824 const char *name;
825 if (context.options_.verbose_)
826 name = $pool.strcat("$", $pool.itoa(offset++), NULL);
827 else {
828 char id[8];
829 id[7] = '\0';
830
831 id:
832 unsigned position(7), local(offset++ + 1);
833
834 do {
835 unsigned index(local % (sizeof(MappingSet) - 1));
836 local /= sizeof(MappingSet) - 1;
837 id[--position] = MappingSet[index];
838 } while (local != 0);
839
840 if (scope.Lookup(context, id + position) != NULL)
841 goto id;
842 // XXX: at some point, this could become a keyword
843
844 name = $pool.strmemdup(id + position, 7 - position);
845 }
846
847 CYIdentifier *identifier(*i);
848 _assert(identifier->next_ == identifier);
849 identifier->next_ = $I(name);
850 }
851 }
852
853 CYStatement *CYReturn::Replace(CYContext &context) {
854 if (context.nonlocal_ != NULL) {
855 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
856 return $ cy::Syntax::Throw($ CYObject(
857 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
858 ));
859 }
860
861 context.Replace(value_);
862 return this;
863 }
864
865 CYTarget *CYRubyBlock::Replace(CYContext &context) {
866 return call_->AddArgument(context, proc_->Replace(context));
867 }
868
869 CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
870 return Replace(context)->AddArgument(context, value);
871 }
872
873 CYTarget *CYRubyProc::Replace(CYContext &context) {
874 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
875 function = CYNonLocalize(context, function);
876 function->implicit_ = true;
877 return function;
878 }
879
880 CYScope::CYScope(bool transparent, CYContext &context) :
881 transparent_(transparent),
882 parent_(context.scope_),
883 damaged_(false),
884 shadow_(NULL),
885 internal_(NULL)
886 {
887 _assert(!transparent_ || parent_ != NULL);
888 context.scope_ = this;
889 }
890
891 void CYScope::Damage() {
892 damaged_ = true;
893 if (parent_ != NULL)
894 parent_->Damage();
895 }
896
897 CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
898 CYForEach (i, internal_)
899 if (strcmp(i->identifier_->Word(), word) == 0)
900 return i;
901 return NULL;
902 }
903
904 CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
905 return Lookup(context, identifier->Word());
906 }
907
908 CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
909 _assert(identifier->next_ == NULL || identifier->next_ == identifier);
910
911 CYIdentifierFlags *existing(Lookup(context, identifier));
912 if (existing == NULL)
913 internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
914 ++internal_->count_;
915 if (existing == NULL)
916 return internal_;
917
918 switch (kind) {
919 case CYIdentifierArgument:
920 case CYIdentifierCatch:
921 case CYIdentifierMagic:
922 _assert(false);
923 default:
924 break;
925 }
926
927 if (existing->kind_ == CYIdentifierGlobal)
928 existing->kind_ = kind;
929 else if (kind == CYIdentifierGlobal)
930 ;
931 else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
932 _assert(false); // XXX: throw new SyntaxError()
933
934 return existing;
935 }
936
937 void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
938 _assert(flags->identifier_->next_ == flags->identifier_);
939 CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
940 flags->identifier_->next_ = existing->identifier_;
941
942 existing->count_ += flags->count_;
943 if (existing->offset_ < flags->offset_)
944 existing->offset_ = flags->offset_;
945 }
946
947 void CYScope::Close(CYContext &context, CYStatement *&statements) {
948 Close(context);
949
950 CYList<CYDeclarations> declarations;
951
952 CYForEach (i, internal_)
953 if (i->kind_ == CYIdentifierVariable)
954 declarations
955 ->* $ CYDeclarations($ CYDeclaration(i->identifier_));
956
957 if (declarations) {
958 CYVar *var($ CYVar(declarations));
959 var->SetNext(statements);
960 statements = var;
961 }
962 }
963
964 void CYScope::Close(CYContext &context) {
965 context.scope_ = parent_;
966
967 CYForEach (i, internal_) {
968 _assert(i->identifier_->next_ == i->identifier_);
969 switch (i->kind_) {
970 case CYIdentifierArgument: {
971 _assert(!transparent_);
972 } break;
973
974 case CYIdentifierLexical: {
975 if (!damaged_) {
976 CYIdentifier *replace(context.Unique());
977 replace->next_ = replace;
978 i->identifier_->next_ = replace;
979 i->identifier_ = replace;
980 }
981
982 if (!transparent_)
983 i->kind_ = CYIdentifierVariable;
984 else
985 parent_->Declare(context, i->identifier_, CYIdentifierVariable);
986 } break;
987
988 case CYIdentifierVariable: {
989 if (transparent_) {
990 parent_->Declare(context, i->identifier_, i->kind_);
991 i->kind_ = CYIdentifierGlobal;
992 }
993 } break;
994 default:; } }
995
996 if (damaged_)
997 return;
998
999 typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
1000 CYIdentifierOffsetMap offsets;
1001
1002 CYForEach (i, internal_) {
1003 _assert(i->identifier_->next_ == i->identifier_);
1004 switch (i->kind_) {
1005 case CYIdentifierArgument:
1006 case CYIdentifierVariable:
1007 offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
1008 break;
1009 default:; } }
1010
1011 unsigned offset(0);
1012
1013 for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
1014 if (offset < i->first)
1015 offset = i->first;
1016 CYIdentifier *identifier(i->second);
1017
1018 if (offset >= context.replace_.size())
1019 context.replace_.resize(offset + 1, NULL);
1020 CYIdentifier *&replace(context.replace_[offset++]);
1021
1022 if (replace == NULL)
1023 replace = identifier;
1024 else {
1025 _assert(replace->next_ == replace);
1026 identifier->next_ = replace;
1027 }
1028 }
1029
1030 if (parent_ == NULL)
1031 return;
1032
1033 CYForEach (i, internal_) {
1034 switch (i->kind_) {
1035 case CYIdentifierGlobal: {
1036 if (i->offset_ < offset)
1037 i->offset_ = offset;
1038 parent_->Merge(context, i);
1039 } break;
1040 default:; } }
1041 }
1042
1043 CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
1044 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
1045 }
1046
1047 CYStatement *CYStatement::Return() {
1048 return this;
1049 }
1050
1051 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1052 size_t size(size_ + rhs->size_);
1053 char *value($ char[size + 1]);
1054 memcpy(value, value_, size_);
1055 memcpy(value + size_, rhs->value_, rhs->size_);
1056 value[size] = '\0';
1057 return $S(value, size);
1058 }
1059
1060 CYNumber *CYString::Number(CYContext &context) {
1061 // XXX: there is a precise algorithm for this
1062 return NULL;
1063 }
1064
1065 CYExpression *CYString::PropertyName(CYContext &context) {
1066 return this;
1067 }
1068
1069 CYString *CYString::String(CYContext &context) {
1070 return this;
1071 }
1072
1073 CYTarget *CYSuperAccess::Replace(CYContext &context) {
1074 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1075 }
1076
1077 CYTarget *CYSuperCall::Replace(CYContext &context) {
1078 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1079 }
1080
1081 CYStatement *CYSwitch::Replace(CYContext &context) {
1082 context.Replace(value_);
1083 clauses_->Replace(context);
1084 return this;
1085 }
1086
1087 CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
1088 if (value == NULL)
1089 return NULL;
1090 return $E($ CYAssign(this, value));
1091 }
1092
1093 CYTarget *CYTemplate::Replace(CYContext &context) {
1094 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
1095 }
1096
1097 CYTarget *CYThis::Replace(CYContext &context) {
1098 if (context.this_ != NULL)
1099 return $V(context.this_->Identifier(context));
1100 return this;
1101 }
1102
1103 namespace cy {
1104 namespace Syntax {
1105
1106 CYStatement *Throw::Replace(CYContext &context) {
1107 context.Replace(value_);
1108 return this;
1109 }
1110
1111 } }
1112
1113 CYTarget *CYTrivial::Replace(CYContext &context) {
1114 return this;
1115 }
1116
1117 CYNumber *CYTrue::Number(CYContext &context) {
1118 return $D(1);
1119 }
1120
1121 CYString *CYTrue::String(CYContext &context) {
1122 return $S("true");
1123 }
1124
1125 namespace cy {
1126 namespace Syntax {
1127
1128 CYStatement *Try::Replace(CYContext &context) {
1129 CYScope scope(true, context);
1130 context.ReplaceAll(code_);
1131 scope.Close(context);
1132
1133 catch_->Replace(context);
1134 finally_->Replace(context);
1135 return this;
1136 }
1137
1138 } }
1139
1140 CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
1141 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
1142 }
1143
1144 CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
1145 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1146 }
1147
1148 CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
1149 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
1150 }
1151
1152 CYStatement *CYTypeDefinition::Replace(CYContext &context) {
1153 return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
1154 }
1155
1156 CYTarget *CYTypeError::Replace(CYContext &context) {
1157 _assert(false);
1158 return NULL;
1159 }
1160
1161 CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
1162 return Replace_(context, type);
1163 }
1164
1165 CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
1166 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
1167 }
1168
1169 CYTarget *CYTypeLong::Replace(CYContext &context) {
1170 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
1171 }
1172
1173 CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
1174 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
1175 }
1176
1177 CYTarget *CYTypeShort::Replace(CYContext &context) {
1178 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
1179 }
1180
1181 CYTarget *CYTypeSigned::Replace(CYContext &context) {
1182 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
1183 }
1184
1185 CYTarget *CYTypeUnsigned::Replace(CYContext &context) {
1186 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
1187 }
1188
1189 CYTarget *CYTypeVariable::Replace(CYContext &context) {
1190 return $V(name_);
1191 }
1192
1193 CYTarget *CYTypeVoid::Replace(CYContext &context) {
1194 return $N1($V("Type"), $ CYString("v"));
1195 }
1196
1197 CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
1198 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
1199 }
1200
1201 CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
1202 return modifier_->Replace(context, specifier_->Replace(context));
1203 }
1204
1205 CYTypeFunctionWith *CYTypedIdentifier::Function() {
1206 CYTypeModifier **modifier(&modifier_);
1207 if (*modifier == NULL)
1208 return NULL;
1209 while ((*modifier)->next_ != NULL)
1210 modifier = &(*modifier)->next_;
1211 CYTypeFunctionWith *function((*modifier)->Function());
1212 if (function == NULL)
1213 return NULL;
1214 *modifier = NULL;
1215 return function;
1216 }
1217
1218 CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
1219 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
1220 }
1221
1222 CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1223 return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
1224 }
1225
1226 CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
1227 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
1228 }
1229
1230 CYForInitializer *CYVar::Replace(CYContext &context) {
1231 if (CYExpression *expression = declarations_->Replace(context, CYIdentifierVariable))
1232 return $E(expression);
1233 return $ CYEmpty();
1234 }
1235
1236 CYTarget *CYVariable::Replace(CYContext &context) {
1237 name_ = name_->Replace(context, CYIdentifierGlobal);
1238 return this;
1239 }
1240
1241 CYFunctionParameter *CYVariable::Parameter() const {
1242 return $ CYFunctionParameter($ CYDeclaration(name_));
1243 }
1244
1245 CYStatement *CYWhile::Replace(CYContext &context) {
1246 context.Replace(test_);
1247 context.ReplaceAll(code_);
1248 return this;
1249 }
1250
1251 CYStatement *CYWith::Replace(CYContext &context) {
1252 context.Replace(scope_);
1253 context.ReplaceAll(code_);
1254 return this;
1255 }
1256
1257 CYExpression *CYWord::PropertyName(CYContext &context) {
1258 return $S(this);
1259 }