]>
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; | |
127c1a9c JF |
201 | |
202 | CYAssignment *value($ CYAssign(Variable(context), initialiser_)); | |
203 | initialiser_ = NULL; | |
204 | return value; | |
15b88a33 JF |
205 | } |
206 | ||
207 | CYVariable *CYDeclaration::Variable(CYContext &context) { | |
208 | return $V(identifier_); | |
3b52fd1a JF |
209 | } |
210 | ||
ad3b38bb JF |
211 | CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) { |
212 | return $ CYVar($L1($L(identifier_, value))); | |
3b52fd1a JF |
213 | } |
214 | ||
029bc65b | 215 | CYExpression *CYDeclaration::Replace(CYContext &context) { |
15b88a33 JF |
216 | context.Replace(identifier_); |
217 | context.scope_->Declare(context, identifier_, CYIdentifierVariable); | |
218 | return Variable(context); | |
219 | } | |
220 | ||
221 | void CYDeclarations::Replace(CYContext &context) { $T() | |
222 | declaration_->Replace(context); | |
223 | next_->Replace(context); | |
3b52fd1a JF |
224 | } |
225 | ||
550ee46a JF |
226 | CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL) |
227 | return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context)); | |
228 | } | |
229 | ||
15b88a33 JF |
230 | CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL) |
231 | return $ CYFunctionParameter(declaration_->identifier_, next_->Parameter(context)); | |
232 | } | |
233 | ||
234 | CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL) | |
235 | return $ CYArgument(declaration_->initialiser_ ?: $U, next_->Argument(context)); | |
236 | } | |
237 | ||
238 | CYCompound *CYDeclarations::Compound(CYContext &context) { $T(NULL) | |
239 | CYCompound *compound(next_->Compound(context) ?: $ CYCompound()); | |
029bc65b JF |
240 | if (CYAssignment *assignment = declaration_->Assignment(context)) |
241 | compound->AddPrev(assignment); | |
242 | return compound; | |
3b52fd1a JF |
243 | } |
244 | ||
245 | CYExpression *CYDirectMember::Replace(CYContext &context) { | |
246 | Replace_(context); | |
029bc65b | 247 | return this; |
3b52fd1a JF |
248 | } |
249 | ||
250 | CYStatement *CYDoWhile::Replace(CYContext &context) { | |
251 | context.Replace(test_); | |
252 | context.Replace(code_); | |
029bc65b | 253 | return this; |
3b52fd1a JF |
254 | } |
255 | ||
256 | void CYElement::Replace(CYContext &context) { $T() | |
257 | context.Replace(value_); | |
258 | next_->Replace(context); | |
259 | } | |
260 | ||
261 | CYStatement *CYEmpty::Replace(CYContext &context) { | |
9cd63688 | 262 | return NULL; |
029bc65b JF |
263 | } |
264 | ||
720a57a8 JF |
265 | CYStatement *CYExpress::Replace(CYContext &context) { |
266 | while (CYExpress *express = dynamic_cast<CYExpress *>(next_)) { | |
267 | CYCompound *compound(dynamic_cast<CYCompound *>(express->expression_)); | |
268 | if (compound == NULL) | |
269 | compound = $ CYCompound(express->expression_); | |
270 | compound->AddPrev(expression_); | |
271 | expression_ = compound; | |
029bc65b JF |
272 | SetNext(express->next_); |
273 | } | |
274 | ||
3b52fd1a | 275 | context.Replace(expression_); |
029bc65b JF |
276 | if (expression_ == NULL) |
277 | return $ CYEmpty(); | |
720a57a8 | 278 | |
029bc65b | 279 | return this; |
3b52fd1a JF |
280 | } |
281 | ||
6c093cce JF |
282 | CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) { |
283 | return $C1(this, value); | |
284 | } | |
285 | ||
3b52fd1a JF |
286 | CYExpression *CYExpression::ClassName(CYContext &context, bool object) { |
287 | return this; | |
288 | } | |
289 | ||
ad3b38bb JF |
290 | CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) { |
291 | return $E($ CYAssign(this, value)); | |
3b52fd1a JF |
292 | } |
293 | ||
ae65d594 JF |
294 | CYAssignment *CYExpression::Assignment(CYContext &context) { |
295 | return NULL; | |
296 | } | |
297 | ||
4644480a JF |
298 | CYNumber *CYFalse::Number(CYContext &context) { |
299 | return $D(0); | |
300 | } | |
301 | ||
302 | CYString *CYFalse::String(CYContext &context) { | |
303 | return $S("false"); | |
304 | } | |
305 | ||
3b52fd1a JF |
306 | void CYFinally::Replace(CYContext &context) { $T() |
307 | code_.Replace(context); | |
308 | } | |
309 | ||
310 | CYStatement *CYFor::Replace(CYContext &context) { | |
029bc65b | 311 | context.Replace(initialiser_); |
3b52fd1a JF |
312 | context.Replace(test_); |
313 | context.Replace(increment_); | |
314 | context.Replace(code_); | |
029bc65b | 315 | return this; |
3b52fd1a JF |
316 | } |
317 | ||
15b88a33 JF |
318 | CYCompound *CYForDeclarations::Replace(CYContext &context) { |
319 | declarations_->Replace(context); | |
320 | return declarations_->Compound(context); | |
321 | } | |
322 | ||
323 | // XXX: this still feels highly suboptimal | |
3b52fd1a | 324 | CYStatement *CYForIn::Replace(CYContext &context) { |
127c1a9c | 325 | if (CYAssignment *assignment = initialiser_->Assignment(context)) |
15b88a33 JF |
326 | return $ CYBlock($$->* |
327 | $E(assignment)->* | |
328 | this | |
329 | ); | |
ae65d594 | 330 | |
127c1a9c JF |
331 | context.Replace(initialiser_); |
332 | context.Replace(set_); | |
333 | context.Replace(code_); | |
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) { |
62fa5fc2 | 475 | return $E($ CYCall($ CYFunctionExpression(NULL, declarations_->Parameter(context), code_), declarations_->Argument(context))); |
550ee46a JF |
476 | } |
477 | ||
3b52fd1a JF |
478 | void CYMember::Replace_(CYContext &context) { |
479 | context.Replace(object_); | |
480 | context.Replace(property_); | |
481 | } | |
482 | ||
2eb8215d JF |
483 | namespace cy { |
484 | namespace Syntax { | |
485 | ||
486 | CYExpression *New::AddArgument(CYContext &context, CYExpression *value) { | |
359e8b82 | 487 | CYSetLast(arguments_, $ CYArgument(value)); |
6c093cce JF |
488 | return this; |
489 | } | |
490 | ||
2eb8215d | 491 | CYExpression *New::Replace(CYContext &context) { |
3b52fd1a JF |
492 | context.Replace(constructor_); |
493 | arguments_->Replace(context); | |
029bc65b | 494 | return this; |
3b52fd1a JF |
495 | } |
496 | ||
2eb8215d JF |
497 | } } |
498 | ||
4644480a JF |
499 | CYNumber *CYNull::Number(CYContext &context) { |
500 | return $D(0); | |
501 | } | |
502 | ||
503 | CYString *CYNull::String(CYContext &context) { | |
504 | return $S("null"); | |
505 | } | |
506 | ||
507 | CYNumber *CYNumber::Number(CYContext &context) { | |
508 | return this; | |
509 | } | |
510 | ||
511 | CYString *CYNumber::String(CYContext &context) { | |
512 | // XXX: there is a precise algorithm for this | |
2eb8215d | 513 | return $S(apr_psprintf($pool, "%.17g", Value())); |
4644480a JF |
514 | } |
515 | ||
3b52fd1a JF |
516 | CYExpression *CYObject::Replace(CYContext &context) { |
517 | properties_->Replace(context); | |
029bc65b | 518 | return this; |
3b52fd1a JF |
519 | } |
520 | ||
4e11a430 JF |
521 | CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) { |
522 | CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_)); | |
523 | parameter = parameter->Replace(context, code); | |
577bfbfa | 524 | context.Replace(initializer_); |
4e11a430 | 525 | |
4e58e244 | 526 | CYVariable *name($V(name_)); |
4e11a430 JF |
527 | code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->* |
528 | $E($ CYAssign(name, initializer_)) | |
529 | )); | |
530 | ||
531 | return parameter; | |
532 | } | |
533 | ||
3b52fd1a JF |
534 | CYExpression *CYPostfix::Replace(CYContext &context) { |
535 | context.Replace(lhs_); | |
029bc65b | 536 | return this; |
3b52fd1a JF |
537 | } |
538 | ||
539 | CYExpression *CYPrefix::Replace(CYContext &context) { | |
540 | context.Replace(rhs_); | |
029bc65b | 541 | return this; |
3b52fd1a JF |
542 | } |
543 | ||
10711823 | 544 | // XXX: this is evil evil black magic. don't ask, don't tell... don't believe! |
45d0c928 | 545 | #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ" |
10711823 | 546 | //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_" |
45d0c928 | 547 | |
e013809d JF |
548 | namespace { |
549 | struct IdentifierUsageLess : | |
550 | std::binary_function<CYIdentifier *, CYIdentifier *, bool> | |
551 | { | |
552 | _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const { | |
553 | if (lhs->usage_ != rhs->usage_) | |
554 | return lhs->usage_ > rhs->usage_; | |
555 | return lhs < rhs; | |
556 | } | |
557 | }; | |
558 | ||
559 | typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages; | |
560 | } | |
561 | ||
3b52fd1a | 562 | void CYProgram::Replace(CYContext &context) { |
daf22a65 | 563 | CYScope scope(CYScopeProgram, context, statements_); |
ab2aa221 | 564 | |
06293152 | 565 | context.nextlocal_ = $ CYNonLocal(); |
cde20a5a | 566 | context.ReplaceAll(statements_); |
ab2aa221 JF |
567 | context.NonLocal(statements_); |
568 | ||
daf22a65 | 569 | scope.Close(); |
14ec9e00 JF |
570 | |
571 | size_t offset(0); | |
572 | ||
6a981250 | 573 | CYCStringSet external; |
a846a8cd | 574 | for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i) |
6a981250 JF |
575 | external.insert((*i)->Word()); |
576 | ||
e013809d JF |
577 | IdentifierUsages usages; |
578 | ||
a846a8cd | 579 | if (offset < context.rename_.size()) |
c2c9f509 JF |
580 | CYForEach (i, context.rename_[offset].identifier_) |
581 | usages.insert(i); | |
e013809d | 582 | |
14ec9e00 | 583 | // XXX: totalling the probable occurrences and sorting by them would improve the result |
a846a8cd | 584 | for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) { |
de9fc71b JF |
585 | //std::cout << *i << ":" << (*i)->offset_ << std::endl; |
586 | ||
14ec9e00 JF |
587 | const char *name; |
588 | ||
589 | if (context.options_.verbose_) | |
2eb8215d | 590 | name = apr_psprintf($pool, "$%"APR_SIZE_T_FMT"", offset); |
14ec9e00 JF |
591 | else { |
592 | char id[8]; | |
593 | id[7] = '\0'; | |
594 | ||
595 | id: | |
596 | unsigned position(7), local(offset + 1); | |
597 | ||
598 | do { | |
45d0c928 JF |
599 | unsigned index(local % (sizeof(MappingSet) - 1)); |
600 | local /= sizeof(MappingSet) - 1; | |
601 | id[--position] = MappingSet[index]; | |
14ec9e00 JF |
602 | } while (local != 0); |
603 | ||
6a981250 | 604 | if (external.find(id + position) != external.end()) { |
14ec9e00 JF |
605 | ++offset; |
606 | goto id; | |
607 | } | |
608 | ||
2eb8215d | 609 | name = apr_pstrmemdup($pool, id + position, 7 - position); |
14ec9e00 JF |
610 | // XXX: at some point, this could become a keyword |
611 | } | |
612 | ||
c2c9f509 | 613 | CYForEach (identifier, i->identifier_) |
14ec9e00 JF |
614 | identifier->Set(name); |
615 | } | |
3b52fd1a JF |
616 | } |
617 | ||
618 | void CYProperty::Replace(CYContext &context) { $T() | |
619 | context.Replace(value_); | |
620 | next_->Replace(context); | |
621 | } | |
622 | ||
623 | CYStatement *CYReturn::Replace(CYContext &context) { | |
ab2aa221 JF |
624 | if (context.nonlocal_ != NULL) { |
625 | CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_)); | |
626 | return $ cy::Syntax::Throw($ CYObject( | |
4e58e244 | 627 | $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value) |
ab2aa221 JF |
628 | )); |
629 | } | |
630 | ||
3b52fd1a | 631 | context.Replace(value_); |
029bc65b JF |
632 | return this; |
633 | } | |
634 | ||
6c093cce JF |
635 | CYExpression *CYRubyBlock::Replace(CYContext &context) { |
636 | // XXX: this needs to do something much more epic to handle return | |
637 | return call_->AddArgument(context, proc_->Replace(context)); | |
638 | } | |
639 | ||
640 | CYExpression *CYRubyProc::Replace(CYContext &context) { | |
ab2aa221 | 641 | CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_)); |
06293152 | 642 | function->nonlocal_ = context.nextlocal_; |
ab2aa221 | 643 | return function; |
6c093cce JF |
644 | } |
645 | ||
daf22a65 JF |
646 | CYScope::CYScope(CYScopeType type, CYContext &context, CYStatement *&statements) : |
647 | type_(type), | |
648 | context_(context), | |
649 | statements_(statements), | |
650 | parent_(context.scope_) | |
651 | { | |
652 | context_.scope_ = this; | |
653 | } | |
654 | ||
e4676127 JF |
655 | CYScope::~CYScope() { |
656 | } | |
657 | ||
daf22a65 JF |
658 | void CYScope::Close() { |
659 | context_.scope_ = parent_; | |
660 | Scope(context_, statements_); | |
661 | } | |
662 | ||
a86e34d0 | 663 | void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) { |
daf22a65 JF |
664 | if (type_ == CYScopeCatch && flags != CYIdentifierCatch) |
665 | parent_->Declare(context, identifier, flags); | |
666 | else | |
667 | internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags)); | |
a86e34d0 JF |
668 | } |
669 | ||
670 | CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) { | |
671 | std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier)); | |
672 | return *insert.first; | |
673 | } | |
674 | ||
6a981250 | 675 | void CYScope::Merge(CYContext &context, CYIdentifier *identifier) { |
10711823 | 676 | std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier)); |
6a981250 JF |
677 | if (!insert.second) { |
678 | if ((*insert.first)->offset_ < identifier->offset_) | |
679 | (*insert.first)->offset_ = identifier->offset_; | |
680 | identifier->replace_ = *insert.first; | |
e013809d | 681 | (*insert.first)->usage_ += identifier->usage_ + 1; |
029bc65b JF |
682 | } |
683 | } | |
684 | ||
10711823 JF |
685 | namespace { |
686 | struct IdentifierOffset { | |
687 | size_t offset_; | |
688 | CYIdentifierFlags flags_; | |
e013809d | 689 | size_t usage_; |
10711823 JF |
690 | CYIdentifier *identifier_; |
691 | ||
e013809d JF |
692 | IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) : |
693 | offset_(identifier->offset_), | |
10711823 | 694 | flags_(flags), |
e013809d | 695 | usage_(identifier->usage_), |
10711823 JF |
696 | identifier_(identifier) |
697 | { | |
698 | } | |
699 | }; | |
700 | ||
701 | struct IdentifierOffsetLess : | |
702 | std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool> | |
703 | { | |
704 | _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const { | |
705 | if (lhs.offset_ != rhs.offset_) | |
706 | return lhs.offset_ < rhs.offset_; | |
707 | if (lhs.flags_ != rhs.flags_) | |
708 | return lhs.flags_ < rhs.flags_; | |
e013809d | 709 | /*if (lhs.usage_ != rhs.usage_) |
a846a8cd | 710 | return lhs.usage_ < rhs.usage_;*/ |
10711823 JF |
711 | return lhs.identifier_ < rhs.identifier_; |
712 | } | |
713 | }; | |
714 | ||
715 | typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets; | |
716 | } | |
717 | ||
029bc65b | 718 | void CYScope::Scope(CYContext &context, CYStatement *&statements) { |
2c81c6df JF |
719 | if (parent_ == NULL) |
720 | return; | |
721 | ||
029bc65b | 722 | CYDeclarations *last(NULL), *curr(NULL); |
14ec9e00 | 723 | |
10711823 | 724 | IdentifierOffsets offsets; |
6a981250 | 725 | |
10711823 | 726 | for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) |
a846a8cd | 727 | if (i->second != CYIdentifierMagic) |
e013809d | 728 | offsets.insert(IdentifierOffset(i->first, i->second)); |
10711823 JF |
729 | |
730 | size_t offset(0); | |
731 | ||
732 | for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) { | |
733 | if (i->flags_ == CYIdentifierVariable) { | |
734 | CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_))); | |
029bc65b JF |
735 | if (last == NULL) |
736 | last = next; | |
737 | if (curr != NULL) | |
738 | curr->SetNext(next); | |
739 | curr = next; | |
740 | } | |
029bc65b | 741 | |
10711823 JF |
742 | if (offset < i->offset_) |
743 | offset = i->offset_; | |
a846a8cd JF |
744 | if (context.rename_.size() <= offset) |
745 | context.rename_.resize(offset + 1); | |
10711823 | 746 | |
a846a8cd | 747 | CYIdentifierUsage &rename(context.rename_[offset++]); |
e013809d JF |
748 | i->identifier_->SetNext(rename.identifier_); |
749 | rename.identifier_ = i->identifier_; | |
750 | rename.usage_ += i->identifier_->usage_ + 1; | |
6a981250 JF |
751 | } |
752 | ||
029bc65b JF |
753 | if (last != NULL) { |
754 | CYVar *var($ CYVar(last)); | |
755 | var->SetNext(statements); | |
756 | statements = var; | |
757 | } | |
758 | ||
6a981250 JF |
759 | for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) |
760 | if (internal_.find(*i) == internal_.end()) { | |
de9fc71b | 761 | //std::cout << *i << '=' << offset << std::endl; |
6a981250 JF |
762 | if ((*i)->offset_ < offset) |
763 | (*i)->offset_ = offset; | |
764 | parent_->Merge(context, *i); | |
765 | } | |
029bc65b JF |
766 | } |
767 | ||
4644480a JF |
768 | CYString *CYString::Concat(CYContext &context, CYString *rhs) const { |
769 | size_t size(size_ + rhs->size_); | |
2eb8215d | 770 | char *value($ char[size + 1]); |
4644480a JF |
771 | memcpy(value, value_, size_); |
772 | memcpy(value + size_, rhs->value_, rhs->size_); | |
773 | value[size] = '\0'; | |
a14eb702 | 774 | return $S(value, size); |
4644480a JF |
775 | } |
776 | ||
777 | CYNumber *CYString::Number(CYContext &context) { | |
778 | // XXX: there is a precise algorithm for this | |
779 | return NULL; | |
780 | } | |
781 | ||
782 | CYString *CYString::String(CYContext &context) { | |
783 | return this; | |
784 | } | |
785 | ||
3b52fd1a JF |
786 | CYStatement *CYSwitch::Replace(CYContext &context) { |
787 | context.Replace(value_); | |
788 | clauses_->Replace(context); | |
029bc65b | 789 | return this; |
3b52fd1a JF |
790 | } |
791 | ||
792 | CYExpression *CYThis::Replace(CYContext &context) { | |
029bc65b | 793 | return this; |
3b52fd1a JF |
794 | } |
795 | ||
37954781 JF |
796 | namespace cy { |
797 | namespace Syntax { | |
798 | ||
799 | CYStatement *Throw::Replace(CYContext &context) { | |
3b52fd1a | 800 | context.Replace(value_); |
029bc65b | 801 | return this; |
3b52fd1a JF |
802 | } |
803 | ||
37954781 JF |
804 | } } |
805 | ||
3b52fd1a | 806 | CYExpression *CYTrivial::Replace(CYContext &context) { |
029bc65b | 807 | return this; |
3b52fd1a JF |
808 | } |
809 | ||
4644480a JF |
810 | CYNumber *CYTrue::Number(CYContext &context) { |
811 | return $D(1); | |
812 | } | |
813 | ||
814 | CYString *CYTrue::String(CYContext &context) { | |
815 | return $S("true"); | |
816 | } | |
817 | ||
37954781 JF |
818 | namespace cy { |
819 | namespace Syntax { | |
820 | ||
821 | CYStatement *Try::Replace(CYContext &context) { | |
3b52fd1a JF |
822 | code_.Replace(context); |
823 | catch_->Replace(context); | |
824 | finally_->Replace(context); | |
029bc65b | 825 | return this; |
3b52fd1a JF |
826 | } |
827 | ||
37954781 JF |
828 | } } |
829 | ||
3b52fd1a | 830 | CYStatement *CYVar::Replace(CYContext &context) { |
15b88a33 JF |
831 | declarations_->Replace(context); |
832 | return $E(declarations_->Compound(context)); | |
3b52fd1a JF |
833 | } |
834 | ||
835 | CYExpression *CYVariable::Replace(CYContext &context) { | |
577bfbfa | 836 | context.Replace(name_); |
029bc65b | 837 | return this; |
3b52fd1a JF |
838 | } |
839 | ||
840 | CYStatement *CYWhile::Replace(CYContext &context) { | |
841 | context.Replace(test_); | |
842 | context.Replace(code_); | |
029bc65b | 843 | return this; |
3b52fd1a JF |
844 | } |
845 | ||
846 | CYStatement *CYWith::Replace(CYContext &context) { | |
847 | context.Replace(scope_); | |
848 | context.Replace(code_); | |
029bc65b | 849 | return this; |
3b52fd1a JF |
850 | } |
851 | ||
852 | CYExpression *CYWord::ClassName(CYContext &context, bool object) { | |
853 | CYString *name($S(this)); | |
854 | if (object) | |
855 | return $C1($V("objc_getClass"), name); | |
856 | else | |
857 | return name; | |
858 | } |