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