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
*CYExternalDefinition::Replace(CYContext
&context
) {
392 return $
E($
CYAssign($
V(typed_
->identifier_
), $
CYExternalExpression(abi_
, typed_
)));
395 CYTarget
*CYExternalExpression::Replace(CYContext
&context
) {
396 return $
C1(typed_
->Replace(context
), $
C2($
V("dlsym"), $
V("RTLD_DEFAULT"), $
S(typed_
->identifier_
->Word())));
399 CYNumber
*CYFalse::Number(CYContext
&context
) {
403 CYString
*CYFalse::String(CYContext
&context
) {
407 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
408 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
409 function
->this_
.SetNext(context
.this_
);
413 void CYFinally::Replace(CYContext
&context
) { $
T()
414 CYScope
scope(true, context
);
415 context
.ReplaceAll(code_
);
416 scope
.Close(context
);
419 CYStatement
*CYFor::Replace(CYContext
&context
) {
420 CYScope
outer(true, context
);
421 context
.Replace(initializer_
);
423 context
.Replace(test_
);
426 CYScope
inner(true, context
);
427 context
.ReplaceAll(code_
);
428 inner
.Close(context
);
431 context
.Replace(increment_
);
433 outer
.Close(context
);
437 CYStatement
*CYForLexical::Initialize(CYContext
&context
, CYExpression
*value
) {
439 if (binding_
->initializer_
== NULL
)
441 value
= binding_
->initializer_
;
444 return $
CYLexical(constant_
, $
B1($
CYBinding(binding_
->identifier_
, value
)));
447 CYTarget
*CYForLexical::Replace(CYContext
&context
) {
448 _assert(binding_
->Replace(context
, CYIdentifierLexical
) == NULL
);
449 return binding_
->Target(context
);
452 CYStatement
*CYForIn::Replace(CYContext
&context
) {
453 CYScope
scope(true, context
);
454 context
.Replace(initializer_
);
455 context
.Replace(iterable_
);
456 context
.ReplaceAll(code_
);
457 scope
.Close(context
);
461 CYStatement
*CYForInitialized::Replace(CYContext
&context
) {
462 CYAssignment
*assignment(binding_
->Replace(context
, CYIdentifierVariable
));
464 ->* (assignment
== NULL
? NULL
: $
CYExpress(assignment
))
465 ->* $
CYForIn(binding_
->Target(context
), iterable_
, code_
));
468 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
469 return $
CYFunctionParameter(binding_
);
472 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
473 return $
CYForIn(binding_
->Target(context
), iterable_
, CYComprehension::Replace(context
, statement
));
476 CYStatement
*CYForOf::Replace(CYContext
&context
) {
477 CYIdentifier
*item(context
.Unique()), *list(context
.Unique());
480 ->* initializer_
->Initialize(context
, NULL
)
481 ->* $
CYLexical(false, $
B2($
B(list
, iterable_
), $
B(item
)))
482 ->* $
CYForIn($
V(item
), $
V(list
), $
CYBlock($$
483 ->* initializer_
->Initialize(context
, $
M($
V(list
), $
V(item
)))
488 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
489 return $
CYFunctionParameter(binding_
);
492 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
493 CYIdentifier
*cys(context
.Unique());
496 ->* $
CYLexical(false, $
B1($
B(cys
, iterable_
)))
497 ->* $
CYForIn(binding_
->Target(context
), $
V(cys
), $
CYBlock($$
498 ->* $
E($
CYAssign(binding_
->Target(context
), $
M($
V(cys
), binding_
->Target(context
))))
499 ->* CYComprehension::Replace(context
, statement
)
503 CYStatement
*CYForVariable::Initialize(CYContext
&context
, CYExpression
*value
) {
505 if (binding_
->initializer_
== NULL
)
507 value
= binding_
->initializer_
;
510 return $
CYVar($
B1($
CYBinding(binding_
->identifier_
, value
)));
513 CYTarget
*CYForVariable::Replace(CYContext
&context
) {
514 _assert(binding_
->Replace(context
, CYIdentifierVariable
) == NULL
);
515 return binding_
->Target(context
);
518 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
519 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
520 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
522 void CYFunction::Replace(CYContext
&context
) {
523 CYThisScope
*_this(context
.this_
);
524 context
.this_
= &this_
;
525 context
.this_
= CYGetLast(context
.this_
);
527 CYIdentifier
*super(context
.super_
);
528 context
.super_
= super_
;
530 CYNonLocal
*nonlocal(context
.nonlocal_
);
531 CYNonLocal
*nextlocal(context
.nextlocal_
);
534 if (nonlocal_
!= NULL
) {
536 context
.nonlocal_
= nonlocal_
;
539 nonlocal_
= $
CYNonLocal();
540 context
.nextlocal_
= nonlocal_
;
543 CYScope
scope(!localize
, context
);
545 $
I("arguments")->Replace(context
, CYIdentifierMagic
);
547 parameters_
->Replace(context
, code_
);
549 context
.ReplaceAll(code_
);
552 CYImplicitReturn(code_
);
554 if (CYIdentifier
*identifier
= this_
.identifier_
) {
555 context
.scope_
->Declare(context
, identifier
, CYIdentifierVariable
);
557 ->* $
E($
CYAssign($
V(identifier
), $
CYThis()))
562 context
.NonLocal(code_
);
564 context
.nextlocal_
= nextlocal
;
565 context
.nonlocal_
= nonlocal
;
567 context
.super_
= super
;
568 context
.this_
= _this
;
570 scope
.Close(context
, code_
);
573 CYTarget
*CYFunctionExpression::Replace(CYContext
&context
) {
574 CYScope
scope(false, context
);
576 name_
= name_
->Replace(context
, CYIdentifierOther
);
578 CYFunction::Replace(context
);
579 scope
.Close(context
);
583 void CYFunctionParameter::Replace(CYContext
&context
, CYStatement
*&statements
) { $
T()
584 CYAssignment
*assignment(binding_
->Replace(context
, CYIdentifierArgument
));
586 next_
->Replace(context
, statements
);
588 if (assignment
!= NULL
)
590 ->* $
CYIf($
CYIdentical($
CYTypeOf(binding_
->Target(context
)), $
S("undefined")), $$
595 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
596 name_
= name_
->Replace(context
, CYIdentifierOther
);
597 CYFunction::Replace(context
);
601 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
, CYIdentifierKind kind
) {
605 return next_
->Replace(context
, kind
);
606 next_
= context
.scope_
->Declare(context
, this, kind
)->identifier_
;
610 CYStatement
*CYIf::Return() {
611 CYImplicitReturn(true_
);
612 CYImplicitReturn(false_
);
616 CYStatement
*CYIf::Replace(CYContext
&context
) {
617 context
.Replace(test_
);
618 context
.ReplaceAll(true_
);
619 context
.ReplaceAll(false_
);
623 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
627 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
628 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
631 CYStatement
*CYImport::Replace(CYContext
&context
) {
632 return $
CYVar($
B1($
B($
I(module_
->part_
->Word()), $
C1($
V("require"), module_
->Replace(context
, "/")))));
635 CYStatement
*CYImportDeclaration::Replace(CYContext
&context
) {
636 CYIdentifier
*module(context
.Unique());
638 CYList
<CYStatement
> statements
;
639 CYForEach (specifier
, specifiers_
)
640 statements
->*specifier
->Replace(context
, module);
643 ->* $
CYLexical(false, $
B1($
B(module, $
C1($
V("require"), module_
))))
647 CYStatement
*CYImportSpecifier::Replace(CYContext
&context
, CYIdentifier
*module) {
648 binding_
= binding_
->Replace(context
, CYIdentifierLexical
);
650 CYExpression
*import($
V(module));
652 import = $
M(import, $
S(name_
));
653 return $
E($
CYAssign($
V(binding_
), import));
656 CYTarget
*CYIndirect::Replace(CYContext
&context
) {
657 return $
M(rhs_
, $
S("$cyi"));
660 CYTarget
*CYIndirectMember::Replace(CYContext
&context
) {
661 return $
M($
CYIndirect(object_
), property_
);
664 CYExpression
*CYInfix::Replace(CYContext
&context
) {
665 context
.Replace(lhs_
);
666 context
.Replace(rhs_
);
670 CYStatement
*CYLabel::Replace(CYContext
&context
) {
671 context
.Replace(statement_
);
675 CYTarget
*CYLambda::Replace(CYContext
&context
) {
676 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), code_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
679 CYForInitializer
*CYLexical::Replace(CYContext
&context
) {
680 if (CYExpression
*expression
= bindings_
->Replace(context
, CYIdentifierLexical
))
681 return $
E(expression
);
685 CYFunctionExpression
*CYMethod::Constructor() {
689 void CYMethod::Replace(CYContext
&context
) {
690 CYFunction::Replace(context
);
693 CYString
*CYModule::Replace(CYContext
&context
, const char *separator
) const {
695 return $
CYString(part_
);
696 return $
CYString($pool
.strcat(next_
->Replace(context
, separator
)->Value(), separator
, part_
->Word(), NULL
));
699 CYExpression
*CYMultiply::Replace(CYContext
&context
) {
700 CYInfix::Replace(context
);
702 if (CYNumber
*lhn
= lhs_
->Number(context
))
703 if (CYNumber
*rhn
= rhs_
->Number(context
))
704 return $
D(lhn
->Value() * rhn
->Value());
712 CYTarget
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
713 CYSetLast(arguments_
) = $
CYArgument(value
);
717 CYTarget
*New::Replace(CYContext
&context
) {
718 context
.Replace(constructor_
);
719 arguments_
->Replace(context
);
725 CYNumber
*CYNull::Number(CYContext
&context
) {
729 CYString
*CYNull::String(CYContext
&context
) {
733 CYNumber
*CYNumber::Number(CYContext
&context
) {
737 CYString
*CYNumber::String(CYContext
&context
) {
738 // XXX: there is a precise algorithm for this
739 return $
S($pool
.sprintf(24, "%.17g", Value()));
742 CYExpression
*CYNumber::PropertyName(CYContext
&context
) {
743 return String(context
);
746 CYTarget
*CYObject::Replace(CYContext
&context
, CYTarget
*seed
) {
748 if (properties_
!= NULL
)
749 properties_
= properties_
->ReplaceAll(context
, builder
, $
CYThis(), seed
!= this);
752 return $
C1($
M($
CYFunctionExpression(NULL
, builder
.bindings_
->Parameter(context
),
754 ->* $
CYReturn($
CYThis())
755 ), $
S("call")), seed
, builder
.bindings_
->Argument(context
));
758 CYForEach (property
, properties_
)
759 property
->Replace(context
);
763 CYTarget
*CYObject::Replace(CYContext
&context
) {
764 return Replace(context
, this);
767 CYTarget
*CYParenthetical::Replace(CYContext
&context
) {
768 // XXX: return expression_;
769 context
.Replace(expression_
);
773 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
774 context
.Replace(lhs_
);
778 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
779 context
.Replace(rhs_
);
783 CYProperty
*CYProperty::ReplaceAll(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, bool update
) {
786 Replace(context
, builder
, self
, false);
788 next_
= next_
->ReplaceAll(context
, builder
, self
, update
);
789 return update
? next_
: this;
792 void CYProperty::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, bool protect
) {
793 CYExpression
*name(name_
->PropertyName(context
));
794 if (name_
->Computed()) {
795 CYIdentifier
*unique(context
.Unique());
797 ->* $
B1($
B(unique
, name
));
801 Replace(context
, builder
, self
, name
, protect
);
804 bool CYProperty::Update() const {
805 return name_
->Computed();
808 void CYPropertyGetter::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
809 CYIdentifier
*unique(context
.Unique());
811 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
813 ->* CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("get"), $
V(unique
)));
816 CYFunctionExpression
*CYPropertyMethod::Constructor() {
817 return name_
->Constructor() ? $
CYFunctionExpression(NULL
, parameters_
, code_
) : NULL
;
820 void CYPropertyMethod::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
821 CYIdentifier
*unique(context
.Unique());
823 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
825 ->* (!protect
? $
E($
CYAssign($
M(self
, name
), $
V(unique
))) :
826 CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("value"), $
V(unique
), $
CYPropertyValue($
S("writable"), $
CYTrue()))));
829 bool CYPropertyMethod::Update() const {
833 void CYPropertySetter::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
834 CYIdentifier
*unique(context
.Unique());
836 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
838 ->* CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("set"), $
V(unique
)));
841 void CYPropertyValue::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
843 CYIdentifier
*unique(context
.Unique());
845 ->* $
B1($
B(unique
, value_
));
847 ->* $
E($
CYAssign($
M(self
, name
), $
V(unique
)));
850 void CYPropertyValue::Replace(CYContext
&context
) {
851 context
.Replace(value_
);
854 void CYScript::Replace(CYContext
&context
) {
855 CYScope
scope(false, context
);
856 context
.scope_
->Damage();
858 context
.nextlocal_
= $
CYNonLocal();
859 context
.ReplaceAll(code_
);
860 context
.NonLocal(code_
);
862 scope
.Close(context
, code_
);
866 for (std::vector
<CYIdentifier
*>::const_iterator
i(context
.replace_
.begin()); i
!= context
.replace_
.end(); ++i
) {
868 if (context
.options_
.verbose_
)
869 name
= $pool
.strcat("$", $pool
.itoa(offset
++), NULL
);
875 unsigned position(7), local(offset
++ + 1);
878 unsigned index(local
% (sizeof(MappingSet
) - 1));
879 local
/= sizeof(MappingSet
) - 1;
880 id
[--position
] = MappingSet
[index
];
881 } while (local
!= 0);
883 if (scope
.Lookup(context
, id
+ position
) != NULL
)
885 // XXX: at some point, this could become a keyword
887 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
890 CYIdentifier
*identifier(*i
);
891 _assert(identifier
->next_
== identifier
);
892 identifier
->next_
= $
I(name
);
896 CYTarget
*CYResolveMember::Replace(CYContext
&context
) {
897 return $
M($
M(object_
, $
S("$cyr")), property_
);
900 CYStatement
*CYReturn::Replace(CYContext
&context
) {
901 if (context
.nonlocal_
!= NULL
) {
902 CYProperty
*value(value_
== NULL
? NULL
: $
CYPropertyValue($
S("$cyv"), value_
));
903 return $
cy::Syntax::Throw($
CYObject(
904 $
CYPropertyValue($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
908 context
.Replace(value_
);
912 CYTarget
*CYRubyBlock::Replace(CYContext
&context
) {
913 return lhs_
->AddArgument(context
, proc_
->Replace(context
));
916 CYTarget
*CYRubyBlock::AddArgument(CYContext
&context
, CYExpression
*value
) {
917 return Replace(context
)->AddArgument(context
, value
);
920 CYTarget
*CYRubyProc::Replace(CYContext
&context
) {
921 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
922 function
= CYNonLocalize(context
, function
);
923 function
->implicit_
= true;
927 CYScope::CYScope(bool transparent
, CYContext
&context
) :
928 transparent_(transparent
),
929 parent_(context
.scope_
),
934 _assert(!transparent_
|| parent_
!= NULL
);
935 context
.scope_
= this;
938 void CYScope::Damage() {
944 CYIdentifierFlags
*CYScope::Lookup(CYContext
&context
, const char *word
) {
945 CYForEach (i
, internal_
)
946 if (strcmp(i
->identifier_
->Word(), word
) == 0)
951 CYIdentifierFlags
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
952 return Lookup(context
, identifier
->Word());
955 CYIdentifierFlags
*CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierKind kind
) {
956 _assert(identifier
->next_
== NULL
|| identifier
->next_
== identifier
);
958 CYIdentifierFlags
*existing(Lookup(context
, identifier
));
959 if (existing
== NULL
)
960 internal_
= $
CYIdentifierFlags(identifier
, kind
, internal_
);
962 if (existing
== NULL
)
965 if (kind
== CYIdentifierGlobal
);
966 else if (existing
->kind_
== CYIdentifierGlobal
|| existing
->kind_
== CYIdentifierMagic
)
967 existing
->kind_
= kind
;
968 else if (existing
->kind_
== CYIdentifierLexical
|| kind
== CYIdentifierLexical
)
969 _assert(false); // XXX: throw new SyntaxError()
974 void CYScope::Merge(CYContext
&context
, const CYIdentifierFlags
*flags
) {
975 _assert(flags
->identifier_
->next_
== flags
->identifier_
);
976 CYIdentifierFlags
*existing(Declare(context
, flags
->identifier_
, flags
->kind_
));
977 flags
->identifier_
->next_
= existing
->identifier_
;
979 existing
->count_
+= flags
->count_
;
980 if (existing
->offset_
< flags
->offset_
)
981 existing
->offset_
= flags
->offset_
;
984 void CYScope::Close(CYContext
&context
, CYStatement
*&statements
) {
987 CYList
<CYBindings
> bindings
;
989 CYForEach (i
, internal_
)
990 if (i
->kind_
== CYIdentifierVariable
)
992 ->* $
CYBindings($
CYBinding(i
->identifier_
));
995 CYVar
*var($
CYVar(bindings
));
996 var
->SetNext(statements
);
1001 void CYScope::Close(CYContext
&context
) {
1002 context
.scope_
= parent_
;
1004 CYForEach (i
, internal_
) {
1005 _assert(i
->identifier_
->next_
== i
->identifier_
);
1007 case CYIdentifierArgument
: {
1008 _assert(!transparent_
);
1011 case CYIdentifierLexical
: {
1013 CYIdentifier
*replace(context
.Unique());
1014 replace
->next_
= replace
;
1015 i
->identifier_
->next_
= replace
;
1016 i
->identifier_
= replace
;
1020 i
->kind_
= CYIdentifierVariable
;
1022 parent_
->Declare(context
, i
->identifier_
, CYIdentifierVariable
);
1025 case CYIdentifierVariable
: {
1027 parent_
->Declare(context
, i
->identifier_
, i
->kind_
);
1028 i
->kind_
= CYIdentifierGlobal
;
1036 typedef std::multimap
<unsigned, CYIdentifier
*> CYIdentifierOffsetMap
;
1037 CYIdentifierOffsetMap offsets
;
1039 CYForEach (i
, internal_
) {
1040 _assert(i
->identifier_
->next_
== i
->identifier_
);
1042 case CYIdentifierArgument
:
1043 case CYIdentifierVariable
:
1044 offsets
.insert(CYIdentifierOffsetMap::value_type(i
->offset_
, i
->identifier_
));
1050 for (CYIdentifierOffsetMap::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
1051 if (offset
< i
->first
)
1053 CYIdentifier
*identifier(i
->second
);
1055 if (offset
>= context
.replace_
.size())
1056 context
.replace_
.resize(offset
+ 1, NULL
);
1057 CYIdentifier
*&replace(context
.replace_
[offset
++]);
1059 if (replace
== NULL
)
1060 replace
= identifier
;
1062 _assert(replace
->next_
== replace
);
1063 identifier
->next_
= replace
;
1067 if (parent_
== NULL
)
1070 CYForEach (i
, internal_
) {
1072 case CYIdentifierGlobal
: {
1073 if (i
->offset_
< offset
)
1074 i
->offset_
= offset
;
1075 parent_
->Merge(context
, i
);
1080 CYTarget
*CYSubscriptMember::Replace(CYContext
&context
) {
1081 return $
C1($
M(object_
, $
S("$cyg")), property_
);
1084 CYElementValue
*CYSpan::Replace(CYContext
&context
) { $
T(NULL
)
1085 return $
CYElementValue(expression_
, $
CYElementValue(string_
, next_
->Replace(context
)));
1088 CYStatement
*CYStatement::Return() {
1092 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
1093 size_t size(size_
+ rhs
->size_
);
1094 char *value($
char[size
+ 1]);
1095 memcpy(value
, value_
, size_
);
1096 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
1098 return $
S(value
, size
);
1101 CYNumber
*CYString::Number(CYContext
&context
) {
1102 // XXX: there is a precise algorithm for this
1106 CYExpression
*CYString::PropertyName(CYContext
&context
) {
1110 CYString
*CYString::String(CYContext
&context
) {
1114 CYStatement
*CYStructDefinition::Replace(CYContext
&context
) {
1115 CYTarget
*target(tail_
->Replace(context
));
1117 target
= $
C1($
M(target
, $
S("withName")), $
S(name_
->Word()));
1118 return $
CYLexical(false, $
B1($
B($
I($pool
.strcat(name_
->Word(), "$cy", NULL
)), target
)));
1121 CYTarget
*CYStructTail::Replace(CYContext
&context
) {
1122 CYList
<CYElementValue
> types
;
1123 CYList
<CYElementValue
> names
;
1125 CYForEach (field
, fields_
) {
1126 CYTypedIdentifier
*typed(field
->typed_
);
1127 types
->*$
CYElementValue(typed
->Replace(context
));
1130 if (typed
->identifier_
== NULL
)
1133 name
= $
S(typed
->identifier_
->Word());
1134 names
->*$
CYElementValue(name
);
1137 return $
N2($
V("Type"), $
CYArray(types
), $
CYArray(names
));
1140 CYTarget
*CYSuperAccess::Replace(CYContext
&context
) {
1141 return $
C1($
M($
M($
M($
V(context
.super_
), $
S("prototype")), property_
), $
S("bind")), $
CYThis());
1144 CYTarget
*CYSuperCall::Replace(CYContext
&context
) {
1145 return $
C($
C1($
M($
V(context
.super_
), $
S("bind")), $
CYThis()), arguments_
);
1148 CYTarget
*CYSymbol::Replace(CYContext
&context
) {
1149 return $
C1($
M($
V("Symbol"), $
S("for")), $
S(name_
));
1152 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
1153 context
.Replace(value_
);
1154 clauses_
->Replace(context
);
1158 CYStatement
*CYTarget::Initialize(CYContext
&context
, CYExpression
*value
) {
1161 return $
E($
CYAssign(this, value
));
1164 CYTarget
*CYTemplate::Replace(CYContext
&context
) {
1165 return $
C2($
M($
M($
M($
V("String"), $
S("prototype")), $
S("concat")), $
S("apply")), $
S(""), $
CYArray($
CYElementValue(string_
, spans_
->Replace(context
))));
1168 CYTarget
*CYThis::Replace(CYContext
&context
) {
1169 if (context
.this_
!= NULL
)
1170 return $
V(context
.this_
->Identifier(context
));
1177 CYStatement
*Throw::Replace(CYContext
&context
) {
1178 context
.Replace(value_
);
1184 CYTarget
*CYTrivial::Replace(CYContext
&context
) {
1188 CYNumber
*CYTrue::Number(CYContext
&context
) {
1192 CYString
*CYTrue::String(CYContext
&context
) {
1199 CYStatement
*Try::Replace(CYContext
&context
) {
1200 CYScope
scope(true, context
);
1201 context
.ReplaceAll(code_
);
1202 scope
.Close(context
);
1204 catch_
->Replace(context
);
1205 finally_
->Replace(context
);
1211 CYTarget
*CYTypeArrayOf::Replace_(CYContext
&context
, CYTarget
*type
) {
1212 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
1215 CYTarget
*CYTypeBlockWith::Replace_(CYContext
&context
, CYTarget
*type
) {
1216 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("blockWith")), parameters_
->Argument(context
)));
1219 CYTarget
*CYTypeCharacter::Replace(CYContext
&context
) {
1221 case CYTypeNeutral
: return $
V("char");
1222 case CYTypeSigned
: return $
V("schar");
1223 case CYTypeUnsigned
: return $
V("uchar");
1224 default: _assert(false);
1228 CYTarget
*CYTypeConstant::Replace_(CYContext
&context
, CYTarget
*type
) {
1229 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
1232 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
1233 CYIdentifier
*identifier(typed_
->identifier_
);
1234 typed_
->identifier_
= NULL
;
1235 return $
CYLexical(false, $
B1($
B(identifier
, $
CYTypeExpression(typed_
))));
1238 CYTarget
*CYTypeError::Replace(CYContext
&context
) {
1243 CYTarget
*CYTypeExpression::Replace(CYContext
&context
) {
1244 return typed_
->Replace(context
);
1247 CYTarget
*CYTypeIntegral::Replace(CYContext
&context
) {
1248 bool u(signing_
== CYTypeUnsigned
);
1250 case 0: return $
V(u
? "ushort" : "short");
1251 case 1: return $
V(u
? "uint" : "int");
1252 case 2: return $
V(u
? "ulong" : "long");
1253 case 3: return $
V(u
? "ulonglong" : "longlong");
1254 default: _assert(false);
1258 CYTarget
*CYTypeModifier::Replace(CYContext
&context
, CYTarget
*type
) { $
T(type
)
1259 return Replace_(context
, type
);
1262 CYTarget
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYTarget
*type
) {
1263 CYList
<CYArgument
> arguments(parameters_
->Argument(context
));
1265 arguments
->*$
C_($
CYNull());
1266 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), arguments
));
1269 CYTarget
*CYTypePointerTo::Replace_(CYContext
&context
, CYTarget
*type
) {
1270 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
1273 CYTarget
*CYTypeReference::Replace(CYContext
&context
) {
1274 return $
V($pool
.strcat(name_
->Word(), "$cy", NULL
));
1277 CYTarget
*CYTypeStruct::Replace(CYContext
&context
) {
1278 CYTarget
*target(tail_
->Replace(context
));
1280 target
= $
C1($
M(target
, $
S("withName")), $
S(name_
->Word()));
1284 CYTarget
*CYTypeVariable::Replace(CYContext
&context
) {
1288 CYTarget
*CYTypeVoid::Replace(CYContext
&context
) {
1289 return $
N1($
V("Type"), $
CYString("v"));
1292 CYTarget
*CYTypeVolatile::Replace_(CYContext
&context
, CYTarget
*type
) {
1293 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
1296 CYTarget
*CYTypedIdentifier::Replace(CYContext
&context
) {
1297 return modifier_
->Replace(context
, specifier_
->Replace(context
));
1300 CYTypeFunctionWith
*CYTypedIdentifier::Function() {
1301 CYTypeModifier
*&modifier(CYGetLast(modifier_
));
1302 if (modifier
== NULL
)
1305 CYTypeFunctionWith
*function(modifier
->Function());
1306 if (function
== NULL
)
1313 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
1314 return $
CYArgument(typed_
->Replace(context
), next_
->Argument(context
));
1317 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
1318 return $
CYFunctionParameter($
CYBinding(typed_
->identifier_
?: context
.Unique()), next_
->Parameters(context
));
1321 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
1322 return next_
->TypeSignature(context
, $
CYAdd(prefix
, typed_
->Replace(context
)));
1325 CYForInitializer
*CYVar::Replace(CYContext
&context
) {
1326 if (CYExpression
*expression
= bindings_
->Replace(context
, CYIdentifierVariable
))
1327 return $
E(expression
);
1331 CYTarget
*CYVariable::Replace(CYContext
&context
) {
1332 name_
= name_
->Replace(context
, CYIdentifierGlobal
);
1336 CYFunctionParameter
*CYVariable::Parameter() const {
1337 return $
CYFunctionParameter($
CYBinding(name_
));
1340 CYStatement
*CYWhile::Replace(CYContext
&context
) {
1341 context
.Replace(test_
);
1342 context
.ReplaceAll(code_
);
1346 CYStatement
*CYWith::Replace(CYContext
&context
) {
1347 context
.Replace(scope_
);
1348 CYScope
scope(true, context
);
1350 context
.ReplaceAll(code_
);
1351 scope
.Close(context
);
1355 CYExpression
*CYWord::PropertyName(CYContext
&context
) {