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