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