]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Parse (but ignore) ECMAScript 6 "spread elements".
[cycript.git] / Replace.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
4644480a
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
4644480a 6/*
f95d2598
JF
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
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
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/>.
b3378a02 19**/
4644480a
JF
20/* }}} */
21
3b52fd1a 22#include "Parser.hpp"
6a981250 23#include "Replace.hpp"
3b52fd1a 24
3b52fd1a
JF
25#include <iomanip>
26
61fe0c53
JF
27CYFunctionExpression *CYNonLocalize(CYContext &context, CYFunctionExpression *function) {
28 function->nonlocal_ = context.nextlocal_;
29 return function;
30}
31
12e37ba3
JF
32static void CYImplicitReturn(CYStatement *&code) {
33 if (CYStatement *&last = CYGetLast(code))
34 last = last->Return();
35}
36
4644480a
JF
37CYExpression *CYAdd::Replace(CYContext &context) {
38 CYInfix::Replace(context);
39
fd5cdf97
JF
40 CYString *lhs(dynamic_cast<CYString *>(lhs_));
41 CYString *rhs(dynamic_cast<CYString *>(rhs_));
4644480a
JF
42
43 if (lhs != NULL || rhs != NULL) {
44 if (lhs == NULL) {
fd5cdf97 45 lhs = lhs_->String(context);
4644480a 46 if (lhs == NULL)
029bc65b 47 return this;
4644480a 48 } else if (rhs == NULL) {
fd5cdf97 49 rhs = rhs_->String(context);
4644480a 50 if (rhs == NULL)
029bc65b 51 return this;
4644480a
JF
52 }
53
54 return lhs->Concat(context, rhs);
55 }
56
fd5cdf97
JF
57 if (CYNumber *lhn = lhs_->Number(context))
58 if (CYNumber *rhn = rhs_->Number(context))
4644480a
JF
59 return $D(lhn->Value() + rhn->Value());
60
029bc65b 61 return this;
4644480a
JF
62}
63
3b52fd1a 64CYExpression *CYAddressOf::Replace(CYContext &context) {
3b52fd1a
JF
65 return $C0($M(rhs_, $S("$cya")));
66}
67
5192746c 68CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
3b52fd1a 69 context.Replace(value_);
5192746c
JF
70 next_ = next_->Replace(context);
71
72 if (value_ == NULL) {
73 if (next_ == NULL)
74 return NULL;
75 else
76 value_ = $U;
77 }
78
79 return this;
3b52fd1a
JF
80}
81
82CYExpression *CYArray::Replace(CYContext &context) {
fc8fc33d
JF
83 if (elements_ != NULL)
84 elements_->Replace(context);
029bc65b 85 return this;
3b52fd1a
JF
86}
87
88CYExpression *CYArrayComprehension::Replace(CYContext &context) {
89 CYVariable *cyv($V("$cyv"));
90
c8a0500b 91 return $C0($F(NULL, $P1($L("$cyv"), comprehensions_->Parameters(context)), $$->*
3b52fd1a
JF
92 $E($ CYAssign(cyv, $ CYArray()))->*
93 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
94 $ CYReturn(cyv)
95 ));
96}
97
98CYExpression *CYAssignment::Replace(CYContext &context) {
99 context.Replace(lhs_);
100 context.Replace(rhs_);
029bc65b 101 return this;
3b52fd1a
JF
102}
103
12e37ba3
JF
104CYStatement *CYBlock::Return() {
105 CYImplicitReturn(code_);
106 return this;
107}
108
3b52fd1a 109CYStatement *CYBlock::Replace(CYContext &context) {
b0385401
JF
110 context.ReplaceAll(code_);
111 if (code_ == NULL)
029bc65b
JF
112 return $ CYEmpty();
113 return this;
3b52fd1a
JF
114}
115
116CYStatement *CYBreak::Replace(CYContext &context) {
029bc65b 117 return this;
3b52fd1a
JF
118}
119
6c093cce
JF
120CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
121 CYArgument **argument(&arguments_);
122 while (*argument != NULL)
123 argument = &(*argument)->next_;
124 *argument = $ CYArgument(value);
125 return this;
126}
127
3b52fd1a
JF
128CYExpression *CYCall::Replace(CYContext &context) {
129 context.Replace(function_);
130 arguments_->Replace(context);
029bc65b 131 return this;
3b52fd1a
JF
132}
133
37954781
JF
134namespace cy {
135namespace Syntax {
136
137void Catch::Replace(CYContext &context) { $T()
50a3d79f 138 CYScope scope(true, context);
daf22a65
JF
139
140 context.Replace(name_);
141 context.scope_->Declare(context, name_, CYIdentifierCatch);
142
b0385401
JF
143 context.ReplaceAll(code_);
144 scope.Close(context, code_);
3b52fd1a
JF
145}
146
37954781
JF
147} }
148
3b52fd1a
JF
149void CYClause::Replace(CYContext &context) { $T()
150 context.Replace(case_);
b0385401 151 context.ReplaceAll(code_);
3b52fd1a
JF
152 next_->Replace(context);
153}
154
155CYExpression *CYCompound::Replace(CYContext &context) {
fd5cdf97
JF
156 context.Replace(expression_);
157 context.Replace(next_);
158
159 if (CYCompound *compound = dynamic_cast<CYCompound *>(expression_)) {
160 expression_ = compound->expression_;
161 compound->expression_ = compound->next_;
162 compound->next_ = next_;
163 next_ = compound;
164 }
165
9efd8b03 166 return this;
3b52fd1a
JF
167}
168
b0385401
JF
169CYFunctionParameter *CYCompound::Parameter() const {
170 CYFunctionParameter *next(next_->Parameter());
171 if (next == NULL)
e06e5ee1 172 return NULL;
0afc9ba3
JF
173
174 CYFunctionParameter *parameter(expression_->Parameter());
175 if (parameter == NULL)
176 return NULL;
177
178 parameter->SetNext(next);
179 return parameter;
180}
181
3b52fd1a
JF
182CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
183 CYFunctionParameter *next(next_->Parameters(context));
184 if (CYFunctionParameter *parameter = Parameter(context)) {
185 parameter->SetNext(next);
186 return parameter;
187 } else
188 return next;
189}
190
191CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
192 return next_ == NULL ? statement : next_->Replace(context, statement);
193}
194
195CYExpression *CYCondition::Replace(CYContext &context) {
196 context.Replace(test_);
197 context.Replace(true_);
198 context.Replace(false_);
029bc65b 199 return this;
3b52fd1a
JF
200}
201
ab2aa221
JF
202void CYContext::NonLocal(CYStatement *&statements) {
203 CYContext &context(*this);
204
06293152 205 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
daf22a65
JF
206 CYIdentifier *cye($I("$cye")->Replace(context));
207 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
208
209 CYStatement *declare(
c8a0500b 210 $ CYVar($L1($ CYDeclaration(unique, $ CYObject()))));
daf22a65
JF
211
212 cy::Syntax::Catch *rescue(
213 $ cy::Syntax::Catch(cye, $$->*
4e58e244
JF
214 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
215 $ CYReturn($M($V(cye), $S("$cyv"))))->*
216 $ cy::Syntax::Throw($V(cye))));
daf22a65 217
b0385401 218 // XXX: I don't understand any of this
577bfbfa 219 context.Replace(declare);
daf22a65 220 rescue->Replace(context);
ab2aa221
JF
221
222 statements = $$->*
daf22a65
JF
223 declare->*
224 $ cy::Syntax::Try(statements, rescue, NULL);
ab2aa221
JF
225 }
226}
227
228CYIdentifier *CYContext::Unique() {
0cbeddf8 229 return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL));
ab2aa221
JF
230}
231
3b52fd1a 232CYStatement *CYContinue::Replace(CYContext &context) {
029bc65b
JF
233 return this;
234}
235
c8a0500b
JF
236CYStatement *CYDebugger::Replace(CYContext &context) {
237 return this;
238}
239
029bc65b 240CYAssignment *CYDeclaration::Assignment(CYContext &context) {
15b88a33
JF
241 if (initialiser_ == NULL)
242 return NULL;
127c1a9c
JF
243
244 CYAssignment *value($ CYAssign(Variable(context), initialiser_));
245 initialiser_ = NULL;
246 return value;
15b88a33
JF
247}
248
249CYVariable *CYDeclaration::Variable(CYContext &context) {
250 return $V(identifier_);
3b52fd1a
JF
251}
252
ad3b38bb 253CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
c8a0500b 254 return $ CYVar($L1($ CYDeclaration(identifier_, value)));
3b52fd1a
JF
255}
256
029bc65b 257CYExpression *CYDeclaration::Replace(CYContext &context) {
15b88a33
JF
258 context.Replace(identifier_);
259 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
260 return Variable(context);
261}
262
263void CYDeclarations::Replace(CYContext &context) { $T()
264 declaration_->Replace(context);
265 next_->Replace(context);
3b52fd1a
JF
266}
267
550ee46a 268CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
5192746c 269 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_, next_->Property(context));
550ee46a
JF
270}
271
15b88a33 272CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
c8a0500b 273 return $ CYFunctionParameter($ CYDeclaration(declaration_->identifier_), next_->Parameter(context));
15b88a33
JF
274}
275
276CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
5192746c 277 return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
15b88a33
JF
278}
279
b0385401
JF
280CYExpression *CYDeclarations::Expression(CYContext &context) { $T(NULL)
281 CYExpression *compound(next_->Expression(context));
029bc65b 282 if (CYAssignment *assignment = declaration_->Assignment(context))
b0385401
JF
283 if (compound == NULL)
284 compound = assignment;
285 else
286 compound = $ CYCompound(assignment, compound);
029bc65b 287 return compound;
3b52fd1a
JF
288}
289
290CYExpression *CYDirectMember::Replace(CYContext &context) {
ca0f097f
JF
291 context.Replace(object_);
292 context.Replace(property_);
029bc65b 293 return this;
3b52fd1a
JF
294}
295
296CYStatement *CYDoWhile::Replace(CYContext &context) {
297 context.Replace(test_);
b0385401 298 context.ReplaceAll(code_);
029bc65b 299 return this;
3b52fd1a
JF
300}
301
fc8fc33d 302void CYElementSpread::Replace(CYContext &context) {
3b52fd1a 303 context.Replace(value_);
fc8fc33d
JF
304}
305
306void CYElementValue::Replace(CYContext &context) {
307 context.Replace(value_);
308 if (next_ != NULL)
309 next_->Replace(context);
3b52fd1a
JF
310}
311
312CYStatement *CYEmpty::Replace(CYContext &context) {
9cd63688 313 return NULL;
029bc65b
JF
314}
315
9a39f705
JF
316CYExpression *CYEncodedType::Replace(CYContext &context) {
317 return typed_->Replace(context);
318}
319
12e37ba3
JF
320CYStatement *CYExpress::Return() {
321 return $ CYReturn(expression_);
322}
323
720a57a8 324CYStatement *CYExpress::Replace(CYContext &context) {
3b52fd1a 325 context.Replace(expression_);
029bc65b 326 return this;
3b52fd1a
JF
327}
328
6c093cce
JF
329CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
330 return $C1(this, value);
331}
332
3b52fd1a
JF
333CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
334 return this;
335}
336
ad3b38bb
JF
337CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
338 return $E($ CYAssign(this, value));
3b52fd1a
JF
339}
340
ae65d594
JF
341CYAssignment *CYExpression::Assignment(CYContext &context) {
342 return NULL;
343}
344
0afc9ba3
JF
345CYFunctionParameter *CYExpression::Parameter() const {
346 return NULL;
347}
348
c5587ed7
JF
349CYStatement *CYExternal::Replace(CYContext &context) {
350 return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word())))));
351}
352
4644480a
JF
353CYNumber *CYFalse::Number(CYContext &context) {
354 return $D(0);
355}
356
357CYString *CYFalse::String(CYContext &context) {
358 return $S("false");
359}
360
a0be43fc
JF
361CYExpression *CYFatArrow::Replace(CYContext &context) {
362 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
363 function->this_.SetNext(context.this_);
364 return function;
365}
366
3b52fd1a 367void CYFinally::Replace(CYContext &context) { $T()
b0385401 368 context.ReplaceAll(code_);
3b52fd1a
JF
369}
370
371CYStatement *CYFor::Replace(CYContext &context) {
029bc65b 372 context.Replace(initialiser_);
3b52fd1a
JF
373 context.Replace(test_);
374 context.Replace(increment_);
b0385401 375 context.ReplaceAll(code_);
029bc65b 376 return this;
3b52fd1a
JF
377}
378
b0385401 379CYExpression *CYForDeclarations::Replace(CYContext &context) {
15b88a33 380 declarations_->Replace(context);
b0385401 381 return declarations_->Expression(context);
15b88a33
JF
382}
383
384// XXX: this still feels highly suboptimal
3b52fd1a 385CYStatement *CYForIn::Replace(CYContext &context) {
127c1a9c 386 if (CYAssignment *assignment = initialiser_->Assignment(context))
15b88a33
JF
387 return $ CYBlock($$->*
388 $E(assignment)->*
389 this
390 );
ae65d594 391
127c1a9c
JF
392 context.Replace(initialiser_);
393 context.Replace(set_);
b0385401 394 context.ReplaceAll(code_);
15b88a33 395 return this;
3b52fd1a
JF
396}
397
398CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
c8a0500b 399 return $ CYFunctionParameter($ CYDeclaration(name_));
3b52fd1a
JF
400}
401
402CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
4e58e244 403 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
3b52fd1a
JF
404}
405
d5618df7 406CYStatement *CYForOf::Replace(CYContext &context) {
04ea132d
JF
407 if (CYAssignment *assignment = initialiser_->Assignment(context))
408 return $ CYBlock($$->*
409 $E(assignment)->*
410 this
411 );
412
2eb8215d 413 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
3b52fd1a 414
c8a0500b 415 return $ CYLetStatement($L2($ CYDeclaration(cys, set_), $ CYDeclaration(cyt)), $$->*
2eb8215d 416 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
ad3b38bb 417 initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))->*
3b52fd1a
JF
418 code_
419 ))
550ee46a 420 );
3b52fd1a
JF
421}
422
d5618df7 423CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
c8a0500b 424 return $ CYFunctionParameter($ CYDeclaration(name_));
3b52fd1a
JF
425}
426
d5618df7 427CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
55c63f1d 428 CYIdentifier *cys($I("$cys"));
3b52fd1a 429
c8a0500b 430 return $E($C0($F(NULL, $P1($L("$cys")), $$->*
2eb8215d
JF
431 $E($ CYAssign($V(cys), set_))->*
432 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
433 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
3b52fd1a
JF
434 CYComprehension::Replace(context, statement)
435 ))
436 )));
437}
438
14ec9e00 439void CYFunction::Inject(CYContext &context) {
6a981250 440 context.Replace(name_);
a86e34d0 441 context.scope_->Declare(context, name_, CYIdentifierOther);
14ec9e00
JF
442}
443
444void CYFunction::Replace_(CYContext &context, bool outer) {
445 if (outer)
446 Inject(context);
447
a0be43fc 448 CYThisScope *_this(context.this_);
12e37ba3
JF
449 context.this_ = &this_;
450 context.this_ = CYGetLast(context.this_);
a0be43fc 451
06293152
JF
452 CYNonLocal *nonlocal(context.nonlocal_);
453 CYNonLocal *nextlocal(context.nextlocal_);
454
ab2aa221 455 bool localize;
06293152 456 if (nonlocal_ != NULL) {
ab2aa221 457 localize = false;
06293152
JF
458 context.nonlocal_ = nonlocal_;
459 } else {
ab2aa221
JF
460 localize = true;
461 nonlocal_ = $ CYNonLocal();
06293152 462 context.nextlocal_ = nonlocal_;
ab2aa221
JF
463 }
464
50a3d79f 465 CYScope scope(!localize, context);
61fe0c53 466
14ec9e00
JF
467 if (!outer && name_ != NULL)
468 Inject(context);
469
c8a0500b 470 parameters_->Replace(context, code_);
b0385401 471 context.ReplaceAll(code_);
029bc65b 472
12e37ba3
JF
473 if (implicit_)
474 CYImplicitReturn(code_);
475
a0be43fc 476 if (CYIdentifier *identifier = this_.identifier_)
b0385401 477 code_ = $$->*
a0be43fc 478 $ CYVar($L1($ CYDeclaration(identifier, $ CYThis())))->*
b0385401 479 code_;
a0be43fc 480
ab2aa221 481 if (localize)
b0385401 482 context.NonLocal(code_);
06293152
JF
483
484 context.nextlocal_ = nextlocal;
ab2aa221
JF
485 context.nonlocal_ = nonlocal;
486
a0be43fc
JF
487 context.this_ = _this;
488
b0385401 489 scope.Close(context, code_);
3b52fd1a
JF
490}
491
492CYExpression *CYFunctionExpression::Replace(CYContext &context) {
14ec9e00 493 Replace_(context, false);
029bc65b
JF
494 return this;
495}
496
b0385401 497void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
c8a0500b
JF
498 CYAssignment *assignment(initialiser_->Assignment(context));
499 context.Replace(initialiser_);
500
b0385401 501 next_->Replace(context, statements);
c8a0500b
JF
502
503 if (assignment != NULL)
b0385401
JF
504 statements = $$->*
505 // XXX: this cast is quite incorrect
506 $ CYIf($ CYIdentical($ CYTypeOf(dynamic_cast<CYExpression *>(initialiser_)), $S("undefined")), $$->*
507 $E(assignment)
508 )->*
509 statements;
3b52fd1a
JF
510}
511
512CYStatement *CYFunctionStatement::Replace(CYContext &context) {
14ec9e00 513 Replace_(context, true);
029bc65b
JF
514 return this;
515}
516
517CYIdentifier *CYIdentifier::Replace(CYContext &context) {
a846a8cd 518 if (replace_ != NULL && replace_ != this)
de9fc71b 519 return replace_->Replace(context);
a846a8cd 520 replace_ = context.scope_->Lookup(context, this);
a86e34d0 521 return replace_;
3b52fd1a
JF
522}
523
12e37ba3
JF
524CYStatement *CYIf::Return() {
525 CYImplicitReturn(true_);
526 CYImplicitReturn(false_);
527 return this;
528}
529
3b52fd1a
JF
530CYStatement *CYIf::Replace(CYContext &context) {
531 context.Replace(test_);
b0385401
JF
532 context.ReplaceAll(true_);
533 context.ReplaceAll(false_);
029bc65b 534 return this;
3b52fd1a
JF
535}
536
537CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
538 return NULL;
539}
540
541CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
542 return $ CYIf(test_, CYComprehension::Replace(context, statement));
543}
544
7b750785
JF
545CYStatement *CYImport::Replace(CYContext &context) {
546 return $ CYVar($L1($L(module_->part_->Word(), $C1($V("require"), module_->Replace(context, "/")))));
547}
548
3b52fd1a 549CYExpression *CYIndirect::Replace(CYContext &context) {
3b52fd1a
JF
550 return $M(rhs_, $S("$cyi"));
551}
552
553CYExpression *CYIndirectMember::Replace(CYContext &context) {
3b52fd1a
JF
554 return $M($ CYIndirect(object_), property_);
555}
556
557CYExpression *CYInfix::Replace(CYContext &context) {
558 context.Replace(lhs_);
559 context.Replace(rhs_);
029bc65b 560 return this;
3b52fd1a
JF
561}
562
563CYStatement *CYLabel::Replace(CYContext &context) {
564 context.Replace(statement_);
029bc65b 565 return this;
3b52fd1a
JF
566}
567
690cf1a8 568CYExpression *CYLambda::Replace(CYContext &context) {
b0385401 569 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
690cf1a8
JF
570}
571
c8a0500b 572CYStatement *CYLetStatement::Replace(CYContext &context) {
61fe0c53 573 return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context)));
550ee46a
JF
574}
575
7b750785
JF
576CYString *CYModule::Replace(CYContext &context, const char *separator) const {
577 if (next_ == NULL)
578 return $ CYString(part_);
579 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
580}
581
62f398e4
JF
582CYExpression *CYMultiply::Replace(CYContext &context) {
583 CYInfix::Replace(context);
584
fd5cdf97
JF
585 if (CYNumber *lhn = lhs_->Number(context))
586 if (CYNumber *rhn = rhs_->Number(context))
62f398e4
JF
587 return $D(lhn->Value() * rhn->Value());
588
589 return this;
590}
591
2eb8215d
JF
592namespace cy {
593namespace Syntax {
594
595CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
bf45251b 596 CYSetLast(arguments_) = $ CYArgument(value);
6c093cce
JF
597 return this;
598}
599
2eb8215d 600CYExpression *New::Replace(CYContext &context) {
3b52fd1a
JF
601 context.Replace(constructor_);
602 arguments_->Replace(context);
029bc65b 603 return this;
3b52fd1a
JF
604}
605
2eb8215d
JF
606} }
607
4644480a
JF
608CYNumber *CYNull::Number(CYContext &context) {
609 return $D(0);
610}
611
612CYString *CYNull::String(CYContext &context) {
613 return $S("null");
614}
615
616CYNumber *CYNumber::Number(CYContext &context) {
617 return this;
618}
619
620CYString *CYNumber::String(CYContext &context) {
621 // XXX: there is a precise algorithm for this
0cbeddf8 622 return $S($pool.sprintf(24, "%.17g", Value()));
4644480a
JF
623}
624
3b52fd1a
JF
625CYExpression *CYObject::Replace(CYContext &context) {
626 properties_->Replace(context);
029bc65b 627 return this;
3b52fd1a
JF
628}
629
b0385401
JF
630CYExpression *CYParenthetical::Replace(CYContext &context) {
631 return expression_;
632}
633
3b52fd1a
JF
634CYExpression *CYPostfix::Replace(CYContext &context) {
635 context.Replace(lhs_);
029bc65b 636 return this;
3b52fd1a
JF
637}
638
639CYExpression *CYPrefix::Replace(CYContext &context) {
640 context.Replace(rhs_);
029bc65b 641 return this;
3b52fd1a
JF
642}
643
10711823 644// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
45d0c928 645#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
10711823 646//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
45d0c928 647
e013809d
JF
648namespace {
649 struct IdentifierUsageLess :
650 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
651 {
652 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
653 if (lhs->usage_ != rhs->usage_)
654 return lhs->usage_ > rhs->usage_;
655 return lhs < rhs;
656 }
657 };
658
659 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
660}
661
a7d8b413 662void CYScript::Replace(CYContext &context) {
50a3d79f 663 CYScope scope(true, context);
ab2aa221 664
06293152 665 context.nextlocal_ = $ CYNonLocal();
b0385401
JF
666 context.ReplaceAll(code_);
667 context.NonLocal(code_);
ab2aa221 668
b0385401 669 scope.Close(context, code_);
14ec9e00
JF
670
671 size_t offset(0);
672
6a981250 673 CYCStringSet external;
a846a8cd 674 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
6a981250
JF
675 external.insert((*i)->Word());
676
e013809d
JF
677 IdentifierUsages usages;
678
a846a8cd 679 if (offset < context.rename_.size())
c2c9f509
JF
680 CYForEach (i, context.rename_[offset].identifier_)
681 usages.insert(i);
e013809d 682
14ec9e00 683 // XXX: totalling the probable occurrences and sorting by them would improve the result
a846a8cd 684 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
de9fc71b
JF
685 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
686
14ec9e00
JF
687 const char *name;
688
689 if (context.options_.verbose_)
0cbeddf8 690 name = $pool.strcat("$", $pool.itoa(offset), NULL);
14ec9e00
JF
691 else {
692 char id[8];
693 id[7] = '\0';
694
695 id:
696 unsigned position(7), local(offset + 1);
697
698 do {
45d0c928
JF
699 unsigned index(local % (sizeof(MappingSet) - 1));
700 local /= sizeof(MappingSet) - 1;
701 id[--position] = MappingSet[index];
14ec9e00
JF
702 } while (local != 0);
703
6a981250 704 if (external.find(id + position) != external.end()) {
14ec9e00
JF
705 ++offset;
706 goto id;
707 }
708
b799113b 709 name = $pool.strmemdup(id + position, 7 - position);
14ec9e00
JF
710 // XXX: at some point, this could become a keyword
711 }
712
c2c9f509 713 CYForEach (identifier, i->identifier_)
14ec9e00
JF
714 identifier->Set(name);
715 }
3b52fd1a
JF
716}
717
718void CYProperty::Replace(CYContext &context) { $T()
719 context.Replace(value_);
720 next_->Replace(context);
5192746c
JF
721 if (value_ == NULL)
722 value_ = $U;
3b52fd1a
JF
723}
724
725CYStatement *CYReturn::Replace(CYContext &context) {
ab2aa221
JF
726 if (context.nonlocal_ != NULL) {
727 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
728 return $ cy::Syntax::Throw($ CYObject(
4e58e244 729 $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
ab2aa221
JF
730 ));
731 }
732
3b52fd1a 733 context.Replace(value_);
029bc65b
JF
734 return this;
735}
736
6c093cce 737CYExpression *CYRubyBlock::Replace(CYContext &context) {
6c093cce
JF
738 return call_->AddArgument(context, proc_->Replace(context));
739}
740
741CYExpression *CYRubyProc::Replace(CYContext &context) {
12e37ba3
JF
742 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
743 function = CYNonLocalize(context, function);
744 function->implicit_ = true;
745 return function;
6c093cce
JF
746}
747
50a3d79f 748CYScope::CYScope(bool transparent, CYContext &context) :
61fe0c53 749 transparent_(transparent),
daf22a65
JF
750 parent_(context.scope_)
751{
50a3d79f 752 context.scope_ = this;
daf22a65
JF
753}
754
a86e34d0 755void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
61fe0c53 756 if (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch)
daf22a65 757 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
61fe0c53
JF
758 else if (parent_ != NULL)
759 parent_->Declare(context, identifier, flags);
a86e34d0
JF
760}
761
762CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
763 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
764 return *insert.first;
765}
766
6a981250 767void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
10711823 768 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
6a981250
JF
769 if (!insert.second) {
770 if ((*insert.first)->offset_ < identifier->offset_)
771 (*insert.first)->offset_ = identifier->offset_;
772 identifier->replace_ = *insert.first;
e013809d 773 (*insert.first)->usage_ += identifier->usage_ + 1;
029bc65b
JF
774 }
775}
776
10711823
JF
777namespace {
778 struct IdentifierOffset {
779 size_t offset_;
780 CYIdentifierFlags flags_;
e013809d 781 size_t usage_;
10711823
JF
782 CYIdentifier *identifier_;
783
e013809d
JF
784 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
785 offset_(identifier->offset_),
10711823 786 flags_(flags),
e013809d 787 usage_(identifier->usage_),
10711823
JF
788 identifier_(identifier)
789 {
790 }
791 };
792
793 struct IdentifierOffsetLess :
794 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
795 {
796 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
797 if (lhs.offset_ != rhs.offset_)
798 return lhs.offset_ < rhs.offset_;
799 if (lhs.flags_ != rhs.flags_)
800 return lhs.flags_ < rhs.flags_;
e013809d 801 /*if (lhs.usage_ != rhs.usage_)
a846a8cd 802 return lhs.usage_ < rhs.usage_;*/
10711823
JF
803 return lhs.identifier_ < rhs.identifier_;
804 }
805 };
806
807 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
808}
809
50a3d79f
JF
810void CYScope::Close(CYContext &context, CYStatement *&statements) {
811 context.scope_ = parent_;
812
2c81c6df
JF
813 if (parent_ == NULL)
814 return;
815
029bc65b 816 CYDeclarations *last(NULL), *curr(NULL);
14ec9e00 817
10711823 818 IdentifierOffsets offsets;
6a981250 819
10711823 820 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
a846a8cd 821 if (i->second != CYIdentifierMagic)
e013809d 822 offsets.insert(IdentifierOffset(i->first, i->second));
10711823
JF
823
824 size_t offset(0);
825
826 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
827 if (i->flags_ == CYIdentifierVariable) {
828 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
029bc65b
JF
829 if (last == NULL)
830 last = next;
831 if (curr != NULL)
832 curr->SetNext(next);
833 curr = next;
834 }
029bc65b 835
10711823
JF
836 if (offset < i->offset_)
837 offset = i->offset_;
a846a8cd
JF
838 if (context.rename_.size() <= offset)
839 context.rename_.resize(offset + 1);
10711823 840
a846a8cd 841 CYIdentifierUsage &rename(context.rename_[offset++]);
e013809d
JF
842 i->identifier_->SetNext(rename.identifier_);
843 rename.identifier_ = i->identifier_;
844 rename.usage_ += i->identifier_->usage_ + 1;
6a981250
JF
845 }
846
029bc65b
JF
847 if (last != NULL) {
848 CYVar *var($ CYVar(last));
849 var->SetNext(statements);
850 statements = var;
851 }
852
6a981250
JF
853 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
854 if (internal_.find(*i) == internal_.end()) {
de9fc71b 855 //std::cout << *i << '=' << offset << std::endl;
6a981250
JF
856 if ((*i)->offset_ < offset)
857 (*i)->offset_ = offset;
858 parent_->Merge(context, *i);
859 }
029bc65b
JF
860}
861
fc8fc33d
JF
862CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
863 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
b900e1a4
JF
864}
865
12e37ba3
JF
866CYStatement *CYStatement::Return() {
867 return this;
868}
869
4644480a
JF
870CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
871 size_t size(size_ + rhs->size_);
2eb8215d 872 char *value($ char[size + 1]);
4644480a
JF
873 memcpy(value, value_, size_);
874 memcpy(value + size_, rhs->value_, rhs->size_);
875 value[size] = '\0';
a14eb702 876 return $S(value, size);
4644480a
JF
877}
878
879CYNumber *CYString::Number(CYContext &context) {
880 // XXX: there is a precise algorithm for this
881 return NULL;
882}
883
884CYString *CYString::String(CYContext &context) {
885 return this;
886}
887
3b52fd1a
JF
888CYStatement *CYSwitch::Replace(CYContext &context) {
889 context.Replace(value_);
890 clauses_->Replace(context);
029bc65b 891 return this;
3b52fd1a
JF
892}
893
b900e1a4 894CYExpression *CYTemplate::Replace(CYContext &context) {
fc8fc33d 895 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
b900e1a4
JF
896}
897
3b52fd1a 898CYExpression *CYThis::Replace(CYContext &context) {
a0be43fc
JF
899 if (context.this_ != NULL)
900 return $V(context.this_->Identifier(context));
029bc65b 901 return this;
3b52fd1a
JF
902}
903
37954781
JF
904namespace cy {
905namespace Syntax {
906
907CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a 908 context.Replace(value_);
029bc65b 909 return this;
3b52fd1a
JF
910}
911
37954781
JF
912} }
913
3b52fd1a 914CYExpression *CYTrivial::Replace(CYContext &context) {
029bc65b 915 return this;
3b52fd1a
JF
916}
917
4644480a
JF
918CYNumber *CYTrue::Number(CYContext &context) {
919 return $D(1);
920}
921
922CYString *CYTrue::String(CYContext &context) {
923 return $S("true");
924}
925
37954781
JF
926namespace cy {
927namespace Syntax {
928
929CYStatement *Try::Replace(CYContext &context) {
b0385401 930 context.ReplaceAll(code_);
3b52fd1a
JF
931 catch_->Replace(context);
932 finally_->Replace(context);
029bc65b 933 return this;
3b52fd1a
JF
934}
935
37954781
JF
936} }
937
9a39f705
JF
938CYExpression *CYTypeArrayOf::Replace_(CYContext &context, CYExpression *type) {
939 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
690cf1a8
JF
940}
941
3fe16be7
JF
942CYExpression *CYTypeBlockWith::Replace_(CYContext &context, CYExpression *type) {
943 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
944}
945
9a39f705
JF
946CYExpression *CYTypeConstant::Replace_(CYContext &context, CYExpression *type) {
947 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
690cf1a8
JF
948}
949
60097023 950CYStatement *CYTypeDefinition::Replace(CYContext &context) {
84759b5b 951 return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
60097023
JF
952}
953
03db6a67
JF
954CYExpression *CYTypeError::Replace(CYContext &context) {
955 _assert(false);
956 return NULL;
957}
958
9a39f705
JF
959CYExpression *CYTypeModifier::Replace(CYContext &context, CYExpression *type) { $T(type)
960 return Replace_(context, type);
961}
962
963CYExpression *CYTypeFunctionWith::Replace_(CYContext &context, CYExpression *type) {
964 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
965}
966
3fe283c5
JF
967CYExpression *CYTypeLong::Replace(CYContext &context) {
968 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
969}
970
9a39f705
JF
971CYExpression *CYTypePointerTo::Replace_(CYContext &context, CYExpression *type) {
972 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
663c538f
JF
973}
974
3fe283c5
JF
975CYExpression *CYTypeShort::Replace(CYContext &context) {
976 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
977}
978
979CYExpression *CYTypeSigned::Replace(CYContext &context) {
980 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
981}
982
983CYExpression *CYTypeUnsigned::Replace(CYContext &context) {
984 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
985}
986
987CYExpression *CYTypeVariable::Replace(CYContext &context) {
988 return $V(name_);
989}
990
991CYExpression *CYTypeVoid::Replace(CYContext &context) {
992 return $N1($V("Type"), $ CYString("v"));
993}
994
9a39f705
JF
995CYExpression *CYTypeVolatile::Replace_(CYContext &context, CYExpression *type) {
996 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
690cf1a8
JF
997}
998
9a39f705 999CYExpression *CYTypedIdentifier::Replace(CYContext &context) {
3fe283c5 1000 return modifier_->Replace(context, specifier_->Replace(context));
690cf1a8
JF
1001}
1002
00b4cb83
JF
1003CYTypeFunctionWith *CYTypedIdentifier::Function() {
1004 CYTypeModifier **modifier(&modifier_);
1005 if (*modifier == NULL)
1006 return NULL;
1007 while ((*modifier)->next_ != NULL)
1008 modifier = &(*modifier)->next_;
1009 CYTypeFunctionWith *function((*modifier)->Function());
1010 if (function == NULL)
1011 return NULL;
1012 *modifier = NULL;
1013 return function;
1014}
1015
663c538f 1016CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
9a39f705 1017 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
663c538f
JF
1018}
1019
690cf1a8
JF
1020CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1021 return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
1022}
1023
1024CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
a2f3ecab 1025 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
690cf1a8
JF
1026}
1027
3b52fd1a 1028CYStatement *CYVar::Replace(CYContext &context) {
15b88a33 1029 declarations_->Replace(context);
b0385401
JF
1030 if (CYExpression *expression = declarations_->Expression(context))
1031 return $E(expression);
fd5cdf97 1032 return $ CYEmpty();
3b52fd1a
JF
1033}
1034
1035CYExpression *CYVariable::Replace(CYContext &context) {
577bfbfa 1036 context.Replace(name_);
029bc65b 1037 return this;
3b52fd1a
JF
1038}
1039
0afc9ba3
JF
1040CYFunctionParameter *CYVariable::Parameter() const {
1041 return $ CYFunctionParameter($ CYDeclaration(name_));
1042}
1043
3b52fd1a
JF
1044CYStatement *CYWhile::Replace(CYContext &context) {
1045 context.Replace(test_);
b0385401 1046 context.ReplaceAll(code_);
029bc65b 1047 return this;
3b52fd1a
JF
1048}
1049
1050CYStatement *CYWith::Replace(CYContext &context) {
1051 context.Replace(scope_);
b0385401 1052 context.ReplaceAll(code_);
029bc65b 1053 return this;
3b52fd1a
JF
1054}
1055
1056CYExpression *CYWord::ClassName(CYContext &context, bool object) {
1057 CYString *name($S(this));
1058 if (object)
1059 return $C1($V("objc_getClass"), name);
1060 else
1061 return name;
1062}