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