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