]> git.saurik.com Git - cycript.git/blob - Replace.cpp
This implementation for Java works, but I hate it.
[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 CYForEach (element, elements_)
105 element->Replace(context);
106 return this;
107 }
108
109 CYTarget *CYArrayComprehension::Replace(CYContext &context) {
110 CYIdentifier *cyv(context.Unique());
111
112 return $C0($F(NULL, $P1($B(cyv), comprehensions_->Parameters(context)), $$
113 ->* $E($ CYAssign($V(cyv), $ CYArray()))
114 ->* comprehensions_->Replace(context, $E($C1($M($V(cyv), $S("push")), expression_)))
115 ->* $ CYReturn($V(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($B(super)), $$
187 ->* $ CYVar($B1($B(constructor, tail_->constructor_)))
188 ->* $ CYVar($B1($B(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.bindings_)
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($B1($B(name_, $ CYClassExpression(name_, tail_))));
201 }
202
203 void CYClause::Replace(CYContext &context) { $T()
204 context.Replace(value_);
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($B1($B(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 *CYBinding::Target(CYContext &context) {
298 return $V(identifier_);
299 }
300
301 CYAssignment *CYBinding::Replace(CYContext &context, CYIdentifierKind kind) {
302 identifier_ = identifier_->Replace(context, kind);
303
304 if (initializer_ == NULL)
305 return NULL;
306
307 CYAssignment *value($ CYAssign(Target(context), initializer_));
308 initializer_ = NULL;
309 return value;
310 }
311
312 CYExpression *CYBindings::Replace(CYContext &context, CYIdentifierKind kind) { $T(NULL)
313 CYAssignment *assignment(binding_->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 *CYBindings::Parameter(CYContext &context) { $T(NULL)
325 return $ CYFunctionParameter($ CYBinding(binding_->identifier_), next_->Parameter(context));
326 }
327
328 CYArgument *CYBindings::Argument(CYContext &context) { $T(NULL)
329 return $ CYArgument(binding_->initializer_, 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 }
351
352 CYForInitializer *CYEmpty::Replace(CYContext &context) {
353 return NULL;
354 }
355
356 CYTarget *CYEncodedType::Replace(CYContext &context) {
357 return typed_->Replace(context);
358 }
359
360 CYTarget *CYEval::Replace(CYContext &context) {
361 context.scope_->Damage();
362 if (arguments_ != NULL)
363 arguments_->value_ = $C1($M($V("Cycript"), $S("compile")), arguments_->value_);
364 return $C($V("eval"), arguments_);
365 }
366
367 CYStatement *CYExpress::Return() {
368 return $ CYReturn(expression_);
369 }
370
371 CYForInitializer *CYExpress::Replace(CYContext &context) {
372 context.Replace(expression_);
373 return this;
374 }
375
376 CYTarget *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
377 return $C1(this, value);
378 }
379
380 CYFunctionParameter *CYExpression::Parameter() const {
381 return NULL;
382 }
383
384 CYTarget *CYExtend::Replace(CYContext &context) {
385 return object_.Replace(context, lhs_);
386 }
387
388 CYStatement *CYExternal::Replace(CYContext &context) {
389 return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word())))));
390 }
391
392 CYNumber *CYFalse::Number(CYContext &context) {
393 return $D(0);
394 }
395
396 CYString *CYFalse::String(CYContext &context) {
397 return $S("false");
398 }
399
400 CYExpression *CYFatArrow::Replace(CYContext &context) {
401 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
402 function->this_.SetNext(context.this_);
403 return function;
404 }
405
406 void CYFinally::Replace(CYContext &context) { $T()
407 CYScope scope(true, context);
408 context.ReplaceAll(code_);
409 scope.Close(context);
410 }
411
412 CYStatement *CYFor::Replace(CYContext &context) {
413 CYScope outer(true, context);
414 context.Replace(initializer_);
415
416 context.Replace(test_);
417
418 {
419 CYScope inner(true, context);
420 context.ReplaceAll(code_);
421 inner.Close(context);
422 }
423
424 context.Replace(increment_);
425
426 outer.Close(context);
427 return this;
428 }
429
430 CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
431 if (value == NULL) {
432 if (binding_->initializer_ == NULL)
433 return NULL;
434 value = binding_->initializer_;
435 }
436
437 return $ CYLexical(constant_, $B1($ CYBinding(binding_->identifier_, value)));
438 }
439
440 CYTarget *CYForLexical::Replace(CYContext &context) {
441 _assert(binding_->Replace(context, CYIdentifierLexical) == NULL);
442 return binding_->Target(context);
443 }
444
445 CYStatement *CYForIn::Replace(CYContext &context) {
446 CYScope scope(true, context);
447 context.Replace(initializer_);
448 context.Replace(iterable_);
449 context.ReplaceAll(code_);
450 scope.Close(context);
451 return this;
452 }
453
454 CYStatement *CYForInitialized::Replace(CYContext &context) {
455 CYAssignment *assignment(binding_->Replace(context, CYIdentifierVariable));
456 return $ CYBlock($$
457 ->* (assignment == NULL ? NULL : $ CYExpress(assignment))
458 ->* $ CYForIn(binding_->Target(context), iterable_, code_));
459 }
460
461 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
462 return $ CYFunctionParameter(binding_);
463 }
464
465 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
466 return $ CYForIn(binding_->Target(context), iterable_, CYComprehension::Replace(context, statement));
467 }
468
469 CYStatement *CYForOf::Replace(CYContext &context) {
470 CYIdentifier *item(context.Unique()), *list(context.Unique());
471
472 return $ CYBlock($$
473 ->* initializer_->Initialize(context, NULL)
474 ->* $ CYLexical(false, $B2($B(list, iterable_), $B(item)))
475 ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
476 ->* initializer_->Initialize(context, $M($V(list), $V(item)))
477 ->* code_
478 )));
479 }
480
481 CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
482 return $ CYFunctionParameter(binding_);
483 }
484
485 CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
486 CYIdentifier *cys(context.Unique());
487
488 return $ CYBlock($$
489 ->* $ CYLexical(false, $B1($B(cys, iterable_)))
490 ->* $ CYForIn(binding_->Target(context), $V(cys), $ CYBlock($$
491 ->* $E($ CYAssign(binding_->Target(context), $M($V(cys), binding_->Target(context))))
492 ->* CYComprehension::Replace(context, statement)
493 )));
494 }
495
496 CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
497 if (value == NULL) {
498 if (binding_->initializer_ == NULL)
499 return NULL;
500 value = binding_->initializer_;
501 }
502
503 return $ CYVar($B1($ CYBinding(binding_->identifier_, value)));
504 }
505
506 CYTarget *CYForVariable::Replace(CYContext &context) {
507 _assert(binding_->Replace(context, CYIdentifierVariable) == NULL);
508 return binding_->Target(context);
509 }
510
511 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
512 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
513 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
514
515 void CYFunction::Replace(CYContext &context) {
516 CYThisScope *_this(context.this_);
517 context.this_ = &this_;
518 context.this_ = CYGetLast(context.this_);
519
520 CYIdentifier *super(context.super_);
521 context.super_ = super_;
522
523 CYNonLocal *nonlocal(context.nonlocal_);
524 CYNonLocal *nextlocal(context.nextlocal_);
525
526 bool localize;
527 if (nonlocal_ != NULL) {
528 localize = false;
529 context.nonlocal_ = nonlocal_;
530 } else {
531 localize = true;
532 nonlocal_ = $ CYNonLocal();
533 context.nextlocal_ = nonlocal_;
534 }
535
536 CYScope scope(!localize, context);
537
538 $I("arguments")->Replace(context, CYIdentifierMagic);
539
540 parameters_->Replace(context, code_);
541
542 context.ReplaceAll(code_);
543
544 if (implicit_)
545 CYImplicitReturn(code_);
546
547 if (CYIdentifier *identifier = this_.identifier_) {
548 context.scope_->Declare(context, identifier, CYIdentifierVariable);
549 code_ = $$
550 ->* $E($ CYAssign($V(identifier), $ CYThis()))
551 ->* code_;
552 }
553
554 if (localize)
555 context.NonLocal(code_);
556
557 context.nextlocal_ = nextlocal;
558 context.nonlocal_ = nonlocal;
559
560 context.super_ = super;
561 context.this_ = _this;
562
563 scope.Close(context, code_);
564 }
565
566 CYTarget *CYFunctionExpression::Replace(CYContext &context) {
567 CYScope scope(false, context);
568 if (name_ != NULL)
569 name_ = name_->Replace(context, CYIdentifierOther);
570
571 CYFunction::Replace(context);
572 scope.Close(context);
573 return this;
574 }
575
576 void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
577 CYAssignment *assignment(binding_->Replace(context, CYIdentifierArgument));
578
579 next_->Replace(context, statements);
580
581 if (assignment != NULL)
582 statements = $$
583 ->* $ CYIf($ CYIdentical($ CYTypeOf(binding_->Target(context)), $S("undefined")), $$
584 ->* $E(assignment))
585 ->* statements;
586 }
587
588 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
589 name_ = name_->Replace(context, CYIdentifierOther);
590 CYFunction::Replace(context);
591 return this;
592 }
593
594 CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
595 if (next_ == this)
596 return this;
597 if (next_ != NULL)
598 return next_->Replace(context, kind);
599 next_ = context.scope_->Declare(context, this, kind)->identifier_;
600 return next_;
601 }
602
603 CYStatement *CYIf::Return() {
604 CYImplicitReturn(true_);
605 CYImplicitReturn(false_);
606 return this;
607 }
608
609 CYStatement *CYIf::Replace(CYContext &context) {
610 context.Replace(test_);
611 context.ReplaceAll(true_);
612 context.ReplaceAll(false_);
613 return this;
614 }
615
616 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
617 return NULL;
618 }
619
620 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
621 return $ CYIf(test_, CYComprehension::Replace(context, statement));
622 }
623
624 CYStatement *CYImport::Replace(CYContext &context) {
625 return $ CYVar($B1($B($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
626 }
627
628 CYStatement *CYImportDeclaration::Replace(CYContext &context) {
629 CYIdentifier *module(context.Unique());
630
631 CYList<CYStatement> statements;
632 CYForEach (specifier, specifiers_)
633 statements->*specifier->Replace(context, module);
634
635 return $ CYBlock($$
636 ->* $ CYLexical(false, $B1($B(module, $C1($V("require"), module_))))
637 ->* statements);
638 }
639
640 CYStatement *CYImportSpecifier::Replace(CYContext &context, CYIdentifier *module) {
641 binding_ = binding_->Replace(context, CYIdentifierLexical);
642
643 CYExpression *import($V(module));
644 if (name_ != NULL)
645 import = $M(import, $S(name_));
646 return $E($ CYAssign($V(binding_), import));
647 }
648
649 CYTarget *CYIndirect::Replace(CYContext &context) {
650 return $M(rhs_, $S("$cyi"));
651 }
652
653 CYTarget *CYIndirectMember::Replace(CYContext &context) {
654 return $M($ CYIndirect(object_), property_);
655 }
656
657 CYExpression *CYInfix::Replace(CYContext &context) {
658 context.Replace(lhs_);
659 context.Replace(rhs_);
660 return this;
661 }
662
663 CYStatement *CYLabel::Replace(CYContext &context) {
664 context.Replace(statement_);
665 return this;
666 }
667
668 CYTarget *CYLambda::Replace(CYContext &context) {
669 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
670 }
671
672 CYForInitializer *CYLexical::Replace(CYContext &context) {
673 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierLexical))
674 return $E(expression);
675 return $ CYEmpty();
676 }
677
678 CYFunctionExpression *CYMethod::Constructor() {
679 return NULL;
680 }
681
682 void CYMethod::Replace(CYContext &context) {
683 CYFunction::Replace(context);
684 }
685
686 CYString *CYModule::Replace(CYContext &context, const char *separator) const {
687 if (next_ == NULL)
688 return $ CYString(part_);
689 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
690 }
691
692 CYExpression *CYMultiply::Replace(CYContext &context) {
693 CYInfix::Replace(context);
694
695 if (CYNumber *lhn = lhs_->Number(context))
696 if (CYNumber *rhn = rhs_->Number(context))
697 return $D(lhn->Value() * rhn->Value());
698
699 return this;
700 }
701
702 namespace cy {
703 namespace Syntax {
704
705 CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
706 CYSetLast(arguments_) = $ CYArgument(value);
707 return this;
708 }
709
710 CYTarget *New::Replace(CYContext &context) {
711 context.Replace(constructor_);
712 arguments_->Replace(context);
713 return this;
714 }
715
716 } }
717
718 CYNumber *CYNull::Number(CYContext &context) {
719 return $D(0);
720 }
721
722 CYString *CYNull::String(CYContext &context) {
723 return $S("null");
724 }
725
726 CYNumber *CYNumber::Number(CYContext &context) {
727 return this;
728 }
729
730 CYString *CYNumber::String(CYContext &context) {
731 // XXX: there is a precise algorithm for this
732 return $S($pool.sprintf(24, "%.17g", Value()));
733 }
734
735 CYExpression *CYNumber::PropertyName(CYContext &context) {
736 return String(context);
737 }
738
739 CYTarget *CYObject::Replace(CYContext &context, CYTarget *seed) {
740 CYBuilder builder;
741 if (properties_ != NULL)
742 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), seed != this);
743
744 if (builder) {
745 return $C1($M($ CYFunctionExpression(NULL, builder.bindings_->Parameter(context),
746 builder.statements_
747 ->* $ CYReturn($ CYThis())
748 ), $S("call")), seed, builder.bindings_->Argument(context));
749 }
750
751 CYForEach (property, properties_)
752 property->Replace(context);
753 return seed;
754 }
755
756 CYTarget *CYObject::Replace(CYContext &context) {
757 return Replace(context, this);
758 }
759
760 CYTarget *CYParenthetical::Replace(CYContext &context) {
761 // XXX: return expression_;
762 context.Replace(expression_);
763 return this;
764 }
765
766 CYExpression *CYPostfix::Replace(CYContext &context) {
767 context.Replace(lhs_);
768 return this;
769 }
770
771 CYExpression *CYPrefix::Replace(CYContext &context) {
772 context.Replace(rhs_);
773 return this;
774 }
775
776 CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
777 update |= Update();
778 if (update)
779 Replace(context, builder, self, false);
780 if (next_ != NULL)
781 next_ = next_->ReplaceAll(context, builder, self, update);
782 return update ? next_ : this;
783 }
784
785 void CYProperty::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect) {
786 CYExpression *name(name_->PropertyName(context));
787 if (name_->Computed()) {
788 CYIdentifier *unique(context.Unique());
789 builder.bindings_
790 ->* $B1($B(unique, name));
791 name = $V(unique);
792 }
793
794 Replace(context, builder, self, name, protect);
795 }
796
797 bool CYProperty::Update() const {
798 return name_->Computed();
799 }
800
801 void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
802 CYIdentifier *unique(context.Unique());
803 builder.bindings_
804 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
805 builder.statements_
806 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
807 }
808
809 CYFunctionExpression *CYPropertyMethod::Constructor() {
810 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
811 }
812
813 void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
814 CYIdentifier *unique(context.Unique());
815 builder.bindings_
816 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
817 builder.statements_
818 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
819 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
820 }
821
822 bool CYPropertyMethod::Update() const {
823 return true;
824 }
825
826 void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
827 CYIdentifier *unique(context.Unique());
828 builder.bindings_
829 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
830 builder.statements_
831 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
832 }
833
834 void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
835 _assert(!protect);
836 CYIdentifier *unique(context.Unique());
837 builder.bindings_
838 ->* $B1($B(unique, value_));
839 builder.statements_
840 ->* $E($ CYAssign($M(self, name), $V(unique)));
841 }
842
843 void CYPropertyValue::Replace(CYContext &context) {
844 context.Replace(value_);
845 }
846
847 void CYScript::Replace(CYContext &context) {
848 CYScope scope(false, context);
849 context.scope_->Damage();
850
851 context.nextlocal_ = $ CYNonLocal();
852 context.ReplaceAll(code_);
853 context.NonLocal(code_);
854
855 scope.Close(context, code_);
856
857 unsigned offset(0);
858
859 for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
860 const char *name;
861 if (context.options_.verbose_)
862 name = $pool.strcat("$", $pool.itoa(offset++), NULL);
863 else {
864 char id[8];
865 id[7] = '\0';
866
867 id:
868 unsigned position(7), local(offset++ + 1);
869
870 do {
871 unsigned index(local % (sizeof(MappingSet) - 1));
872 local /= sizeof(MappingSet) - 1;
873 id[--position] = MappingSet[index];
874 } while (local != 0);
875
876 if (scope.Lookup(context, id + position) != NULL)
877 goto id;
878 // XXX: at some point, this could become a keyword
879
880 name = $pool.strmemdup(id + position, 7 - position);
881 }
882
883 CYIdentifier *identifier(*i);
884 _assert(identifier->next_ == identifier);
885 identifier->next_ = $I(name);
886 }
887 }
888
889 CYTarget *CYResolveMember::Replace(CYContext &context) {
890 return $M($M(object_, $S("$cyr")), property_);
891 }
892
893 CYStatement *CYReturn::Replace(CYContext &context) {
894 if (context.nonlocal_ != NULL) {
895 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
896 return $ cy::Syntax::Throw($ CYObject(
897 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
898 ));
899 }
900
901 context.Replace(value_);
902 return this;
903 }
904
905 CYTarget *CYRubyBlock::Replace(CYContext &context) {
906 return lhs_->AddArgument(context, proc_->Replace(context));
907 }
908
909 CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
910 return Replace(context)->AddArgument(context, value);
911 }
912
913 CYTarget *CYRubyProc::Replace(CYContext &context) {
914 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
915 function = CYNonLocalize(context, function);
916 function->implicit_ = true;
917 return function;
918 }
919
920 CYScope::CYScope(bool transparent, CYContext &context) :
921 transparent_(transparent),
922 parent_(context.scope_),
923 damaged_(false),
924 shadow_(NULL),
925 internal_(NULL)
926 {
927 _assert(!transparent_ || parent_ != NULL);
928 context.scope_ = this;
929 }
930
931 void CYScope::Damage() {
932 damaged_ = true;
933 if (parent_ != NULL)
934 parent_->Damage();
935 }
936
937 CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
938 CYForEach (i, internal_)
939 if (strcmp(i->identifier_->Word(), word) == 0)
940 return i;
941 return NULL;
942 }
943
944 CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
945 return Lookup(context, identifier->Word());
946 }
947
948 CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
949 _assert(identifier->next_ == NULL || identifier->next_ == identifier);
950
951 CYIdentifierFlags *existing(Lookup(context, identifier));
952 if (existing == NULL)
953 internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
954 ++internal_->count_;
955 if (existing == NULL)
956 return internal_;
957
958 if (kind == CYIdentifierGlobal);
959 else if (existing->kind_ == CYIdentifierGlobal || existing->kind_ == CYIdentifierMagic)
960 existing->kind_ = kind;
961 else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
962 _assert(false); // XXX: throw new SyntaxError()
963
964 return existing;
965 }
966
967 void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
968 _assert(flags->identifier_->next_ == flags->identifier_);
969 CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
970 flags->identifier_->next_ = existing->identifier_;
971
972 existing->count_ += flags->count_;
973 if (existing->offset_ < flags->offset_)
974 existing->offset_ = flags->offset_;
975 }
976
977 void CYScope::Close(CYContext &context, CYStatement *&statements) {
978 Close(context);
979
980 CYList<CYBindings> bindings;
981
982 CYForEach (i, internal_)
983 if (i->kind_ == CYIdentifierVariable)
984 bindings
985 ->* $ CYBindings($ CYBinding(i->identifier_));
986
987 if (bindings) {
988 CYVar *var($ CYVar(bindings));
989 var->SetNext(statements);
990 statements = var;
991 }
992 }
993
994 void CYScope::Close(CYContext &context) {
995 context.scope_ = parent_;
996
997 CYForEach (i, internal_) {
998 _assert(i->identifier_->next_ == i->identifier_);
999 switch (i->kind_) {
1000 case CYIdentifierArgument: {
1001 _assert(!transparent_);
1002 } break;
1003
1004 case CYIdentifierLexical: {
1005 if (!damaged_) {
1006 CYIdentifier *replace(context.Unique());
1007 replace->next_ = replace;
1008 i->identifier_->next_ = replace;
1009 i->identifier_ = replace;
1010 }
1011
1012 if (!transparent_)
1013 i->kind_ = CYIdentifierVariable;
1014 else
1015 parent_->Declare(context, i->identifier_, CYIdentifierVariable);
1016 } break;
1017
1018 case CYIdentifierVariable: {
1019 if (transparent_) {
1020 parent_->Declare(context, i->identifier_, i->kind_);
1021 i->kind_ = CYIdentifierGlobal;
1022 }
1023 } break;
1024 default:; } }
1025
1026 if (damaged_)
1027 return;
1028
1029 typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
1030 CYIdentifierOffsetMap offsets;
1031
1032 CYForEach (i, internal_) {
1033 _assert(i->identifier_->next_ == i->identifier_);
1034 switch (i->kind_) {
1035 case CYIdentifierArgument:
1036 case CYIdentifierVariable:
1037 offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
1038 break;
1039 default:; } }
1040
1041 unsigned offset(0);
1042
1043 for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
1044 if (offset < i->first)
1045 offset = i->first;
1046 CYIdentifier *identifier(i->second);
1047
1048 if (offset >= context.replace_.size())
1049 context.replace_.resize(offset + 1, NULL);
1050 CYIdentifier *&replace(context.replace_[offset++]);
1051
1052 if (replace == NULL)
1053 replace = identifier;
1054 else {
1055 _assert(replace->next_ == replace);
1056 identifier->next_ = replace;
1057 }
1058 }
1059
1060 if (parent_ == NULL)
1061 return;
1062
1063 CYForEach (i, internal_) {
1064 switch (i->kind_) {
1065 case CYIdentifierGlobal: {
1066 if (i->offset_ < offset)
1067 i->offset_ = offset;
1068 parent_->Merge(context, i);
1069 } break;
1070 default:; } }
1071 }
1072
1073 CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
1074 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
1075 }
1076
1077 CYStatement *CYStatement::Return() {
1078 return this;
1079 }
1080
1081 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1082 size_t size(size_ + rhs->size_);
1083 char *value($ char[size + 1]);
1084 memcpy(value, value_, size_);
1085 memcpy(value + size_, rhs->value_, rhs->size_);
1086 value[size] = '\0';
1087 return $S(value, size);
1088 }
1089
1090 CYNumber *CYString::Number(CYContext &context) {
1091 // XXX: there is a precise algorithm for this
1092 return NULL;
1093 }
1094
1095 CYExpression *CYString::PropertyName(CYContext &context) {
1096 return this;
1097 }
1098
1099 CYString *CYString::String(CYContext &context) {
1100 return this;
1101 }
1102
1103 CYStatement *CYStructDefinition::Replace(CYContext &context) {
1104 CYTarget *target(tail_->Replace(context));
1105 if (name_ != NULL)
1106 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1107 return $ CYLexical(false, $B1($B($I($pool.strcat(name_->Word(), "$cy", NULL)), target)));
1108 }
1109
1110 CYTarget *CYStructTail::Replace(CYContext &context) {
1111 CYList<CYElementValue> types;
1112 CYList<CYElementValue> names;
1113
1114 CYForEach (field, fields_) {
1115 CYTypedIdentifier *typed(field->typed_);
1116 types->*$ CYElementValue(typed->Replace(context));
1117
1118 CYExpression *name;
1119 if (typed->identifier_ == NULL)
1120 name = NULL;
1121 else
1122 name = $S(typed->identifier_->Word());
1123 names->*$ CYElementValue(name);
1124 }
1125
1126 return $N2($V("Type"), $ CYArray(types), $ CYArray(names));
1127 }
1128
1129 CYTarget *CYSuperAccess::Replace(CYContext &context) {
1130 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1131 }
1132
1133 CYTarget *CYSuperCall::Replace(CYContext &context) {
1134 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1135 }
1136
1137 CYTarget *CYSymbol::Replace(CYContext &context) {
1138 return $C1($M($V("Symbol"), $S("for")), $S(name_));
1139 }
1140
1141 CYStatement *CYSwitch::Replace(CYContext &context) {
1142 context.Replace(value_);
1143 clauses_->Replace(context);
1144 return this;
1145 }
1146
1147 CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
1148 if (value == NULL)
1149 return NULL;
1150 return $E($ CYAssign(this, value));
1151 }
1152
1153 CYTarget *CYTemplate::Replace(CYContext &context) {
1154 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
1155 }
1156
1157 CYTarget *CYThis::Replace(CYContext &context) {
1158 if (context.this_ != NULL)
1159 return $V(context.this_->Identifier(context));
1160 return this;
1161 }
1162
1163 namespace cy {
1164 namespace Syntax {
1165
1166 CYStatement *Throw::Replace(CYContext &context) {
1167 context.Replace(value_);
1168 return this;
1169 }
1170
1171 } }
1172
1173 CYTarget *CYTrivial::Replace(CYContext &context) {
1174 return this;
1175 }
1176
1177 CYNumber *CYTrue::Number(CYContext &context) {
1178 return $D(1);
1179 }
1180
1181 CYString *CYTrue::String(CYContext &context) {
1182 return $S("true");
1183 }
1184
1185 namespace cy {
1186 namespace Syntax {
1187
1188 CYStatement *Try::Replace(CYContext &context) {
1189 CYScope scope(true, context);
1190 context.ReplaceAll(code_);
1191 scope.Close(context);
1192
1193 catch_->Replace(context);
1194 finally_->Replace(context);
1195 return this;
1196 }
1197
1198 } }
1199
1200 CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
1201 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
1202 }
1203
1204 CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
1205 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1206 }
1207
1208 CYTarget *CYTypeCharacter::Replace(CYContext &context) {
1209 switch (signing_) {
1210 case CYTypeNeutral: return $V("char");
1211 case CYTypeSigned: return $V("schar");
1212 case CYTypeUnsigned: return $V("uchar");
1213 default: _assert(false);
1214 }
1215 }
1216
1217 CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
1218 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
1219 }
1220
1221 CYStatement *CYTypeDefinition::Replace(CYContext &context) {
1222 CYIdentifier *identifier(typed_->identifier_);
1223 typed_->identifier_ = NULL;
1224 return $ CYLexical(false, $B1($B(identifier, $ CYTypeExpression(typed_))));
1225 }
1226
1227 CYTarget *CYTypeError::Replace(CYContext &context) {
1228 _assert(false);
1229 return NULL;
1230 }
1231
1232 CYTarget *CYTypeExpression::Replace(CYContext &context) {
1233 return typed_->Replace(context);
1234 }
1235
1236 CYTarget *CYTypeIntegral::Replace(CYContext &context) {
1237 bool u(signing_ == CYTypeUnsigned);
1238 switch (length_) {
1239 case 0: return $V(u ? "ushort" : "short");
1240 case 1: return $V(u ? "uint" : "int");
1241 case 2: return $V(u ? "ulong" : "long");
1242 case 3: return $V(u ? "ulonglong" : "longlong");
1243 default: _assert(false);
1244 }
1245 }
1246
1247 CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
1248 return Replace_(context, type);
1249 }
1250
1251 CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
1252 CYList<CYArgument> arguments(parameters_->Argument(context));
1253 if (variadic_)
1254 arguments->*$C_($ CYNull());
1255 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), arguments));
1256 }
1257
1258 CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
1259 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
1260 }
1261
1262 CYTarget *CYTypeReference::Replace(CYContext &context) {
1263 return $V($pool.strcat(name_->Word(), "$cy", NULL));
1264 }
1265
1266 CYTarget *CYTypeStruct::Replace(CYContext &context) {
1267 CYTarget *target(tail_->Replace(context));
1268 if (name_ != NULL)
1269 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1270 return target;
1271 }
1272
1273 CYTarget *CYTypeVariable::Replace(CYContext &context) {
1274 return $V(name_);
1275 }
1276
1277 CYTarget *CYTypeVoid::Replace(CYContext &context) {
1278 return $N1($V("Type"), $ CYString("v"));
1279 }
1280
1281 CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
1282 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
1283 }
1284
1285 CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
1286 return modifier_->Replace(context, specifier_->Replace(context));
1287 }
1288
1289 CYTypeFunctionWith *CYTypedIdentifier::Function() {
1290 CYTypeModifier *&modifier(CYGetLast(modifier_));
1291 if (modifier == NULL)
1292 return NULL;
1293
1294 CYTypeFunctionWith *function(modifier->Function());
1295 if (function == NULL)
1296 return NULL;
1297
1298 modifier = NULL;
1299 return function;
1300 }
1301
1302 CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
1303 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
1304 }
1305
1306 CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1307 return $ CYFunctionParameter($ CYBinding(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
1308 }
1309
1310 CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
1311 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
1312 }
1313
1314 CYForInitializer *CYVar::Replace(CYContext &context) {
1315 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierVariable))
1316 return $E(expression);
1317 return $ CYEmpty();
1318 }
1319
1320 CYTarget *CYVariable::Replace(CYContext &context) {
1321 name_ = name_->Replace(context, CYIdentifierGlobal);
1322 return this;
1323 }
1324
1325 CYFunctionParameter *CYVariable::Parameter() const {
1326 return $ CYFunctionParameter($ CYBinding(name_));
1327 }
1328
1329 CYStatement *CYWhile::Replace(CYContext &context) {
1330 context.Replace(test_);
1331 context.ReplaceAll(code_);
1332 return this;
1333 }
1334
1335 CYStatement *CYWith::Replace(CYContext &context) {
1336 context.Replace(scope_);
1337 CYScope scope(true, context);
1338 scope.Damage();
1339 context.ReplaceAll(code_);
1340 scope.Close(context);
1341 return this;
1342 }
1343
1344 CYExpression *CYWord::PropertyName(CYContext &context) {
1345 return $S(this);
1346 }