1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
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.
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/>.
25 #include "Replace.hpp"
28 CYFunctionExpression
*CYNonLocalize(CYContext
&context
, CYFunctionExpression
*function
) {
29 function
->nonlocal_
= context
.nextlocal_
;
33 CYFunctionExpression
*CYSuperize(CYContext
&context
, CYFunctionExpression
*function
) {
34 function
->super_
= context
.super_
;
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
)
45 static void CYImplicitReturn(CYStatement
*&code
) {
46 if (CYStatement
*&last
= CYGetLast(code
))
47 last
= last
->Return();
50 CYExpression
*CYAdd::Replace(CYContext
&context
) {
51 CYInfix::Replace(context
);
53 CYString
*lhs(dynamic_cast<CYString
*>(lhs_
));
54 CYString
*rhs(dynamic_cast<CYString
*>(rhs_
));
56 if (lhs
!= NULL
|| rhs
!= NULL
) {
58 lhs
= lhs_
->String(context
);
61 } else if (rhs
== NULL
) {
62 rhs
= rhs_
->String(context
);
67 return lhs
->Concat(context
, rhs
);
70 if (CYNumber
*lhn
= lhs_
->Number(context
))
71 if (CYNumber
*rhn
= rhs_
->Number(context
))
72 return $
D(lhn
->Value() + rhn
->Value());
77 CYExpression
*CYAddressOf::Replace(CYContext
&context
) {
78 return $
C0($
M(rhs_
, $
S("$cya")));
81 CYTarget
*CYApply::AddArgument(CYContext
&context
, CYExpression
*value
) {
82 CYArgument
**argument(&arguments_
);
83 while (*argument
!= NULL
)
84 argument
= &(*argument
)->next_
;
85 *argument
= $
CYArgument(value
);
89 CYArgument
*CYArgument::Replace(CYContext
&context
) { $
T(NULL
)
90 context
.Replace(value_
);
91 next_
= next_
->Replace(context
);
103 CYTarget
*CYArray::Replace(CYContext
&context
) {
104 CYForEach (element
, elements_
)
105 element
->Replace(context
);
109 CYTarget
*CYArrayComprehension::Replace(CYContext
&context
) {
110 CYIdentifier
*cyv(context
.Unique());
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
))
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_
);
128 CYStatement
*CYBlock::Return() {
129 CYImplicitReturn(code_
);
133 CYStatement
*CYBlock::Replace(CYContext
&context
) {
134 CYScope
scope(true, context
);
135 context
.ReplaceAll(code_
);
136 scope
.Close(context
);
143 CYStatement
*CYBreak::Replace(CYContext
&context
) {
147 CYTarget
*CYCall::Replace(CYContext
&context
) {
148 context
.Replace(function_
);
149 arguments_
->Replace(context
);
156 void Catch::Replace(CYContext
&context
) { $
T()
157 CYScope
scope(true, context
);
159 name_
= name_
->Replace(context
, CYIdentifierCatch
);
161 context
.ReplaceAll(code_
);
162 scope
.Close(context
);
167 CYTarget
*CYClassExpression::Replace(CYContext
&context
) {
170 CYIdentifier
*super(context
.Unique());
172 CYIdentifier
*old(context
.super_
);
173 context
.super_
= super
;
175 CYIdentifier
*constructor(context
.Unique());
176 CYForEach (member
, tail_
->static_
)
177 member
->Replace(context
, builder
, $
V(constructor
), true);
179 CYIdentifier
*prototype(context
.Unique());
180 CYForEach (member
, tail_
->instance_
)
181 member
->Replace(context
, builder
, $
V(prototype
), true);
183 if (tail_
->constructor_
== NULL
)
184 tail_
->constructor_
= $
CYFunctionExpression(NULL
, NULL
, NULL
);
185 tail_
->constructor_
= CYSuperize(context
, tail_
->constructor_
);
187 context
.super_
= old
;
189 return $
C1($
CYFunctionExpression(NULL
, $
P($
B(super
)), $$
190 ->* $
CYVar($
B1($
B(constructor
, tail_
->constructor_
)))
191 ->* $
CYVar($
B1($
B(prototype
, $
CYFunctionExpression(NULL
, NULL
, NULL
))))
192 ->* $
E($
CYAssign($
M($
V(prototype
), $
S("prototype")), $
M($
V(super
), $
S("prototype"))))
193 ->* $
E($
CYAssign($
V(prototype
), $
N($
V(prototype
))))
194 ->* CYDefineProperty($
V(prototype
), $
S("constructor"), false, false, $
CYPropertyValue($
S("value"), $
V(constructor
)))
195 ->* $
CYVar(builder
.bindings_
)
196 ->* builder
.statements_
197 ->* CYDefineProperty($
V(constructor
), $
S("prototype"), false, false, $
CYPropertyValue($
S("value"), $
V(prototype
)))
198 ->* $
CYReturn($
V(constructor
))
199 ), tail_
->extends_
?: $
V($
I("Object")));
202 CYStatement
*CYClassStatement::Replace(CYContext
&context
) {
203 return $
CYVar($
B1($
B(name_
, $
CYClassExpression(name_
, tail_
))));
206 void CYClause::Replace(CYContext
&context
) { $
T()
207 context
.Replace(value_
);
208 context
.ReplaceAll(code_
);
209 next_
->Replace(context
);
212 CYExpression
*CYCompound::Replace(CYContext
&context
) {
213 context
.Replace(expression_
);
214 context
.Replace(next_
);
216 if (CYCompound
*compound
= dynamic_cast<CYCompound
*>(expression_
)) {
217 expression_
= compound
->expression_
;
218 compound
->expression_
= compound
->next_
;
219 compound
->next_
= next_
;
226 CYFunctionParameter
*CYCompound::Parameter() const {
227 CYFunctionParameter
*next(next_
->Parameter());
231 CYFunctionParameter
*parameter(expression_
->Parameter());
232 if (parameter
== NULL
)
235 parameter
->SetNext(next
);
239 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
240 CYFunctionParameter
*next(next_
->Parameters(context
));
241 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
242 parameter
->SetNext(next
);
248 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
249 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
252 CYExpression
*CYComputed::PropertyName(CYContext
&context
) {
256 CYExpression
*CYCondition::Replace(CYContext
&context
) {
257 context
.Replace(test_
);
258 context
.Replace(true_
);
259 context
.Replace(false_
);
263 void CYContext::NonLocal(CYStatement
*&statements
) {
264 CYContext
&context(*this);
266 if (nextlocal_
!= NULL
&& nextlocal_
->identifier_
!= NULL
) {
267 CYIdentifier
*cye($
I("$cye")->Replace(context
, CYIdentifierGlobal
));
268 CYIdentifier
*unique(nextlocal_
->identifier_
->Replace(context
, CYIdentifierGlobal
));
270 CYStatement
*declare(
271 $
CYVar($
B1($
B(unique
, $
CYObject()))));
273 cy::Syntax::Catch
*rescue(
274 $
cy::Syntax::Catch(cye
, $$
275 ->* $
CYIf($
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
276 ->* $
CYReturn($
M($
V(cye
), $
S("$cyv"))))
277 ->* $
cy::Syntax::Throw($
V(cye
))));
279 context
.Replace(declare
);
280 rescue
->Replace(context
);
284 ->* $
cy::Syntax::Try(statements
, rescue
, NULL
);
288 CYIdentifier
*CYContext::Unique() {
289 return $
CYIdentifier($pool
.strcat("$cy", $pool
.itoa(unique_
++), NULL
));
292 CYStatement
*CYContinue::Replace(CYContext
&context
) {
296 CYStatement
*CYDebugger::Replace(CYContext
&context
) {
300 CYTarget
*CYBinding::Target(CYContext
&context
) {
301 return $
V(identifier_
);
304 CYAssignment
*CYBinding::Replace(CYContext
&context
, CYIdentifierKind kind
) {
305 identifier_
= identifier_
->Replace(context
, kind
);
307 if (initializer_
== NULL
)
310 CYAssignment
*value($
CYAssign(Target(context
), initializer_
));
315 CYExpression
*CYBindings::Replace(CYContext
&context
, CYIdentifierKind kind
) { $
T(NULL
)
316 CYAssignment
*assignment(binding_
->Replace(context
, kind
));
317 CYExpression
*compound(next_
->Replace(context
, kind
));
319 if (assignment
!= NULL
)
320 if (compound
== NULL
)
321 compound
= assignment
;
323 compound
= $
CYCompound(assignment
, compound
);
327 CYFunctionParameter
*CYBindings::Parameter(CYContext
&context
) { $
T(NULL
)
328 return $
CYFunctionParameter($
CYBinding(binding_
->identifier_
), next_
->Parameter(context
));
331 CYArgument
*CYBindings::Argument(CYContext
&context
) { $
T(NULL
)
332 return $
CYArgument(binding_
->initializer_
, next_
->Argument(context
));
335 CYTarget
*CYDirectMember::Replace(CYContext
&context
) {
336 context
.Replace(object_
);
337 context
.Replace(property_
);
341 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
342 context
.Replace(test_
);
343 context
.ReplaceAll(code_
);
347 void CYElementSpread::Replace(CYContext
&context
) {
348 context
.Replace(value_
);
351 void CYElementValue::Replace(CYContext
&context
) {
352 context
.Replace(value_
);
355 CYForInitializer
*CYEmpty::Replace(CYContext
&context
) {
359 CYTarget
*CYEncodedType::Replace(CYContext
&context
) {
360 return typed_
->Replace(context
);
363 CYTarget
*CYEval::Replace(CYContext
&context
) {
364 context
.scope_
->Damage();
365 if (arguments_
!= NULL
)
366 arguments_
->value_
= $
C1($
M($
V("Cycript"), $
S("compile")), arguments_
->value_
);
367 return $
C($
V("eval"), arguments_
);
370 CYStatement
*CYExpress::Return() {
371 return $
CYReturn(expression_
);
374 CYForInitializer
*CYExpress::Replace(CYContext
&context
) {
375 context
.Replace(expression_
);
379 CYTarget
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
380 return $
C1(this, value
);
383 CYFunctionParameter
*CYExpression::Parameter() const {
387 CYTarget
*CYExtend::Replace(CYContext
&context
) {
388 return object_
.Replace(context
, lhs_
);
391 CYStatement
*CYExternal::Replace(CYContext
&context
) {
392 return $
E($
CYAssign($
V(typed_
->identifier_
), $
C1(typed_
->Replace(context
), $
C2($
V("dlsym"), $
V("RTLD_DEFAULT"), $
S(typed_
->identifier_
->Word())))));
395 CYNumber
*CYFalse::Number(CYContext
&context
) {
399 CYString
*CYFalse::String(CYContext
&context
) {
403 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
404 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
405 function
->this_
.SetNext(context
.this_
);
409 void CYFinally::Replace(CYContext
&context
) { $
T()
410 CYScope
scope(true, context
);
411 context
.ReplaceAll(code_
);
412 scope
.Close(context
);
415 CYStatement
*CYFor::Replace(CYContext
&context
) {
416 CYScope
outer(true, context
);
417 context
.Replace(initializer_
);
419 context
.Replace(test_
);
422 CYScope
inner(true, context
);
423 context
.ReplaceAll(code_
);
424 inner
.Close(context
);
427 context
.Replace(increment_
);
429 outer
.Close(context
);
433 CYStatement
*CYForLexical::Initialize(CYContext
&context
, CYExpression
*value
) {
435 if (binding_
->initializer_
== NULL
)
437 value
= binding_
->initializer_
;
440 return $
CYLexical(constant_
, $
B1($
CYBinding(binding_
->identifier_
, value
)));
443 CYTarget
*CYForLexical::Replace(CYContext
&context
) {
444 _assert(binding_
->Replace(context
, CYIdentifierLexical
) == NULL
);
445 return binding_
->Target(context
);
448 CYStatement
*CYForIn::Replace(CYContext
&context
) {
449 CYScope
scope(true, context
);
450 context
.Replace(initializer_
);
451 context
.Replace(iterable_
);
452 context
.ReplaceAll(code_
);
453 scope
.Close(context
);
457 CYStatement
*CYForInitialized::Replace(CYContext
&context
) {
458 CYAssignment
*assignment(binding_
->Replace(context
, CYIdentifierVariable
));
460 ->* (assignment
== NULL
? NULL
: $
CYExpress(assignment
))
461 ->* $
CYForIn(binding_
->Target(context
), iterable_
, code_
));
464 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
465 return $
CYFunctionParameter(binding_
);
468 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
469 return $
CYForIn(binding_
->Target(context
), iterable_
, CYComprehension::Replace(context
, statement
));
472 CYStatement
*CYForOf::Replace(CYContext
&context
) {
473 CYIdentifier
*item(context
.Unique()), *list(context
.Unique());
476 ->* initializer_
->Initialize(context
, NULL
)
477 ->* $
CYLexical(false, $
B2($
B(list
, iterable_
), $
B(item
)))
478 ->* $
CYForIn($
V(item
), $
V(list
), $
CYBlock($$
479 ->* initializer_
->Initialize(context
, $
M($
V(list
), $
V(item
)))
484 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
485 return $
CYFunctionParameter(binding_
);
488 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
489 CYIdentifier
*cys(context
.Unique());
492 ->* $
CYLexical(false, $
B1($
B(cys
, iterable_
)))
493 ->* $
CYForIn(binding_
->Target(context
), $
V(cys
), $
CYBlock($$
494 ->* $
E($
CYAssign(binding_
->Target(context
), $
M($
V(cys
), binding_
->Target(context
))))
495 ->* CYComprehension::Replace(context
, statement
)
499 CYStatement
*CYForVariable::Initialize(CYContext
&context
, CYExpression
*value
) {
501 if (binding_
->initializer_
== NULL
)
503 value
= binding_
->initializer_
;
506 return $
CYVar($
B1($
CYBinding(binding_
->identifier_
, value
)));
509 CYTarget
*CYForVariable::Replace(CYContext
&context
) {
510 _assert(binding_
->Replace(context
, CYIdentifierVariable
) == NULL
);
511 return binding_
->Target(context
);
514 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
515 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
516 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
518 void CYFunction::Replace(CYContext
&context
) {
519 CYThisScope
*_this(context
.this_
);
520 context
.this_
= &this_
;
521 context
.this_
= CYGetLast(context
.this_
);
523 CYIdentifier
*super(context
.super_
);
524 context
.super_
= super_
;
526 CYNonLocal
*nonlocal(context
.nonlocal_
);
527 CYNonLocal
*nextlocal(context
.nextlocal_
);
530 if (nonlocal_
!= NULL
) {
532 context
.nonlocal_
= nonlocal_
;
535 nonlocal_
= $
CYNonLocal();
536 context
.nextlocal_
= nonlocal_
;
539 CYScope
scope(!localize
, context
);
541 $
I("arguments")->Replace(context
, CYIdentifierMagic
);
543 parameters_
->Replace(context
, code_
);
545 context
.ReplaceAll(code_
);
548 CYImplicitReturn(code_
);
550 if (CYIdentifier
*identifier
= this_
.identifier_
) {
551 context
.scope_
->Declare(context
, identifier
, CYIdentifierVariable
);
553 ->* $
E($
CYAssign($
V(identifier
), $
CYThis()))
558 context
.NonLocal(code_
);
560 context
.nextlocal_
= nextlocal
;
561 context
.nonlocal_
= nonlocal
;
563 context
.super_
= super
;
564 context
.this_
= _this
;
566 scope
.Close(context
, code_
);
569 CYTarget
*CYFunctionExpression::Replace(CYContext
&context
) {
570 CYScope
scope(false, context
);
572 name_
= name_
->Replace(context
, CYIdentifierOther
);
574 CYFunction::Replace(context
);
575 scope
.Close(context
);
579 void CYFunctionParameter::Replace(CYContext
&context
, CYStatement
*&statements
) { $
T()
580 CYAssignment
*assignment(binding_
->Replace(context
, CYIdentifierArgument
));
582 next_
->Replace(context
, statements
);
584 if (assignment
!= NULL
)
586 ->* $
CYIf($
CYIdentical($
CYTypeOf(binding_
->Target(context
)), $
S("undefined")), $$
591 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
592 name_
= name_
->Replace(context
, CYIdentifierOther
);
593 CYFunction::Replace(context
);
597 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
, CYIdentifierKind kind
) {
601 return next_
->Replace(context
, kind
);
602 next_
= context
.scope_
->Declare(context
, this, kind
)->identifier_
;
606 CYStatement
*CYIf::Return() {
607 CYImplicitReturn(true_
);
608 CYImplicitReturn(false_
);
612 CYStatement
*CYIf::Replace(CYContext
&context
) {
613 context
.Replace(test_
);
614 context
.ReplaceAll(true_
);
615 context
.ReplaceAll(false_
);
619 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
623 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
624 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
627 CYStatement
*CYImport::Replace(CYContext
&context
) {
628 return $
CYVar($
B1($
B($
I(module_
->part_
->Word()), $
C1($
V("require"), module_
->Replace(context
, "/")))));
631 CYStatement
*CYImportDeclaration::Replace(CYContext
&context
) {
632 CYIdentifier
*module(context
.Unique());
634 CYList
<CYStatement
> statements
;
635 CYForEach (specifier
, specifiers_
)
636 statements
->*specifier
->Replace(context
, module);
639 ->* $
CYLexical(false, $
B1($
B(module, $
C1($
V("require"), module_
))))
643 CYStatement
*CYImportSpecifier::Replace(CYContext
&context
, CYIdentifier
*module) {
644 binding_
= binding_
->Replace(context
, CYIdentifierLexical
);
646 CYExpression
*import($
V(module));
648 import = $
M(import, $
S(name_
));
649 return $
E($
CYAssign($
V(binding_
), import));
652 CYTarget
*CYIndirect::Replace(CYContext
&context
) {
653 return $
M(rhs_
, $
S("$cyi"));
656 CYTarget
*CYIndirectMember::Replace(CYContext
&context
) {
657 return $
M($
CYIndirect(object_
), property_
);
660 CYExpression
*CYInfix::Replace(CYContext
&context
) {
661 context
.Replace(lhs_
);
662 context
.Replace(rhs_
);
666 CYStatement
*CYLabel::Replace(CYContext
&context
) {
667 context
.Replace(statement_
);
671 CYTarget
*CYLambda::Replace(CYContext
&context
) {
672 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), code_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
675 CYForInitializer
*CYLexical::Replace(CYContext
&context
) {
676 if (CYExpression
*expression
= bindings_
->Replace(context
, CYIdentifierLexical
))
677 return $
E(expression
);
681 CYFunctionExpression
*CYMethod::Constructor() {
685 void CYMethod::Replace(CYContext
&context
) {
686 CYFunction::Replace(context
);
689 CYString
*CYModule::Replace(CYContext
&context
, const char *separator
) const {
691 return $
CYString(part_
);
692 return $
CYString($pool
.strcat(next_
->Replace(context
, separator
)->Value(), separator
, part_
->Word(), NULL
));
695 CYExpression
*CYMultiply::Replace(CYContext
&context
) {
696 CYInfix::Replace(context
);
698 if (CYNumber
*lhn
= lhs_
->Number(context
))
699 if (CYNumber
*rhn
= rhs_
->Number(context
))
700 return $
D(lhn
->Value() * rhn
->Value());
708 CYTarget
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
709 CYSetLast(arguments_
) = $
CYArgument(value
);
713 CYTarget
*New::Replace(CYContext
&context
) {
714 context
.Replace(constructor_
);
715 arguments_
->Replace(context
);
721 CYNumber
*CYNull::Number(CYContext
&context
) {
725 CYString
*CYNull::String(CYContext
&context
) {
729 CYNumber
*CYNumber::Number(CYContext
&context
) {
733 CYString
*CYNumber::String(CYContext
&context
) {
734 // XXX: there is a precise algorithm for this
735 return $
S($pool
.sprintf(24, "%.17g", Value()));
738 CYExpression
*CYNumber::PropertyName(CYContext
&context
) {
739 return String(context
);
742 CYTarget
*CYObject::Replace(CYContext
&context
, CYTarget
*seed
) {
744 if (properties_
!= NULL
)
745 properties_
= properties_
->ReplaceAll(context
, builder
, $
CYThis(), seed
!= this);
748 return $
C1($
M($
CYFunctionExpression(NULL
, builder
.bindings_
->Parameter(context
),
750 ->* $
CYReturn($
CYThis())
751 ), $
S("call")), seed
, builder
.bindings_
->Argument(context
));
754 CYForEach (property
, properties_
)
755 property
->Replace(context
);
759 CYTarget
*CYObject::Replace(CYContext
&context
) {
760 return Replace(context
, this);
763 CYTarget
*CYParenthetical::Replace(CYContext
&context
) {
764 // XXX: return expression_;
765 context
.Replace(expression_
);
769 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
770 context
.Replace(lhs_
);
774 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
775 context
.Replace(rhs_
);
779 CYProperty
*CYProperty::ReplaceAll(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, bool update
) {
782 Replace(context
, builder
, self
, false);
784 next_
= next_
->ReplaceAll(context
, builder
, self
, update
);
785 return update
? next_
: this;
788 void CYProperty::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, bool protect
) {
789 CYExpression
*name(name_
->PropertyName(context
));
790 if (name_
->Computed()) {
791 CYIdentifier
*unique(context
.Unique());
793 ->* $
B1($
B(unique
, name
));
797 Replace(context
, builder
, self
, name
, protect
);
800 bool CYProperty::Update() const {
801 return name_
->Computed();
804 void CYPropertyGetter::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
805 CYIdentifier
*unique(context
.Unique());
807 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
809 ->* CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("get"), $
V(unique
)));
812 CYFunctionExpression
*CYPropertyMethod::Constructor() {
813 return name_
->Constructor() ? $
CYFunctionExpression(NULL
, parameters_
, code_
) : NULL
;
816 void CYPropertyMethod::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
817 CYIdentifier
*unique(context
.Unique());
819 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
821 ->* (!protect
? $
E($
CYAssign($
M(self
, name
), $
V(unique
))) :
822 CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("value"), $
V(unique
), $
CYPropertyValue($
S("writable"), $
CYTrue()))));
825 bool CYPropertyMethod::Update() const {
829 void CYPropertySetter::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
830 CYIdentifier
*unique(context
.Unique());
832 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
834 ->* CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("set"), $
V(unique
)));
837 void CYPropertyValue::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
839 CYIdentifier
*unique(context
.Unique());
841 ->* $
B1($
B(unique
, value_
));
843 ->* $
E($
CYAssign($
M(self
, name
), $
V(unique
)));
846 void CYPropertyValue::Replace(CYContext
&context
) {
847 context
.Replace(value_
);
850 void CYScript::Replace(CYContext
&context
) {
851 CYScope
scope(false, context
);
852 context
.scope_
->Damage();
854 context
.nextlocal_
= $
CYNonLocal();
855 context
.ReplaceAll(code_
);
856 context
.NonLocal(code_
);
858 scope
.Close(context
, code_
);
862 for (std::vector
<CYIdentifier
*>::const_iterator
i(context
.replace_
.begin()); i
!= context
.replace_
.end(); ++i
) {
864 if (context
.options_
.verbose_
)
865 name
= $pool
.strcat("$", $pool
.itoa(offset
++), NULL
);
871 unsigned position(7), local(offset
++ + 1);
874 unsigned index(local
% (sizeof(MappingSet
) - 1));
875 local
/= sizeof(MappingSet
) - 1;
876 id
[--position
] = MappingSet
[index
];
877 } while (local
!= 0);
879 if (scope
.Lookup(context
, id
+ position
) != NULL
)
881 // XXX: at some point, this could become a keyword
883 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
886 CYIdentifier
*identifier(*i
);
887 _assert(identifier
->next_
== identifier
);
888 identifier
->next_
= $
I(name
);
892 CYTarget
*CYResolveMember::Replace(CYContext
&context
) {
893 return $
M($
M(object_
, $
S("$cyr")), property_
);
896 CYStatement
*CYReturn::Replace(CYContext
&context
) {
897 if (context
.nonlocal_
!= NULL
) {
898 CYProperty
*value(value_
== NULL
? NULL
: $
CYPropertyValue($
S("$cyv"), value_
));
899 return $
cy::Syntax::Throw($
CYObject(
900 $
CYPropertyValue($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
904 context
.Replace(value_
);
908 CYTarget
*CYRubyBlock::Replace(CYContext
&context
) {
909 return lhs_
->AddArgument(context
, proc_
->Replace(context
));
912 CYTarget
*CYRubyBlock::AddArgument(CYContext
&context
, CYExpression
*value
) {
913 return Replace(context
)->AddArgument(context
, value
);
916 CYTarget
*CYRubyProc::Replace(CYContext
&context
) {
917 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
918 function
= CYNonLocalize(context
, function
);
919 function
->implicit_
= true;
923 CYScope::CYScope(bool transparent
, CYContext
&context
) :
924 transparent_(transparent
),
925 parent_(context
.scope_
),
930 _assert(!transparent_
|| parent_
!= NULL
);
931 context
.scope_
= this;
934 void CYScope::Damage() {
940 CYIdentifierFlags
*CYScope::Lookup(CYContext
&context
, const char *word
) {
941 CYForEach (i
, internal_
)
942 if (strcmp(i
->identifier_
->Word(), word
) == 0)
947 CYIdentifierFlags
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
948 return Lookup(context
, identifier
->Word());
951 CYIdentifierFlags
*CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierKind kind
) {
952 _assert(identifier
->next_
== NULL
|| identifier
->next_
== identifier
);
954 CYIdentifierFlags
*existing(Lookup(context
, identifier
));
955 if (existing
== NULL
)
956 internal_
= $
CYIdentifierFlags(identifier
, kind
, internal_
);
958 if (existing
== NULL
)
961 if (kind
== CYIdentifierGlobal
);
962 else if (existing
->kind_
== CYIdentifierGlobal
|| existing
->kind_
== CYIdentifierMagic
)
963 existing
->kind_
= kind
;
964 else if (existing
->kind_
== CYIdentifierLexical
|| kind
== CYIdentifierLexical
)
965 _assert(false); // XXX: throw new SyntaxError()
970 void CYScope::Merge(CYContext
&context
, const CYIdentifierFlags
*flags
) {
971 _assert(flags
->identifier_
->next_
== flags
->identifier_
);
972 CYIdentifierFlags
*existing(Declare(context
, flags
->identifier_
, flags
->kind_
));
973 flags
->identifier_
->next_
= existing
->identifier_
;
975 existing
->count_
+= flags
->count_
;
976 if (existing
->offset_
< flags
->offset_
)
977 existing
->offset_
= flags
->offset_
;
980 void CYScope::Close(CYContext
&context
, CYStatement
*&statements
) {
983 CYList
<CYBindings
> bindings
;
985 CYForEach (i
, internal_
)
986 if (i
->kind_
== CYIdentifierVariable
)
988 ->* $
CYBindings($
CYBinding(i
->identifier_
));
991 CYVar
*var($
CYVar(bindings
));
992 var
->SetNext(statements
);
997 void CYScope::Close(CYContext
&context
) {
998 context
.scope_
= parent_
;
1000 CYForEach (i
, internal_
) {
1001 _assert(i
->identifier_
->next_
== i
->identifier_
);
1003 case CYIdentifierArgument
: {
1004 _assert(!transparent_
);
1007 case CYIdentifierLexical
: {
1009 CYIdentifier
*replace(context
.Unique());
1010 replace
->next_
= replace
;
1011 i
->identifier_
->next_
= replace
;
1012 i
->identifier_
= replace
;
1016 i
->kind_
= CYIdentifierVariable
;
1018 parent_
->Declare(context
, i
->identifier_
, CYIdentifierVariable
);
1021 case CYIdentifierVariable
: {
1023 parent_
->Declare(context
, i
->identifier_
, i
->kind_
);
1024 i
->kind_
= CYIdentifierGlobal
;
1032 typedef std::multimap
<unsigned, CYIdentifier
*> CYIdentifierOffsetMap
;
1033 CYIdentifierOffsetMap offsets
;
1035 CYForEach (i
, internal_
) {
1036 _assert(i
->identifier_
->next_
== i
->identifier_
);
1038 case CYIdentifierArgument
:
1039 case CYIdentifierVariable
:
1040 offsets
.insert(CYIdentifierOffsetMap::value_type(i
->offset_
, i
->identifier_
));
1046 for (CYIdentifierOffsetMap::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
1047 if (offset
< i
->first
)
1049 CYIdentifier
*identifier(i
->second
);
1051 if (offset
>= context
.replace_
.size())
1052 context
.replace_
.resize(offset
+ 1, NULL
);
1053 CYIdentifier
*&replace(context
.replace_
[offset
++]);
1055 if (replace
== NULL
)
1056 replace
= identifier
;
1058 _assert(replace
->next_
== replace
);
1059 identifier
->next_
= replace
;
1063 if (parent_
== NULL
)
1066 CYForEach (i
, internal_
) {
1068 case CYIdentifierGlobal
: {
1069 if (i
->offset_
< offset
)
1070 i
->offset_
= offset
;
1071 parent_
->Merge(context
, i
);
1076 CYTarget
*CYSubscriptMember::Replace(CYContext
&context
) {
1077 return $
C1($
M(object_
, $
S("$cyg")), property_
);
1080 CYElementValue
*CYSpan::Replace(CYContext
&context
) { $
T(NULL
)
1081 return $
CYElementValue(expression_
, $
CYElementValue(string_
, next_
->Replace(context
)));
1084 CYStatement
*CYStatement::Return() {
1088 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
1089 size_t size(size_
+ rhs
->size_
);
1090 char *value($
char[size
+ 1]);
1091 memcpy(value
, value_
, size_
);
1092 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
1094 return $
S(value
, size
);
1097 CYNumber
*CYString::Number(CYContext
&context
) {
1098 // XXX: there is a precise algorithm for this
1102 CYExpression
*CYString::PropertyName(CYContext
&context
) {
1106 CYString
*CYString::String(CYContext
&context
) {
1110 CYStatement
*CYStructDefinition::Replace(CYContext
&context
) {
1111 CYTarget
*target(tail_
->Replace(context
));
1113 target
= $
C1($
M(target
, $
S("withName")), $
S(name_
->Word()));
1114 return $
CYLexical(false, $
B1($
B($
I($pool
.strcat(name_
->Word(), "$cy", NULL
)), target
)));
1117 CYTarget
*CYStructTail::Replace(CYContext
&context
) {
1118 CYList
<CYElementValue
> types
;
1119 CYList
<CYElementValue
> names
;
1121 CYForEach (field
, fields_
) {
1122 CYTypedIdentifier
*typed(field
->typed_
);
1123 types
->*$
CYElementValue(typed
->Replace(context
));
1126 if (typed
->identifier_
== NULL
)
1129 name
= $
S(typed
->identifier_
->Word());
1130 names
->*$
CYElementValue(name
);
1133 return $
N2($
V("Type"), $
CYArray(types
), $
CYArray(names
));
1136 CYTarget
*CYSuperAccess::Replace(CYContext
&context
) {
1137 return $
C1($
M($
M($
M($
V(context
.super_
), $
S("prototype")), property_
), $
S("bind")), $
CYThis());
1140 CYTarget
*CYSuperCall::Replace(CYContext
&context
) {
1141 return $
C($
C1($
M($
V(context
.super_
), $
S("bind")), $
CYThis()), arguments_
);
1144 CYTarget
*CYSymbol::Replace(CYContext
&context
) {
1145 return $
C1($
M($
V("Symbol"), $
S("for")), $
S(name_
));
1148 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
1149 context
.Replace(value_
);
1150 clauses_
->Replace(context
);
1154 CYStatement
*CYTarget::Initialize(CYContext
&context
, CYExpression
*value
) {
1157 return $
E($
CYAssign(this, value
));
1160 CYTarget
*CYTemplate::Replace(CYContext
&context
) {
1161 return $
C2($
M($
M($
M($
V("String"), $
S("prototype")), $
S("concat")), $
S("apply")), $
S(""), $
CYArray($
CYElementValue(string_
, spans_
->Replace(context
))));
1164 CYTarget
*CYThis::Replace(CYContext
&context
) {
1165 if (context
.this_
!= NULL
)
1166 return $
V(context
.this_
->Identifier(context
));
1173 CYStatement
*Throw::Replace(CYContext
&context
) {
1174 context
.Replace(value_
);
1180 CYTarget
*CYTrivial::Replace(CYContext
&context
) {
1184 CYNumber
*CYTrue::Number(CYContext
&context
) {
1188 CYString
*CYTrue::String(CYContext
&context
) {
1195 CYStatement
*Try::Replace(CYContext
&context
) {
1196 CYScope
scope(true, context
);
1197 context
.ReplaceAll(code_
);
1198 scope
.Close(context
);
1200 catch_
->Replace(context
);
1201 finally_
->Replace(context
);
1207 CYTarget
*CYTypeArrayOf::Replace_(CYContext
&context
, CYTarget
*type
) {
1208 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
1211 CYTarget
*CYTypeBlockWith::Replace_(CYContext
&context
, CYTarget
*type
) {
1212 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("blockWith")), parameters_
->Argument(context
)));
1215 CYTarget
*CYTypeCharacter::Replace(CYContext
&context
) {
1217 case CYTypeNeutral
: return $
V("char");
1218 case CYTypeSigned
: return $
V("schar");
1219 case CYTypeUnsigned
: return $
V("uchar");
1220 default: _assert(false);
1224 CYTarget
*CYTypeConstant::Replace_(CYContext
&context
, CYTarget
*type
) {
1225 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
1228 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
1229 CYIdentifier
*identifier(typed_
->identifier_
);
1230 typed_
->identifier_
= NULL
;
1231 return $
CYLexical(false, $
B1($
B(identifier
, $
CYTypeExpression(typed_
))));
1234 CYTarget
*CYTypeError::Replace(CYContext
&context
) {
1239 CYTarget
*CYTypeExpression::Replace(CYContext
&context
) {
1240 return typed_
->Replace(context
);
1243 CYTarget
*CYTypeIntegral::Replace(CYContext
&context
) {
1244 bool u(signing_
== CYTypeUnsigned
);
1246 case 0: return $
V(u
? "ushort" : "short");
1247 case 1: return $
V(u
? "uint" : "int");
1248 case 2: return $
V(u
? "ulong" : "long");
1249 case 3: return $
V(u
? "ulonglong" : "longlong");
1250 default: _assert(false);
1254 CYTarget
*CYTypeModifier::Replace(CYContext
&context
, CYTarget
*type
) { $
T(type
)
1255 return Replace_(context
, type
);
1258 CYTarget
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYTarget
*type
) {
1259 CYList
<CYArgument
> arguments(parameters_
->Argument(context
));
1261 arguments
->*$
C_($
CYNull());
1262 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), arguments
));
1265 CYTarget
*CYTypePointerTo::Replace_(CYContext
&context
, CYTarget
*type
) {
1266 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
1269 CYTarget
*CYTypeReference::Replace(CYContext
&context
) {
1270 return $
V($pool
.strcat(name_
->Word(), "$cy", NULL
));
1273 CYTarget
*CYTypeStruct::Replace(CYContext
&context
) {
1274 CYTarget
*target(tail_
->Replace(context
));
1276 target
= $
C1($
M(target
, $
S("withName")), $
S(name_
->Word()));
1280 CYTarget
*CYTypeVariable::Replace(CYContext
&context
) {
1284 CYTarget
*CYTypeVoid::Replace(CYContext
&context
) {
1285 return $
N1($
V("Type"), $
CYString("v"));
1288 CYTarget
*CYTypeVolatile::Replace_(CYContext
&context
, CYTarget
*type
) {
1289 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
1292 CYTarget
*CYTypedIdentifier::Replace(CYContext
&context
) {
1293 return modifier_
->Replace(context
, specifier_
->Replace(context
));
1296 CYTypeFunctionWith
*CYTypedIdentifier::Function() {
1297 CYTypeModifier
*&modifier(CYGetLast(modifier_
));
1298 if (modifier
== NULL
)
1301 CYTypeFunctionWith
*function(modifier
->Function());
1302 if (function
== NULL
)
1309 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
1310 return $
CYArgument(typed_
->Replace(context
), next_
->Argument(context
));
1313 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
1314 return $
CYFunctionParameter($
CYBinding(typed_
->identifier_
?: context
.Unique()), next_
->Parameters(context
));
1317 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
1318 return next_
->TypeSignature(context
, $
CYAdd(prefix
, typed_
->Replace(context
)));
1321 CYForInitializer
*CYVar::Replace(CYContext
&context
) {
1322 if (CYExpression
*expression
= bindings_
->Replace(context
, CYIdentifierVariable
))
1323 return $
E(expression
);
1327 CYTarget
*CYVariable::Replace(CYContext
&context
) {
1328 name_
= name_
->Replace(context
, CYIdentifierGlobal
);
1332 CYFunctionParameter
*CYVariable::Parameter() const {
1333 return $
CYFunctionParameter($
CYBinding(name_
));
1336 CYStatement
*CYWhile::Replace(CYContext
&context
) {
1337 context
.Replace(test_
);
1338 context
.ReplaceAll(code_
);
1342 CYStatement
*CYWith::Replace(CYContext
&context
) {
1343 context
.Replace(scope_
);
1344 CYScope
scope(true, context
);
1346 context
.ReplaceAll(code_
);
1347 scope
.Close(context
);
1351 CYExpression
*CYWord::PropertyName(CYContext
&context
) {