]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Progress on autoconf doom!
[cycript.git] / Replace.cpp
CommitLineData
e91fbe93 1/* Cycript - Inlining/Optimizing JavaScript Compiler
4644480a
JF
2 * Copyright (C) 2009 Jay Freeman (saurik)
3*/
4
5/* Modified BSD License {{{ */
6/*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/* }}} */
39
3b52fd1a 40#include "Parser.hpp"
6a981250 41#include "Replace.hpp"
3b52fd1a 42
3b52fd1a
JF
43#include <iomanip>
44
4644480a
JF
45CYExpression *CYAdd::Replace(CYContext &context) {
46 CYInfix::Replace(context);
47
48 CYExpression *lhp(lhs_->Primitive(context));
49 CYExpression *rhp(rhs_->Primitive(context));
50
51 CYString *lhs(dynamic_cast<CYString *>(lhp));
52 CYString *rhs(dynamic_cast<CYString *>(rhp));
53
54 if (lhs != NULL || rhs != NULL) {
55 if (lhs == NULL) {
56 lhs = lhp->String(context);
57 if (lhs == NULL)
029bc65b 58 return this;
4644480a
JF
59 } else if (rhs == NULL) {
60 rhs = rhp->String(context);
61 if (rhs == NULL)
029bc65b 62 return this;
4644480a
JF
63 }
64
65 return lhs->Concat(context, rhs);
66 }
67
68 if (CYNumber *lhn = lhp->Number(context))
69 if (CYNumber *rhn = rhp->Number(context))
70 return $D(lhn->Value() + rhn->Value());
71
029bc65b 72 return this;
4644480a
JF
73}
74
3b52fd1a
JF
75CYExpression *CYAddressOf::Replace(CYContext &context) {
76 CYPrefix::Replace(context);
77 return $C0($M(rhs_, $S("$cya")));
78}
79
80void CYArgument::Replace(CYContext &context) { $T()
81 context.Replace(value_);
82 next_->Replace(context);
83}
84
85CYExpression *CYArray::Replace(CYContext &context) {
86 elements_->Replace(context);
029bc65b 87 return this;
3b52fd1a
JF
88}
89
90CYExpression *CYArrayComprehension::Replace(CYContext &context) {
91 CYVariable *cyv($V("$cyv"));
92
93 return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->*
94 $E($ CYAssign(cyv, $ CYArray()))->*
95 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
96 $ CYReturn(cyv)
97 ));
98}
99
100CYExpression *CYAssignment::Replace(CYContext &context) {
101 context.Replace(lhs_);
102 context.Replace(rhs_);
029bc65b 103 return this;
3b52fd1a
JF
104}
105
106CYStatement *CYBlock::Replace(CYContext &context) {
107 statements_ = statements_->ReplaceAll(context);
029bc65b
JF
108 if (statements_ == NULL)
109 return $ CYEmpty();
110 return this;
3b52fd1a
JF
111}
112
113CYStatement *CYBreak::Replace(CYContext &context) {
029bc65b 114 return this;
3b52fd1a
JF
115}
116
117CYExpression *CYCall::Replace(CYContext &context) {
118 context.Replace(function_);
119 arguments_->Replace(context);
029bc65b 120 return this;
3b52fd1a
JF
121}
122
37954781
JF
123namespace cy {
124namespace Syntax {
125
126void Catch::Replace(CYContext &context) { $T()
3b52fd1a
JF
127 code_.Replace(context);
128}
129
37954781
JF
130} }
131
3b52fd1a
JF
132void CYClause::Replace(CYContext &context) { $T()
133 context.Replace(case_);
134 statements_ = statements_->ReplaceAll(context);
135 next_->Replace(context);
136}
137
320ce753 138CYStatement *CYComment::Replace(CYContext &context) {
029bc65b 139 return this;
320ce753
JF
140}
141
3b52fd1a
JF
142CYExpression *CYCompound::Replace(CYContext &context) {
143 expressions_ = expressions_->ReplaceAll(context);
029bc65b 144 return expressions_ == NULL ? NULL : this;
3b52fd1a
JF
145}
146
147CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
148 CYFunctionParameter *next(next_->Parameters(context));
149 if (CYFunctionParameter *parameter = Parameter(context)) {
150 parameter->SetNext(next);
151 return parameter;
152 } else
153 return next;
154}
155
156CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
157 return next_ == NULL ? statement : next_->Replace(context, statement);
158}
159
160CYExpression *CYCondition::Replace(CYContext &context) {
161 context.Replace(test_);
162 context.Replace(true_);
163 context.Replace(false_);
029bc65b 164 return this;
3b52fd1a
JF
165}
166
167CYStatement *CYContinue::Replace(CYContext &context) {
029bc65b
JF
168 return this;
169}
170
171CYAssignment *CYDeclaration::Assignment(CYContext &context) {
172 CYExpression *variable(Replace(context));
173 return initialiser_ == NULL ? NULL : $ CYAssign(variable, initialiser_);
3b52fd1a
JF
174}
175
176CYExpression *CYDeclaration::ForEachIn(CYContext &context) {
177 return $ CYVariable(identifier_);
178}
179
029bc65b 180CYExpression *CYDeclaration::Replace(CYContext &context) {
a86e34d0
JF
181 context.Replace(identifier_);
182 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
183 return $ CYVariable(identifier_);
3b52fd1a
JF
184}
185
550ee46a
JF
186CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
187 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context));
188}
189
029bc65b
JF
190CYCompound *CYDeclarations::Replace(CYContext &context) {
191 CYCompound *compound;
192 if (next_ == NULL) compound:
193 compound = $ CYCompound();
194 else {
195 compound = next_->Replace(context);
196 if (compound == NULL)
197 goto compound;
198 }
199
200 if (CYAssignment *assignment = declaration_->Assignment(context))
201 compound->AddPrev(assignment);
202 return compound;
3b52fd1a
JF
203}
204
205CYExpression *CYDirectMember::Replace(CYContext &context) {
206 Replace_(context);
029bc65b 207 return this;
3b52fd1a
JF
208}
209
210CYStatement *CYDoWhile::Replace(CYContext &context) {
211 context.Replace(test_);
212 context.Replace(code_);
029bc65b 213 return this;
3b52fd1a
JF
214}
215
216void CYElement::Replace(CYContext &context) { $T()
217 context.Replace(value_);
218 next_->Replace(context);
219}
220
029bc65b
JF
221CYStatement *CYEmpty::Collapse(CYContext &context) {
222 return next_;
223}
224
3b52fd1a 225CYStatement *CYEmpty::Replace(CYContext &context) {
029bc65b
JF
226 return this;
227}
228
229CYStatement *CYExpress::Collapse(CYContext &context) {
230 if (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
231 CYCompound *next(dynamic_cast<CYCompound *>(express->expression_));
232 if (next == NULL)
233 next = $ CYCompound(express->expression_);
234 next->AddPrev(expression_);
235 expression_ = next;
236 SetNext(express->next_);
237 }
238
239 return this;
3b52fd1a
JF
240}
241
242CYStatement *CYExpress::Replace(CYContext &context) {
243 context.Replace(expression_);
029bc65b
JF
244 if (expression_ == NULL)
245 return $ CYEmpty();
246 return this;
3b52fd1a
JF
247}
248
249CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
250 return this;
251}
252
253CYExpression *CYExpression::ForEachIn(CYContext &context) {
254 return this;
255}
256
257CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL)
258 CYExpression *replace(this);
259 context.Replace(replace);
260
261 if (CYExpression *next = next_->ReplaceAll(context))
262 replace->SetNext(next);
263 else
264 replace->SetNext(next_);
265
266 return replace;
267}
268
4644480a
JF
269CYNumber *CYFalse::Number(CYContext &context) {
270 return $D(0);
271}
272
273CYString *CYFalse::String(CYContext &context) {
274 return $S("false");
275}
276
3b52fd1a
JF
277void CYFinally::Replace(CYContext &context) { $T()
278 code_.Replace(context);
279}
280
281CYStatement *CYFor::Replace(CYContext &context) {
029bc65b 282 context.Replace(initialiser_);
3b52fd1a
JF
283 context.Replace(test_);
284 context.Replace(increment_);
285 context.Replace(code_);
029bc65b 286 return this;
3b52fd1a
JF
287}
288
289CYStatement *CYForIn::Replace(CYContext &context) {
029bc65b
JF
290 // XXX: this actually might need a prefix statement
291 context.Replace(initialiser_);
3b52fd1a
JF
292 context.Replace(set_);
293 context.Replace(code_);
029bc65b 294 return this;
3b52fd1a
JF
295}
296
297CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
298 return $ CYFunctionParameter(name_);
299}
300
301CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
302 return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement));
303}
304
305CYStatement *CYForEachIn::Replace(CYContext &context) {
306 CYVariable *cys($V("$cys")), *cyt($V("$cyt"));
307
550ee46a 308 return $ CYLet($L2($L($I("$cys"), set_), $L($I("$cyt"))), $$->*
3b52fd1a
JF
309 $ CYForIn(cyt, cys, $ CYBlock($$->*
310 $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->*
311 code_
312 ))
550ee46a 313 );
3b52fd1a
JF
314}
315
316CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
317 return $ CYFunctionParameter(name_);
318}
319
320CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
321 CYVariable *cys($V("$cys")), *name($ CYVariable(name_));
322
323 return $E($C0($F(NULL, $P1("$cys"), $$->*
324 $E($ CYAssign(cys, set_))->*
325 $ CYForIn(name, cys, $ CYBlock($$->*
326 $E($ CYAssign(name, $M(cys, name)))->*
327 CYComprehension::Replace(context, statement)
328 ))
329 )));
330}
331
14ec9e00 332void CYFunction::Inject(CYContext &context) {
6a981250 333 context.Replace(name_);
a86e34d0 334 context.scope_->Declare(context, name_, CYIdentifierOther);
14ec9e00
JF
335}
336
337void CYFunction::Replace_(CYContext &context, bool outer) {
338 if (outer)
339 Inject(context);
340
a846a8cd
JF
341 CYScope scope;
342 scope.parent_ = context.scope_;
343 context.scope_ = &scope;
029bc65b 344
14ec9e00
JF
345 if (!outer && name_ != NULL)
346 Inject(context);
347
029bc65b 348 parameters_->Replace(context);
3b52fd1a 349 code_.Replace(context);
029bc65b 350
a846a8cd
JF
351 context.scope_ = scope.parent_;
352 scope.Scope(context, code_.statements_);
3b52fd1a
JF
353}
354
355CYExpression *CYFunctionExpression::Replace(CYContext &context) {
14ec9e00 356 Replace_(context, false);
029bc65b
JF
357 return this;
358}
359
360void CYFunctionParameter::Replace(CYContext &context) { $T()
361 name_ = name_->Replace(context);
a86e34d0 362 context.scope_->Declare(context, name_, CYIdentifierArgument);
029bc65b 363 next_->Replace(context);
3b52fd1a
JF
364}
365
366CYStatement *CYFunctionStatement::Replace(CYContext &context) {
14ec9e00 367 Replace_(context, true);
029bc65b
JF
368 return this;
369}
370
371CYIdentifier *CYIdentifier::Replace(CYContext &context) {
a846a8cd 372 if (replace_ != NULL && replace_ != this)
de9fc71b 373 return replace_->Replace(context);
a846a8cd 374 replace_ = context.scope_->Lookup(context, this);
a86e34d0 375 return replace_;
3b52fd1a
JF
376}
377
378CYStatement *CYIf::Replace(CYContext &context) {
379 context.Replace(test_);
380 context.Replace(true_);
381 context.Replace(false_);
029bc65b 382 return this;
3b52fd1a
JF
383}
384
385CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
386 return NULL;
387}
388
389CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
390 return $ CYIf(test_, CYComprehension::Replace(context, statement));
391}
392
393CYExpression *CYIndirect::Replace(CYContext &context) {
394 CYPrefix::Replace(context);
395 return $M(rhs_, $S("$cyi"));
396}
397
398CYExpression *CYIndirectMember::Replace(CYContext &context) {
399 Replace_(context);
400 return $M($ CYIndirect(object_), property_);
401}
402
403CYExpression *CYInfix::Replace(CYContext &context) {
404 context.Replace(lhs_);
405 context.Replace(rhs_);
029bc65b 406 return this;
3b52fd1a
JF
407}
408
409CYStatement *CYLabel::Replace(CYContext &context) {
410 context.Replace(statement_);
029bc65b 411 return this;
3b52fd1a
JF
412}
413
550ee46a
JF
414CYStatement *CYLet::Replace(CYContext &context) {
415 return $ CYWith($ CYObject(declarations_->Property(context)), &code_);
416}
417
3b52fd1a
JF
418void CYMember::Replace_(CYContext &context) {
419 context.Replace(object_);
420 context.Replace(property_);
421}
422
3b52fd1a
JF
423CYExpression *CYNew::Replace(CYContext &context) {
424 context.Replace(constructor_);
425 arguments_->Replace(context);
029bc65b 426 return this;
3b52fd1a
JF
427}
428
4644480a
JF
429CYNumber *CYNull::Number(CYContext &context) {
430 return $D(0);
431}
432
433CYString *CYNull::String(CYContext &context) {
434 return $S("null");
435}
436
437CYNumber *CYNumber::Number(CYContext &context) {
438 return this;
439}
440
441CYString *CYNumber::String(CYContext &context) {
442 // XXX: there is a precise algorithm for this
443 return $S(apr_psprintf(context.pool_, "%.17g", Value()));
444}
445
3b52fd1a
JF
446CYExpression *CYObject::Replace(CYContext &context) {
447 properties_->Replace(context);
029bc65b 448 return this;
3b52fd1a
JF
449}
450
451CYExpression *CYPostfix::Replace(CYContext &context) {
452 context.Replace(lhs_);
029bc65b 453 return this;
3b52fd1a
JF
454}
455
456CYExpression *CYPrefix::Replace(CYContext &context) {
457 context.Replace(rhs_);
029bc65b 458 return this;
3b52fd1a
JF
459}
460
10711823 461// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
45d0c928 462#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
10711823 463//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
45d0c928 464
e013809d
JF
465namespace {
466 struct IdentifierUsageLess :
467 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
468 {
469 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
470 if (lhs->usage_ != rhs->usage_)
471 return lhs->usage_ > rhs->usage_;
472 return lhs < rhs;
473 }
474 };
475
476 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
477}
478
3b52fd1a 479void CYProgram::Replace(CYContext &context) {
a846a8cd
JF
480 CYScope scope;
481 scope.parent_ = context.scope_;
482 context.scope_ = &scope;
3b52fd1a 483 statements_ = statements_->ReplaceAll(context);
a846a8cd 484 context.scope_ = scope.parent_;
a846a8cd 485 scope.Scope(context, statements_);
14ec9e00
JF
486
487 size_t offset(0);
488
6a981250 489 CYCStringSet external;
a846a8cd 490 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
6a981250
JF
491 external.insert((*i)->Word());
492
e013809d
JF
493 IdentifierUsages usages;
494
a846a8cd
JF
495 if (offset < context.rename_.size())
496 for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_)
497 usages.insert(i);
e013809d 498
14ec9e00 499 // XXX: totalling the probable occurrences and sorting by them would improve the result
a846a8cd 500 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
de9fc71b
JF
501 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
502
14ec9e00
JF
503 const char *name;
504
505 if (context.options_.verbose_)
506 name = apr_psprintf(context.pool_, "$%"APR_SIZE_T_FMT"", offset);
507 else {
508 char id[8];
509 id[7] = '\0';
510
511 id:
512 unsigned position(7), local(offset + 1);
513
514 do {
45d0c928
JF
515 unsigned index(local % (sizeof(MappingSet) - 1));
516 local /= sizeof(MappingSet) - 1;
517 id[--position] = MappingSet[index];
14ec9e00
JF
518 } while (local != 0);
519
6a981250 520 if (external.find(id + position) != external.end()) {
14ec9e00
JF
521 ++offset;
522 goto id;
523 }
524
525 name = apr_pstrmemdup(context.pool_, id + position, 7 - position);
526 // XXX: at some point, this could become a keyword
527 }
528
e013809d 529 for (CYIdentifier *identifier(i->identifier_); identifier != NULL; identifier = identifier->next_)
14ec9e00
JF
530 identifier->Set(name);
531 }
3b52fd1a
JF
532}
533
534void CYProperty::Replace(CYContext &context) { $T()
535 context.Replace(value_);
536 next_->Replace(context);
537}
538
539CYStatement *CYReturn::Replace(CYContext &context) {
540 context.Replace(value_);
029bc65b
JF
541 return this;
542}
543
a86e34d0
JF
544void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
545 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
546}
547
548CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
549 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
550 return *insert.first;
551}
552
6a981250 553void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
10711823 554 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
6a981250
JF
555 if (!insert.second) {
556 if ((*insert.first)->offset_ < identifier->offset_)
557 (*insert.first)->offset_ = identifier->offset_;
558 identifier->replace_ = *insert.first;
e013809d 559 (*insert.first)->usage_ += identifier->usage_ + 1;
029bc65b
JF
560 }
561}
562
10711823
JF
563namespace {
564 struct IdentifierOffset {
565 size_t offset_;
566 CYIdentifierFlags flags_;
e013809d 567 size_t usage_;
10711823
JF
568 CYIdentifier *identifier_;
569
e013809d
JF
570 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
571 offset_(identifier->offset_),
10711823 572 flags_(flags),
e013809d 573 usage_(identifier->usage_),
10711823
JF
574 identifier_(identifier)
575 {
576 }
577 };
578
579 struct IdentifierOffsetLess :
580 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
581 {
582 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
583 if (lhs.offset_ != rhs.offset_)
584 return lhs.offset_ < rhs.offset_;
585 if (lhs.flags_ != rhs.flags_)
586 return lhs.flags_ < rhs.flags_;
e013809d 587 /*if (lhs.usage_ != rhs.usage_)
a846a8cd 588 return lhs.usage_ < rhs.usage_;*/
10711823
JF
589 return lhs.identifier_ < rhs.identifier_;
590 }
591 };
592
593 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
594}
595
029bc65b 596void CYScope::Scope(CYContext &context, CYStatement *&statements) {
2c81c6df
JF
597 if (parent_ == NULL)
598 return;
599
029bc65b 600 CYDeclarations *last(NULL), *curr(NULL);
14ec9e00 601
10711823 602 IdentifierOffsets offsets;
6a981250 603
10711823 604 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
a846a8cd 605 if (i->second != CYIdentifierMagic)
e013809d 606 offsets.insert(IdentifierOffset(i->first, i->second));
10711823
JF
607
608 size_t offset(0);
609
610 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
611 if (i->flags_ == CYIdentifierVariable) {
612 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
029bc65b
JF
613 if (last == NULL)
614 last = next;
615 if (curr != NULL)
616 curr->SetNext(next);
617 curr = next;
618 }
029bc65b 619
10711823
JF
620 if (offset < i->offset_)
621 offset = i->offset_;
a846a8cd
JF
622 if (context.rename_.size() <= offset)
623 context.rename_.resize(offset + 1);
10711823 624
a846a8cd 625 CYIdentifierUsage &rename(context.rename_[offset++]);
e013809d
JF
626 i->identifier_->SetNext(rename.identifier_);
627 rename.identifier_ = i->identifier_;
628 rename.usage_ += i->identifier_->usage_ + 1;
6a981250
JF
629 }
630
029bc65b
JF
631 if (last != NULL) {
632 CYVar *var($ CYVar(last));
633 var->SetNext(statements);
634 statements = var;
635 }
636
6a981250
JF
637 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
638 if (internal_.find(*i) == internal_.end()) {
de9fc71b 639 //std::cout << *i << '=' << offset << std::endl;
6a981250
JF
640 if ((*i)->offset_ < offset)
641 (*i)->offset_ = offset;
642 parent_->Merge(context, *i);
643 }
029bc65b
JF
644}
645
646CYStatement *CYStatement::Collapse(CYContext &context) {
647 return this;
3b52fd1a
JF
648}
649
3b52fd1a
JF
650CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL)
651 CYStatement *replace(this);
652 context.Replace(replace);
029bc65b
JF
653 replace->SetNext(next_->ReplaceAll(context));
654 return replace->Collapse(context);
3b52fd1a
JF
655}
656
4644480a
JF
657CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
658 size_t size(size_ + rhs->size_);
659 char *value(new(context.pool_) char[size + 1]);
660 memcpy(value, value_, size_);
661 memcpy(value + size_, rhs->value_, rhs->size_);
662 value[size] = '\0';
a14eb702 663 return $S(value, size);
4644480a
JF
664}
665
666CYNumber *CYString::Number(CYContext &context) {
667 // XXX: there is a precise algorithm for this
668 return NULL;
669}
670
671CYString *CYString::String(CYContext &context) {
672 return this;
673}
674
3b52fd1a
JF
675CYStatement *CYSwitch::Replace(CYContext &context) {
676 context.Replace(value_);
677 clauses_->Replace(context);
029bc65b 678 return this;
3b52fd1a
JF
679}
680
681CYExpression *CYThis::Replace(CYContext &context) {
029bc65b 682 return this;
3b52fd1a
JF
683}
684
37954781
JF
685namespace cy {
686namespace Syntax {
687
688CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a 689 context.Replace(value_);
029bc65b 690 return this;
3b52fd1a
JF
691}
692
37954781
JF
693} }
694
3b52fd1a 695CYExpression *CYTrivial::Replace(CYContext &context) {
029bc65b 696 return this;
3b52fd1a
JF
697}
698
4644480a
JF
699CYNumber *CYTrue::Number(CYContext &context) {
700 return $D(1);
701}
702
703CYString *CYTrue::String(CYContext &context) {
704 return $S("true");
705}
706
37954781
JF
707namespace cy {
708namespace Syntax {
709
710CYStatement *Try::Replace(CYContext &context) {
3b52fd1a
JF
711 code_.Replace(context);
712 catch_->Replace(context);
713 finally_->Replace(context);
029bc65b 714 return this;
3b52fd1a
JF
715}
716
37954781
JF
717} }
718
3b52fd1a 719CYStatement *CYVar::Replace(CYContext &context) {
029bc65b 720 return $E(declarations_->Replace(context));
3b52fd1a
JF
721}
722
723CYExpression *CYVariable::Replace(CYContext &context) {
029bc65b
JF
724 name_ = name_->Replace(context);
725 return this;
3b52fd1a
JF
726}
727
728CYStatement *CYWhile::Replace(CYContext &context) {
729 context.Replace(test_);
730 context.Replace(code_);
029bc65b 731 return this;
3b52fd1a
JF
732}
733
734CYStatement *CYWith::Replace(CYContext &context) {
735 context.Replace(scope_);
736 context.Replace(code_);
029bc65b 737 return this;
3b52fd1a
JF
738}
739
740CYExpression *CYWord::ClassName(CYContext &context, bool object) {
741 CYString *name($S(this));
742 if (object)
743 return $C1($V("objc_getClass"), name);
744 else
745 return name;
746}