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