]>
Commit | Line | Data |
---|---|---|
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" |
029bc65b | 41 | #include "Context.hpp" |
3b52fd1a | 42 | |
3b52fd1a JF |
43 | #include <iomanip> |
44 | ||
4de0686f | 45 | #include "Replace.hpp" |
3b52fd1a | 46 | |
4644480a JF |
47 | CYExpression *CYAdd::Replace(CYContext &context) { |
48 | CYInfix::Replace(context); | |
49 | ||
50 | CYExpression *lhp(lhs_->Primitive(context)); | |
51 | CYExpression *rhp(rhs_->Primitive(context)); | |
52 | ||
53 | CYString *lhs(dynamic_cast<CYString *>(lhp)); | |
54 | CYString *rhs(dynamic_cast<CYString *>(rhp)); | |
55 | ||
56 | if (lhs != NULL || rhs != NULL) { | |
57 | if (lhs == NULL) { | |
58 | lhs = lhp->String(context); | |
59 | if (lhs == NULL) | |
029bc65b | 60 | return this; |
4644480a JF |
61 | } else if (rhs == NULL) { |
62 | rhs = rhp->String(context); | |
63 | if (rhs == NULL) | |
029bc65b | 64 | return this; |
4644480a JF |
65 | } |
66 | ||
67 | return lhs->Concat(context, rhs); | |
68 | } | |
69 | ||
70 | if (CYNumber *lhn = lhp->Number(context)) | |
71 | if (CYNumber *rhn = rhp->Number(context)) | |
72 | return $D(lhn->Value() + rhn->Value()); | |
73 | ||
029bc65b | 74 | return this; |
4644480a JF |
75 | } |
76 | ||
3b52fd1a JF |
77 | CYExpression *CYAddressOf::Replace(CYContext &context) { |
78 | CYPrefix::Replace(context); | |
79 | return $C0($M(rhs_, $S("$cya"))); | |
80 | } | |
81 | ||
82 | void CYArgument::Replace(CYContext &context) { $T() | |
83 | context.Replace(value_); | |
84 | next_->Replace(context); | |
85 | } | |
86 | ||
87 | CYExpression *CYArray::Replace(CYContext &context) { | |
88 | elements_->Replace(context); | |
029bc65b | 89 | return this; |
3b52fd1a JF |
90 | } |
91 | ||
92 | CYExpression *CYArrayComprehension::Replace(CYContext &context) { | |
93 | CYVariable *cyv($V("$cyv")); | |
94 | ||
95 | return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->* | |
96 | $E($ CYAssign(cyv, $ CYArray()))->* | |
97 | comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->* | |
98 | $ CYReturn(cyv) | |
99 | )); | |
100 | } | |
101 | ||
102 | CYExpression *CYAssignment::Replace(CYContext &context) { | |
103 | context.Replace(lhs_); | |
104 | context.Replace(rhs_); | |
029bc65b | 105 | return this; |
3b52fd1a JF |
106 | } |
107 | ||
108 | CYStatement *CYBlock::Replace(CYContext &context) { | |
109 | statements_ = statements_->ReplaceAll(context); | |
029bc65b JF |
110 | if (statements_ == NULL) |
111 | return $ CYEmpty(); | |
112 | return this; | |
3b52fd1a JF |
113 | } |
114 | ||
115 | CYStatement *CYBreak::Replace(CYContext &context) { | |
029bc65b | 116 | return this; |
3b52fd1a JF |
117 | } |
118 | ||
119 | CYExpression *CYCall::Replace(CYContext &context) { | |
120 | context.Replace(function_); | |
121 | arguments_->Replace(context); | |
029bc65b | 122 | return this; |
3b52fd1a JF |
123 | } |
124 | ||
37954781 JF |
125 | namespace cy { |
126 | namespace Syntax { | |
127 | ||
128 | void Catch::Replace(CYContext &context) { $T() | |
3b52fd1a JF |
129 | code_.Replace(context); |
130 | } | |
131 | ||
37954781 JF |
132 | } } |
133 | ||
3b52fd1a JF |
134 | void CYClause::Replace(CYContext &context) { $T() |
135 | context.Replace(case_); | |
136 | statements_ = statements_->ReplaceAll(context); | |
137 | next_->Replace(context); | |
138 | } | |
139 | ||
320ce753 | 140 | CYStatement *CYComment::Replace(CYContext &context) { |
029bc65b | 141 | return this; |
320ce753 JF |
142 | } |
143 | ||
3b52fd1a JF |
144 | CYExpression *CYCompound::Replace(CYContext &context) { |
145 | expressions_ = expressions_->ReplaceAll(context); | |
029bc65b | 146 | return expressions_ == NULL ? NULL : this; |
3b52fd1a JF |
147 | } |
148 | ||
149 | CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL) | |
150 | CYFunctionParameter *next(next_->Parameters(context)); | |
151 | if (CYFunctionParameter *parameter = Parameter(context)) { | |
152 | parameter->SetNext(next); | |
153 | return parameter; | |
154 | } else | |
155 | return next; | |
156 | } | |
157 | ||
158 | CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
159 | return next_ == NULL ? statement : next_->Replace(context, statement); | |
160 | } | |
161 | ||
162 | CYExpression *CYCondition::Replace(CYContext &context) { | |
163 | context.Replace(test_); | |
164 | context.Replace(true_); | |
165 | context.Replace(false_); | |
029bc65b | 166 | return this; |
3b52fd1a JF |
167 | } |
168 | ||
169 | CYStatement *CYContinue::Replace(CYContext &context) { | |
029bc65b JF |
170 | return this; |
171 | } | |
172 | ||
173 | CYAssignment *CYDeclaration::Assignment(CYContext &context) { | |
174 | CYExpression *variable(Replace(context)); | |
175 | return initialiser_ == NULL ? NULL : $ CYAssign(variable, initialiser_); | |
3b52fd1a JF |
176 | } |
177 | ||
178 | CYExpression *CYDeclaration::ForEachIn(CYContext &context) { | |
179 | return $ CYVariable(identifier_); | |
180 | } | |
181 | ||
029bc65b | 182 | CYExpression *CYDeclaration::Replace(CYContext &context) { |
a86e34d0 JF |
183 | context.Replace(identifier_); |
184 | context.scope_->Declare(context, identifier_, CYIdentifierVariable); | |
185 | return $ CYVariable(identifier_); | |
3b52fd1a JF |
186 | } |
187 | ||
550ee46a JF |
188 | CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL) |
189 | return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context)); | |
190 | } | |
191 | ||
029bc65b JF |
192 | CYCompound *CYDeclarations::Replace(CYContext &context) { |
193 | CYCompound *compound; | |
194 | if (next_ == NULL) compound: | |
195 | compound = $ CYCompound(); | |
196 | else { | |
197 | compound = next_->Replace(context); | |
198 | if (compound == NULL) | |
199 | goto compound; | |
200 | } | |
201 | ||
202 | if (CYAssignment *assignment = declaration_->Assignment(context)) | |
203 | compound->AddPrev(assignment); | |
204 | return compound; | |
3b52fd1a JF |
205 | } |
206 | ||
207 | CYExpression *CYDirectMember::Replace(CYContext &context) { | |
208 | Replace_(context); | |
029bc65b | 209 | return this; |
3b52fd1a JF |
210 | } |
211 | ||
212 | CYStatement *CYDoWhile::Replace(CYContext &context) { | |
213 | context.Replace(test_); | |
214 | context.Replace(code_); | |
029bc65b | 215 | return this; |
3b52fd1a JF |
216 | } |
217 | ||
218 | void CYElement::Replace(CYContext &context) { $T() | |
219 | context.Replace(value_); | |
220 | next_->Replace(context); | |
221 | } | |
222 | ||
029bc65b JF |
223 | CYStatement *CYEmpty::Collapse(CYContext &context) { |
224 | return next_; | |
225 | } | |
226 | ||
3b52fd1a | 227 | CYStatement *CYEmpty::Replace(CYContext &context) { |
029bc65b JF |
228 | return this; |
229 | } | |
230 | ||
231 | CYStatement *CYExpress::Collapse(CYContext &context) { | |
232 | if (CYExpress *express = dynamic_cast<CYExpress *>(next_)) { | |
233 | CYCompound *next(dynamic_cast<CYCompound *>(express->expression_)); | |
234 | if (next == NULL) | |
235 | next = $ CYCompound(express->expression_); | |
236 | next->AddPrev(expression_); | |
237 | expression_ = next; | |
238 | SetNext(express->next_); | |
239 | } | |
240 | ||
241 | return this; | |
3b52fd1a JF |
242 | } |
243 | ||
244 | CYStatement *CYExpress::Replace(CYContext &context) { | |
245 | context.Replace(expression_); | |
029bc65b JF |
246 | if (expression_ == NULL) |
247 | return $ CYEmpty(); | |
248 | return this; | |
3b52fd1a JF |
249 | } |
250 | ||
251 | CYExpression *CYExpression::ClassName(CYContext &context, bool object) { | |
252 | return this; | |
253 | } | |
254 | ||
255 | CYExpression *CYExpression::ForEachIn(CYContext &context) { | |
256 | return this; | |
257 | } | |
258 | ||
259 | CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL) | |
260 | CYExpression *replace(this); | |
261 | context.Replace(replace); | |
262 | ||
263 | if (CYExpression *next = next_->ReplaceAll(context)) | |
264 | replace->SetNext(next); | |
265 | else | |
266 | replace->SetNext(next_); | |
267 | ||
268 | return replace; | |
269 | } | |
270 | ||
4644480a JF |
271 | CYNumber *CYFalse::Number(CYContext &context) { |
272 | return $D(0); | |
273 | } | |
274 | ||
275 | CYString *CYFalse::String(CYContext &context) { | |
276 | return $S("false"); | |
277 | } | |
278 | ||
3b52fd1a JF |
279 | void CYFinally::Replace(CYContext &context) { $T() |
280 | code_.Replace(context); | |
281 | } | |
282 | ||
283 | CYStatement *CYFor::Replace(CYContext &context) { | |
029bc65b | 284 | context.Replace(initialiser_); |
3b52fd1a JF |
285 | context.Replace(test_); |
286 | context.Replace(increment_); | |
287 | context.Replace(code_); | |
029bc65b | 288 | return this; |
3b52fd1a JF |
289 | } |
290 | ||
291 | CYStatement *CYForIn::Replace(CYContext &context) { | |
029bc65b JF |
292 | // XXX: this actually might need a prefix statement |
293 | context.Replace(initialiser_); | |
3b52fd1a JF |
294 | context.Replace(set_); |
295 | context.Replace(code_); | |
029bc65b | 296 | return this; |
3b52fd1a JF |
297 | } |
298 | ||
299 | CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const { | |
300 | return $ CYFunctionParameter(name_); | |
301 | } | |
302 | ||
303 | CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
304 | return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement)); | |
305 | } | |
306 | ||
307 | CYStatement *CYForEachIn::Replace(CYContext &context) { | |
308 | CYVariable *cys($V("$cys")), *cyt($V("$cyt")); | |
309 | ||
550ee46a | 310 | return $ CYLet($L2($L($I("$cys"), set_), $L($I("$cyt"))), $$->* |
3b52fd1a JF |
311 | $ CYForIn(cyt, cys, $ CYBlock($$->* |
312 | $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->* | |
313 | code_ | |
314 | )) | |
550ee46a | 315 | ); |
3b52fd1a JF |
316 | } |
317 | ||
318 | CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const { | |
319 | return $ CYFunctionParameter(name_); | |
320 | } | |
321 | ||
322 | CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
323 | CYVariable *cys($V("$cys")), *name($ CYVariable(name_)); | |
324 | ||
325 | return $E($C0($F(NULL, $P1("$cys"), $$->* | |
326 | $E($ CYAssign(cys, set_))->* | |
327 | $ CYForIn(name, cys, $ CYBlock($$->* | |
328 | $E($ CYAssign(name, $M(cys, name)))->* | |
329 | CYComprehension::Replace(context, statement) | |
330 | )) | |
331 | ))); | |
332 | } | |
333 | ||
14ec9e00 JF |
334 | void CYFunction::Inject(CYContext &context) { |
335 | name_ = name_->Replace(context); | |
a86e34d0 | 336 | context.scope_->Declare(context, name_, CYIdentifierOther); |
14ec9e00 JF |
337 | } |
338 | ||
339 | void CYFunction::Replace_(CYContext &context, bool outer) { | |
340 | if (outer) | |
341 | Inject(context); | |
342 | ||
029bc65b JF |
343 | parent_ = context.scope_; |
344 | context.scope_ = this; | |
345 | ||
14ec9e00 JF |
346 | if (!outer && name_ != NULL) |
347 | Inject(context); | |
348 | ||
029bc65b | 349 | parameters_->Replace(context); |
3b52fd1a | 350 | code_.Replace(context); |
029bc65b JF |
351 | |
352 | context.scope_ = parent_; | |
353 | Scope(context, code_.statements_); | |
3b52fd1a JF |
354 | } |
355 | ||
356 | CYExpression *CYFunctionExpression::Replace(CYContext &context) { | |
14ec9e00 | 357 | Replace_(context, false); |
029bc65b JF |
358 | return this; |
359 | } | |
360 | ||
361 | void CYFunctionParameter::Replace(CYContext &context) { $T() | |
362 | name_ = name_->Replace(context); | |
a86e34d0 | 363 | context.scope_->Declare(context, name_, CYIdentifierArgument); |
029bc65b | 364 | next_->Replace(context); |
3b52fd1a JF |
365 | } |
366 | ||
367 | CYStatement *CYFunctionStatement::Replace(CYContext &context) { | |
14ec9e00 | 368 | Replace_(context, true); |
029bc65b JF |
369 | return this; |
370 | } | |
371 | ||
372 | CYIdentifier *CYIdentifier::Replace(CYContext &context) { | |
a86e34d0 JF |
373 | if (replace_ == NULL) |
374 | replace_ = context.scope_->Lookup(context, this); | |
375 | return replace_; | |
3b52fd1a JF |
376 | } |
377 | ||
378 | CYStatement *CYIf::Replace(CYContext &context) { | |
379 | context.Replace(test_); | |
380 | context.Replace(true_); | |
381 | context.Replace(false_); | |
029bc65b | 382 | return this; |
3b52fd1a JF |
383 | } |
384 | ||
385 | CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const { | |
386 | return NULL; | |
387 | } | |
388 | ||
389 | CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
390 | return $ CYIf(test_, CYComprehension::Replace(context, statement)); | |
391 | } | |
392 | ||
393 | CYExpression *CYIndirect::Replace(CYContext &context) { | |
394 | CYPrefix::Replace(context); | |
395 | return $M(rhs_, $S("$cyi")); | |
396 | } | |
397 | ||
398 | CYExpression *CYIndirectMember::Replace(CYContext &context) { | |
399 | Replace_(context); | |
400 | return $M($ CYIndirect(object_), property_); | |
401 | } | |
402 | ||
403 | CYExpression *CYInfix::Replace(CYContext &context) { | |
404 | context.Replace(lhs_); | |
405 | context.Replace(rhs_); | |
029bc65b | 406 | return this; |
3b52fd1a JF |
407 | } |
408 | ||
409 | CYStatement *CYLabel::Replace(CYContext &context) { | |
410 | context.Replace(statement_); | |
029bc65b | 411 | return this; |
3b52fd1a JF |
412 | } |
413 | ||
550ee46a JF |
414 | CYStatement *CYLet::Replace(CYContext &context) { |
415 | return $ CYWith($ CYObject(declarations_->Property(context)), &code_); | |
416 | } | |
417 | ||
3b52fd1a JF |
418 | void CYMember::Replace_(CYContext &context) { |
419 | context.Replace(object_); | |
420 | context.Replace(property_); | |
421 | } | |
422 | ||
3b52fd1a JF |
423 | CYExpression *CYNew::Replace(CYContext &context) { |
424 | context.Replace(constructor_); | |
425 | arguments_->Replace(context); | |
029bc65b | 426 | return this; |
3b52fd1a JF |
427 | } |
428 | ||
4644480a JF |
429 | CYNumber *CYNull::Number(CYContext &context) { |
430 | return $D(0); | |
431 | } | |
432 | ||
433 | CYString *CYNull::String(CYContext &context) { | |
434 | return $S("null"); | |
435 | } | |
436 | ||
437 | CYNumber *CYNumber::Number(CYContext &context) { | |
438 | return this; | |
439 | } | |
440 | ||
441 | CYString *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 |
446 | CYExpression *CYObject::Replace(CYContext &context) { |
447 | properties_->Replace(context); | |
029bc65b | 448 | return this; |
3b52fd1a JF |
449 | } |
450 | ||
451 | CYExpression *CYPostfix::Replace(CYContext &context) { | |
452 | context.Replace(lhs_); | |
029bc65b | 453 | return this; |
3b52fd1a JF |
454 | } |
455 | ||
456 | CYExpression *CYPrefix::Replace(CYContext &context) { | |
457 | context.Replace(rhs_); | |
029bc65b | 458 | return this; |
3b52fd1a JF |
459 | } |
460 | ||
461 | void CYProgram::Replace(CYContext &context) { | |
029bc65b | 462 | parent_ = context.scope_; |
14ec9e00 JF |
463 | CYProgram *program(context.program_); |
464 | ||
029bc65b | 465 | context.scope_ = this; |
14ec9e00 JF |
466 | context.program_ = this; |
467 | ||
3b52fd1a | 468 | statements_ = statements_->ReplaceAll(context); |
14ec9e00 | 469 | |
029bc65b | 470 | context.scope_ = parent_; |
14ec9e00 | 471 | context.program_ = program; |
a86e34d0 | 472 | Scope(context, statements_); |
14ec9e00 JF |
473 | |
474 | size_t offset(0); | |
475 | ||
476 | // XXX: totalling the probable occurrences and sorting by them would improve the result | |
477 | for (CYIdentifierAddressVector::const_iterator i(rename_.begin()); i != rename_.end(); ++i, ++offset) { | |
478 | const char *name; | |
479 | ||
480 | if (context.options_.verbose_) | |
481 | name = apr_psprintf(context.pool_, "$%"APR_SIZE_T_FMT"", offset); | |
482 | else { | |
483 | char id[8]; | |
484 | id[7] = '\0'; | |
485 | ||
486 | id: | |
487 | unsigned position(7), local(offset + 1); | |
488 | ||
489 | do { | |
490 | unsigned index(local % 53); | |
491 | local /= 53; | |
492 | id[--position] = index == 0 ? '0' : index < 27 ? index - 1 + 'a' : index - 27 + 'A'; | |
493 | } while (local != 0); | |
494 | ||
495 | if (external_.find(id + position) != external_.end()) { | |
496 | ++offset; | |
497 | goto id; | |
498 | } | |
499 | ||
500 | name = apr_pstrmemdup(context.pool_, id + position, 7 - position); | |
501 | // XXX: at some point, this could become a keyword | |
502 | } | |
503 | ||
504 | for (CYIdentifier *identifier(*i); identifier != NULL; identifier = identifier->next_) | |
505 | identifier->Set(name); | |
506 | } | |
3b52fd1a JF |
507 | } |
508 | ||
509 | void CYProperty::Replace(CYContext &context) { $T() | |
510 | context.Replace(value_); | |
511 | next_->Replace(context); | |
512 | } | |
513 | ||
514 | CYStatement *CYReturn::Replace(CYContext &context) { | |
515 | context.Replace(value_); | |
029bc65b JF |
516 | return this; |
517 | } | |
518 | ||
a86e34d0 JF |
519 | void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) { |
520 | internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags)); | |
521 | } | |
522 | ||
523 | CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) { | |
524 | std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier)); | |
525 | return *insert.first; | |
526 | } | |
527 | ||
528 | void CYScope::Merge(CYContext &context, CYIdentifierAddressVector &external) { | |
14ec9e00 | 529 | for (CYIdentifierAddressVector::const_iterator i(external.begin()); i != external.end(); ++i) { |
029bc65b JF |
530 | std::pair<CYIdentifierAddressSet::iterator, bool> insert(identifiers_.insert(*i)); |
531 | if (!insert.second) | |
532 | (*i)->replace_ = *insert.first; | |
533 | } | |
534 | } | |
535 | ||
536 | void CYScope::Scope(CYContext &context, CYStatement *&statements) { | |
14ec9e00 | 537 | CYIdentifierAddressVector external; |
029bc65b | 538 | |
14ec9e00 JF |
539 | for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) |
540 | if (internal_.find(*i) == internal_.end()) | |
541 | external.push_back(*i); | |
029bc65b JF |
542 | |
543 | CYDeclarations *last(NULL), *curr(NULL); | |
14ec9e00 JF |
544 | CYProgram *program(context.program_); |
545 | ||
546 | // XXX: we don't want to do this in order, we want to sort it by probable occurrence | |
547 | for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) { | |
548 | if (program != NULL && i->second != CYIdentifierMagic) { | |
549 | if (program->rename_.size() <= offset_) | |
550 | program->rename_.resize(offset_ + 1); | |
551 | CYIdentifier *&identifier(program->rename_[offset_++]); | |
552 | i->first->SetNext(identifier); | |
553 | identifier = i->first; | |
554 | } | |
029bc65b | 555 | |
14ec9e00 JF |
556 | if (i->second == CYIdentifierVariable) { |
557 | CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->first))); | |
029bc65b JF |
558 | if (last == NULL) |
559 | last = next; | |
560 | if (curr != NULL) | |
561 | curr->SetNext(next); | |
562 | curr = next; | |
563 | } | |
14ec9e00 | 564 | } |
029bc65b JF |
565 | |
566 | if (last != NULL) { | |
567 | CYVar *var($ CYVar(last)); | |
568 | var->SetNext(statements); | |
569 | statements = var; | |
570 | } | |
571 | ||
572 | if (parent_ != NULL) { | |
573 | if (parent_->offset_ < offset_) | |
574 | parent_->offset_ = offset_; | |
a86e34d0 | 575 | parent_->Merge(context, external); |
14ec9e00 JF |
576 | } else if (program != NULL) |
577 | for (CYIdentifierAddressVector::const_iterator i(external.begin()); i != external.end(); ++i) | |
578 | program->external_.insert((*i)->Word()); | |
029bc65b JF |
579 | } |
580 | ||
581 | CYStatement *CYStatement::Collapse(CYContext &context) { | |
582 | return this; | |
3b52fd1a JF |
583 | } |
584 | ||
3b52fd1a JF |
585 | CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL) |
586 | CYStatement *replace(this); | |
587 | context.Replace(replace); | |
029bc65b JF |
588 | replace->SetNext(next_->ReplaceAll(context)); |
589 | return replace->Collapse(context); | |
3b52fd1a JF |
590 | } |
591 | ||
4644480a JF |
592 | CYString *CYString::Concat(CYContext &context, CYString *rhs) const { |
593 | size_t size(size_ + rhs->size_); | |
594 | char *value(new(context.pool_) char[size + 1]); | |
595 | memcpy(value, value_, size_); | |
596 | memcpy(value + size_, rhs->value_, rhs->size_); | |
597 | value[size] = '\0'; | |
a14eb702 | 598 | return $S(value, size); |
4644480a JF |
599 | } |
600 | ||
601 | CYNumber *CYString::Number(CYContext &context) { | |
602 | // XXX: there is a precise algorithm for this | |
603 | return NULL; | |
604 | } | |
605 | ||
606 | CYString *CYString::String(CYContext &context) { | |
607 | return this; | |
608 | } | |
609 | ||
3b52fd1a JF |
610 | CYStatement *CYSwitch::Replace(CYContext &context) { |
611 | context.Replace(value_); | |
612 | clauses_->Replace(context); | |
029bc65b | 613 | return this; |
3b52fd1a JF |
614 | } |
615 | ||
616 | CYExpression *CYThis::Replace(CYContext &context) { | |
029bc65b | 617 | return this; |
3b52fd1a JF |
618 | } |
619 | ||
37954781 JF |
620 | namespace cy { |
621 | namespace Syntax { | |
622 | ||
623 | CYStatement *Throw::Replace(CYContext &context) { | |
3b52fd1a | 624 | context.Replace(value_); |
029bc65b | 625 | return this; |
3b52fd1a JF |
626 | } |
627 | ||
37954781 JF |
628 | } } |
629 | ||
3b52fd1a | 630 | CYExpression *CYTrivial::Replace(CYContext &context) { |
029bc65b | 631 | return this; |
3b52fd1a JF |
632 | } |
633 | ||
4644480a JF |
634 | CYNumber *CYTrue::Number(CYContext &context) { |
635 | return $D(1); | |
636 | } | |
637 | ||
638 | CYString *CYTrue::String(CYContext &context) { | |
639 | return $S("true"); | |
640 | } | |
641 | ||
37954781 JF |
642 | namespace cy { |
643 | namespace Syntax { | |
644 | ||
645 | CYStatement *Try::Replace(CYContext &context) { | |
3b52fd1a JF |
646 | code_.Replace(context); |
647 | catch_->Replace(context); | |
648 | finally_->Replace(context); | |
029bc65b | 649 | return this; |
3b52fd1a JF |
650 | } |
651 | ||
37954781 JF |
652 | } } |
653 | ||
3b52fd1a | 654 | CYStatement *CYVar::Replace(CYContext &context) { |
029bc65b | 655 | return $E(declarations_->Replace(context)); |
3b52fd1a JF |
656 | } |
657 | ||
658 | CYExpression *CYVariable::Replace(CYContext &context) { | |
029bc65b JF |
659 | name_ = name_->Replace(context); |
660 | return this; | |
3b52fd1a JF |
661 | } |
662 | ||
663 | CYStatement *CYWhile::Replace(CYContext &context) { | |
664 | context.Replace(test_); | |
665 | context.Replace(code_); | |
029bc65b | 666 | return this; |
3b52fd1a JF |
667 | } |
668 | ||
669 | CYStatement *CYWith::Replace(CYContext &context) { | |
670 | context.Replace(scope_); | |
671 | context.Replace(code_); | |
029bc65b | 672 | return this; |
3b52fd1a JF |
673 | } |
674 | ||
675 | CYExpression *CYWord::ClassName(CYContext &context, bool object) { | |
676 | CYString *name($S(this)); | |
677 | if (object) | |
678 | return $C1($V("objc_getClass"), name); | |
679 | else | |
680 | return name; | |
681 | } |