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_
->name_
= name_
;
186 tail_
->constructor_
= CYSuperize(context
, tail_
->constructor_
);
188 context
.super_
= old
;
190 return $
C1($
CYFunctionExpression(NULL
, $
P($
B(super
)), $$
191 ->* $
CYVar($
B1($
B(constructor
, tail_
->constructor_
)))
192 ->* $
CYVar($
B1($
B(prototype
, $
CYFunctionExpression(NULL
, NULL
, NULL
))))
193 ->* $
E($
CYAssign($
M($
V(prototype
), $
S("prototype")), $
M($
V(super
), $
S("prototype"))))
194 ->* $
E($
CYAssign($
V(prototype
), $
N($
V(prototype
))))
195 ->* CYDefineProperty($
V(prototype
), $
S("constructor"), false, false, $
CYPropertyValue($
S("value"), $
V(constructor
)))
196 ->* $
CYVar(builder
.bindings_
)
197 ->* builder
.statements_
198 ->* CYDefineProperty($
V(constructor
), $
S("prototype"), false, false, $
CYPropertyValue($
S("value"), $
V(prototype
)))
199 ->* $
CYReturn($
V(constructor
))
200 ), tail_
->extends_
?: $
V($
I("Object")));
203 CYStatement
*CYClassStatement::Replace(CYContext
&context
) {
204 return $
CYVar($
B1($
B(name_
, $
CYClassExpression(name_
, tail_
))));
207 void CYClause::Replace(CYContext
&context
) { $
T()
208 context
.Replace(value_
);
209 context
.ReplaceAll(code_
);
210 next_
->Replace(context
);
213 CYExpression
*CYCompound::Replace(CYContext
&context
) {
214 context
.Replace(expression_
);
215 context
.Replace(next_
);
217 if (CYCompound
*compound
= dynamic_cast<CYCompound
*>(expression_
)) {
218 expression_
= compound
->expression_
;
219 compound
->expression_
= compound
->next_
;
220 compound
->next_
= next_
;
227 CYFunctionParameter
*CYCompound::Parameter() const {
228 CYFunctionParameter
*next(next_
->Parameter());
232 CYFunctionParameter
*parameter(expression_
->Parameter());
233 if (parameter
== NULL
)
236 parameter
->SetNext(next
);
240 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
241 CYFunctionParameter
*next(next_
->Parameters(context
));
242 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
243 parameter
->SetNext(next
);
249 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
250 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
253 CYExpression
*CYComputed::PropertyName(CYContext
&context
) {
257 CYExpression
*CYCondition::Replace(CYContext
&context
) {
258 context
.Replace(test_
);
259 context
.Replace(true_
);
260 context
.Replace(false_
);
264 void CYContext::NonLocal(CYStatement
*&statements
) {
265 CYContext
&context(*this);
267 if (nextlocal_
!= NULL
&& nextlocal_
->identifier_
!= NULL
) {
268 CYIdentifier
*cye($
I("$cye")->Replace(context
, CYIdentifierGlobal
));
269 CYIdentifier
*unique(nextlocal_
->identifier_
->Replace(context
, CYIdentifierGlobal
));
271 CYStatement
*declare(
272 $
CYVar($
B1($
B(unique
, $
CYObject()))));
274 cy::Syntax::Catch
*rescue(
275 $
cy::Syntax::Catch(cye
, $$
276 ->* $
CYIf($
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
277 ->* $
CYReturn($
M($
V(cye
), $
S("$cyv"))))
278 ->* $
cy::Syntax::Throw($
V(cye
))));
280 context
.Replace(declare
);
281 rescue
->Replace(context
);
285 ->* $
cy::Syntax::Try(statements
, rescue
, NULL
);
289 CYIdentifier
*CYContext::Unique() {
290 return $
CYIdentifier($pool
.strcat("$cy", $pool
.itoa(unique_
++), NULL
));
293 CYStatement
*CYContinue::Replace(CYContext
&context
) {
297 CYStatement
*CYDebugger::Replace(CYContext
&context
) {
301 CYTarget
*CYBinding::Target(CYContext
&context
) {
302 return $
V(identifier_
);
305 CYAssignment
*CYBinding::Replace(CYContext
&context
, CYIdentifierKind kind
) {
306 identifier_
= identifier_
->Replace(context
, kind
);
308 if (initializer_
== NULL
)
311 CYAssignment
*value($
CYAssign(Target(context
), initializer_
));
316 CYExpression
*CYBindings::Replace(CYContext
&context
, CYIdentifierKind kind
) { $
T(NULL
)
317 CYAssignment
*assignment(binding_
->Replace(context
, kind
));
318 CYExpression
*compound(next_
->Replace(context
, kind
));
320 if (assignment
!= NULL
)
321 if (compound
== NULL
)
322 compound
= assignment
;
324 compound
= $
CYCompound(assignment
, compound
);
328 CYFunctionParameter
*CYBindings::Parameter(CYContext
&context
) { $
T(NULL
)
329 return $
CYFunctionParameter($
CYBinding(binding_
->identifier_
), next_
->Parameter(context
));
332 CYArgument
*CYBindings::Argument(CYContext
&context
) { $
T(NULL
)
333 return $
CYArgument(binding_
->initializer_
, next_
->Argument(context
));
336 CYTarget
*CYDirectMember::Replace(CYContext
&context
) {
337 context
.Replace(object_
);
338 context
.Replace(property_
);
342 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
343 context
.Replace(test_
);
344 context
.ReplaceAll(code_
);
348 void CYElementSpread::Replace(CYContext
&context
) {
349 context
.Replace(value_
);
352 void CYElementValue::Replace(CYContext
&context
) {
353 context
.Replace(value_
);
356 CYForInitializer
*CYEmpty::Replace(CYContext
&context
) {
360 CYTarget
*CYEncodedType::Replace(CYContext
&context
) {
361 return typed_
->Replace(context
);
364 CYTarget
*CYEval::Replace(CYContext
&context
) {
365 context
.scope_
->Damage();
366 if (arguments_
!= NULL
)
367 arguments_
->value_
= $
C1($
M($
V("Cycript"), $
S("compile")), arguments_
->value_
);
368 return $
C($
V("eval"), arguments_
);
371 CYStatement
*CYExpress::Return() {
372 return $
CYReturn(expression_
);
375 CYForInitializer
*CYExpress::Replace(CYContext
&context
) {
376 context
.Replace(expression_
);
380 CYTarget
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
381 return $
C1(this, value
);
384 CYFunctionParameter
*CYExpression::Parameter() const {
388 CYTarget
*CYExtend::Replace(CYContext
&context
) {
389 return object_
.Replace(context
, lhs_
);
392 CYStatement
*CYExternalDefinition::Replace(CYContext
&context
) {
393 return $
E($
CYAssign($
V(name_
), $
CYExternalExpression(abi_
, type_
, name_
)));
396 CYTarget
*CYExternalExpression::Replace(CYContext
&context
) {
397 CYExpression
*expression(name_
->Number(context
));
398 if (expression
== NULL
)
399 expression
= $
C2($
V("dlsym"), $
V("RTLD_DEFAULT"), name_
->PropertyName(context
));
400 return $
C1(type_
->Replace(context
), expression
);
403 CYNumber
*CYFalse::Number(CYContext
&context
) {
407 CYString
*CYFalse::String(CYContext
&context
) {
411 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
412 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
413 function
->this_
.SetNext(context
.this_
);
417 void CYFinally::Replace(CYContext
&context
) { $
T()
418 CYScope
scope(true, context
);
419 context
.ReplaceAll(code_
);
420 scope
.Close(context
);
423 CYStatement
*CYFor::Replace(CYContext
&context
) {
424 CYScope
outer(true, context
);
425 context
.Replace(initializer_
);
427 context
.Replace(test_
);
430 CYScope
inner(true, context
);
431 context
.ReplaceAll(code_
);
432 inner
.Close(context
);
435 context
.Replace(increment_
);
437 outer
.Close(context
);
441 CYStatement
*CYForLexical::Initialize(CYContext
&context
, CYExpression
*value
) {
443 if (binding_
->initializer_
== NULL
)
445 value
= binding_
->initializer_
;
448 return $
CYLexical(constant_
, $
B1($
CYBinding(binding_
->identifier_
, value
)));
451 CYTarget
*CYForLexical::Replace(CYContext
&context
) {
452 _assert(binding_
->Replace(context
, CYIdentifierLexical
) == NULL
);
453 return binding_
->Target(context
);
456 CYStatement
*CYForIn::Replace(CYContext
&context
) {
457 CYScope
scope(true, context
);
458 context
.Replace(initializer_
);
459 context
.Replace(iterable_
);
460 context
.ReplaceAll(code_
);
461 scope
.Close(context
);
465 CYStatement
*CYForInitialized::Replace(CYContext
&context
) {
466 CYAssignment
*assignment(binding_
->Replace(context
, CYIdentifierVariable
));
468 ->* (assignment
== NULL
? NULL
: $
CYExpress(assignment
))
469 ->* $
CYForIn(binding_
->Target(context
), iterable_
, code_
));
472 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
473 return $
CYFunctionParameter(binding_
);
476 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
477 return $
CYForIn(binding_
->Target(context
), iterable_
, CYComprehension::Replace(context
, statement
));
480 CYStatement
*CYForOf::Replace(CYContext
&context
) {
481 CYIdentifier
*item(context
.Unique()), *list(context
.Unique());
484 ->* initializer_
->Initialize(context
, NULL
)
485 ->* $
CYLexical(false, $
B2($
B(list
, iterable_
), $
B(item
)))
486 ->* $
CYForIn($
V(item
), $
V(list
), $
CYBlock($$
487 ->* initializer_
->Initialize(context
, $
M($
V(list
), $
V(item
)))
492 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
493 return $
CYFunctionParameter(binding_
);
496 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
497 CYIdentifier
*cys(context
.Unique());
500 ->* $
CYLexical(false, $
B1($
B(cys
, iterable_
)))
501 ->* $
CYForIn(binding_
->Target(context
), $
V(cys
), $
CYBlock($$
502 ->* $
E($
CYAssign(binding_
->Target(context
), $
M($
V(cys
), binding_
->Target(context
))))
503 ->* CYComprehension::Replace(context
, statement
)
507 CYStatement
*CYForVariable::Initialize(CYContext
&context
, CYExpression
*value
) {
509 if (binding_
->initializer_
== NULL
)
511 value
= binding_
->initializer_
;
514 return $
CYVar($
B1($
CYBinding(binding_
->identifier_
, value
)));
517 CYTarget
*CYForVariable::Replace(CYContext
&context
) {
518 _assert(binding_
->Replace(context
, CYIdentifierVariable
) == NULL
);
519 return binding_
->Target(context
);
522 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
523 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
524 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
526 void CYFunction::Replace(CYContext
&context
) {
527 CYThisScope
*_this(context
.this_
);
528 context
.this_
= &this_
;
529 context
.this_
= CYGetLast(context
.this_
);
531 CYIdentifier
*super(context
.super_
);
532 context
.super_
= super_
;
534 CYNonLocal
*nonlocal(context
.nonlocal_
);
535 CYNonLocal
*nextlocal(context
.nextlocal_
);
538 if (nonlocal_
!= NULL
) {
540 context
.nonlocal_
= nonlocal_
;
543 nonlocal_
= $
CYNonLocal();
544 context
.nextlocal_
= nonlocal_
;
547 CYScope
scope(!localize
, context
);
549 $
I("arguments")->Replace(context
, CYIdentifierMagic
);
551 parameters_
->Replace(context
, code_
);
553 context
.ReplaceAll(code_
);
556 CYImplicitReturn(code_
);
558 if (CYIdentifier
*identifier
= this_
.identifier_
) {
559 context
.scope_
->Declare(context
, identifier
, CYIdentifierVariable
);
561 ->* $
E($
CYAssign($
V(identifier
), $
CYThis()))
566 context
.NonLocal(code_
);
568 context
.nextlocal_
= nextlocal
;
569 context
.nonlocal_
= nonlocal
;
571 context
.super_
= super
;
572 context
.this_
= _this
;
574 scope
.Close(context
, code_
);
577 CYTarget
*CYFunctionExpression::Replace(CYContext
&context
) {
578 CYScope
scope(false, context
);
580 name_
= name_
->Replace(context
, CYIdentifierOther
);
582 CYFunction::Replace(context
);
583 scope
.Close(context
);
587 void CYFunctionParameter::Replace(CYContext
&context
, CYStatement
*&statements
) { $
T()
588 CYAssignment
*assignment(binding_
->Replace(context
, CYIdentifierArgument
));
590 next_
->Replace(context
, statements
);
592 if (assignment
!= NULL
)
594 ->* $
CYIf($
CYIdentical($
CYTypeOf(binding_
->Target(context
)), $
S("undefined")), $$
599 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
600 name_
= name_
->Replace(context
, CYIdentifierOther
);
601 CYFunction::Replace(context
);
605 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
, CYIdentifierKind kind
) {
609 return next_
->Replace(context
, kind
);
610 next_
= context
.scope_
->Declare(context
, this, kind
)->identifier_
;
614 CYStatement
*CYIf::Return() {
615 CYImplicitReturn(true_
);
616 CYImplicitReturn(false_
);
620 CYStatement
*CYIf::Replace(CYContext
&context
) {
621 context
.Replace(test_
);
622 context
.ReplaceAll(true_
);
623 context
.ReplaceAll(false_
);
627 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
631 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
632 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
635 CYStatement
*CYImport::Replace(CYContext
&context
) {
636 return $
CYVar($
B1($
B($
I(module_
->part_
->Word()), $
C1($
V("require"), module_
->Replace(context
, "/")))));
639 CYStatement
*CYImportDeclaration::Replace(CYContext
&context
) {
640 CYIdentifier
*module(context
.Unique());
642 CYList
<CYStatement
> statements
;
643 CYForEach (specifier
, specifiers_
)
644 statements
->*specifier
->Replace(context
, module);
647 ->* $
CYLexical(false, $
B1($
B(module, $
C1($
V("require"), module_
))))
651 CYStatement
*CYImportSpecifier::Replace(CYContext
&context
, CYIdentifier
*module) {
652 binding_
= binding_
->Replace(context
, CYIdentifierLexical
);
654 CYExpression
*import($
V(module));
656 import = $
M(import, $
S(name_
));
657 return $
E($
CYAssign($
V(binding_
), import));
660 CYTarget
*CYIndirect::Replace(CYContext
&context
) {
661 return $
M(rhs_
, $
S("$cyi"));
664 CYTarget
*CYIndirectMember::Replace(CYContext
&context
) {
665 return $
M($
CYIndirect(object_
), property_
);
668 CYExpression
*CYInfix::Replace(CYContext
&context
) {
669 context
.Replace(lhs_
);
670 context
.Replace(rhs_
);
674 CYStatement
*CYLabel::Replace(CYContext
&context
) {
675 context
.Replace(statement_
);
679 CYTarget
*CYLambda::Replace(CYContext
&context
) {
680 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), code_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
683 CYForInitializer
*CYLexical::Replace(CYContext
&context
) {
684 if (CYExpression
*expression
= bindings_
->Replace(context
, CYIdentifierLexical
))
685 return $
E(expression
);
689 CYFunctionExpression
*CYMethod::Constructor() {
693 void CYMethod::Replace(CYContext
&context
) {
694 CYFunction::Replace(context
);
697 CYString
*CYModule::Replace(CYContext
&context
, const char *separator
) const {
699 return $
CYString(part_
);
700 return $
CYString($pool
.strcat(next_
->Replace(context
, separator
)->Value(), separator
, part_
->Word(), NULL
));
703 CYExpression
*CYMultiply::Replace(CYContext
&context
) {
704 CYInfix::Replace(context
);
706 if (CYNumber
*lhn
= lhs_
->Number(context
))
707 if (CYNumber
*rhn
= rhs_
->Number(context
))
708 return $
D(lhn
->Value() * rhn
->Value());
716 CYTarget
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
717 CYSetLast(arguments_
) = $
CYArgument(value
);
721 CYTarget
*New::Replace(CYContext
&context
) {
722 context
.Replace(constructor_
);
723 arguments_
->Replace(context
);
729 CYNumber
*CYNull::Number(CYContext
&context
) {
733 CYString
*CYNull::String(CYContext
&context
) {
737 CYNumber
*CYNumber::Number(CYContext
&context
) {
741 CYString
*CYNumber::String(CYContext
&context
) {
742 // XXX: there is a precise algorithm for this
743 return $
S($pool
.sprintf(24, "%.17g", Value()));
746 CYExpression
*CYNumber::PropertyName(CYContext
&context
) {
747 return String(context
);
750 CYTarget
*CYObject::Replace(CYContext
&context
, CYTarget
*seed
) {
752 if (properties_
!= NULL
)
753 properties_
= properties_
->ReplaceAll(context
, builder
, $
CYThis(), seed
!= this);
756 return $
C1($
M($
CYFunctionExpression(NULL
, builder
.bindings_
->Parameter(context
),
758 ->* $
CYReturn($
CYThis())
759 ), $
S("call")), seed
, builder
.bindings_
->Argument(context
));
762 CYForEach (property
, properties_
)
763 property
->Replace(context
);
767 CYTarget
*CYObject::Replace(CYContext
&context
) {
768 return Replace(context
, this);
771 CYTarget
*CYParenthetical::Replace(CYContext
&context
) {
772 // XXX: return expression_;
773 context
.Replace(expression_
);
777 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
778 context
.Replace(lhs_
);
782 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
783 context
.Replace(rhs_
);
787 CYProperty
*CYProperty::ReplaceAll(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, bool update
) {
790 Replace(context
, builder
, self
, false);
792 next_
= next_
->ReplaceAll(context
, builder
, self
, update
);
793 return update
? next_
: this;
796 void CYProperty::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, bool protect
) {
797 CYExpression
*name(name_
->PropertyName(context
));
798 if (name_
->Computed()) {
799 CYIdentifier
*unique(context
.Unique());
801 ->* $
B1($
B(unique
, name
));
805 Replace(context
, builder
, self
, name
, protect
);
808 bool CYProperty::Update() const {
809 return name_
->Computed();
812 void CYPropertyGetter::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
813 CYIdentifier
*unique(context
.Unique());
815 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
817 ->* CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("get"), $
V(unique
)));
820 CYFunctionExpression
*CYPropertyMethod::Constructor() {
821 return name_
->Constructor() ? $
CYFunctionExpression(NULL
, parameters_
, code_
) : NULL
;
824 void CYPropertyMethod::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
825 CYIdentifier
*unique(context
.Unique());
827 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
829 ->* (!protect
? $
E($
CYAssign($
M(self
, name
), $
V(unique
))) :
830 CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("value"), $
V(unique
), $
CYPropertyValue($
S("writable"), $
CYTrue()))));
833 bool CYPropertyMethod::Update() const {
837 void CYPropertySetter::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
838 CYIdentifier
*unique(context
.Unique());
840 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
842 ->* CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("set"), $
V(unique
)));
845 void CYPropertyValue::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
847 CYIdentifier
*unique(context
.Unique());
849 ->* $
B1($
B(unique
, value_
));
851 ->* $
E($
CYAssign($
M(self
, name
), $
V(unique
)));
854 void CYPropertyValue::Replace(CYContext
&context
) {
855 context
.Replace(value_
);
858 void CYScript::Replace(CYContext
&context
) {
859 CYScope
scope(false, context
);
860 context
.scope_
->Damage();
862 context
.nextlocal_
= $
CYNonLocal();
863 context
.ReplaceAll(code_
);
864 context
.NonLocal(code_
);
866 scope
.Close(context
, code_
);
870 for (std::vector
<CYIdentifier
*>::const_iterator
i(context
.replace_
.begin()); i
!= context
.replace_
.end(); ++i
) {
872 if (context
.options_
.verbose_
)
873 name
= $pool
.strcat("$", $pool
.itoa(offset
++), NULL
);
879 unsigned position(7), local(offset
++ + 1);
882 unsigned index(local
% (sizeof(MappingSet
) - 1));
883 local
/= sizeof(MappingSet
) - 1;
884 id
[--position
] = MappingSet
[index
];
885 } while (local
!= 0);
887 if (scope
.Lookup(context
, id
+ position
) != NULL
)
889 // XXX: at some point, this could become a keyword
891 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
894 CYIdentifier
*identifier(*i
);
895 _assert(identifier
->next_
== identifier
);
896 identifier
->next_
= $
I(name
);
900 CYTarget
*CYResolveMember::Replace(CYContext
&context
) {
901 return $
M($
M(object_
, $
S("$cyr")), property_
);
904 CYStatement
*CYReturn::Replace(CYContext
&context
) {
905 if (context
.nonlocal_
!= NULL
) {
906 CYProperty
*value(value_
== NULL
? NULL
: $
CYPropertyValue($
S("$cyv"), value_
));
907 return $
cy::Syntax::Throw($
CYObject(
908 $
CYPropertyValue($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
912 context
.Replace(value_
);
916 CYTarget
*CYRubyBlock::Replace(CYContext
&context
) {
917 return lhs_
->AddArgument(context
, proc_
->Replace(context
));
920 CYTarget
*CYRubyBlock::AddArgument(CYContext
&context
, CYExpression
*value
) {
921 return Replace(context
)->AddArgument(context
, value
);
924 CYTarget
*CYRubyProc::Replace(CYContext
&context
) {
925 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
926 function
= CYNonLocalize(context
, function
);
927 function
->implicit_
= true;
931 CYScope::CYScope(bool transparent
, CYContext
&context
) :
932 transparent_(transparent
),
933 parent_(context
.scope_
),
938 _assert(!transparent_
|| parent_
!= NULL
);
939 context
.scope_
= this;
942 void CYScope::Damage() {
948 CYIdentifierFlags
*CYScope::Lookup(CYContext
&context
, const char *word
) {
949 CYForEach (i
, internal_
)
950 if (strcmp(i
->identifier_
->Word(), word
) == 0)
955 CYIdentifierFlags
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
956 return Lookup(context
, identifier
->Word());
959 CYIdentifierFlags
*CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierKind kind
) {
960 _assert(identifier
->next_
== NULL
|| identifier
->next_
== identifier
);
962 CYIdentifierFlags
*existing(Lookup(context
, identifier
));
963 if (existing
== NULL
)
964 internal_
= $
CYIdentifierFlags(identifier
, kind
, internal_
);
966 if (existing
== NULL
)
969 if (kind
== CYIdentifierGlobal
);
970 else if (existing
->kind_
== CYIdentifierGlobal
|| existing
->kind_
== CYIdentifierMagic
)
971 existing
->kind_
= kind
;
972 else if (existing
->kind_
== CYIdentifierLexical
|| kind
== CYIdentifierLexical
)
974 else if (transparent_
&& existing
->kind_
== CYIdentifierArgument
&& kind
== CYIdentifierVariable
)
976 // XXX: throw new SyntaxError() instead of these asserts
981 void CYScope::Merge(CYContext
&context
, const CYIdentifierFlags
*flags
) {
982 _assert(flags
->identifier_
->next_
== flags
->identifier_
);
983 CYIdentifierFlags
*existing(Declare(context
, flags
->identifier_
, flags
->kind_
));
984 flags
->identifier_
->next_
= existing
->identifier_
;
986 existing
->count_
+= flags
->count_
;
987 if (existing
->offset_
< flags
->offset_
)
988 existing
->offset_
= flags
->offset_
;
991 void CYScope::Close(CYContext
&context
, CYStatement
*&statements
) {
994 CYList
<CYBindings
> bindings
;
996 CYForEach (i
, internal_
)
997 if (i
->kind_
== CYIdentifierVariable
)
999 ->* $
CYBindings($
CYBinding(i
->identifier_
));
1002 CYVar
*var($
CYVar(bindings
));
1003 var
->SetNext(statements
);
1008 void CYScope::Close(CYContext
&context
) {
1009 context
.scope_
= parent_
;
1011 CYForEach (i
, internal_
) {
1012 _assert(i
->identifier_
->next_
== i
->identifier_
);
1014 case CYIdentifierLexical
: {
1016 CYIdentifier
*replace(context
.Unique());
1017 replace
->next_
= replace
;
1018 i
->identifier_
->next_
= replace
;
1019 i
->identifier_
= replace
;
1023 i
->kind_
= CYIdentifierVariable
;
1025 parent_
->Declare(context
, i
->identifier_
, CYIdentifierVariable
);
1028 case CYIdentifierVariable
: {
1030 parent_
->Declare(context
, i
->identifier_
, i
->kind_
);
1031 i
->kind_
= CYIdentifierGlobal
;
1039 typedef std::multimap
<unsigned, CYIdentifier
*> CYIdentifierOffsetMap
;
1040 CYIdentifierOffsetMap offsets
;
1042 CYForEach (i
, internal_
) {
1043 _assert(i
->identifier_
->next_
== i
->identifier_
);
1045 case CYIdentifierArgument
:
1046 case CYIdentifierVariable
:
1047 offsets
.insert(CYIdentifierOffsetMap::value_type(i
->offset_
, i
->identifier_
));
1053 for (CYIdentifierOffsetMap::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
1054 if (offset
< i
->first
)
1056 CYIdentifier
*identifier(i
->second
);
1058 if (offset
>= context
.replace_
.size())
1059 context
.replace_
.resize(offset
+ 1, NULL
);
1060 CYIdentifier
*&replace(context
.replace_
[offset
++]);
1062 if (replace
== NULL
)
1063 replace
= identifier
;
1065 _assert(replace
->next_
== replace
);
1066 identifier
->next_
= replace
;
1070 if (parent_
== NULL
)
1073 CYForEach (i
, internal_
) {
1075 case CYIdentifierGlobal
: {
1076 if (i
->offset_
< offset
)
1077 i
->offset_
= offset
;
1078 parent_
->Merge(context
, i
);
1083 CYTarget
*CYSubscriptMember::Replace(CYContext
&context
) {
1084 return $
C1($
M(object_
, $
S("$cyg")), property_
);
1087 CYElementValue
*CYSpan::Replace(CYContext
&context
) { $
T(NULL
)
1088 return $
CYElementValue(expression_
, $
CYElementValue(string_
, next_
->Replace(context
)));
1091 CYStatement
*CYStatement::Return() {
1095 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
1096 size_t size(size_
+ rhs
->size_
);
1097 char *value($
char[size
+ 1]);
1098 memcpy(value
, value_
, size_
);
1099 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
1101 return $
S(value
, size
);
1104 CYIdentifier
*CYString::Identifier() const {
1105 if (const char *word
= Word())
1106 return $
CYIdentifier(word
);
1110 CYNumber
*CYString::Number(CYContext
&context
) {
1111 // XXX: there is a precise algorithm for this
1115 CYExpression
*CYString::PropertyName(CYContext
&context
) {
1119 CYString
*CYString::String(CYContext
&context
) {
1123 CYStatement
*CYStructDefinition::Replace(CYContext
&context
) {
1124 CYTarget
*target(tail_
->Replace(context
));
1126 target
= $
C1($
M(target
, $
S("withName")), $
S(name_
->Word()));
1127 return $
CYLexical(false, $
B1($
B($
I($pool
.strcat(name_
->Word(), "$cy", NULL
)), target
)));
1130 CYTarget
*CYStructTail::Replace(CYContext
&context
) {
1131 CYList
<CYElementValue
> types
;
1132 CYList
<CYElementValue
> names
;
1134 CYForEach (field
, fields_
) {
1135 types
->*$
CYElementValue(field
->type_
->Replace(context
));
1138 if (field
->name_
== NULL
)
1141 name
= field
->name_
->PropertyName(context
);
1142 names
->*$
CYElementValue(name
);
1145 return $
N2($
V("Type"), $
CYArray(types
), $
CYArray(names
));
1148 CYTarget
*CYSuperAccess::Replace(CYContext
&context
) {
1149 return $
C1($
M($
M($
M($
V(context
.super_
), $
S("prototype")), property_
), $
S("bind")), $
CYThis());
1152 CYTarget
*CYSuperCall::Replace(CYContext
&context
) {
1153 return $
C($
C1($
M($
V(context
.super_
), $
S("bind")), $
CYThis()), arguments_
);
1156 CYTarget
*CYSymbol::Replace(CYContext
&context
) {
1157 return $
C1($
M($
V("Symbol"), $
S("for")), $
S(name_
));
1160 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
1161 context
.Replace(value_
);
1162 clauses_
->Replace(context
);
1166 CYStatement
*CYTarget::Initialize(CYContext
&context
, CYExpression
*value
) {
1169 return $
E($
CYAssign(this, value
));
1172 CYTarget
*CYTemplate::Replace(CYContext
&context
) {
1173 return $
C2($
M($
M($
M($
V("String"), $
S("prototype")), $
S("concat")), $
S("apply")), $
S(""), $
CYArray($
CYElementValue(string_
, spans_
->Replace(context
))));
1176 CYString
*CYTemplate::String(CYContext
&context
) {
1177 // XXX: implement this over local concat
1183 CYTarget
*CYThis::Replace(CYContext
&context
) {
1184 if (context
.this_
!= NULL
)
1185 return $
V(context
.this_
->Identifier(context
));
1192 CYStatement
*Throw::Replace(CYContext
&context
) {
1193 context
.Replace(value_
);
1199 CYTarget
*CYTrivial::Replace(CYContext
&context
) {
1203 CYNumber
*CYTrue::Number(CYContext
&context
) {
1207 CYString
*CYTrue::String(CYContext
&context
) {
1214 CYStatement
*Try::Replace(CYContext
&context
) {
1215 CYScope
scope(true, context
);
1216 context
.ReplaceAll(code_
);
1217 scope
.Close(context
);
1219 catch_
->Replace(context
);
1220 finally_
->Replace(context
);
1226 CYTarget
*CYTypeArrayOf::Replace_(CYContext
&context
, CYTarget
*type
) {
1227 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
1230 CYTarget
*CYTypeBlockWith::Replace_(CYContext
&context
, CYTarget
*type
) {
1231 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("blockWith")), parameters_
->Argument(context
)));
1234 CYTarget
*CYTypeCharacter::Replace(CYContext
&context
) {
1236 case CYTypeNeutral
: return $
V("char");
1237 case CYTypeSigned
: return $
V("schar");
1238 case CYTypeUnsigned
: return $
V("uchar");
1239 default: _assert(false);
1243 CYTarget
*CYTypeConstant::Replace_(CYContext
&context
, CYTarget
*type
) {
1244 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
1247 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
1248 return $
CYLexical(false, $
B1($
B(name_
, $
CYTypeExpression(type_
))));
1251 CYTarget
*CYTypeEnum::Replace(CYContext
&context
) {
1252 CYList
<CYProperty
> properties
;
1253 CYForEach (constant
, constants_
)
1254 properties
->*$
CYPropertyValue($
S(constant
->name_
->Word()), constant
->value_
);
1255 CYObject
*constants($
CYObject(properties
));
1257 if (specifier_
== NULL
)
1258 return $
N1($
V("Type"), constants
);
1260 return $
C1($
M(specifier_
->Replace(context
), $
S("enumFor")), constants
);
1263 CYTarget
*CYTypeError::Replace(CYContext
&context
) {
1268 CYTarget
*CYTypeExpression::Replace(CYContext
&context
) {
1269 return typed_
->Replace(context
);
1272 CYTarget
*CYTypeInt128::Replace(CYContext
&context
) {
1273 return $
V(signing_
== CYTypeUnsigned
? "uint128" : "int128");
1276 CYTarget
*CYTypeIntegral::Replace(CYContext
&context
) {
1277 bool u(signing_
== CYTypeUnsigned
);
1279 case 0: return $
V(u
? "ushort" : "short");
1280 case 1: return $
V(u
? "uint" : "int");
1281 case 2: return $
V(u
? "ulong" : "long");
1282 case 3: return $
V(u
? "ulonglong" : "longlong");
1283 default: _assert(false);
1287 CYTarget
*CYTypeModifier::Replace(CYContext
&context
, CYTarget
*type
) { $
T(type
)
1288 return Replace_(context
, type
);
1291 CYTarget
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYTarget
*type
) {
1292 CYList
<CYArgument
> arguments(parameters_
->Argument(context
));
1294 arguments
->*$
C_($
CYNull());
1295 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), arguments
));
1298 CYTarget
*CYTypePointerTo::Replace_(CYContext
&context
, CYTarget
*type
) {
1299 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
1302 CYTarget
*CYTypeReference::Replace(CYContext
&context
) {
1305 case CYTypeReferenceStruct
: prefix
= "$cys"; break;
1306 case CYTypeReferenceEnum
: prefix
= "$cye"; break;
1307 default: _assert(false);
1310 return $
V($pool
.strcat(prefix
, name_
->Word(), NULL
));
1313 CYTarget
*CYTypeStruct::Replace(CYContext
&context
) {
1314 CYTarget
*target(tail_
->Replace(context
));
1316 target
= $
C1($
M(target
, $
S("withName")), $
S(name_
->Word()));
1320 CYTarget
*CYTypeVariable::Replace(CYContext
&context
) {
1324 CYTarget
*CYTypeVoid::Replace(CYContext
&context
) {
1325 return $
N1($
V("Type"), $
CYString("v"));
1328 CYTarget
*CYTypeVolatile::Replace_(CYContext
&context
, CYTarget
*type
) {
1329 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
1332 CYTarget
*CYType::Replace(CYContext
&context
) {
1333 return modifier_
->Replace(context
, specifier_
->Replace(context
));
1336 CYTypeFunctionWith
*CYType::Function() {
1337 CYTypeModifier
*&modifier(CYGetLast(modifier_
));
1338 if (modifier
== NULL
)
1341 CYTypeFunctionWith
*function(modifier
->Function());
1342 if (function
== NULL
)
1349 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
1350 return $
CYArgument(type_
->Replace(context
), next_
->Argument(context
));
1353 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
1354 return $
CYFunctionParameter($
CYBinding(name_
?: context
.Unique()), next_
->Parameters(context
));
1357 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
1358 return next_
->TypeSignature(context
, $
CYAdd(prefix
, type_
->Replace(context
)));
1361 CYForInitializer
*CYVar::Replace(CYContext
&context
) {
1362 if (CYExpression
*expression
= bindings_
->Replace(context
, CYIdentifierVariable
))
1363 return $
E(expression
);
1367 CYTarget
*CYVariable::Replace(CYContext
&context
) {
1368 name_
= name_
->Replace(context
, CYIdentifierGlobal
);
1372 CYFunctionParameter
*CYVariable::Parameter() const {
1373 return $
CYFunctionParameter($
CYBinding(name_
));
1376 CYStatement
*CYWhile::Replace(CYContext
&context
) {
1377 context
.Replace(test_
);
1378 context
.ReplaceAll(code_
);
1382 CYStatement
*CYWith::Replace(CYContext
&context
) {
1383 context
.Replace(scope_
);
1384 CYScope
scope(true, context
);
1386 context
.ReplaceAll(code_
);
1387 scope
.Close(context
);
1391 CYExpression
*CYWord::PropertyName(CYContext
&context
) {