1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 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 context
.Replace(lhs_
);
121 context
.Replace(rhs_
);
125 CYStatement
*CYBlock::Return() {
126 CYImplicitReturn(code_
);
130 CYStatement
*CYBlock::Replace(CYContext
&context
) {
131 CYScope
scope(true, context
);
132 context
.ReplaceAll(code_
);
133 scope
.Close(context
);
140 CYStatement
*CYBreak::Replace(CYContext
&context
) {
144 CYTarget
*CYCall::Replace(CYContext
&context
) {
145 context
.Replace(function_
);
146 arguments_
->Replace(context
);
153 void Catch::Replace(CYContext
&context
) { $
T()
154 CYScope
scope(true, context
);
156 name_
= name_
->Replace(context
, CYIdentifierCatch
);
158 context
.ReplaceAll(code_
);
159 scope
.Close(context
);
164 CYTarget
*CYClassExpression::Replace(CYContext
&context
) {
167 CYIdentifier
*super(context
.Unique());
169 CYIdentifier
*old(context
.super_
);
170 context
.super_
= super
;
172 CYIdentifier
*constructor(context
.Unique());
173 CYForEach (member
, tail_
->static_
)
174 member
->Replace(context
, builder
, $
V(constructor
), true);
176 CYIdentifier
*prototype(context
.Unique());
177 CYForEach (member
, tail_
->instance_
)
178 member
->Replace(context
, builder
, $
V(prototype
), true);
180 if (tail_
->constructor_
== NULL
)
181 tail_
->constructor_
= $
CYFunctionExpression(NULL
, NULL
, NULL
);
182 tail_
->constructor_
= CYSuperize(context
, tail_
->constructor_
);
184 context
.super_
= old
;
186 return $
C1($
CYFunctionExpression(NULL
, $
P($
B(super
)), $$
187 ->* $
CYVar($
B1($
B(constructor
, tail_
->constructor_
)))
188 ->* $
CYVar($
B1($
B(prototype
, $
CYFunctionExpression(NULL
, NULL
, NULL
))))
189 ->* $
E($
CYAssign($
M($
V(prototype
), $
S("prototype")), $
M($
V(super
), $
S("prototype"))))
190 ->* $
E($
CYAssign($
V(prototype
), $
N($
V(prototype
))))
191 ->* CYDefineProperty($
V(prototype
), $
S("constructor"), false, false, $
CYPropertyValue($
S("value"), $
V(constructor
)))
192 ->* $
CYVar(builder
.bindings_
)
193 ->* builder
.statements_
194 ->* CYDefineProperty($
V(constructor
), $
S("prototype"), false, false, $
CYPropertyValue($
S("value"), $
V(prototype
)))
195 ->* $
CYReturn($
V(constructor
))
196 ), tail_
->extends_
?: $
V($
I("Object")));
199 CYStatement
*CYClassStatement::Replace(CYContext
&context
) {
200 return $
CYVar($
B1($
B(name_
, $
CYClassExpression(name_
, tail_
))));
203 void CYClause::Replace(CYContext
&context
) { $
T()
204 context
.Replace(value_
);
205 context
.ReplaceAll(code_
);
206 next_
->Replace(context
);
209 CYExpression
*CYCompound::Replace(CYContext
&context
) {
210 context
.Replace(expression_
);
211 context
.Replace(next_
);
213 if (CYCompound
*compound
= dynamic_cast<CYCompound
*>(expression_
)) {
214 expression_
= compound
->expression_
;
215 compound
->expression_
= compound
->next_
;
216 compound
->next_
= next_
;
223 CYFunctionParameter
*CYCompound::Parameter() const {
224 CYFunctionParameter
*next(next_
->Parameter());
228 CYFunctionParameter
*parameter(expression_
->Parameter());
229 if (parameter
== NULL
)
232 parameter
->SetNext(next
);
236 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
237 CYFunctionParameter
*next(next_
->Parameters(context
));
238 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
239 parameter
->SetNext(next
);
245 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
246 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
249 CYExpression
*CYComputed::PropertyName(CYContext
&context
) {
253 CYExpression
*CYCondition::Replace(CYContext
&context
) {
254 context
.Replace(test_
);
255 context
.Replace(true_
);
256 context
.Replace(false_
);
260 void CYContext::NonLocal(CYStatement
*&statements
) {
261 CYContext
&context(*this);
263 if (nextlocal_
!= NULL
&& nextlocal_
->identifier_
!= NULL
) {
264 CYIdentifier
*cye($
I("$cye")->Replace(context
, CYIdentifierGlobal
));
265 CYIdentifier
*unique(nextlocal_
->identifier_
->Replace(context
, CYIdentifierGlobal
));
267 CYStatement
*declare(
268 $
CYVar($
B1($
B(unique
, $
CYObject()))));
270 cy::Syntax::Catch
*rescue(
271 $
cy::Syntax::Catch(cye
, $$
272 ->* $
CYIf($
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
273 ->* $
CYReturn($
M($
V(cye
), $
S("$cyv"))))
274 ->* $
cy::Syntax::Throw($
V(cye
))));
276 context
.Replace(declare
);
277 rescue
->Replace(context
);
281 ->* $
cy::Syntax::Try(statements
, rescue
, NULL
);
285 CYIdentifier
*CYContext::Unique() {
286 return $
CYIdentifier($pool
.strcat("$cy", $pool
.itoa(unique_
++), NULL
));
289 CYStatement
*CYContinue::Replace(CYContext
&context
) {
293 CYStatement
*CYDebugger::Replace(CYContext
&context
) {
297 CYTarget
*CYBinding::Target(CYContext
&context
) {
298 return $
V(identifier_
);
301 CYAssignment
*CYBinding::Replace(CYContext
&context
, CYIdentifierKind kind
) {
302 identifier_
= identifier_
->Replace(context
, kind
);
304 if (initializer_
== NULL
)
307 CYAssignment
*value($
CYAssign(Target(context
), initializer_
));
312 CYExpression
*CYBindings::Replace(CYContext
&context
, CYIdentifierKind kind
) { $
T(NULL
)
313 CYAssignment
*assignment(binding_
->Replace(context
, kind
));
314 CYExpression
*compound(next_
->Replace(context
, kind
));
316 if (assignment
!= NULL
)
317 if (compound
== NULL
)
318 compound
= assignment
;
320 compound
= $
CYCompound(assignment
, compound
);
324 CYFunctionParameter
*CYBindings::Parameter(CYContext
&context
) { $
T(NULL
)
325 return $
CYFunctionParameter($
CYBinding(binding_
->identifier_
), next_
->Parameter(context
));
328 CYArgument
*CYBindings::Argument(CYContext
&context
) { $
T(NULL
)
329 return $
CYArgument(binding_
->initializer_
, next_
->Argument(context
));
332 CYTarget
*CYDirectMember::Replace(CYContext
&context
) {
333 context
.Replace(object_
);
334 context
.Replace(property_
);
338 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
339 context
.Replace(test_
);
340 context
.ReplaceAll(code_
);
344 void CYElementSpread::Replace(CYContext
&context
) {
345 context
.Replace(value_
);
348 void CYElementValue::Replace(CYContext
&context
) {
349 context
.Replace(value_
);
352 CYForInitializer
*CYEmpty::Replace(CYContext
&context
) {
356 CYTarget
*CYEncodedType::Replace(CYContext
&context
) {
357 return typed_
->Replace(context
);
360 CYTarget
*CYEval::Replace(CYContext
&context
) {
361 context
.scope_
->Damage();
362 if (arguments_
!= NULL
)
363 arguments_
->value_
= $
C1($
M($
V("Cycript"), $
S("compile")), arguments_
->value_
);
364 return $
C($
V("eval"), arguments_
);
367 CYStatement
*CYExpress::Return() {
368 return $
CYReturn(expression_
);
371 CYForInitializer
*CYExpress::Replace(CYContext
&context
) {
372 context
.Replace(expression_
);
376 CYTarget
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
377 return $
C1(this, value
);
380 CYFunctionParameter
*CYExpression::Parameter() const {
384 CYStatement
*CYExternal::Replace(CYContext
&context
) {
385 return $
E($
CYAssign($
V(typed_
->identifier_
), $
C1(typed_
->Replace(context
), $
C2($
V("dlsym"), $
V("RTLD_DEFAULT"), $
S(typed_
->identifier_
->Word())))));
388 CYNumber
*CYFalse::Number(CYContext
&context
) {
392 CYString
*CYFalse::String(CYContext
&context
) {
396 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
397 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
398 function
->this_
.SetNext(context
.this_
);
402 void CYFinally::Replace(CYContext
&context
) { $
T()
403 CYScope
scope(true, context
);
404 context
.ReplaceAll(code_
);
405 scope
.Close(context
);
408 CYStatement
*CYFor::Replace(CYContext
&context
) {
409 CYScope
outer(true, context
);
410 context
.Replace(initializer_
);
412 context
.Replace(test_
);
415 CYScope
inner(true, context
);
416 context
.ReplaceAll(code_
);
417 inner
.Close(context
);
420 context
.Replace(increment_
);
422 outer
.Close(context
);
426 CYStatement
*CYForLexical::Initialize(CYContext
&context
, CYExpression
*value
) {
428 if (binding_
->initializer_
== NULL
)
430 value
= binding_
->initializer_
;
433 return $
CYLexical(constant_
, $
B1($
CYBinding(binding_
->identifier_
, value
)));
436 CYTarget
*CYForLexical::Replace(CYContext
&context
) {
437 _assert(binding_
->Replace(context
, CYIdentifierLexical
) == NULL
);
438 return binding_
->Target(context
);
441 CYStatement
*CYForIn::Replace(CYContext
&context
) {
442 CYScope
scope(true, context
);
443 context
.Replace(initializer_
);
444 context
.Replace(iterable_
);
445 context
.ReplaceAll(code_
);
446 scope
.Close(context
);
450 CYStatement
*CYForInitialized::Replace(CYContext
&context
) {
451 CYAssignment
*assignment(binding_
->Replace(context
, CYIdentifierVariable
));
453 ->* (assignment
== NULL
? NULL
: $
CYExpress(assignment
))
454 ->* $
CYForIn(binding_
->Target(context
), iterable_
, code_
));
457 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
458 return $
CYFunctionParameter(binding_
);
461 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
462 return $
CYForIn(binding_
->Target(context
), iterable_
, CYComprehension::Replace(context
, statement
));
465 CYStatement
*CYForOf::Replace(CYContext
&context
) {
466 CYIdentifier
*item(context
.Unique()), *list(context
.Unique());
469 ->* initializer_
->Initialize(context
, NULL
)
470 ->* $
CYLexical(false, $
B2($
B(list
, iterable_
), $
B(item
)))
471 ->* $
CYForIn($
V(item
), $
V(list
), $
CYBlock($$
472 ->* initializer_
->Initialize(context
, $
M($
V(list
), $
V(item
)))
477 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
478 return $
CYFunctionParameter(binding_
);
481 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
482 CYIdentifier
*cys(context
.Unique());
485 ->* $
CYLexical(false, $
B1($
B(cys
, iterable_
)))
486 ->* $
CYForIn(binding_
->Target(context
), $
V(cys
), $
CYBlock($$
487 ->* $
E($
CYAssign(binding_
->Target(context
), $
M($
V(cys
), binding_
->Target(context
))))
488 ->* CYComprehension::Replace(context
, statement
)
492 CYStatement
*CYForVariable::Initialize(CYContext
&context
, CYExpression
*value
) {
494 if (binding_
->initializer_
== NULL
)
496 value
= binding_
->initializer_
;
499 return $
CYVar($
B1($
CYBinding(binding_
->identifier_
, value
)));
502 CYTarget
*CYForVariable::Replace(CYContext
&context
) {
503 _assert(binding_
->Replace(context
, CYIdentifierVariable
) == NULL
);
504 return binding_
->Target(context
);
507 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
508 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
509 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
511 void CYFunction::Replace(CYContext
&context
) {
512 CYThisScope
*_this(context
.this_
);
513 context
.this_
= &this_
;
514 context
.this_
= CYGetLast(context
.this_
);
516 CYIdentifier
*super(context
.super_
);
517 context
.super_
= super_
;
519 CYNonLocal
*nonlocal(context
.nonlocal_
);
520 CYNonLocal
*nextlocal(context
.nextlocal_
);
523 if (nonlocal_
!= NULL
) {
525 context
.nonlocal_
= nonlocal_
;
528 nonlocal_
= $
CYNonLocal();
529 context
.nextlocal_
= nonlocal_
;
532 CYScope
scope(!localize
, context
);
534 $
I("arguments")->Replace(context
, CYIdentifierMagic
);
536 parameters_
->Replace(context
, code_
);
538 context
.ReplaceAll(code_
);
541 CYImplicitReturn(code_
);
543 if (CYIdentifier
*identifier
= this_
.identifier_
) {
544 context
.scope_
->Declare(context
, identifier
, CYIdentifierVariable
);
546 ->* $
E($
CYAssign($
V(identifier
), $
CYThis()))
551 context
.NonLocal(code_
);
553 context
.nextlocal_
= nextlocal
;
554 context
.nonlocal_
= nonlocal
;
556 context
.super_
= super
;
557 context
.this_
= _this
;
559 scope
.Close(context
, code_
);
562 CYTarget
*CYFunctionExpression::Replace(CYContext
&context
) {
563 CYScope
scope(false, context
);
565 name_
= name_
->Replace(context
, CYIdentifierOther
);
567 CYFunction::Replace(context
);
568 scope
.Close(context
);
572 void CYFunctionParameter::Replace(CYContext
&context
, CYStatement
*&statements
) { $
T()
573 CYAssignment
*assignment(binding_
->Replace(context
, CYIdentifierArgument
));
575 next_
->Replace(context
, statements
);
577 if (assignment
!= NULL
)
579 ->* $
CYIf($
CYIdentical($
CYTypeOf(binding_
->Target(context
)), $
S("undefined")), $$
584 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
585 name_
= name_
->Replace(context
, CYIdentifierOther
);
586 CYFunction::Replace(context
);
590 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
, CYIdentifierKind kind
) {
594 return next_
->Replace(context
, kind
);
595 next_
= context
.scope_
->Declare(context
, this, kind
)->identifier_
;
599 CYStatement
*CYIf::Return() {
600 CYImplicitReturn(true_
);
601 CYImplicitReturn(false_
);
605 CYStatement
*CYIf::Replace(CYContext
&context
) {
606 context
.Replace(test_
);
607 context
.ReplaceAll(true_
);
608 context
.ReplaceAll(false_
);
612 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
616 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
617 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
620 CYStatement
*CYImport::Replace(CYContext
&context
) {
621 return $
CYVar($
B1($
B($
I(module_
->part_
->Word()), $
C1($
V("require"), module_
->Replace(context
, "/")))));
624 CYStatement
*CYImportDeclaration::Replace(CYContext
&context
) {
625 CYIdentifier
*module(context
.Unique());
627 CYList
<CYStatement
> statements
;
628 CYForEach (specifier
, specifiers_
)
629 statements
->*specifier
->Replace(context
, module);
632 ->* $
CYLexical(false, $
B1($
B(module, $
C1($
V("require"), module_
))))
636 CYStatement
*CYImportSpecifier::Replace(CYContext
&context
, CYIdentifier
*module) {
637 binding_
= binding_
->Replace(context
, CYIdentifierLexical
);
639 CYExpression
*import($
V(module));
641 import = $
M(import, $
S(name_
));
642 return $
E($
CYAssign($
V(binding_
), import));
645 CYTarget
*CYIndirect::Replace(CYContext
&context
) {
646 return $
M(rhs_
, $
S("$cyi"));
649 CYTarget
*CYIndirectMember::Replace(CYContext
&context
) {
650 return $
M($
CYIndirect(object_
), property_
);
653 CYExpression
*CYInfix::Replace(CYContext
&context
) {
654 context
.Replace(lhs_
);
655 context
.Replace(rhs_
);
659 CYStatement
*CYLabel::Replace(CYContext
&context
) {
660 context
.Replace(statement_
);
664 CYTarget
*CYLambda::Replace(CYContext
&context
) {
665 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), code_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
668 CYForInitializer
*CYLexical::Replace(CYContext
&context
) {
669 if (CYExpression
*expression
= bindings_
->Replace(context
, CYIdentifierLexical
))
670 return $
E(expression
);
674 CYFunctionExpression
*CYMethod::Constructor() {
678 void CYMethod::Replace(CYContext
&context
) {
679 CYFunction::Replace(context
);
682 CYString
*CYModule::Replace(CYContext
&context
, const char *separator
) const {
684 return $
CYString(part_
);
685 return $
CYString($pool
.strcat(next_
->Replace(context
, separator
)->Value(), separator
, part_
->Word(), NULL
));
688 CYExpression
*CYMultiply::Replace(CYContext
&context
) {
689 CYInfix::Replace(context
);
691 if (CYNumber
*lhn
= lhs_
->Number(context
))
692 if (CYNumber
*rhn
= rhs_
->Number(context
))
693 return $
D(lhn
->Value() * rhn
->Value());
701 CYTarget
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
702 CYSetLast(arguments_
) = $
CYArgument(value
);
706 CYTarget
*New::Replace(CYContext
&context
) {
707 context
.Replace(constructor_
);
708 arguments_
->Replace(context
);
714 CYNumber
*CYNull::Number(CYContext
&context
) {
718 CYString
*CYNull::String(CYContext
&context
) {
722 CYNumber
*CYNumber::Number(CYContext
&context
) {
726 CYString
*CYNumber::String(CYContext
&context
) {
727 // XXX: there is a precise algorithm for this
728 return $
S($pool
.sprintf(24, "%.17g", Value()));
731 CYExpression
*CYNumber::PropertyName(CYContext
&context
) {
732 return String(context
);
735 CYTarget
*CYObject::Replace(CYContext
&context
) {
737 if (properties_
!= NULL
)
738 properties_
= properties_
->ReplaceAll(context
, builder
, $
CYThis(), false);
741 return $
C1($
M($
CYFunctionExpression(NULL
, builder
.bindings_
->Parameter(context
),
743 ->* $
CYReturn($
CYThis())
744 ), $
S("call")), this, builder
.bindings_
->Argument(context
));
747 CYForEach (property
, properties_
)
748 property
->Replace(context
);
752 CYTarget
*CYParenthetical::Replace(CYContext
&context
) {
753 // XXX: return expression_;
754 context
.Replace(expression_
);
758 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
759 context
.Replace(lhs_
);
763 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
764 context
.Replace(rhs_
);
768 CYProperty
*CYProperty::ReplaceAll(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, bool update
) {
771 Replace(context
, builder
, self
, false);
773 next_
= next_
->ReplaceAll(context
, builder
, self
, update
);
774 return update
? next_
: this;
777 void CYProperty::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, bool protect
) {
778 CYExpression
*name(name_
->PropertyName(context
));
779 if (name_
->Computed()) {
780 CYIdentifier
*unique(context
.Unique());
782 ->* $
B1($
B(unique
, name
));
786 Replace(context
, builder
, self
, name
, protect
);
789 bool CYProperty::Update() const {
790 return name_
->Computed();
793 void CYPropertyGetter::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
794 CYIdentifier
*unique(context
.Unique());
796 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
798 ->* CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("get"), $
V(unique
)));
801 CYFunctionExpression
*CYPropertyMethod::Constructor() {
802 return name_
->Constructor() ? $
CYFunctionExpression(NULL
, parameters_
, code_
) : NULL
;
805 void CYPropertyMethod::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
806 CYIdentifier
*unique(context
.Unique());
808 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
810 ->* (!protect
? $
E($
CYAssign($
M(self
, name
), $
V(unique
))) :
811 CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("value"), $
V(unique
), $
CYPropertyValue($
S("writable"), $
CYTrue()))));
814 bool CYPropertyMethod::Update() const {
818 void CYPropertySetter::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
819 CYIdentifier
*unique(context
.Unique());
821 ->* $
B1($
B(unique
, CYSuperize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
))));
823 ->* CYDefineProperty(self
, name
, true, !protect
, $
CYPropertyValue($
S("set"), $
V(unique
)));
826 void CYPropertyValue::Replace(CYContext
&context
, CYBuilder
&builder
, CYExpression
*self
, CYExpression
*name
, bool protect
) {
828 CYIdentifier
*unique(context
.Unique());
830 ->* $
B1($
B(unique
, value_
));
832 ->* $
E($
CYAssign($
M(self
, name
), $
V(unique
)));
835 void CYPropertyValue::Replace(CYContext
&context
) {
836 context
.Replace(value_
);
839 void CYScript::Replace(CYContext
&context
) {
840 CYScope
scope(false, context
);
841 context
.scope_
->Damage();
843 context
.nextlocal_
= $
CYNonLocal();
844 context
.ReplaceAll(code_
);
845 context
.NonLocal(code_
);
847 scope
.Close(context
, code_
);
851 for (std::vector
<CYIdentifier
*>::const_iterator
i(context
.replace_
.begin()); i
!= context
.replace_
.end(); ++i
) {
853 if (context
.options_
.verbose_
)
854 name
= $pool
.strcat("$", $pool
.itoa(offset
++), NULL
);
860 unsigned position(7), local(offset
++ + 1);
863 unsigned index(local
% (sizeof(MappingSet
) - 1));
864 local
/= sizeof(MappingSet
) - 1;
865 id
[--position
] = MappingSet
[index
];
866 } while (local
!= 0);
868 if (scope
.Lookup(context
, id
+ position
) != NULL
)
870 // XXX: at some point, this could become a keyword
872 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
875 CYIdentifier
*identifier(*i
);
876 _assert(identifier
->next_
== identifier
);
877 identifier
->next_
= $
I(name
);
881 CYTarget
*CYResolveMember::Replace(CYContext
&context
) {
882 return $
M($
M(object_
, $
S("$cyr")), property_
);
885 CYStatement
*CYReturn::Replace(CYContext
&context
) {
886 if (context
.nonlocal_
!= NULL
) {
887 CYProperty
*value(value_
== NULL
? NULL
: $
CYPropertyValue($
S("$cyv"), value_
));
888 return $
cy::Syntax::Throw($
CYObject(
889 $
CYPropertyValue($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
893 context
.Replace(value_
);
897 CYTarget
*CYRubyBlock::Replace(CYContext
&context
) {
898 return call_
->AddArgument(context
, proc_
->Replace(context
));
901 CYTarget
*CYRubyBlock::AddArgument(CYContext
&context
, CYExpression
*value
) {
902 return Replace(context
)->AddArgument(context
, value
);
905 CYTarget
*CYRubyProc::Replace(CYContext
&context
) {
906 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
907 function
= CYNonLocalize(context
, function
);
908 function
->implicit_
= true;
912 CYScope::CYScope(bool transparent
, CYContext
&context
) :
913 transparent_(transparent
),
914 parent_(context
.scope_
),
919 _assert(!transparent_
|| parent_
!= NULL
);
920 context
.scope_
= this;
923 void CYScope::Damage() {
929 CYIdentifierFlags
*CYScope::Lookup(CYContext
&context
, const char *word
) {
930 CYForEach (i
, internal_
)
931 if (strcmp(i
->identifier_
->Word(), word
) == 0)
936 CYIdentifierFlags
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
937 return Lookup(context
, identifier
->Word());
940 CYIdentifierFlags
*CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierKind kind
) {
941 _assert(identifier
->next_
== NULL
|| identifier
->next_
== identifier
);
943 CYIdentifierFlags
*existing(Lookup(context
, identifier
));
944 if (existing
== NULL
)
945 internal_
= $
CYIdentifierFlags(identifier
, kind
, internal_
);
947 if (existing
== NULL
)
950 if (kind
== CYIdentifierGlobal
);
951 else if (existing
->kind_
== CYIdentifierGlobal
|| existing
->kind_
== CYIdentifierMagic
)
952 existing
->kind_
= kind
;
953 else if (existing
->kind_
== CYIdentifierLexical
|| kind
== CYIdentifierLexical
)
954 _assert(false); // XXX: throw new SyntaxError()
959 void CYScope::Merge(CYContext
&context
, const CYIdentifierFlags
*flags
) {
960 _assert(flags
->identifier_
->next_
== flags
->identifier_
);
961 CYIdentifierFlags
*existing(Declare(context
, flags
->identifier_
, flags
->kind_
));
962 flags
->identifier_
->next_
= existing
->identifier_
;
964 existing
->count_
+= flags
->count_
;
965 if (existing
->offset_
< flags
->offset_
)
966 existing
->offset_
= flags
->offset_
;
969 void CYScope::Close(CYContext
&context
, CYStatement
*&statements
) {
972 CYList
<CYBindings
> bindings
;
974 CYForEach (i
, internal_
)
975 if (i
->kind_
== CYIdentifierVariable
)
977 ->* $
CYBindings($
CYBinding(i
->identifier_
));
980 CYVar
*var($
CYVar(bindings
));
981 var
->SetNext(statements
);
986 void CYScope::Close(CYContext
&context
) {
987 context
.scope_
= parent_
;
989 CYForEach (i
, internal_
) {
990 _assert(i
->identifier_
->next_
== i
->identifier_
);
992 case CYIdentifierArgument
: {
993 _assert(!transparent_
);
996 case CYIdentifierLexical
: {
998 CYIdentifier
*replace(context
.Unique());
999 replace
->next_
= replace
;
1000 i
->identifier_
->next_
= replace
;
1001 i
->identifier_
= replace
;
1005 i
->kind_
= CYIdentifierVariable
;
1007 parent_
->Declare(context
, i
->identifier_
, CYIdentifierVariable
);
1010 case CYIdentifierVariable
: {
1012 parent_
->Declare(context
, i
->identifier_
, i
->kind_
);
1013 i
->kind_
= CYIdentifierGlobal
;
1021 typedef std::multimap
<unsigned, CYIdentifier
*> CYIdentifierOffsetMap
;
1022 CYIdentifierOffsetMap offsets
;
1024 CYForEach (i
, internal_
) {
1025 _assert(i
->identifier_
->next_
== i
->identifier_
);
1027 case CYIdentifierArgument
:
1028 case CYIdentifierVariable
:
1029 offsets
.insert(CYIdentifierOffsetMap::value_type(i
->offset_
, i
->identifier_
));
1035 for (CYIdentifierOffsetMap::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
1036 if (offset
< i
->first
)
1038 CYIdentifier
*identifier(i
->second
);
1040 if (offset
>= context
.replace_
.size())
1041 context
.replace_
.resize(offset
+ 1, NULL
);
1042 CYIdentifier
*&replace(context
.replace_
[offset
++]);
1044 if (replace
== NULL
)
1045 replace
= identifier
;
1047 _assert(replace
->next_
== replace
);
1048 identifier
->next_
= replace
;
1052 if (parent_
== NULL
)
1055 CYForEach (i
, internal_
) {
1057 case CYIdentifierGlobal
: {
1058 if (i
->offset_
< offset
)
1059 i
->offset_
= offset
;
1060 parent_
->Merge(context
, i
);
1065 CYElementValue
*CYSpan::Replace(CYContext
&context
) { $
T(NULL
)
1066 return $
CYElementValue(expression_
, $
CYElementValue(string_
, next_
->Replace(context
)));
1069 CYStatement
*CYStatement::Return() {
1073 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
1074 size_t size(size_
+ rhs
->size_
);
1075 char *value($
char[size
+ 1]);
1076 memcpy(value
, value_
, size_
);
1077 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
1079 return $
S(value
, size
);
1082 CYNumber
*CYString::Number(CYContext
&context
) {
1083 // XXX: there is a precise algorithm for this
1087 CYExpression
*CYString::PropertyName(CYContext
&context
) {
1091 CYString
*CYString::String(CYContext
&context
) {
1095 CYStatement
*CYStructDefinition::Replace(CYContext
&context
) {
1096 CYTarget
*target(tail_
->Replace(context
));
1098 target
= $
C1($
M(target
, $
S("withName")), $
S(name_
->Word()));
1099 return $
CYLexical(false, $
B1($
B($
I($pool
.strcat(name_
->Word(), "$cy", NULL
)), target
)));
1102 CYTarget
*CYStructTail::Replace(CYContext
&context
) {
1103 CYList
<CYElementValue
> types
;
1104 CYList
<CYElementValue
> names
;
1106 CYForEach (field
, fields_
) {
1107 CYTypedIdentifier
*typed(field
->typed_
);
1108 types
->*$
CYElementValue(typed
->Replace(context
));
1111 if (typed
->identifier_
== NULL
)
1114 name
= $
S(typed
->identifier_
->Word());
1115 names
->*$
CYElementValue(name
);
1118 return $
N2($
V("Type"), $
CYArray(types
), $
CYArray(names
));
1121 CYTarget
*CYSuperAccess::Replace(CYContext
&context
) {
1122 return $
C1($
M($
M($
M($
V(context
.super_
), $
S("prototype")), property_
), $
S("bind")), $
CYThis());
1125 CYTarget
*CYSuperCall::Replace(CYContext
&context
) {
1126 return $
C($
C1($
M($
V(context
.super_
), $
S("bind")), $
CYThis()), arguments_
);
1129 CYTarget
*CYSymbol::Replace(CYContext
&context
) {
1130 return $
C1($
M($
V("Symbol"), $
S("for")), $
S(name_
));
1133 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
1134 context
.Replace(value_
);
1135 clauses_
->Replace(context
);
1139 CYStatement
*CYTarget::Initialize(CYContext
&context
, CYExpression
*value
) {
1142 return $
E($
CYAssign(this, value
));
1145 CYTarget
*CYTemplate::Replace(CYContext
&context
) {
1146 return $
C2($
M($
M($
M($
V("String"), $
S("prototype")), $
S("concat")), $
S("apply")), $
S(""), $
CYArray($
CYElementValue(string_
, spans_
->Replace(context
))));
1149 CYTarget
*CYThis::Replace(CYContext
&context
) {
1150 if (context
.this_
!= NULL
)
1151 return $
V(context
.this_
->Identifier(context
));
1158 CYStatement
*Throw::Replace(CYContext
&context
) {
1159 context
.Replace(value_
);
1165 CYTarget
*CYTrivial::Replace(CYContext
&context
) {
1169 CYNumber
*CYTrue::Number(CYContext
&context
) {
1173 CYString
*CYTrue::String(CYContext
&context
) {
1180 CYStatement
*Try::Replace(CYContext
&context
) {
1181 CYScope
scope(true, context
);
1182 context
.ReplaceAll(code_
);
1183 scope
.Close(context
);
1185 catch_
->Replace(context
);
1186 finally_
->Replace(context
);
1192 CYTarget
*CYTypeArrayOf::Replace_(CYContext
&context
, CYTarget
*type
) {
1193 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
1196 CYTarget
*CYTypeBlockWith::Replace_(CYContext
&context
, CYTarget
*type
) {
1197 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("blockWith")), parameters_
->Argument(context
)));
1200 CYTarget
*CYTypeCharacter::Replace(CYContext
&context
) {
1202 case CYTypeNeutral
: return $
V("char");
1203 case CYTypeSigned
: return $
V("schar");
1204 case CYTypeUnsigned
: return $
V("uchar");
1205 default: _assert(false);
1209 CYTarget
*CYTypeConstant::Replace_(CYContext
&context
, CYTarget
*type
) {
1210 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
1213 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
1214 CYIdentifier
*identifier(typed_
->identifier_
);
1215 typed_
->identifier_
= NULL
;
1216 return $
CYLexical(false, $
B1($
B(identifier
, $
CYTypeExpression(typed_
))));
1219 CYTarget
*CYTypeError::Replace(CYContext
&context
) {
1224 CYTarget
*CYTypeExpression::Replace(CYContext
&context
) {
1225 return typed_
->Replace(context
);
1228 CYTarget
*CYTypeIntegral::Replace(CYContext
&context
) {
1229 bool u(signing_
== CYTypeUnsigned
);
1231 case 0: return $
V(u
? "ushort" : "short");
1232 case 1: return $
V(u
? "uint" : "int");
1233 case 2: return $
V(u
? "ulong" : "long");
1234 case 3: return $
V(u
? "ulonglong" : "longlong");
1235 default: _assert(false);
1239 CYTarget
*CYTypeModifier::Replace(CYContext
&context
, CYTarget
*type
) { $
T(type
)
1240 return Replace_(context
, type
);
1243 CYTarget
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYTarget
*type
) {
1244 CYList
<CYArgument
> arguments(parameters_
->Argument(context
));
1246 arguments
->*$
C_($
CYNull());
1247 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), arguments
));
1250 CYTarget
*CYTypePointerTo::Replace_(CYContext
&context
, CYTarget
*type
) {
1251 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
1254 CYTarget
*CYTypeReference::Replace(CYContext
&context
) {
1255 return $
V($pool
.strcat(name_
->Word(), "$cy", NULL
));
1258 CYTarget
*CYTypeStruct::Replace(CYContext
&context
) {
1259 CYTarget
*target(tail_
->Replace(context
));
1261 target
= $
C1($
M(target
, $
S("withName")), $
S(name_
->Word()));
1265 CYTarget
*CYTypeVariable::Replace(CYContext
&context
) {
1269 CYTarget
*CYTypeVoid::Replace(CYContext
&context
) {
1270 return $
N1($
V("Type"), $
CYString("v"));
1273 CYTarget
*CYTypeVolatile::Replace_(CYContext
&context
, CYTarget
*type
) {
1274 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
1277 CYTarget
*CYTypedIdentifier::Replace(CYContext
&context
) {
1278 return modifier_
->Replace(context
, specifier_
->Replace(context
));
1281 CYTypeFunctionWith
*CYTypedIdentifier::Function() {
1282 CYTypeModifier
*&modifier(CYGetLast(modifier_
));
1283 if (modifier
== NULL
)
1286 CYTypeFunctionWith
*function(modifier
->Function());
1287 if (function
== NULL
)
1294 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
1295 return $
CYArgument(typed_
->Replace(context
), next_
->Argument(context
));
1298 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
1299 return $
CYFunctionParameter($
CYBinding(typed_
->identifier_
?: context
.Unique()), next_
->Parameters(context
));
1302 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
1303 return next_
->TypeSignature(context
, $
CYAdd(prefix
, typed_
->Replace(context
)));
1306 CYForInitializer
*CYVar::Replace(CYContext
&context
) {
1307 if (CYExpression
*expression
= bindings_
->Replace(context
, CYIdentifierVariable
))
1308 return $
E(expression
);
1312 CYTarget
*CYVariable::Replace(CYContext
&context
) {
1313 name_
= name_
->Replace(context
, CYIdentifierGlobal
);
1317 CYFunctionParameter
*CYVariable::Parameter() const {
1318 return $
CYFunctionParameter($
CYBinding(name_
));
1321 CYStatement
*CYWhile::Replace(CYContext
&context
) {
1322 context
.Replace(test_
);
1323 context
.ReplaceAll(code_
);
1327 CYStatement
*CYWith::Replace(CYContext
&context
) {
1328 context
.Replace(scope_
);
1329 CYScope
scope(true, context
);
1331 context
.ReplaceAll(code_
);
1332 scope
.Close(context
);
1336 CYExpression
*CYWord::PropertyName(CYContext
&context
) {