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