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