]> git.saurik.com Git - cycript.git/blame_incremental - Replace.cpp
Show more explicit types for Objective-C Instance.
[cycript.git] / Replace.cpp
... / ...
CommitLineData
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3*/
4
5/* GNU Affero General Public License, Version 3 {{{ */
6/*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19**/
20/* }}} */
21
22#include <iomanip>
23#include <map>
24
25#include "Replace.hpp"
26#include "Syntax.hpp"
27
28CYFunctionExpression *CYNonLocalize(CYContext &context, CYFunctionExpression *function) {
29 function->nonlocal_ = context.nextlocal_;
30 return function;
31}
32
33CYFunctionExpression *CYSuperize(CYContext &context, CYFunctionExpression *function) {
34 function->super_ = context.super_;
35 return function;
36}
37
38CYStatement *CYDefineProperty(CYExpression *object, CYExpression *name, bool configurable, bool enumerable, CYProperty *descriptor) {
39 return $E($C3($M($V("Object"), $S("defineProperty")), object, name, $ CYObject(CYList<CYProperty>()
40 ->* (configurable ? $ CYPropertyValue($S("configurable"), $ CYTrue()) : NULL)
41 ->* (enumerable ? $ CYPropertyValue($S("enumerable"), $ CYTrue()) : NULL)
42 ->* descriptor)));
43}
44
45static void CYImplicitReturn(CYStatement *&code) {
46 if (CYStatement *&last = CYGetLast(code))
47 last = last->Return();
48}
49
50CYExpression *CYAdd::Replace(CYContext &context) {
51 CYInfix::Replace(context);
52
53 CYString *lhs(dynamic_cast<CYString *>(lhs_));
54 CYString *rhs(dynamic_cast<CYString *>(rhs_));
55
56 if (lhs != NULL || rhs != NULL) {
57 if (lhs == NULL) {
58 lhs = lhs_->String(context);
59 if (lhs == NULL)
60 return this;
61 } else if (rhs == NULL) {
62 rhs = rhs_->String(context);
63 if (rhs == NULL)
64 return this;
65 }
66
67 return lhs->Concat(context, rhs);
68 }
69
70 if (CYNumber *lhn = lhs_->Number(context))
71 if (CYNumber *rhn = rhs_->Number(context))
72 return $D(lhn->Value() + rhn->Value());
73
74 return this;
75}
76
77CYExpression *CYAddressOf::Replace(CYContext &context) {
78 return $C0($M(rhs_, $S("$cya")));
79}
80
81CYTarget *CYApply::AddArgument(CYContext &context, CYExpression *value) {
82 CYArgument **argument(&arguments_);
83 while (*argument != NULL)
84 argument = &(*argument)->next_;
85 *argument = $ CYArgument(value);
86 return this;
87}
88
89CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
90 context.Replace(value_);
91 next_ = next_->Replace(context);
92
93 if (value_ == NULL) {
94 if (next_ == NULL)
95 return NULL;
96 else
97 value_ = $U;
98 }
99
100 return this;
101}
102
103CYTarget *CYArray::Replace(CYContext &context) {
104 CYForEach (element, elements_)
105 element->Replace(context);
106 return this;
107}
108
109CYTarget *CYArrayComprehension::Replace(CYContext &context) {
110 CYIdentifier *cyv(context.Unique());
111
112 return $C0($F(NULL, $P1($B(cyv), comprehensions_->Parameters(context)), $$
113 ->* $E($ CYAssign($V(cyv), $ CYArray()))
114 ->* comprehensions_->Replace(context, $E($C1($M($V(cyv), $S("push")), expression_)))
115 ->* $ CYReturn($V(cyv))
116 ));
117}
118
119CYExpression *CYAssignment::Replace(CYContext &context) {
120 context.Replace(lhs_);
121 context.Replace(rhs_);
122 return this;
123}
124
125CYStatement *CYBlock::Return() {
126 CYImplicitReturn(code_);
127 return this;
128}
129
130CYStatement *CYBlock::Replace(CYContext &context) {
131 CYScope scope(true, context);
132 context.ReplaceAll(code_);
133 scope.Close(context);
134
135 if (code_ == NULL)
136 return $ CYEmpty();
137 return this;
138}
139
140CYStatement *CYBreak::Replace(CYContext &context) {
141 return this;
142}
143
144CYTarget *CYCall::Replace(CYContext &context) {
145 context.Replace(function_);
146 arguments_->Replace(context);
147 return this;
148}
149
150namespace cy {
151namespace Syntax {
152
153void Catch::Replace(CYContext &context) { $T()
154 CYScope scope(true, context);
155
156 name_ = name_->Replace(context, CYIdentifierCatch);
157
158 context.ReplaceAll(code_);
159 scope.Close(context);
160}
161
162} }
163
164CYTarget *CYClassExpression::Replace(CYContext &context) {
165 CYBuilder builder;
166
167 CYIdentifier *super(context.Unique());
168
169 CYIdentifier *old(context.super_);
170 context.super_ = super;
171
172 CYIdentifier *constructor(context.Unique());
173 CYForEach (member, tail_->static_)
174 member->Replace(context, builder, $V(constructor), true);
175
176 CYIdentifier *prototype(context.Unique());
177 CYForEach (member, tail_->instance_)
178 member->Replace(context, builder, $V(prototype), true);
179
180 if (tail_->constructor_ == NULL)
181 tail_->constructor_ = $ CYFunctionExpression(NULL, NULL, NULL);
182 tail_->constructor_ = CYSuperize(context, tail_->constructor_);
183
184 context.super_ = old;
185
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")));
197}
198
199CYStatement *CYClassStatement::Replace(CYContext &context) {
200 return $ CYVar($B1($B(name_, $ CYClassExpression(name_, tail_))));
201}
202
203void CYClause::Replace(CYContext &context) { $T()
204 context.Replace(value_);
205 context.ReplaceAll(code_);
206 next_->Replace(context);
207}
208
209CYExpression *CYCompound::Replace(CYContext &context) {
210 context.Replace(expression_);
211 context.Replace(next_);
212
213 if (CYCompound *compound = dynamic_cast<CYCompound *>(expression_)) {
214 expression_ = compound->expression_;
215 compound->expression_ = compound->next_;
216 compound->next_ = next_;
217 next_ = compound;
218 }
219
220 return this;
221}
222
223CYFunctionParameter *CYCompound::Parameter() const {
224 CYFunctionParameter *next(next_->Parameter());
225 if (next == NULL)
226 return NULL;
227
228 CYFunctionParameter *parameter(expression_->Parameter());
229 if (parameter == NULL)
230 return NULL;
231
232 parameter->SetNext(next);
233 return parameter;
234}
235
236CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
237 CYFunctionParameter *next(next_->Parameters(context));
238 if (CYFunctionParameter *parameter = Parameter(context)) {
239 parameter->SetNext(next);
240 return parameter;
241 } else
242 return next;
243}
244
245CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
246 return next_ == NULL ? statement : next_->Replace(context, statement);
247}
248
249CYExpression *CYComputed::PropertyName(CYContext &context) {
250 return expression_;
251}
252
253CYExpression *CYCondition::Replace(CYContext &context) {
254 context.Replace(test_);
255 context.Replace(true_);
256 context.Replace(false_);
257 return this;
258}
259
260void CYContext::NonLocal(CYStatement *&statements) {
261 CYContext &context(*this);
262
263 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
264 CYIdentifier *cye($I("$cye")->Replace(context, CYIdentifierGlobal));
265 CYIdentifier *unique(nextlocal_->identifier_->Replace(context, CYIdentifierGlobal));
266
267 CYStatement *declare(
268 $ CYVar($B1($B(unique, $ CYObject()))));
269
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))));
275
276 context.Replace(declare);
277 rescue->Replace(context);
278
279 statements = $$
280 ->* declare
281 ->* $ cy::Syntax::Try(statements, rescue, NULL);
282 }
283}
284
285CYIdentifier *CYContext::Unique() {
286 return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL));
287}
288
289CYStatement *CYContinue::Replace(CYContext &context) {
290 return this;
291}
292
293CYStatement *CYDebugger::Replace(CYContext &context) {
294 return this;
295}
296
297CYTarget *CYBinding::Target(CYContext &context) {
298 return $V(identifier_);
299}
300
301CYAssignment *CYBinding::Replace(CYContext &context, CYIdentifierKind kind) {
302 identifier_ = identifier_->Replace(context, kind);
303
304 if (initializer_ == NULL)
305 return NULL;
306
307 CYAssignment *value($ CYAssign(Target(context), initializer_));
308 initializer_ = NULL;
309 return value;
310}
311
312CYExpression *CYBindings::Replace(CYContext &context, CYIdentifierKind kind) { $T(NULL)
313 CYAssignment *assignment(binding_->Replace(context, kind));
314 CYExpression *compound(next_->Replace(context, kind));
315
316 if (assignment != NULL)
317 if (compound == NULL)
318 compound = assignment;
319 else
320 compound = $ CYCompound(assignment, compound);
321 return compound;
322}
323
324CYFunctionParameter *CYBindings::Parameter(CYContext &context) { $T(NULL)
325 return $ CYFunctionParameter($ CYBinding(binding_->identifier_), next_->Parameter(context));
326}
327
328CYArgument *CYBindings::Argument(CYContext &context) { $T(NULL)
329 return $ CYArgument(binding_->initializer_, next_->Argument(context));
330}
331
332CYTarget *CYDirectMember::Replace(CYContext &context) {
333 context.Replace(object_);
334 context.Replace(property_);
335 return this;
336}
337
338CYStatement *CYDoWhile::Replace(CYContext &context) {
339 context.Replace(test_);
340 context.ReplaceAll(code_);
341 return this;
342}
343
344void CYElementSpread::Replace(CYContext &context) {
345 context.Replace(value_);
346}
347
348void CYElementValue::Replace(CYContext &context) {
349 context.Replace(value_);
350}
351
352CYForInitializer *CYEmpty::Replace(CYContext &context) {
353 return NULL;
354}
355
356CYTarget *CYEncodedType::Replace(CYContext &context) {
357 return typed_->Replace(context);
358}
359
360CYTarget *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_);
365}
366
367CYStatement *CYExpress::Return() {
368 return $ CYReturn(expression_);
369}
370
371CYForInitializer *CYExpress::Replace(CYContext &context) {
372 context.Replace(expression_);
373 return this;
374}
375
376CYTarget *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
377 return $C1(this, value);
378}
379
380CYFunctionParameter *CYExpression::Parameter() const {
381 return NULL;
382}
383
384CYStatement *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())))));
386}
387
388CYNumber *CYFalse::Number(CYContext &context) {
389 return $D(0);
390}
391
392CYString *CYFalse::String(CYContext &context) {
393 return $S("false");
394}
395
396CYExpression *CYFatArrow::Replace(CYContext &context) {
397 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
398 function->this_.SetNext(context.this_);
399 return function;
400}
401
402void CYFinally::Replace(CYContext &context) { $T()
403 CYScope scope(true, context);
404 context.ReplaceAll(code_);
405 scope.Close(context);
406}
407
408CYStatement *CYFor::Replace(CYContext &context) {
409 CYScope outer(true, context);
410 context.Replace(initializer_);
411
412 context.Replace(test_);
413
414 {
415 CYScope inner(true, context);
416 context.ReplaceAll(code_);
417 inner.Close(context);
418 }
419
420 context.Replace(increment_);
421
422 outer.Close(context);
423 return this;
424}
425
426CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
427 if (value == NULL) {
428 if (binding_->initializer_ == NULL)
429 return NULL;
430 value = binding_->initializer_;
431 }
432
433 return $ CYLexical(constant_, $B1($ CYBinding(binding_->identifier_, value)));
434}
435
436CYTarget *CYForLexical::Replace(CYContext &context) {
437 _assert(binding_->Replace(context, CYIdentifierLexical) == NULL);
438 return binding_->Target(context);
439}
440
441CYStatement *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);
447 return this;
448}
449
450CYStatement *CYForInitialized::Replace(CYContext &context) {
451 CYAssignment *assignment(binding_->Replace(context, CYIdentifierVariable));
452 return $ CYBlock($$
453 ->* (assignment == NULL ? NULL : $ CYExpress(assignment))
454 ->* $ CYForIn(binding_->Target(context), iterable_, code_));
455}
456
457CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
458 return $ CYFunctionParameter(binding_);
459}
460
461CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
462 return $ CYForIn(binding_->Target(context), iterable_, CYComprehension::Replace(context, statement));
463}
464
465CYStatement *CYForOf::Replace(CYContext &context) {
466 CYIdentifier *item(context.Unique()), *list(context.Unique());
467
468 return $ CYBlock($$
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)))
473 ->* code_
474 )));
475}
476
477CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
478 return $ CYFunctionParameter(binding_);
479}
480
481CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
482 CYIdentifier *cys(context.Unique());
483
484 return $ CYBlock($$
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)
489 )));
490}
491
492CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
493 if (value == NULL) {
494 if (binding_->initializer_ == NULL)
495 return NULL;
496 value = binding_->initializer_;
497 }
498
499 return $ CYVar($B1($ CYBinding(binding_->identifier_, value)));
500}
501
502CYTarget *CYForVariable::Replace(CYContext &context) {
503 _assert(binding_->Replace(context, CYIdentifierVariable) == NULL);
504 return binding_->Target(context);
505}
506
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$_"
510
511void CYFunction::Replace(CYContext &context) {
512 CYThisScope *_this(context.this_);
513 context.this_ = &this_;
514 context.this_ = CYGetLast(context.this_);
515
516 CYIdentifier *super(context.super_);
517 context.super_ = super_;
518
519 CYNonLocal *nonlocal(context.nonlocal_);
520 CYNonLocal *nextlocal(context.nextlocal_);
521
522 bool localize;
523 if (nonlocal_ != NULL) {
524 localize = false;
525 context.nonlocal_ = nonlocal_;
526 } else {
527 localize = true;
528 nonlocal_ = $ CYNonLocal();
529 context.nextlocal_ = nonlocal_;
530 }
531
532 CYScope scope(!localize, context);
533
534 $I("arguments")->Replace(context, CYIdentifierMagic);
535
536 parameters_->Replace(context, code_);
537
538 context.ReplaceAll(code_);
539
540 if (implicit_)
541 CYImplicitReturn(code_);
542
543 if (CYIdentifier *identifier = this_.identifier_) {
544 context.scope_->Declare(context, identifier, CYIdentifierVariable);
545 code_ = $$
546 ->* $E($ CYAssign($V(identifier), $ CYThis()))
547 ->* code_;
548 }
549
550 if (localize)
551 context.NonLocal(code_);
552
553 context.nextlocal_ = nextlocal;
554 context.nonlocal_ = nonlocal;
555
556 context.super_ = super;
557 context.this_ = _this;
558
559 scope.Close(context, code_);
560}
561
562CYTarget *CYFunctionExpression::Replace(CYContext &context) {
563 CYScope scope(false, context);
564 if (name_ != NULL)
565 name_ = name_->Replace(context, CYIdentifierOther);
566
567 CYFunction::Replace(context);
568 scope.Close(context);
569 return this;
570}
571
572void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
573 CYAssignment *assignment(binding_->Replace(context, CYIdentifierArgument));
574
575 next_->Replace(context, statements);
576
577 if (assignment != NULL)
578 statements = $$
579 ->* $ CYIf($ CYIdentical($ CYTypeOf(binding_->Target(context)), $S("undefined")), $$
580 ->* $E(assignment))
581 ->* statements;
582}
583
584CYStatement *CYFunctionStatement::Replace(CYContext &context) {
585 name_ = name_->Replace(context, CYIdentifierOther);
586 CYFunction::Replace(context);
587 return this;
588}
589
590CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
591 if (next_ == this)
592 return this;
593 if (next_ != NULL)
594 return next_->Replace(context, kind);
595 next_ = context.scope_->Declare(context, this, kind)->identifier_;
596 return next_;
597}
598
599CYStatement *CYIf::Return() {
600 CYImplicitReturn(true_);
601 CYImplicitReturn(false_);
602 return this;
603}
604
605CYStatement *CYIf::Replace(CYContext &context) {
606 context.Replace(test_);
607 context.ReplaceAll(true_);
608 context.ReplaceAll(false_);
609 return this;
610}
611
612CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
613 return NULL;
614}
615
616CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
617 return $ CYIf(test_, CYComprehension::Replace(context, statement));
618}
619
620CYStatement *CYImport::Replace(CYContext &context) {
621 return $ CYVar($B1($B($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
622}
623
624CYStatement *CYImportDeclaration::Replace(CYContext &context) {
625 CYIdentifier *module(context.Unique());
626
627 CYList<CYStatement> statements;
628 CYForEach (specifier, specifiers_)
629 statements->*specifier->Replace(context, module);
630
631 return $ CYBlock($$
632 ->* $ CYLexical(false, $B1($B(module, $C1($V("require"), module_))))
633 ->* statements);
634}
635
636CYStatement *CYImportSpecifier::Replace(CYContext &context, CYIdentifier *module) {
637 binding_ = binding_->Replace(context, CYIdentifierLexical);
638
639 CYExpression *import($V(module));
640 if (name_ != NULL)
641 import = $M(import, $S(name_));
642 return $E($ CYAssign($V(binding_), import));
643}
644
645CYTarget *CYIndirect::Replace(CYContext &context) {
646 return $M(rhs_, $S("$cyi"));
647}
648
649CYTarget *CYIndirectMember::Replace(CYContext &context) {
650 return $M($ CYIndirect(object_), property_);
651}
652
653CYExpression *CYInfix::Replace(CYContext &context) {
654 context.Replace(lhs_);
655 context.Replace(rhs_);
656 return this;
657}
658
659CYStatement *CYLabel::Replace(CYContext &context) {
660 context.Replace(statement_);
661 return this;
662}
663
664CYTarget *CYLambda::Replace(CYContext &context) {
665 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
666}
667
668CYForInitializer *CYLexical::Replace(CYContext &context) {
669 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierLexical))
670 return $E(expression);
671 return $ CYEmpty();
672}
673
674CYFunctionExpression *CYMethod::Constructor() {
675 return NULL;
676}
677
678void CYMethod::Replace(CYContext &context) {
679 CYFunction::Replace(context);
680}
681
682CYString *CYModule::Replace(CYContext &context, const char *separator) const {
683 if (next_ == NULL)
684 return $ CYString(part_);
685 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
686}
687
688CYExpression *CYMultiply::Replace(CYContext &context) {
689 CYInfix::Replace(context);
690
691 if (CYNumber *lhn = lhs_->Number(context))
692 if (CYNumber *rhn = rhs_->Number(context))
693 return $D(lhn->Value() * rhn->Value());
694
695 return this;
696}
697
698namespace cy {
699namespace Syntax {
700
701CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
702 CYSetLast(arguments_) = $ CYArgument(value);
703 return this;
704}
705
706CYTarget *New::Replace(CYContext &context) {
707 context.Replace(constructor_);
708 arguments_->Replace(context);
709 return this;
710}
711
712} }
713
714CYNumber *CYNull::Number(CYContext &context) {
715 return $D(0);
716}
717
718CYString *CYNull::String(CYContext &context) {
719 return $S("null");
720}
721
722CYNumber *CYNumber::Number(CYContext &context) {
723 return this;
724}
725
726CYString *CYNumber::String(CYContext &context) {
727 // XXX: there is a precise algorithm for this
728 return $S($pool.sprintf(24, "%.17g", Value()));
729}
730
731CYExpression *CYNumber::PropertyName(CYContext &context) {
732 return String(context);
733}
734
735CYTarget *CYObject::Replace(CYContext &context) {
736 CYBuilder builder;
737 if (properties_ != NULL)
738 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
739
740 if (builder) {
741 return $C1($M($ CYFunctionExpression(NULL, builder.bindings_->Parameter(context),
742 builder.statements_
743 ->* $ CYReturn($ CYThis())
744 ), $S("call")), this, builder.bindings_->Argument(context));
745 }
746
747 CYForEach (property, properties_)
748 property->Replace(context);
749 return this;
750}
751
752CYTarget *CYParenthetical::Replace(CYContext &context) {
753 // XXX: return expression_;
754 context.Replace(expression_);
755 return this;
756}
757
758CYExpression *CYPostfix::Replace(CYContext &context) {
759 context.Replace(lhs_);
760 return this;
761}
762
763CYExpression *CYPrefix::Replace(CYContext &context) {
764 context.Replace(rhs_);
765 return this;
766}
767
768CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
769 update |= Update();
770 if (update)
771 Replace(context, builder, self, false);
772 if (next_ != NULL)
773 next_ = next_->ReplaceAll(context, builder, self, update);
774 return update ? next_ : this;
775}
776
777void 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());
781 builder.bindings_
782 ->* $B1($B(unique, name));
783 name = $V(unique);
784 }
785
786 Replace(context, builder, self, name, protect);
787}
788
789bool CYProperty::Update() const {
790 return name_->Computed();
791}
792
793void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
794 CYIdentifier *unique(context.Unique());
795 builder.bindings_
796 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
797 builder.statements_
798 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
799}
800
801CYFunctionExpression *CYPropertyMethod::Constructor() {
802 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
803}
804
805void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
806 CYIdentifier *unique(context.Unique());
807 builder.bindings_
808 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
809 builder.statements_
810 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
811 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
812}
813
814bool CYPropertyMethod::Update() const {
815 return true;
816}
817
818void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
819 CYIdentifier *unique(context.Unique());
820 builder.bindings_
821 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
822 builder.statements_
823 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
824}
825
826void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
827 _assert(!protect);
828 CYIdentifier *unique(context.Unique());
829 builder.bindings_
830 ->* $B1($B(unique, value_));
831 builder.statements_
832 ->* $E($ CYAssign($M(self, name), $V(unique)));
833}
834
835void CYPropertyValue::Replace(CYContext &context) {
836 context.Replace(value_);
837}
838
839void CYScript::Replace(CYContext &context) {
840 CYScope scope(false, context);
841 context.scope_->Damage();
842
843 context.nextlocal_ = $ CYNonLocal();
844 context.ReplaceAll(code_);
845 context.NonLocal(code_);
846
847 scope.Close(context, code_);
848
849 unsigned offset(0);
850
851 for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
852 const char *name;
853 if (context.options_.verbose_)
854 name = $pool.strcat("$", $pool.itoa(offset++), NULL);
855 else {
856 char id[8];
857 id[7] = '\0';
858
859 id:
860 unsigned position(7), local(offset++ + 1);
861
862 do {
863 unsigned index(local % (sizeof(MappingSet) - 1));
864 local /= sizeof(MappingSet) - 1;
865 id[--position] = MappingSet[index];
866 } while (local != 0);
867
868 if (scope.Lookup(context, id + position) != NULL)
869 goto id;
870 // XXX: at some point, this could become a keyword
871
872 name = $pool.strmemdup(id + position, 7 - position);
873 }
874
875 CYIdentifier *identifier(*i);
876 _assert(identifier->next_ == identifier);
877 identifier->next_ = $I(name);
878 }
879}
880
881CYStatement *CYReturn::Replace(CYContext &context) {
882 if (context.nonlocal_ != NULL) {
883 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
884 return $ cy::Syntax::Throw($ CYObject(
885 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
886 ));
887 }
888
889 context.Replace(value_);
890 return this;
891}
892
893CYTarget *CYRubyBlock::Replace(CYContext &context) {
894 return call_->AddArgument(context, proc_->Replace(context));
895}
896
897CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
898 return Replace(context)->AddArgument(context, value);
899}
900
901CYTarget *CYRubyProc::Replace(CYContext &context) {
902 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
903 function = CYNonLocalize(context, function);
904 function->implicit_ = true;
905 return function;
906}
907
908CYScope::CYScope(bool transparent, CYContext &context) :
909 transparent_(transparent),
910 parent_(context.scope_),
911 damaged_(false),
912 shadow_(NULL),
913 internal_(NULL)
914{
915 _assert(!transparent_ || parent_ != NULL);
916 context.scope_ = this;
917}
918
919void CYScope::Damage() {
920 damaged_ = true;
921 if (parent_ != NULL)
922 parent_->Damage();
923}
924
925CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
926 CYForEach (i, internal_)
927 if (strcmp(i->identifier_->Word(), word) == 0)
928 return i;
929 return NULL;
930}
931
932CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
933 return Lookup(context, identifier->Word());
934}
935
936CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
937 _assert(identifier->next_ == NULL || identifier->next_ == identifier);
938
939 CYIdentifierFlags *existing(Lookup(context, identifier));
940 if (existing == NULL)
941 internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
942 ++internal_->count_;
943 if (existing == NULL)
944 return internal_;
945
946 if (kind == CYIdentifierGlobal);
947 else if (existing->kind_ == CYIdentifierGlobal || existing->kind_ == CYIdentifierMagic)
948 existing->kind_ = kind;
949 else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
950 _assert(false); // XXX: throw new SyntaxError()
951
952 return existing;
953}
954
955void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
956 _assert(flags->identifier_->next_ == flags->identifier_);
957 CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
958 flags->identifier_->next_ = existing->identifier_;
959
960 existing->count_ += flags->count_;
961 if (existing->offset_ < flags->offset_)
962 existing->offset_ = flags->offset_;
963}
964
965void CYScope::Close(CYContext &context, CYStatement *&statements) {
966 Close(context);
967
968 CYList<CYBindings> bindings;
969
970 CYForEach (i, internal_)
971 if (i->kind_ == CYIdentifierVariable)
972 bindings
973 ->* $ CYBindings($ CYBinding(i->identifier_));
974
975 if (bindings) {
976 CYVar *var($ CYVar(bindings));
977 var->SetNext(statements);
978 statements = var;
979 }
980}
981
982void CYScope::Close(CYContext &context) {
983 context.scope_ = parent_;
984
985 CYForEach (i, internal_) {
986 _assert(i->identifier_->next_ == i->identifier_);
987 switch (i->kind_) {
988 case CYIdentifierArgument: {
989 _assert(!transparent_);
990 } break;
991
992 case CYIdentifierLexical: {
993 if (!damaged_) {
994 CYIdentifier *replace(context.Unique());
995 replace->next_ = replace;
996 i->identifier_->next_ = replace;
997 i->identifier_ = replace;
998 }
999
1000 if (!transparent_)
1001 i->kind_ = CYIdentifierVariable;
1002 else
1003 parent_->Declare(context, i->identifier_, CYIdentifierVariable);
1004 } break;
1005
1006 case CYIdentifierVariable: {
1007 if (transparent_) {
1008 parent_->Declare(context, i->identifier_, i->kind_);
1009 i->kind_ = CYIdentifierGlobal;
1010 }
1011 } break;
1012 default:; } }
1013
1014 if (damaged_)
1015 return;
1016
1017 typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
1018 CYIdentifierOffsetMap offsets;
1019
1020 CYForEach (i, internal_) {
1021 _assert(i->identifier_->next_ == i->identifier_);
1022 switch (i->kind_) {
1023 case CYIdentifierArgument:
1024 case CYIdentifierVariable:
1025 offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
1026 break;
1027 default:; } }
1028
1029 unsigned offset(0);
1030
1031 for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
1032 if (offset < i->first)
1033 offset = i->first;
1034 CYIdentifier *identifier(i->second);
1035
1036 if (offset >= context.replace_.size())
1037 context.replace_.resize(offset + 1, NULL);
1038 CYIdentifier *&replace(context.replace_[offset++]);
1039
1040 if (replace == NULL)
1041 replace = identifier;
1042 else {
1043 _assert(replace->next_ == replace);
1044 identifier->next_ = replace;
1045 }
1046 }
1047
1048 if (parent_ == NULL)
1049 return;
1050
1051 CYForEach (i, internal_) {
1052 switch (i->kind_) {
1053 case CYIdentifierGlobal: {
1054 if (i->offset_ < offset)
1055 i->offset_ = offset;
1056 parent_->Merge(context, i);
1057 } break;
1058 default:; } }
1059}
1060
1061CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
1062 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
1063}
1064
1065CYStatement *CYStatement::Return() {
1066 return this;
1067}
1068
1069CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1070 size_t size(size_ + rhs->size_);
1071 char *value($ char[size + 1]);
1072 memcpy(value, value_, size_);
1073 memcpy(value + size_, rhs->value_, rhs->size_);
1074 value[size] = '\0';
1075 return $S(value, size);
1076}
1077
1078CYNumber *CYString::Number(CYContext &context) {
1079 // XXX: there is a precise algorithm for this
1080 return NULL;
1081}
1082
1083CYExpression *CYString::PropertyName(CYContext &context) {
1084 return this;
1085}
1086
1087CYString *CYString::String(CYContext &context) {
1088 return this;
1089}
1090
1091CYStatement *CYStructDefinition::Replace(CYContext &context) {
1092 CYTarget *target(tail_->Replace(context));
1093 if (name_ != NULL)
1094 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1095 return $ CYLexical(false, $B1($B($I($pool.strcat(name_->Word(), "$cy", NULL)), target)));
1096}
1097
1098CYTarget *CYStructTail::Replace(CYContext &context) {
1099 CYList<CYElementValue> types;
1100 CYList<CYElementValue> names;
1101
1102 CYForEach (field, fields_) {
1103 CYTypedIdentifier *typed(field->typed_);
1104 types->*$ CYElementValue(typed->Replace(context));
1105
1106 CYExpression *name;
1107 if (typed->identifier_ == NULL)
1108 name = NULL;
1109 else
1110 name = $S(typed->identifier_->Word());
1111 names->*$ CYElementValue(name);
1112 }
1113
1114 return $N2($V("Type"), $ CYArray(types), $ CYArray(names));
1115}
1116
1117CYTarget *CYSuperAccess::Replace(CYContext &context) {
1118 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1119}
1120
1121CYTarget *CYSuperCall::Replace(CYContext &context) {
1122 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1123}
1124
1125CYStatement *CYSwitch::Replace(CYContext &context) {
1126 context.Replace(value_);
1127 clauses_->Replace(context);
1128 return this;
1129}
1130
1131CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
1132 if (value == NULL)
1133 return NULL;
1134 return $E($ CYAssign(this, value));
1135}
1136
1137CYTarget *CYTemplate::Replace(CYContext &context) {
1138 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
1139}
1140
1141CYTarget *CYThis::Replace(CYContext &context) {
1142 if (context.this_ != NULL)
1143 return $V(context.this_->Identifier(context));
1144 return this;
1145}
1146
1147namespace cy {
1148namespace Syntax {
1149
1150CYStatement *Throw::Replace(CYContext &context) {
1151 context.Replace(value_);
1152 return this;
1153}
1154
1155} }
1156
1157CYTarget *CYTrivial::Replace(CYContext &context) {
1158 return this;
1159}
1160
1161CYNumber *CYTrue::Number(CYContext &context) {
1162 return $D(1);
1163}
1164
1165CYString *CYTrue::String(CYContext &context) {
1166 return $S("true");
1167}
1168
1169namespace cy {
1170namespace Syntax {
1171
1172CYStatement *Try::Replace(CYContext &context) {
1173 CYScope scope(true, context);
1174 context.ReplaceAll(code_);
1175 scope.Close(context);
1176
1177 catch_->Replace(context);
1178 finally_->Replace(context);
1179 return this;
1180}
1181
1182} }
1183
1184CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
1185 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
1186}
1187
1188CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
1189 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1190}
1191
1192CYTarget *CYTypeCharacter::Replace(CYContext &context) {
1193 switch (signing_) {
1194 case CYTypeNeutral: return $V("char");
1195 case CYTypeSigned: return $V("schar");
1196 case CYTypeUnsigned: return $V("uchar");
1197 default: _assert(false);
1198 }
1199}
1200
1201CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
1202 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
1203}
1204
1205CYStatement *CYTypeDefinition::Replace(CYContext &context) {
1206 CYIdentifier *identifier(typed_->identifier_);
1207 typed_->identifier_ = NULL;
1208 return $ CYLexical(false, $B1($B(identifier, $ CYTypeExpression(typed_))));
1209}
1210
1211CYTarget *CYTypeError::Replace(CYContext &context) {
1212 _assert(false);
1213 return NULL;
1214}
1215
1216CYTarget *CYTypeExpression::Replace(CYContext &context) {
1217 return typed_->Replace(context);
1218}
1219
1220CYTarget *CYTypeIntegral::Replace(CYContext &context) {
1221 bool u(signing_ == CYTypeUnsigned);
1222 switch (length_) {
1223 case 0: return $V(u ? "ushort" : "short");
1224 case 1: return $V(u ? "uint" : "int");
1225 case 2: return $V(u ? "ulong" : "long");
1226 case 3: return $V(u ? "ulonglong" : "longlong");
1227 default: _assert(false);
1228 }
1229}
1230
1231CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
1232 return Replace_(context, type);
1233}
1234
1235CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
1236 CYList<CYArgument> arguments(parameters_->Argument(context));
1237 if (variadic_)
1238 arguments->*$C_($ CYNull());
1239 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), arguments));
1240}
1241
1242CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
1243 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
1244}
1245
1246CYTarget *CYTypeReference::Replace(CYContext &context) {
1247 return $V($pool.strcat(name_->Word(), "$cy", NULL));
1248}
1249
1250CYTarget *CYTypeStruct::Replace(CYContext &context) {
1251 CYTarget *target(tail_->Replace(context));
1252 if (name_ != NULL)
1253 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1254 return target;
1255}
1256
1257CYTarget *CYTypeVariable::Replace(CYContext &context) {
1258 return $V(name_);
1259}
1260
1261CYTarget *CYTypeVoid::Replace(CYContext &context) {
1262 return $N1($V("Type"), $ CYString("v"));
1263}
1264
1265CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
1266 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
1267}
1268
1269CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
1270 return modifier_->Replace(context, specifier_->Replace(context));
1271}
1272
1273CYTypeFunctionWith *CYTypedIdentifier::Function() {
1274 CYTypeModifier *&modifier(CYGetLast(modifier_));
1275 if (modifier == NULL)
1276 return NULL;
1277
1278 CYTypeFunctionWith *function(modifier->Function());
1279 if (function == NULL)
1280 return NULL;
1281
1282 modifier = NULL;
1283 return function;
1284}
1285
1286CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
1287 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
1288}
1289
1290CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1291 return $ CYFunctionParameter($ CYBinding(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
1292}
1293
1294CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
1295 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
1296}
1297
1298CYForInitializer *CYVar::Replace(CYContext &context) {
1299 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierVariable))
1300 return $E(expression);
1301 return $ CYEmpty();
1302}
1303
1304CYTarget *CYVariable::Replace(CYContext &context) {
1305 name_ = name_->Replace(context, CYIdentifierGlobal);
1306 return this;
1307}
1308
1309CYFunctionParameter *CYVariable::Parameter() const {
1310 return $ CYFunctionParameter($ CYBinding(name_));
1311}
1312
1313CYStatement *CYWhile::Replace(CYContext &context) {
1314 context.Replace(test_);
1315 context.ReplaceAll(code_);
1316 return this;
1317}
1318
1319CYStatement *CYWith::Replace(CYContext &context) {
1320 context.Replace(scope_);
1321 CYScope scope(true, context);
1322 scope.Damage();
1323 context.ReplaceAll(code_);
1324 scope.Close(context);
1325 return this;
1326}
1327
1328CYExpression *CYWord::PropertyName(CYContext &context) {
1329 return $S(this);
1330}