]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Stupid mistake while outputting statement blocks: last statement was getting center...
[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) {
89 statements_ = statements_->ReplaceAll(context);
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_);
130 statements_ = statements_->ReplaceAll(context);
131 next_->Replace(context);
132}
133
320ce753 134CYStatement *CYComment::Replace(CYContext &context) {
029bc65b 135 return this;
320ce753
JF
136}
137
3b52fd1a
JF
138CYExpression *CYCompound::Replace(CYContext &context) {
139 expressions_ = expressions_->ReplaceAll(context);
029bc65b 140 return expressions_ == NULL ? NULL : 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
286CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL)
287 CYExpression *replace(this);
288 context.Replace(replace);
289
290 if (CYExpression *next = next_->ReplaceAll(context))
291 replace->SetNext(next);
292 else
293 replace->SetNext(next_);
294
295 return replace;
296}
297
4644480a
JF
298CYNumber *CYFalse::Number(CYContext &context) {
299 return $D(0);
300}
301
302CYString *CYFalse::String(CYContext &context) {
303 return $S("false");
304}
305
3b52fd1a
JF
306void CYFinally::Replace(CYContext &context) { $T()
307 code_.Replace(context);
308}
309
310CYStatement *CYFor::Replace(CYContext &context) {
029bc65b 311 context.Replace(initialiser_);
3b52fd1a
JF
312 context.Replace(test_);
313 context.Replace(increment_);
314 context.Replace(code_);
029bc65b 315 return this;
3b52fd1a
JF
316}
317
318CYStatement *CYForIn::Replace(CYContext &context) {
029bc65b
JF
319 // XXX: this actually might need a prefix statement
320 context.Replace(initialiser_);
3b52fd1a
JF
321 context.Replace(set_);
322 context.Replace(code_);
029bc65b 323 return this;
3b52fd1a
JF
324}
325
326CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
327 return $ CYFunctionParameter(name_);
328}
329
330CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
4e58e244 331 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
3b52fd1a
JF
332}
333
334CYStatement *CYForEachIn::Replace(CYContext &context) {
2eb8215d 335 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
3b52fd1a 336
2eb8215d
JF
337 return $ CYLet($L2($L(cys, set_), $L(cyt)), $$->*
338 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
339 $E($ CYAssign(initialiser_->ForEachIn(context), $M($V(cys), $V(cyt))))->*
3b52fd1a
JF
340 code_
341 ))
550ee46a 342 );
3b52fd1a
JF
343}
344
345CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
346 return $ CYFunctionParameter(name_);
347}
348
349CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
2eb8215d 350 CYIdentifier *cys($I("cys"));
3b52fd1a
JF
351
352 return $E($C0($F(NULL, $P1("$cys"), $$->*
2eb8215d
JF
353 $E($ CYAssign($V(cys), set_))->*
354 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
355 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
3b52fd1a
JF
356 CYComprehension::Replace(context, statement)
357 ))
358 )));
359}
360
14ec9e00 361void CYFunction::Inject(CYContext &context) {
6a981250 362 context.Replace(name_);
a86e34d0 363 context.scope_->Declare(context, name_, CYIdentifierOther);
14ec9e00
JF
364}
365
366void CYFunction::Replace_(CYContext &context, bool outer) {
367 if (outer)
368 Inject(context);
369
daf22a65 370 CYScope scope(CYScopeFunction, context, code_.statements_);
029bc65b 371
06293152
JF
372 CYNonLocal *nonlocal(context.nonlocal_);
373 CYNonLocal *nextlocal(context.nextlocal_);
374
ab2aa221 375 bool localize;
06293152 376 if (nonlocal_ != NULL) {
ab2aa221 377 localize = false;
06293152
JF
378 context.nonlocal_ = nonlocal_;
379 } else {
ab2aa221
JF
380 localize = true;
381 nonlocal_ = $ CYNonLocal();
06293152 382 context.nextlocal_ = nonlocal_;
ab2aa221
JF
383 }
384
14ec9e00
JF
385 if (!outer && name_ != NULL)
386 Inject(context);
387
6c093cce
JF
388 if (parameters_ != NULL)
389 parameters_ = parameters_->Replace(context, code_);
daf22a65 390
3b52fd1a 391 code_.Replace(context);
029bc65b 392
ab2aa221
JF
393 if (localize)
394 context.NonLocal(code_.statements_);
06293152
JF
395
396 context.nextlocal_ = nextlocal;
ab2aa221
JF
397 context.nonlocal_ = nonlocal;
398
daf22a65 399 scope.Close();
3b52fd1a
JF
400}
401
402CYExpression *CYFunctionExpression::Replace(CYContext &context) {
14ec9e00 403 Replace_(context, false);
029bc65b
JF
404 return this;
405}
406
4e11a430 407CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
577bfbfa 408 context.Replace(name_);
a86e34d0 409 context.scope_->Declare(context, name_, CYIdentifierArgument);
4e11a430
JF
410 if (next_ != NULL)
411 next_ = next_->Replace(context, code);
412 return this;
3b52fd1a
JF
413}
414
415CYStatement *CYFunctionStatement::Replace(CYContext &context) {
14ec9e00 416 Replace_(context, true);
029bc65b
JF
417 return this;
418}
419
420CYIdentifier *CYIdentifier::Replace(CYContext &context) {
a846a8cd 421 if (replace_ != NULL && replace_ != this)
de9fc71b 422 return replace_->Replace(context);
a846a8cd 423 replace_ = context.scope_->Lookup(context, this);
a86e34d0 424 return replace_;
3b52fd1a
JF
425}
426
427CYStatement *CYIf::Replace(CYContext &context) {
428 context.Replace(test_);
429 context.Replace(true_);
430 context.Replace(false_);
029bc65b 431 return this;
3b52fd1a
JF
432}
433
434CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
435 return NULL;
436}
437
438CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
439 return $ CYIf(test_, CYComprehension::Replace(context, statement));
440}
441
442CYExpression *CYIndirect::Replace(CYContext &context) {
443 CYPrefix::Replace(context);
444 return $M(rhs_, $S("$cyi"));
445}
446
447CYExpression *CYIndirectMember::Replace(CYContext &context) {
448 Replace_(context);
449 return $M($ CYIndirect(object_), property_);
450}
451
452CYExpression *CYInfix::Replace(CYContext &context) {
453 context.Replace(lhs_);
454 context.Replace(rhs_);
029bc65b 455 return this;
3b52fd1a
JF
456}
457
458CYStatement *CYLabel::Replace(CYContext &context) {
459 context.Replace(statement_);
029bc65b 460 return this;
3b52fd1a
JF
461}
462
550ee46a
JF
463CYStatement *CYLet::Replace(CYContext &context) {
464 return $ CYWith($ CYObject(declarations_->Property(context)), &code_);
465}
466
3b52fd1a
JF
467void CYMember::Replace_(CYContext &context) {
468 context.Replace(object_);
469 context.Replace(property_);
470}
471
2eb8215d
JF
472namespace cy {
473namespace Syntax {
474
475CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
6c093cce
JF
476 CYArgument **argument(&arguments_);
477 while (*argument != NULL)
478 argument = &(*argument)->next_;
479 *argument = $ CYArgument(value);
480 return this;
481}
482
2eb8215d 483CYExpression *New::Replace(CYContext &context) {
3b52fd1a
JF
484 context.Replace(constructor_);
485 arguments_->Replace(context);
029bc65b 486 return this;
3b52fd1a
JF
487}
488
2eb8215d
JF
489} }
490
4644480a
JF
491CYNumber *CYNull::Number(CYContext &context) {
492 return $D(0);
493}
494
495CYString *CYNull::String(CYContext &context) {
496 return $S("null");
497}
498
499CYNumber *CYNumber::Number(CYContext &context) {
500 return this;
501}
502
503CYString *CYNumber::String(CYContext &context) {
504 // XXX: there is a precise algorithm for this
2eb8215d 505 return $S(apr_psprintf($pool, "%.17g", Value()));
4644480a
JF
506}
507
3b52fd1a
JF
508CYExpression *CYObject::Replace(CYContext &context) {
509 properties_->Replace(context);
029bc65b 510 return this;
3b52fd1a
JF
511}
512
4e11a430
JF
513CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
514 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
515 parameter = parameter->Replace(context, code);
577bfbfa 516 context.Replace(initializer_);
4e11a430 517
4e58e244 518 CYVariable *name($V(name_));
4e11a430
JF
519 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
520 $E($ CYAssign(name, initializer_))
521 ));
522
523 return parameter;
524}
525
3b52fd1a
JF
526CYExpression *CYPostfix::Replace(CYContext &context) {
527 context.Replace(lhs_);
029bc65b 528 return this;
3b52fd1a
JF
529}
530
531CYExpression *CYPrefix::Replace(CYContext &context) {
532 context.Replace(rhs_);
029bc65b 533 return this;
3b52fd1a
JF
534}
535
10711823 536// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
45d0c928 537#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
10711823 538//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
45d0c928 539
e013809d
JF
540namespace {
541 struct IdentifierUsageLess :
542 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
543 {
544 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
545 if (lhs->usage_ != rhs->usage_)
546 return lhs->usage_ > rhs->usage_;
547 return lhs < rhs;
548 }
549 };
550
551 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
552}
553
3b52fd1a 554void CYProgram::Replace(CYContext &context) {
daf22a65 555 CYScope scope(CYScopeProgram, context, statements_);
ab2aa221 556
06293152 557 context.nextlocal_ = $ CYNonLocal();
3b52fd1a 558 statements_ = statements_->ReplaceAll(context);
ab2aa221
JF
559 context.NonLocal(statements_);
560
daf22a65 561 scope.Close();
14ec9e00
JF
562
563 size_t offset(0);
564
6a981250 565 CYCStringSet external;
a846a8cd 566 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
6a981250
JF
567 external.insert((*i)->Word());
568
e013809d
JF
569 IdentifierUsages usages;
570
a846a8cd
JF
571 if (offset < context.rename_.size())
572 for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_)
573 usages.insert(i);
e013809d 574
14ec9e00 575 // XXX: totalling the probable occurrences and sorting by them would improve the result
a846a8cd 576 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
de9fc71b
JF
577 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
578
14ec9e00
JF
579 const char *name;
580
581 if (context.options_.verbose_)
2eb8215d 582 name = apr_psprintf($pool, "$%"APR_SIZE_T_FMT"", offset);
14ec9e00
JF
583 else {
584 char id[8];
585 id[7] = '\0';
586
587 id:
588 unsigned position(7), local(offset + 1);
589
590 do {
45d0c928
JF
591 unsigned index(local % (sizeof(MappingSet) - 1));
592 local /= sizeof(MappingSet) - 1;
593 id[--position] = MappingSet[index];
14ec9e00
JF
594 } while (local != 0);
595
6a981250 596 if (external.find(id + position) != external.end()) {
14ec9e00
JF
597 ++offset;
598 goto id;
599 }
600
2eb8215d 601 name = apr_pstrmemdup($pool, id + position, 7 - position);
14ec9e00
JF
602 // XXX: at some point, this could become a keyword
603 }
604
e013809d 605 for (CYIdentifier *identifier(i->identifier_); identifier != NULL; identifier = identifier->next_)
14ec9e00
JF
606 identifier->Set(name);
607 }
3b52fd1a
JF
608}
609
610void CYProperty::Replace(CYContext &context) { $T()
611 context.Replace(value_);
612 next_->Replace(context);
613}
614
615CYStatement *CYReturn::Replace(CYContext &context) {
ab2aa221
JF
616 if (context.nonlocal_ != NULL) {
617 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
618 return $ cy::Syntax::Throw($ CYObject(
4e58e244 619 $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
ab2aa221
JF
620 ));
621 }
622
3b52fd1a 623 context.Replace(value_);
029bc65b
JF
624 return this;
625}
626
6c093cce
JF
627CYExpression *CYRubyBlock::Replace(CYContext &context) {
628 // XXX: this needs to do something much more epic to handle return
629 return call_->AddArgument(context, proc_->Replace(context));
630}
631
632CYExpression *CYRubyProc::Replace(CYContext &context) {
ab2aa221 633 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
06293152 634 function->nonlocal_ = context.nextlocal_;
ab2aa221 635 return function;
6c093cce
JF
636}
637
daf22a65
JF
638CYScope::CYScope(CYScopeType type, CYContext &context, CYStatement *&statements) :
639 type_(type),
640 context_(context),
641 statements_(statements),
642 parent_(context.scope_)
643{
644 context_.scope_ = this;
645}
646
647void CYScope::Close() {
648 context_.scope_ = parent_;
649 Scope(context_, statements_);
650}
651
a86e34d0 652void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
daf22a65
JF
653 if (type_ == CYScopeCatch && flags != CYIdentifierCatch)
654 parent_->Declare(context, identifier, flags);
655 else
656 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
a86e34d0
JF
657}
658
659CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
660 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
661 return *insert.first;
662}
663
6a981250 664void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
10711823 665 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
6a981250
JF
666 if (!insert.second) {
667 if ((*insert.first)->offset_ < identifier->offset_)
668 (*insert.first)->offset_ = identifier->offset_;
669 identifier->replace_ = *insert.first;
e013809d 670 (*insert.first)->usage_ += identifier->usage_ + 1;
029bc65b
JF
671 }
672}
673
10711823
JF
674namespace {
675 struct IdentifierOffset {
676 size_t offset_;
677 CYIdentifierFlags flags_;
e013809d 678 size_t usage_;
10711823
JF
679 CYIdentifier *identifier_;
680
e013809d
JF
681 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
682 offset_(identifier->offset_),
10711823 683 flags_(flags),
e013809d 684 usage_(identifier->usage_),
10711823
JF
685 identifier_(identifier)
686 {
687 }
688 };
689
690 struct IdentifierOffsetLess :
691 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
692 {
693 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
694 if (lhs.offset_ != rhs.offset_)
695 return lhs.offset_ < rhs.offset_;
696 if (lhs.flags_ != rhs.flags_)
697 return lhs.flags_ < rhs.flags_;
e013809d 698 /*if (lhs.usage_ != rhs.usage_)
a846a8cd 699 return lhs.usage_ < rhs.usage_;*/
10711823
JF
700 return lhs.identifier_ < rhs.identifier_;
701 }
702 };
703
704 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
705}
706
029bc65b 707void CYScope::Scope(CYContext &context, CYStatement *&statements) {
2c81c6df
JF
708 if (parent_ == NULL)
709 return;
710
029bc65b 711 CYDeclarations *last(NULL), *curr(NULL);
14ec9e00 712
10711823 713 IdentifierOffsets offsets;
6a981250 714
10711823 715 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
a846a8cd 716 if (i->second != CYIdentifierMagic)
e013809d 717 offsets.insert(IdentifierOffset(i->first, i->second));
10711823
JF
718
719 size_t offset(0);
720
721 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
722 if (i->flags_ == CYIdentifierVariable) {
723 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
029bc65b
JF
724 if (last == NULL)
725 last = next;
726 if (curr != NULL)
727 curr->SetNext(next);
728 curr = next;
729 }
029bc65b 730
10711823
JF
731 if (offset < i->offset_)
732 offset = i->offset_;
a846a8cd
JF
733 if (context.rename_.size() <= offset)
734 context.rename_.resize(offset + 1);
10711823 735
a846a8cd 736 CYIdentifierUsage &rename(context.rename_[offset++]);
e013809d
JF
737 i->identifier_->SetNext(rename.identifier_);
738 rename.identifier_ = i->identifier_;
739 rename.usage_ += i->identifier_->usage_ + 1;
6a981250
JF
740 }
741
029bc65b
JF
742 if (last != NULL) {
743 CYVar *var($ CYVar(last));
744 var->SetNext(statements);
745 statements = var;
746 }
747
6a981250
JF
748 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
749 if (internal_.find(*i) == internal_.end()) {
de9fc71b 750 //std::cout << *i << '=' << offset << std::endl;
6a981250
JF
751 if ((*i)->offset_ < offset)
752 (*i)->offset_ = offset;
753 parent_->Merge(context, *i);
754 }
029bc65b
JF
755}
756
757CYStatement *CYStatement::Collapse(CYContext &context) {
758 return this;
3b52fd1a
JF
759}
760
3b52fd1a
JF
761CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL)
762 CYStatement *replace(this);
763 context.Replace(replace);
029bc65b
JF
764 replace->SetNext(next_->ReplaceAll(context));
765 return replace->Collapse(context);
3b52fd1a
JF
766}
767
4644480a
JF
768CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
769 size_t size(size_ + rhs->size_);
2eb8215d 770 char *value($ char[size + 1]);
4644480a
JF
771 memcpy(value, value_, size_);
772 memcpy(value + size_, rhs->value_, rhs->size_);
773 value[size] = '\0';
a14eb702 774 return $S(value, size);
4644480a
JF
775}
776
777CYNumber *CYString::Number(CYContext &context) {
778 // XXX: there is a precise algorithm for this
779 return NULL;
780}
781
782CYString *CYString::String(CYContext &context) {
783 return this;
784}
785
3b52fd1a
JF
786CYStatement *CYSwitch::Replace(CYContext &context) {
787 context.Replace(value_);
788 clauses_->Replace(context);
029bc65b 789 return this;
3b52fd1a
JF
790}
791
792CYExpression *CYThis::Replace(CYContext &context) {
029bc65b 793 return this;
3b52fd1a
JF
794}
795
37954781
JF
796namespace cy {
797namespace Syntax {
798
799CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a 800 context.Replace(value_);
029bc65b 801 return this;
3b52fd1a
JF
802}
803
37954781
JF
804} }
805
3b52fd1a 806CYExpression *CYTrivial::Replace(CYContext &context) {
029bc65b 807 return this;
3b52fd1a
JF
808}
809
4644480a
JF
810CYNumber *CYTrue::Number(CYContext &context) {
811 return $D(1);
812}
813
814CYString *CYTrue::String(CYContext &context) {
815 return $S("true");
816}
817
37954781
JF
818namespace cy {
819namespace Syntax {
820
821CYStatement *Try::Replace(CYContext &context) {
3b52fd1a
JF
822 code_.Replace(context);
823 catch_->Replace(context);
824 finally_->Replace(context);
029bc65b 825 return this;
3b52fd1a
JF
826}
827
37954781
JF
828} }
829
3b52fd1a 830CYStatement *CYVar::Replace(CYContext &context) {
029bc65b 831 return $E(declarations_->Replace(context));
3b52fd1a
JF
832}
833
834CYExpression *CYVariable::Replace(CYContext &context) {
577bfbfa 835 context.Replace(name_);
029bc65b 836 return this;
3b52fd1a
JF
837}
838
839CYStatement *CYWhile::Replace(CYContext &context) {
840 context.Replace(test_);
841 context.Replace(code_);
029bc65b 842 return this;
3b52fd1a
JF
843}
844
845CYStatement *CYWith::Replace(CYContext &context) {
846 context.Replace(scope_);
847 context.Replace(code_);
029bc65b 848 return this;
3b52fd1a
JF
849}
850
851CYExpression *CYWord::ClassName(CYContext &context, bool object) {
852 CYString *name($S(this));
853 if (object)
854 return $C1($V("objc_getClass"), name);
855 else
856 return name;
857}