]> git.saurik.com Git - cycript.git/blob - Replace.cpp
Instance's toPointer() should return as CFTypeRef.
[cycript.git] / Replace.cpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 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 // XXX: this is a horrible hack but I'm a month over schedule :(
121 if (CYSubscriptMember *subscript = dynamic_cast<CYSubscriptMember *>(lhs_))
122 return $C2($M(subscript->object_, $S("$cys")), subscript->property_, rhs_);
123 context.Replace(lhs_);
124 context.Replace(rhs_);
125 return this;
126 }
127
128 CYTarget *CYAttemptMember::Replace(CYContext &context) {
129 CYIdentifier *value(context.Unique());
130
131 return $C1($F(NULL, $P1($B(value)), $$
132 ->* $ CYReturn($ CYCondition($V(value), $M($V(value), property_), $V(value)))
133 ), object_);
134 }
135
136 CYStatement *CYBlock::Return() {
137 CYImplicitReturn(code_);
138 return this;
139 }
140
141 CYStatement *CYBlock::Replace(CYContext &context) {
142 CYScope scope(true, context);
143 context.ReplaceAll(code_);
144 scope.Close(context);
145
146 if (code_ == NULL)
147 return $ CYEmpty();
148 return this;
149 }
150
151 CYStatement *CYBreak::Replace(CYContext &context) {
152 return this;
153 }
154
155 CYTarget *CYCall::Replace(CYContext &context) {
156 // XXX: this also is a horrible hack but I'm still a month over schedule :(
157 if (CYAttemptMember *member = dynamic_cast<CYAttemptMember *>(function_)) {
158 CYIdentifier *value(context.Unique());
159
160 return $C1($F(NULL, $P1($B(value)), $$
161 ->* $ CYReturn($ CYCondition($V(value), $C($M($V(value), member->property_), arguments_), $V(value)))
162 ), member->object_);
163 }
164
165 context.Replace(function_);
166 arguments_->Replace(context);
167 return this;
168 }
169
170 namespace cy {
171 namespace Syntax {
172
173 void Catch::Replace(CYContext &context) { $T()
174 CYScope scope(true, context);
175
176 name_ = name_->Replace(context, CYIdentifierCatch);
177
178 context.ReplaceAll(code_);
179 scope.Close(context);
180 }
181
182 } }
183
184 CYTarget *CYClassExpression::Replace(CYContext &context) {
185 CYBuilder builder;
186
187 CYIdentifier *super(context.Unique());
188
189 CYIdentifier *old(context.super_);
190 context.super_ = super;
191
192 CYIdentifier *constructor(context.Unique());
193 CYForEach (member, tail_->static_)
194 member->Replace(context, builder, $V(constructor), true);
195
196 CYIdentifier *prototype(context.Unique());
197 CYForEach (member, tail_->instance_)
198 member->Replace(context, builder, $V(prototype), true);
199
200 if (tail_->constructor_ == NULL)
201 tail_->constructor_ = $ CYFunctionExpression(NULL, NULL, NULL);
202 tail_->constructor_->name_ = name_;
203 tail_->constructor_ = CYSuperize(context, tail_->constructor_);
204
205 context.super_ = old;
206
207 return $C1($ CYFunctionExpression(NULL, $P($B(super)), $$
208 ->* $ CYVar($B1($B(constructor, tail_->constructor_)))
209 ->* $ CYVar($B1($B(prototype, $ CYFunctionExpression(NULL, NULL, NULL))))
210 ->* $E($ CYAssign($M($V(prototype), $S("prototype")), $M($V(super), $S("prototype"))))
211 ->* $E($ CYAssign($V(prototype), $N($V(prototype))))
212 ->* CYDefineProperty($V(prototype), $S("constructor"), false, false, $ CYPropertyValue($S("value"), $V(constructor)))
213 ->* $ CYVar(builder.bindings_)
214 ->* builder.statements_
215 ->* CYDefineProperty($V(constructor), $S("prototype"), false, false, $ CYPropertyValue($S("value"), $V(prototype)))
216 ->* $ CYReturn($V(constructor))
217 ), tail_->extends_ ?: $V($I("Object")));
218 }
219
220 CYStatement *CYClassStatement::Replace(CYContext &context) {
221 return $ CYVar($B1($B(name_, $ CYClassExpression(name_, tail_))));
222 }
223
224 void CYClause::Replace(CYContext &context) { $T()
225 context.Replace(value_);
226 context.ReplaceAll(code_);
227 next_->Replace(context);
228 }
229
230 CYExpression *CYCompound::Replace(CYContext &context) {
231 context.Replace(expression_);
232 context.Replace(next_);
233
234 if (CYCompound *compound = dynamic_cast<CYCompound *>(expression_)) {
235 expression_ = compound->expression_;
236 compound->expression_ = compound->next_;
237 compound->next_ = next_;
238 next_ = compound;
239 }
240
241 return this;
242 }
243
244 CYFunctionParameter *CYCompound::Parameter() const {
245 CYFunctionParameter *next(next_->Parameter());
246 if (next == NULL)
247 return NULL;
248
249 CYFunctionParameter *parameter(expression_->Parameter());
250 if (parameter == NULL)
251 return NULL;
252
253 parameter->SetNext(next);
254 return parameter;
255 }
256
257 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
258 CYFunctionParameter *next(next_->Parameters(context));
259 if (CYFunctionParameter *parameter = Parameter(context)) {
260 parameter->SetNext(next);
261 return parameter;
262 } else
263 return next;
264 }
265
266 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
267 return next_ == NULL ? statement : next_->Replace(context, statement);
268 }
269
270 CYExpression *CYComputed::PropertyName(CYContext &context) {
271 return expression_;
272 }
273
274 CYExpression *CYCondition::Replace(CYContext &context) {
275 context.Replace(test_);
276 context.Replace(true_);
277 context.Replace(false_);
278 return this;
279 }
280
281 void CYContext::NonLocal(CYStatement *&statements) {
282 CYContext &context(*this);
283
284 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
285 CYIdentifier *cye($I("$cye")->Replace(context, CYIdentifierGlobal));
286 CYIdentifier *unique(nextlocal_->identifier_->Replace(context, CYIdentifierGlobal));
287
288 CYStatement *declare(
289 $ CYVar($B1($B(unique, $ CYObject()))));
290
291 cy::Syntax::Catch *rescue(
292 $ cy::Syntax::Catch(cye, $$
293 ->* $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$
294 ->* $ CYReturn($M($V(cye), $S("$cyv"))))
295 ->* $ cy::Syntax::Throw($V(cye))));
296
297 context.Replace(declare);
298 rescue->Replace(context);
299
300 statements = $$
301 ->* declare
302 ->* $ cy::Syntax::Try(statements, rescue, NULL);
303 }
304 }
305
306 CYIdentifier *CYContext::Unique() {
307 return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL));
308 }
309
310 CYStatement *CYContinue::Replace(CYContext &context) {
311 return this;
312 }
313
314 CYStatement *CYDebugger::Replace(CYContext &context) {
315 return this;
316 }
317
318 CYTarget *CYBinding::Target(CYContext &context) {
319 return $V(identifier_);
320 }
321
322 CYAssignment *CYBinding::Replace(CYContext &context, CYIdentifierKind kind) {
323 identifier_ = identifier_->Replace(context, kind);
324
325 if (initializer_ == NULL)
326 return NULL;
327
328 CYAssignment *value($ CYAssign(Target(context), initializer_));
329 initializer_ = NULL;
330 return value;
331 }
332
333 CYExpression *CYBindings::Replace(CYContext &context, CYIdentifierKind kind) { $T(NULL)
334 CYAssignment *assignment(binding_->Replace(context, kind));
335 CYExpression *compound(next_->Replace(context, kind));
336
337 if (assignment != NULL)
338 if (compound == NULL)
339 compound = assignment;
340 else
341 compound = $ CYCompound(assignment, compound);
342 return compound;
343 }
344
345 CYFunctionParameter *CYBindings::Parameter(CYContext &context) { $T(NULL)
346 return $ CYFunctionParameter($ CYBinding(binding_->identifier_), next_->Parameter(context));
347 }
348
349 CYArgument *CYBindings::Argument(CYContext &context) { $T(NULL)
350 return $ CYArgument(binding_->initializer_, next_->Argument(context));
351 }
352
353 CYTarget *CYDirectMember::Replace(CYContext &context) {
354 context.Replace(object_);
355 context.Replace(property_);
356 return this;
357 }
358
359 CYStatement *CYDoWhile::Replace(CYContext &context) {
360 context.Replace(test_);
361 context.ReplaceAll(code_);
362 return this;
363 }
364
365 void CYElementSpread::Replace(CYContext &context) {
366 context.Replace(value_);
367 }
368
369 void CYElementValue::Replace(CYContext &context) {
370 context.Replace(value_);
371 }
372
373 CYForInitializer *CYEmpty::Replace(CYContext &context) {
374 return NULL;
375 }
376
377 CYTarget *CYEncodedType::Replace(CYContext &context) {
378 return typed_->Replace(context);
379 }
380
381 CYTarget *CYEval::Replace(CYContext &context) {
382 context.scope_->Damage();
383 if (arguments_ != NULL)
384 arguments_->value_ = $C1($M($V("Cycript"), $S("compile")), arguments_->value_);
385 return $C($V("eval"), arguments_);
386 }
387
388 CYStatement *CYExpress::Return() {
389 return $ CYReturn(expression_);
390 }
391
392 CYForInitializer *CYExpress::Replace(CYContext &context) {
393 context.Replace(expression_);
394 return this;
395 }
396
397 CYTarget *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
398 return $C1(this, value);
399 }
400
401 CYFunctionParameter *CYExpression::Parameter() const {
402 return NULL;
403 }
404
405 CYTarget *CYExtend::Replace(CYContext &context) {
406 return object_.Replace(context, lhs_);
407 }
408
409 CYStatement *CYExternalDefinition::Replace(CYContext &context) {
410 return $E($ CYAssign($V(name_), $ CYExternalExpression(abi_, type_, name_)));
411 }
412
413 CYTarget *CYExternalExpression::Replace(CYContext &context) {
414 CYExpression *expression(name_->Number(context));
415 if (expression == NULL)
416 expression = $C2($V("dlsym"), $V("RTLD_DEFAULT"), name_->PropertyName(context));
417 return $C1(type_->Replace(context), expression);
418 }
419
420 CYNumber *CYFalse::Number(CYContext &context) {
421 return $D(0);
422 }
423
424 CYString *CYFalse::String(CYContext &context) {
425 return $S("false");
426 }
427
428 CYExpression *CYFatArrow::Replace(CYContext &context) {
429 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
430 function->this_.SetNext(context.this_);
431 return function;
432 }
433
434 void CYFinally::Replace(CYContext &context) { $T()
435 CYScope scope(true, context);
436 context.ReplaceAll(code_);
437 scope.Close(context);
438 }
439
440 CYStatement *CYFor::Replace(CYContext &context) {
441 CYScope outer(true, context);
442 context.Replace(initializer_);
443
444 context.Replace(test_);
445
446 {
447 CYScope inner(true, context);
448 context.ReplaceAll(code_);
449 inner.Close(context);
450 }
451
452 context.Replace(increment_);
453
454 outer.Close(context);
455 return this;
456 }
457
458 CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
459 if (value == NULL) {
460 if (binding_->initializer_ == NULL)
461 return NULL;
462 value = binding_->initializer_;
463 }
464
465 return $ CYLexical(constant_, $B1($ CYBinding(binding_->identifier_, value)));
466 }
467
468 CYTarget *CYForLexical::Replace(CYContext &context) {
469 _assert(binding_->Replace(context, CYIdentifierLexical) == NULL);
470 return binding_->Target(context);
471 }
472
473 CYStatement *CYForIn::Replace(CYContext &context) {
474 CYScope scope(true, context);
475 context.Replace(initializer_);
476 context.Replace(iterable_);
477 context.ReplaceAll(code_);
478 scope.Close(context);
479 return this;
480 }
481
482 CYStatement *CYForInitialized::Replace(CYContext &context) {
483 CYAssignment *assignment(binding_->Replace(context, CYIdentifierVariable));
484 return $ CYBlock($$
485 ->* (assignment == NULL ? NULL : $ CYExpress(assignment))
486 ->* $ CYForIn(binding_->Target(context), iterable_, code_));
487 }
488
489 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
490 return $ CYFunctionParameter(binding_);
491 }
492
493 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
494 return $ CYForIn(binding_->Target(context), iterable_, CYComprehension::Replace(context, statement));
495 }
496
497 CYStatement *CYForOf::Replace(CYContext &context) {
498 CYIdentifier *item(context.Unique()), *list(context.Unique());
499
500 return $ CYBlock($$
501 ->* initializer_->Initialize(context, NULL)
502 ->* $ CYLexical(false, $B2($B(list, iterable_), $B(item)))
503 ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
504 ->* initializer_->Initialize(context, $M($V(list), $V(item)))
505 ->* code_
506 )));
507 }
508
509 CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
510 return $ CYFunctionParameter(binding_);
511 }
512
513 CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
514 CYIdentifier *cys(context.Unique());
515
516 return $ CYBlock($$
517 ->* $ CYLexical(false, $B1($B(cys, iterable_)))
518 ->* $ CYForIn(binding_->Target(context), $V(cys), $ CYBlock($$
519 ->* $E($ CYAssign(binding_->Target(context), $M($V(cys), binding_->Target(context))))
520 ->* CYComprehension::Replace(context, statement)
521 )));
522 }
523
524 CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
525 if (value == NULL) {
526 if (binding_->initializer_ == NULL)
527 return NULL;
528 value = binding_->initializer_;
529 }
530
531 return $ CYVar($B1($ CYBinding(binding_->identifier_, value)));
532 }
533
534 CYTarget *CYForVariable::Replace(CYContext &context) {
535 _assert(binding_->Replace(context, CYIdentifierVariable) == NULL);
536 return binding_->Target(context);
537 }
538
539 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
540 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
541 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
542
543 void CYFunction::Replace(CYContext &context) {
544 CYThisScope *_this(context.this_);
545 context.this_ = &this_;
546 context.this_ = CYGetLast(context.this_);
547
548 CYIdentifier *super(context.super_);
549 context.super_ = super_;
550
551 CYNonLocal *nonlocal(context.nonlocal_);
552 CYNonLocal *nextlocal(context.nextlocal_);
553
554 bool localize;
555 if (nonlocal_ != NULL) {
556 localize = false;
557 context.nonlocal_ = nonlocal_;
558 } else {
559 localize = true;
560 nonlocal_ = $ CYNonLocal();
561 context.nextlocal_ = nonlocal_;
562 }
563
564 CYScope scope(!localize, context);
565
566 $I("arguments")->Replace(context, CYIdentifierMagic);
567
568 parameters_->Replace(context, code_);
569
570 context.ReplaceAll(code_);
571
572 if (implicit_)
573 CYImplicitReturn(code_);
574
575 if (CYIdentifier *identifier = this_.identifier_) {
576 context.scope_->Declare(context, identifier, CYIdentifierVariable);
577 code_ = $$
578 ->* $E($ CYAssign($V(identifier), $ CYThis()))
579 ->* code_;
580 }
581
582 if (localize)
583 context.NonLocal(code_);
584
585 context.nextlocal_ = nextlocal;
586 context.nonlocal_ = nonlocal;
587
588 context.super_ = super;
589 context.this_ = _this;
590
591 scope.Close(context, code_);
592 }
593
594 CYTarget *CYFunctionExpression::Replace(CYContext &context) {
595 CYScope scope(false, context);
596 if (name_ != NULL)
597 name_ = name_->Replace(context, CYIdentifierOther);
598
599 CYFunction::Replace(context);
600 scope.Close(context);
601 return this;
602 }
603
604 void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
605 CYAssignment *assignment(binding_->Replace(context, CYIdentifierArgument));
606
607 next_->Replace(context, statements);
608
609 if (assignment != NULL)
610 statements = $$
611 ->* $ CYIf($ CYIdentical($ CYTypeOf(binding_->Target(context)), $S("undefined")), $$
612 ->* $E(assignment))
613 ->* statements;
614 }
615
616 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
617 name_ = name_->Replace(context, CYIdentifierOther);
618 CYFunction::Replace(context);
619 return this;
620 }
621
622 CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
623 if (next_ == this)
624 return this;
625 if (next_ != NULL)
626 return next_->Replace(context, kind);
627 next_ = context.scope_->Declare(context, this, kind)->identifier_;
628 return next_;
629 }
630
631 CYStatement *CYIf::Return() {
632 CYImplicitReturn(true_);
633 CYImplicitReturn(false_);
634 return this;
635 }
636
637 CYStatement *CYIf::Replace(CYContext &context) {
638 context.Replace(test_);
639 context.ReplaceAll(true_);
640 context.ReplaceAll(false_);
641 return this;
642 }
643
644 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
645 return NULL;
646 }
647
648 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
649 return $ CYIf(test_, CYComprehension::Replace(context, statement));
650 }
651
652 CYStatement *CYImport::Replace(CYContext &context) {
653 return $ CYVar($B1($B($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
654 }
655
656 CYStatement *CYImportDeclaration::Replace(CYContext &context) {
657 CYIdentifier *module(context.Unique());
658
659 CYList<CYStatement> statements;
660 CYForEach (specifier, specifiers_)
661 statements->*specifier->Replace(context, module);
662
663 return $ CYBlock($$
664 ->* $ CYLexical(false, $B1($B(module, $C1($V("require"), module_))))
665 ->* statements);
666 }
667
668 CYStatement *CYImportSpecifier::Replace(CYContext &context, CYIdentifier *module) {
669 binding_ = binding_->Replace(context, CYIdentifierLexical);
670
671 CYExpression *import($V(module));
672 if (name_ != NULL)
673 import = $M(import, $S(name_));
674 return $E($ CYAssign($V(binding_), import));
675 }
676
677 CYTarget *CYIndirect::Replace(CYContext &context) {
678 return $M(rhs_, $S("$cyi"));
679 }
680
681 CYTarget *CYIndirectMember::Replace(CYContext &context) {
682 return $M($ CYIndirect(object_), property_);
683 }
684
685 CYExpression *CYInfix::Replace(CYContext &context) {
686 context.Replace(lhs_);
687 context.Replace(rhs_);
688 return this;
689 }
690
691 CYStatement *CYLabel::Replace(CYContext &context) {
692 context.Replace(statement_);
693 return this;
694 }
695
696 CYTarget *CYLambda::Replace(CYContext &context) {
697 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
698 }
699
700 CYForInitializer *CYLexical::Replace(CYContext &context) {
701 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierLexical))
702 return $E(expression);
703 return $ CYEmpty();
704 }
705
706 CYFunctionExpression *CYMethod::Constructor() {
707 return NULL;
708 }
709
710 void CYMethod::Replace(CYContext &context) {
711 CYFunction::Replace(context);
712 }
713
714 CYString *CYModule::Replace(CYContext &context, const char *separator) const {
715 if (next_ == NULL)
716 return $ CYString(part_);
717 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
718 }
719
720 CYExpression *CYMultiply::Replace(CYContext &context) {
721 CYInfix::Replace(context);
722
723 if (CYNumber *lhn = lhs_->Number(context))
724 if (CYNumber *rhn = rhs_->Number(context))
725 return $D(lhn->Value() * rhn->Value());
726
727 return this;
728 }
729
730 namespace cy {
731 namespace Syntax {
732
733 CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
734 CYSetLast(arguments_) = $ CYArgument(value);
735 return this;
736 }
737
738 CYTarget *New::Replace(CYContext &context) {
739 context.Replace(constructor_);
740 arguments_->Replace(context);
741 return this;
742 }
743
744 } }
745
746 CYNumber *CYNull::Number(CYContext &context) {
747 return $D(0);
748 }
749
750 CYString *CYNull::String(CYContext &context) {
751 return $S("null");
752 }
753
754 CYNumber *CYNumber::Number(CYContext &context) {
755 return this;
756 }
757
758 CYString *CYNumber::String(CYContext &context) {
759 // XXX: there is a precise algorithm for this
760 return $S($pool.sprintf(24, "%.17g", Value()));
761 }
762
763 CYExpression *CYNumber::PropertyName(CYContext &context) {
764 return String(context);
765 }
766
767 CYTarget *CYObject::Replace(CYContext &context, CYTarget *seed) {
768 CYBuilder builder;
769 if (properties_ != NULL)
770 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), seed != this);
771
772 if (builder) {
773 return $C1($M($ CYFunctionExpression(NULL, builder.bindings_->Parameter(context),
774 builder.statements_
775 ->* $ CYReturn($ CYThis())
776 ), $S("call")), seed, builder.bindings_->Argument(context));
777 }
778
779 CYForEach (property, properties_)
780 property->Replace(context);
781 return seed;
782 }
783
784 CYTarget *CYObject::Replace(CYContext &context) {
785 return Replace(context, this);
786 }
787
788 CYTarget *CYParenthetical::Replace(CYContext &context) {
789 // XXX: return expression_;
790 context.Replace(expression_);
791 return this;
792 }
793
794 CYExpression *CYPostfix::Replace(CYContext &context) {
795 context.Replace(lhs_);
796 return this;
797 }
798
799 CYExpression *CYPrefix::Replace(CYContext &context) {
800 context.Replace(rhs_);
801 return this;
802 }
803
804 CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
805 update |= Update();
806 if (update)
807 Replace(context, builder, self, false);
808 if (next_ != NULL)
809 next_ = next_->ReplaceAll(context, builder, self, update);
810 return update ? next_ : this;
811 }
812
813 void CYProperty::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect) {
814 CYExpression *name(name_->PropertyName(context));
815 if (name_->Computed()) {
816 CYIdentifier *unique(context.Unique());
817 builder.bindings_
818 ->* $B1($B(unique, name));
819 name = $V(unique);
820 }
821
822 Replace(context, builder, self, name, protect);
823 }
824
825 bool CYProperty::Update() const {
826 return name_->Computed();
827 }
828
829 void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
830 CYIdentifier *unique(context.Unique());
831 builder.bindings_
832 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
833 builder.statements_
834 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
835 }
836
837 CYFunctionExpression *CYPropertyMethod::Constructor() {
838 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
839 }
840
841 void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
842 CYIdentifier *unique(context.Unique());
843 builder.bindings_
844 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
845 builder.statements_
846 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
847 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
848 }
849
850 bool CYPropertyMethod::Update() const {
851 return true;
852 }
853
854 void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
855 CYIdentifier *unique(context.Unique());
856 builder.bindings_
857 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
858 builder.statements_
859 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
860 }
861
862 void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
863 _assert(!protect);
864 CYIdentifier *unique(context.Unique());
865 builder.bindings_
866 ->* $B1($B(unique, value_));
867 builder.statements_
868 ->* $E($ CYAssign($M(self, name), $V(unique)));
869 }
870
871 void CYPropertyValue::Replace(CYContext &context) {
872 context.Replace(value_);
873 }
874
875 void CYScript::Replace(CYContext &context) {
876 CYScope scope(false, context);
877 context.scope_->Damage();
878
879 context.nextlocal_ = $ CYNonLocal();
880 context.ReplaceAll(code_);
881 context.NonLocal(code_);
882
883 scope.Close(context, code_);
884
885 unsigned offset(0);
886
887 for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
888 const char *name;
889 if (context.options_.verbose_)
890 name = $pool.strcat("$", $pool.itoa(offset++), NULL);
891 else {
892 char id[8];
893 id[7] = '\0';
894
895 id:
896 unsigned position(7), local(offset++ + 1);
897
898 do {
899 unsigned index(local % (sizeof(MappingSet) - 1));
900 local /= sizeof(MappingSet) - 1;
901 id[--position] = MappingSet[index];
902 } while (local != 0);
903
904 if (scope.Lookup(context, id + position) != NULL)
905 goto id;
906 // XXX: at some point, this could become a keyword
907
908 name = $pool.strmemdup(id + position, 7 - position);
909 }
910
911 CYIdentifier *identifier(*i);
912 _assert(identifier->next_ == identifier);
913 identifier->next_ = $I(name);
914 }
915 }
916
917 CYTarget *CYResolveMember::Replace(CYContext &context) {
918 return $M($M(object_, $S("$cyr")), property_);
919 }
920
921 CYStatement *CYReturn::Replace(CYContext &context) {
922 if (context.nonlocal_ != NULL) {
923 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
924 return $ cy::Syntax::Throw($ CYObject(
925 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
926 ));
927 }
928
929 context.Replace(value_);
930 return this;
931 }
932
933 CYTarget *CYRubyBlock::Replace(CYContext &context) {
934 return lhs_->AddArgument(context, proc_->Replace(context));
935 }
936
937 CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
938 return Replace(context)->AddArgument(context, value);
939 }
940
941 CYTarget *CYRubyProc::Replace(CYContext &context) {
942 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
943 function = CYNonLocalize(context, function);
944 function->implicit_ = true;
945 return function;
946 }
947
948 CYScope::CYScope(bool transparent, CYContext &context) :
949 transparent_(transparent),
950 parent_(context.scope_),
951 damaged_(false),
952 shadow_(NULL),
953 internal_(NULL)
954 {
955 _assert(!transparent_ || parent_ != NULL);
956 context.scope_ = this;
957 }
958
959 void CYScope::Damage() {
960 damaged_ = true;
961 if (parent_ != NULL)
962 parent_->Damage();
963 }
964
965 CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
966 CYForEach (i, internal_)
967 if (strcmp(i->identifier_->Word(), word) == 0)
968 return i;
969 return NULL;
970 }
971
972 CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
973 return Lookup(context, identifier->Word());
974 }
975
976 CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
977 _assert(identifier->next_ == NULL || identifier->next_ == identifier);
978
979 CYIdentifierFlags *existing(Lookup(context, identifier));
980 if (existing == NULL)
981 internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
982 ++internal_->count_;
983 if (existing == NULL)
984 return internal_;
985
986 if (kind == CYIdentifierGlobal);
987 else if (existing->kind_ == CYIdentifierGlobal || existing->kind_ == CYIdentifierMagic)
988 existing->kind_ = kind;
989 else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
990 _assert(false);
991 else if (transparent_ && existing->kind_ == CYIdentifierArgument && kind == CYIdentifierVariable)
992 _assert(false);
993 // XXX: throw new SyntaxError() instead of these asserts
994
995 return existing;
996 }
997
998 void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
999 _assert(flags->identifier_->next_ == flags->identifier_);
1000 CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
1001 flags->identifier_->next_ = existing->identifier_;
1002
1003 existing->count_ += flags->count_;
1004 if (existing->offset_ < flags->offset_)
1005 existing->offset_ = flags->offset_;
1006 }
1007
1008 void CYScope::Close(CYContext &context, CYStatement *&statements) {
1009 Close(context);
1010
1011 CYList<CYBindings> bindings;
1012
1013 CYForEach (i, internal_)
1014 if (i->kind_ == CYIdentifierVariable)
1015 bindings
1016 ->* $ CYBindings($ CYBinding(i->identifier_));
1017
1018 if (bindings) {
1019 CYVar *var($ CYVar(bindings));
1020 var->SetNext(statements);
1021 statements = var;
1022 }
1023 }
1024
1025 void CYScope::Close(CYContext &context) {
1026 context.scope_ = parent_;
1027
1028 CYForEach (i, internal_) {
1029 _assert(i->identifier_->next_ == i->identifier_);
1030 switch (i->kind_) {
1031 case CYIdentifierLexical: {
1032 if (!damaged_) {
1033 CYIdentifier *replace(context.Unique());
1034 replace->next_ = replace;
1035 i->identifier_->next_ = replace;
1036 i->identifier_ = replace;
1037 }
1038
1039 if (!transparent_)
1040 i->kind_ = CYIdentifierVariable;
1041 else
1042 parent_->Declare(context, i->identifier_, CYIdentifierVariable);
1043 } break;
1044
1045 case CYIdentifierVariable: {
1046 if (transparent_) {
1047 parent_->Declare(context, i->identifier_, i->kind_);
1048 i->kind_ = CYIdentifierGlobal;
1049 }
1050 } break;
1051 default:; } }
1052
1053 if (damaged_)
1054 return;
1055
1056 typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
1057 CYIdentifierOffsetMap offsets;
1058
1059 CYForEach (i, internal_) {
1060 _assert(i->identifier_->next_ == i->identifier_);
1061 switch (i->kind_) {
1062 case CYIdentifierArgument:
1063 case CYIdentifierVariable:
1064 offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
1065 break;
1066 default:; } }
1067
1068 unsigned offset(0);
1069
1070 for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
1071 if (offset < i->first)
1072 offset = i->first;
1073 CYIdentifier *identifier(i->second);
1074
1075 if (offset >= context.replace_.size())
1076 context.replace_.resize(offset + 1, NULL);
1077 CYIdentifier *&replace(context.replace_[offset++]);
1078
1079 if (replace == NULL)
1080 replace = identifier;
1081 else {
1082 _assert(replace->next_ == replace);
1083 identifier->next_ = replace;
1084 }
1085 }
1086
1087 if (parent_ == NULL)
1088 return;
1089
1090 CYForEach (i, internal_) {
1091 switch (i->kind_) {
1092 case CYIdentifierGlobal: {
1093 if (i->offset_ < offset)
1094 i->offset_ = offset;
1095 parent_->Merge(context, i);
1096 } break;
1097 default:; } }
1098 }
1099
1100 CYTarget *CYSubscriptMember::Replace(CYContext &context) {
1101 return $C1($M(object_, $S("$cyg")), property_);
1102 }
1103
1104 CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
1105 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
1106 }
1107
1108 CYStatement *CYStatement::Return() {
1109 return this;
1110 }
1111
1112 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1113 size_t size(size_ + rhs->size_);
1114 char *value($ char[size + 1]);
1115 memcpy(value, value_, size_);
1116 memcpy(value + size_, rhs->value_, rhs->size_);
1117 value[size] = '\0';
1118 return $S(value, size);
1119 }
1120
1121 CYIdentifier *CYString::Identifier() const {
1122 if (const char *word = Word())
1123 return $ CYIdentifier(word);
1124 return NULL;
1125 }
1126
1127 CYNumber *CYString::Number(CYContext &context) {
1128 // XXX: there is a precise algorithm for this
1129 return NULL;
1130 }
1131
1132 CYExpression *CYString::PropertyName(CYContext &context) {
1133 return this;
1134 }
1135
1136 CYString *CYString::String(CYContext &context) {
1137 return this;
1138 }
1139
1140 CYStatement *CYStructDefinition::Replace(CYContext &context) {
1141 CYTarget *target(tail_->Replace(context));
1142 if (name_ != NULL)
1143 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1144 return $ CYLexical(false, $B1($B($I($pool.strcat(name_->Word(), "$cy", NULL)), target)));
1145 }
1146
1147 CYTarget *CYStructTail::Replace(CYContext &context) {
1148 CYList<CYElementValue> types;
1149 CYList<CYElementValue> names;
1150
1151 CYForEach (field, fields_) {
1152 types->*$ CYElementValue(field->type_->Replace(context));
1153
1154 CYExpression *name;
1155 if (field->name_ == NULL)
1156 name = NULL;
1157 else
1158 name = field->name_->PropertyName(context);
1159 names->*$ CYElementValue(name);
1160 }
1161
1162 return $N2($V("Type"), $ CYArray(types), $ CYArray(names));
1163 }
1164
1165 CYTarget *CYSuperAccess::Replace(CYContext &context) {
1166 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1167 }
1168
1169 CYTarget *CYSuperCall::Replace(CYContext &context) {
1170 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1171 }
1172
1173 CYTarget *CYSymbol::Replace(CYContext &context) {
1174 return $C1($M($V("Symbol"), $S("for")), $S(name_));
1175 }
1176
1177 CYStatement *CYSwitch::Replace(CYContext &context) {
1178 context.Replace(value_);
1179 clauses_->Replace(context);
1180 return this;
1181 }
1182
1183 CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
1184 if (value == NULL)
1185 return NULL;
1186 return $E($ CYAssign(this, value));
1187 }
1188
1189 CYTarget *CYTemplate::Replace(CYContext &context) {
1190 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
1191 }
1192
1193 CYString *CYTemplate::String(CYContext &context) {
1194 // XXX: implement this over local concat
1195 if (spans_ != NULL)
1196 return NULL;
1197 return string_;
1198 }
1199
1200 CYTarget *CYThis::Replace(CYContext &context) {
1201 if (context.this_ != NULL)
1202 return $V(context.this_->Identifier(context));
1203 return this;
1204 }
1205
1206 namespace cy {
1207 namespace Syntax {
1208
1209 CYStatement *Throw::Replace(CYContext &context) {
1210 context.Replace(value_);
1211 return this;
1212 }
1213
1214 } }
1215
1216 CYTarget *CYTrivial::Replace(CYContext &context) {
1217 return this;
1218 }
1219
1220 CYNumber *CYTrue::Number(CYContext &context) {
1221 return $D(1);
1222 }
1223
1224 CYString *CYTrue::String(CYContext &context) {
1225 return $S("true");
1226 }
1227
1228 namespace cy {
1229 namespace Syntax {
1230
1231 CYStatement *Try::Replace(CYContext &context) {
1232 CYScope scope(true, context);
1233 context.ReplaceAll(code_);
1234 scope.Close(context);
1235
1236 catch_->Replace(context);
1237 finally_->Replace(context);
1238 return this;
1239 }
1240
1241 } }
1242
1243 CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
1244 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
1245 }
1246
1247 CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
1248 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1249 }
1250
1251 CYTarget *CYTypeCharacter::Replace(CYContext &context) {
1252 switch (signing_) {
1253 case CYTypeNeutral: return $V("char");
1254 case CYTypeSigned: return $V("schar");
1255 case CYTypeUnsigned: return $V("uchar");
1256 default: _assert(false);
1257 }
1258 }
1259
1260 CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
1261 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
1262 }
1263
1264 CYStatement *CYTypeDefinition::Replace(CYContext &context) {
1265 return $ CYLexical(false, $B1($B(name_, $ CYTypeExpression(type_))));
1266 }
1267
1268 CYTarget *CYTypeEnum::Replace(CYContext &context) {
1269 CYList<CYProperty> properties;
1270 CYForEach (constant, constants_)
1271 properties->*$ CYPropertyValue($S(constant->name_->Word()), constant->value_);
1272 CYObject *constants($ CYObject(properties));
1273
1274 if (specifier_ == NULL)
1275 return $N1($V("Type"), constants);
1276 else
1277 return $C1($M(specifier_->Replace(context), $S("enumFor")), constants);
1278 }
1279
1280 CYTarget *CYTypeError::Replace(CYContext &context) {
1281 _assert(false);
1282 return NULL;
1283 }
1284
1285 CYTarget *CYTypeExpression::Replace(CYContext &context) {
1286 return typed_->Replace(context);
1287 }
1288
1289 CYTarget *CYTypeFloating::Replace(CYContext &context) {
1290 switch (length_) {
1291 case 0: return $V("float");
1292 case 1: return $V("double");
1293 case 2: return $V("longdouble");
1294 default: _assert(false);
1295 }
1296 }
1297
1298 CYTarget *CYTypeInt128::Replace(CYContext &context) {
1299 return $V(signing_ == CYTypeUnsigned ? "uint128" : "int128");
1300 }
1301
1302 CYTarget *CYTypeIntegral::Replace(CYContext &context) {
1303 bool u(signing_ == CYTypeUnsigned);
1304 switch (length_) {
1305 case 0: return $V(u ? "ushort" : "short");
1306 case 1: return $V(u ? "uint" : "int");
1307 case 2: return $V(u ? "ulong" : "long");
1308 case 3: return $V(u ? "ulonglong" : "longlong");
1309 default: _assert(false);
1310 }
1311 }
1312
1313 CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
1314 return Replace_(context, type);
1315 }
1316
1317 CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
1318 CYList<CYArgument> arguments(parameters_->Argument(context));
1319 if (variadic_)
1320 arguments->*$C_($ CYNull());
1321 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), arguments));
1322 }
1323
1324 CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
1325 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
1326 }
1327
1328 CYTarget *CYTypeReference::Replace(CYContext &context) {
1329 const char *prefix;
1330 switch (kind_) {
1331 case CYTypeReferenceStruct: prefix = "$cys"; break;
1332 case CYTypeReferenceEnum: prefix = "$cye"; break;
1333 default: _assert(false);
1334 }
1335
1336 return $V($pool.strcat(prefix, name_->Word(), NULL));
1337 }
1338
1339 CYTarget *CYTypeStruct::Replace(CYContext &context) {
1340 CYTarget *target(tail_->Replace(context));
1341 if (name_ != NULL)
1342 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1343 return target;
1344 }
1345
1346 CYTarget *CYTypeVariable::Replace(CYContext &context) {
1347 return $V(name_);
1348 }
1349
1350 CYTarget *CYTypeVoid::Replace(CYContext &context) {
1351 return $N1($V("Type"), $ CYString("v"));
1352 }
1353
1354 CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
1355 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
1356 }
1357
1358 CYTarget *CYType::Replace(CYContext &context) {
1359 return modifier_->Replace(context, specifier_->Replace(context));
1360 }
1361
1362 CYTypeFunctionWith *CYType::Function() {
1363 CYTypeModifier *&modifier(CYGetLast(modifier_));
1364 if (modifier == NULL)
1365 return NULL;
1366
1367 CYTypeFunctionWith *function(modifier->Function());
1368 if (function == NULL)
1369 return NULL;
1370
1371 modifier = NULL;
1372 return function;
1373 }
1374
1375 CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
1376 return $ CYArgument(type_->Replace(context), next_->Argument(context));
1377 }
1378
1379 CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1380 return $ CYFunctionParameter($ CYBinding(name_ ?: context.Unique()), next_->Parameters(context));
1381 }
1382
1383 CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
1384 return next_->TypeSignature(context, $ CYAdd(prefix, type_->Replace(context)));
1385 }
1386
1387 CYForInitializer *CYVar::Replace(CYContext &context) {
1388 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierVariable))
1389 return $E(expression);
1390 return $ CYEmpty();
1391 }
1392
1393 CYTarget *CYVariable::Replace(CYContext &context) {
1394 name_ = name_->Replace(context, CYIdentifierGlobal);
1395 return this;
1396 }
1397
1398 CYFunctionParameter *CYVariable::Parameter() const {
1399 return $ CYFunctionParameter($ CYBinding(name_));
1400 }
1401
1402 CYStatement *CYWhile::Replace(CYContext &context) {
1403 context.Replace(test_);
1404 context.ReplaceAll(code_);
1405 return this;
1406 }
1407
1408 CYStatement *CYWith::Replace(CYContext &context) {
1409 context.Replace(scope_);
1410 CYScope scope(true, context);
1411 scope.Damage();
1412 context.ReplaceAll(code_);
1413 scope.Close(context);
1414 return this;
1415 }
1416
1417 CYExpression *CYWord::PropertyName(CYContext &context) {
1418 return $S(this);
1419 }