]>
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 | ||
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() { | |
0cbeddf8 | 213 | return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL)); |
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 | ||
9a39f705 JF |
292 | CYExpression *CYEncodedType::Replace(CYContext &context) { |
293 | return typed_->Replace(context); | |
294 | } | |
295 | ||
720a57a8 JF |
296 | CYStatement *CYExpress::Replace(CYContext &context) { |
297 | while (CYExpress *express = dynamic_cast<CYExpress *>(next_)) { | |
298 | CYCompound *compound(dynamic_cast<CYCompound *>(express->expression_)); | |
299 | if (compound == NULL) | |
300 | compound = $ CYCompound(express->expression_); | |
301 | compound->AddPrev(expression_); | |
302 | expression_ = compound; | |
029bc65b JF |
303 | SetNext(express->next_); |
304 | } | |
305 | ||
3b52fd1a | 306 | context.Replace(expression_); |
029bc65b JF |
307 | if (expression_ == NULL) |
308 | return $ CYEmpty(); | |
720a57a8 | 309 | |
029bc65b | 310 | return this; |
3b52fd1a JF |
311 | } |
312 | ||
6c093cce JF |
313 | CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) { |
314 | return $C1(this, value); | |
315 | } | |
316 | ||
3b52fd1a JF |
317 | CYExpression *CYExpression::ClassName(CYContext &context, bool object) { |
318 | return this; | |
319 | } | |
320 | ||
ad3b38bb JF |
321 | CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) { |
322 | return $E($ CYAssign(this, value)); | |
3b52fd1a JF |
323 | } |
324 | ||
ae65d594 JF |
325 | CYAssignment *CYExpression::Assignment(CYContext &context) { |
326 | return NULL; | |
327 | } | |
328 | ||
4644480a JF |
329 | CYNumber *CYFalse::Number(CYContext &context) { |
330 | return $D(0); | |
331 | } | |
332 | ||
333 | CYString *CYFalse::String(CYContext &context) { | |
334 | return $S("false"); | |
335 | } | |
336 | ||
a0be43fc JF |
337 | CYExpression *CYFatArrow::Replace(CYContext &context) { |
338 | CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_)); | |
339 | function->this_.SetNext(context.this_); | |
340 | return function; | |
341 | } | |
342 | ||
3b52fd1a JF |
343 | void CYFinally::Replace(CYContext &context) { $T() |
344 | code_.Replace(context); | |
345 | } | |
346 | ||
347 | CYStatement *CYFor::Replace(CYContext &context) { | |
029bc65b | 348 | context.Replace(initialiser_); |
3b52fd1a JF |
349 | context.Replace(test_); |
350 | context.Replace(increment_); | |
351 | context.Replace(code_); | |
029bc65b | 352 | return this; |
3b52fd1a JF |
353 | } |
354 | ||
15b88a33 JF |
355 | CYCompound *CYForDeclarations::Replace(CYContext &context) { |
356 | declarations_->Replace(context); | |
357 | return declarations_->Compound(context); | |
358 | } | |
359 | ||
360 | // XXX: this still feels highly suboptimal | |
3b52fd1a | 361 | CYStatement *CYForIn::Replace(CYContext &context) { |
127c1a9c | 362 | if (CYAssignment *assignment = initialiser_->Assignment(context)) |
15b88a33 JF |
363 | return $ CYBlock($$->* |
364 | $E(assignment)->* | |
365 | this | |
366 | ); | |
ae65d594 | 367 | |
127c1a9c JF |
368 | context.Replace(initialiser_); |
369 | context.Replace(set_); | |
370 | context.Replace(code_); | |
15b88a33 | 371 | return this; |
3b52fd1a JF |
372 | } |
373 | ||
374 | CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const { | |
c8a0500b | 375 | return $ CYFunctionParameter($ CYDeclaration(name_)); |
3b52fd1a JF |
376 | } |
377 | ||
378 | CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
4e58e244 | 379 | return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement)); |
3b52fd1a JF |
380 | } |
381 | ||
d5618df7 | 382 | CYStatement *CYForOf::Replace(CYContext &context) { |
04ea132d JF |
383 | if (CYAssignment *assignment = initialiser_->Assignment(context)) |
384 | return $ CYBlock($$->* | |
385 | $E(assignment)->* | |
386 | this | |
387 | ); | |
388 | ||
2eb8215d | 389 | CYIdentifier *cys($I("$cys")), *cyt($I("$cyt")); |
3b52fd1a | 390 | |
c8a0500b | 391 | return $ CYLetStatement($L2($ CYDeclaration(cys, set_), $ CYDeclaration(cyt)), $$->* |
2eb8215d | 392 | $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->* |
ad3b38bb | 393 | initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))->* |
3b52fd1a JF |
394 | code_ |
395 | )) | |
550ee46a | 396 | ); |
3b52fd1a JF |
397 | } |
398 | ||
d5618df7 | 399 | CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const { |
c8a0500b | 400 | return $ CYFunctionParameter($ CYDeclaration(name_)); |
3b52fd1a JF |
401 | } |
402 | ||
d5618df7 | 403 | CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const { |
2eb8215d | 404 | CYIdentifier *cys($I("cys")); |
3b52fd1a | 405 | |
c8a0500b | 406 | return $E($C0($F(NULL, $P1($L("$cys")), $$->* |
2eb8215d JF |
407 | $E($ CYAssign($V(cys), set_))->* |
408 | $ CYForIn($V(name_), $V(cys), $ CYBlock($$->* | |
409 | $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->* | |
3b52fd1a JF |
410 | CYComprehension::Replace(context, statement) |
411 | )) | |
412 | ))); | |
413 | } | |
414 | ||
14ec9e00 | 415 | void CYFunction::Inject(CYContext &context) { |
6a981250 | 416 | context.Replace(name_); |
a86e34d0 | 417 | context.scope_->Declare(context, name_, CYIdentifierOther); |
14ec9e00 JF |
418 | } |
419 | ||
420 | void CYFunction::Replace_(CYContext &context, bool outer) { | |
421 | if (outer) | |
422 | Inject(context); | |
423 | ||
a0be43fc JF |
424 | CYThisScope *_this(context.this_); |
425 | context.this_ = CYGetLast(&this_); | |
426 | ||
06293152 JF |
427 | CYNonLocal *nonlocal(context.nonlocal_); |
428 | CYNonLocal *nextlocal(context.nextlocal_); | |
429 | ||
ab2aa221 | 430 | bool localize; |
06293152 | 431 | if (nonlocal_ != NULL) { |
ab2aa221 | 432 | localize = false; |
06293152 JF |
433 | context.nonlocal_ = nonlocal_; |
434 | } else { | |
ab2aa221 JF |
435 | localize = true; |
436 | nonlocal_ = $ CYNonLocal(); | |
06293152 | 437 | context.nextlocal_ = nonlocal_; |
ab2aa221 JF |
438 | } |
439 | ||
61fe0c53 JF |
440 | CYScope scope(!localize, context, code_.statements_); |
441 | ||
14ec9e00 JF |
442 | if (!outer && name_ != NULL) |
443 | Inject(context); | |
444 | ||
c8a0500b | 445 | parameters_->Replace(context, code_); |
3b52fd1a | 446 | code_.Replace(context); |
029bc65b | 447 | |
a0be43fc JF |
448 | if (CYIdentifier *identifier = this_.identifier_) |
449 | code_.statements_ = $$->* | |
450 | $ CYVar($L1($ CYDeclaration(identifier, $ CYThis())))->* | |
451 | code_.statements_; | |
452 | ||
ab2aa221 JF |
453 | if (localize) |
454 | context.NonLocal(code_.statements_); | |
06293152 JF |
455 | |
456 | context.nextlocal_ = nextlocal; | |
ab2aa221 JF |
457 | context.nonlocal_ = nonlocal; |
458 | ||
a0be43fc JF |
459 | context.this_ = _this; |
460 | ||
daf22a65 | 461 | scope.Close(); |
3b52fd1a JF |
462 | } |
463 | ||
464 | CYExpression *CYFunctionExpression::Replace(CYContext &context) { | |
14ec9e00 | 465 | Replace_(context, false); |
029bc65b JF |
466 | return this; |
467 | } | |
468 | ||
c8a0500b JF |
469 | void CYFunctionParameter::Replace(CYContext &context, CYBlock &code) { $T() |
470 | CYAssignment *assignment(initialiser_->Assignment(context)); | |
471 | context.Replace(initialiser_); | |
472 | ||
473 | next_->Replace(context, code); | |
474 | ||
475 | if (assignment != NULL) | |
476 | // XXX: this cast is quite incorrect | |
477 | code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(dynamic_cast<CYExpression *>(initialiser_)), $S("undefined")), $$->* | |
478 | $E(assignment) | |
479 | )); | |
3b52fd1a JF |
480 | } |
481 | ||
482 | CYStatement *CYFunctionStatement::Replace(CYContext &context) { | |
14ec9e00 | 483 | Replace_(context, true); |
029bc65b JF |
484 | return this; |
485 | } | |
486 | ||
487 | CYIdentifier *CYIdentifier::Replace(CYContext &context) { | |
a846a8cd | 488 | if (replace_ != NULL && replace_ != this) |
de9fc71b | 489 | return replace_->Replace(context); |
a846a8cd | 490 | replace_ = context.scope_->Lookup(context, this); |
a86e34d0 | 491 | return replace_; |
3b52fd1a JF |
492 | } |
493 | ||
494 | CYStatement *CYIf::Replace(CYContext &context) { | |
495 | context.Replace(test_); | |
496 | context.Replace(true_); | |
497 | context.Replace(false_); | |
029bc65b | 498 | return this; |
3b52fd1a JF |
499 | } |
500 | ||
501 | CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const { | |
502 | return NULL; | |
503 | } | |
504 | ||
505 | CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const { | |
506 | return $ CYIf(test_, CYComprehension::Replace(context, statement)); | |
507 | } | |
508 | ||
509 | CYExpression *CYIndirect::Replace(CYContext &context) { | |
3b52fd1a JF |
510 | return $M(rhs_, $S("$cyi")); |
511 | } | |
512 | ||
513 | CYExpression *CYIndirectMember::Replace(CYContext &context) { | |
3b52fd1a JF |
514 | return $M($ CYIndirect(object_), property_); |
515 | } | |
516 | ||
517 | CYExpression *CYInfix::Replace(CYContext &context) { | |
518 | context.Replace(lhs_); | |
519 | context.Replace(rhs_); | |
029bc65b | 520 | return this; |
3b52fd1a JF |
521 | } |
522 | ||
523 | CYStatement *CYLabel::Replace(CYContext &context) { | |
524 | context.Replace(statement_); | |
029bc65b | 525 | return this; |
3b52fd1a JF |
526 | } |
527 | ||
690cf1a8 | 528 | CYExpression *CYLambda::Replace(CYContext &context) { |
9a39f705 | 529 | return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), statements_), parameters_->TypeSignature(context, typed_->Replace(context))); |
690cf1a8 JF |
530 | } |
531 | ||
c8a0500b | 532 | CYStatement *CYLetStatement::Replace(CYContext &context) { |
61fe0c53 | 533 | return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context))); |
550ee46a JF |
534 | } |
535 | ||
62f398e4 JF |
536 | CYExpression *CYMultiply::Replace(CYContext &context) { |
537 | CYInfix::Replace(context); | |
538 | ||
539 | CYExpression *lhp(lhs_->Primitive(context)); | |
540 | CYExpression *rhp(rhs_->Primitive(context)); | |
541 | ||
542 | if (CYNumber *lhn = lhp->Number(context)) | |
543 | if (CYNumber *rhn = rhp->Number(context)) | |
544 | return $D(lhn->Value() * rhn->Value()); | |
545 | ||
546 | return this; | |
547 | } | |
548 | ||
2eb8215d JF |
549 | namespace cy { |
550 | namespace Syntax { | |
551 | ||
552 | CYExpression *New::AddArgument(CYContext &context, CYExpression *value) { | |
bf45251b | 553 | CYSetLast(arguments_) = $ CYArgument(value); |
6c093cce JF |
554 | return this; |
555 | } | |
556 | ||
2eb8215d | 557 | CYExpression *New::Replace(CYContext &context) { |
3b52fd1a JF |
558 | context.Replace(constructor_); |
559 | arguments_->Replace(context); | |
029bc65b | 560 | return this; |
3b52fd1a JF |
561 | } |
562 | ||
2eb8215d JF |
563 | } } |
564 | ||
4644480a JF |
565 | CYNumber *CYNull::Number(CYContext &context) { |
566 | return $D(0); | |
567 | } | |
568 | ||
569 | CYString *CYNull::String(CYContext &context) { | |
570 | return $S("null"); | |
571 | } | |
572 | ||
573 | CYNumber *CYNumber::Number(CYContext &context) { | |
574 | return this; | |
575 | } | |
576 | ||
577 | CYString *CYNumber::String(CYContext &context) { | |
578 | // XXX: there is a precise algorithm for this | |
0cbeddf8 | 579 | return $S($pool.sprintf(24, "%.17g", Value())); |
4644480a JF |
580 | } |
581 | ||
3b52fd1a JF |
582 | CYExpression *CYObject::Replace(CYContext &context) { |
583 | properties_->Replace(context); | |
029bc65b | 584 | return this; |
3b52fd1a JF |
585 | } |
586 | ||
587 | CYExpression *CYPostfix::Replace(CYContext &context) { | |
588 | context.Replace(lhs_); | |
029bc65b | 589 | return this; |
3b52fd1a JF |
590 | } |
591 | ||
592 | CYExpression *CYPrefix::Replace(CYContext &context) { | |
593 | context.Replace(rhs_); | |
029bc65b | 594 | return this; |
3b52fd1a JF |
595 | } |
596 | ||
10711823 | 597 | // XXX: this is evil evil black magic. don't ask, don't tell... don't believe! |
45d0c928 | 598 | #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ" |
10711823 | 599 | //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_" |
45d0c928 | 600 | |
e013809d JF |
601 | namespace { |
602 | struct IdentifierUsageLess : | |
603 | std::binary_function<CYIdentifier *, CYIdentifier *, bool> | |
604 | { | |
605 | _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const { | |
606 | if (lhs->usage_ != rhs->usage_) | |
607 | return lhs->usage_ > rhs->usage_; | |
608 | return lhs < rhs; | |
609 | } | |
610 | }; | |
611 | ||
612 | typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages; | |
613 | } | |
614 | ||
3b52fd1a | 615 | void CYProgram::Replace(CYContext &context) { |
61fe0c53 | 616 | CYScope scope(true, context, statements_); |
ab2aa221 | 617 | |
06293152 | 618 | context.nextlocal_ = $ CYNonLocal(); |
cde20a5a | 619 | context.ReplaceAll(statements_); |
ab2aa221 JF |
620 | context.NonLocal(statements_); |
621 | ||
daf22a65 | 622 | scope.Close(); |
14ec9e00 JF |
623 | |
624 | size_t offset(0); | |
625 | ||
6a981250 | 626 | CYCStringSet external; |
a846a8cd | 627 | for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i) |
6a981250 JF |
628 | external.insert((*i)->Word()); |
629 | ||
e013809d JF |
630 | IdentifierUsages usages; |
631 | ||
a846a8cd | 632 | if (offset < context.rename_.size()) |
c2c9f509 JF |
633 | CYForEach (i, context.rename_[offset].identifier_) |
634 | usages.insert(i); | |
e013809d | 635 | |
14ec9e00 | 636 | // XXX: totalling the probable occurrences and sorting by them would improve the result |
a846a8cd | 637 | for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) { |
de9fc71b JF |
638 | //std::cout << *i << ":" << (*i)->offset_ << std::endl; |
639 | ||
14ec9e00 JF |
640 | const char *name; |
641 | ||
642 | if (context.options_.verbose_) | |
0cbeddf8 | 643 | name = $pool.strcat("$", $pool.itoa(offset), NULL); |
14ec9e00 JF |
644 | else { |
645 | char id[8]; | |
646 | id[7] = '\0'; | |
647 | ||
648 | id: | |
649 | unsigned position(7), local(offset + 1); | |
650 | ||
651 | do { | |
45d0c928 JF |
652 | unsigned index(local % (sizeof(MappingSet) - 1)); |
653 | local /= sizeof(MappingSet) - 1; | |
654 | id[--position] = MappingSet[index]; | |
14ec9e00 JF |
655 | } while (local != 0); |
656 | ||
6a981250 | 657 | if (external.find(id + position) != external.end()) { |
14ec9e00 JF |
658 | ++offset; |
659 | goto id; | |
660 | } | |
661 | ||
b799113b | 662 | name = $pool.strmemdup(id + position, 7 - position); |
14ec9e00 JF |
663 | // XXX: at some point, this could become a keyword |
664 | } | |
665 | ||
c2c9f509 | 666 | CYForEach (identifier, i->identifier_) |
14ec9e00 JF |
667 | identifier->Set(name); |
668 | } | |
3b52fd1a JF |
669 | } |
670 | ||
671 | void CYProperty::Replace(CYContext &context) { $T() | |
672 | context.Replace(value_); | |
673 | next_->Replace(context); | |
5192746c JF |
674 | if (value_ == NULL) |
675 | value_ = $U; | |
3b52fd1a JF |
676 | } |
677 | ||
678 | CYStatement *CYReturn::Replace(CYContext &context) { | |
ab2aa221 JF |
679 | if (context.nonlocal_ != NULL) { |
680 | CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_)); | |
681 | return $ cy::Syntax::Throw($ CYObject( | |
4e58e244 | 682 | $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value) |
ab2aa221 JF |
683 | )); |
684 | } | |
685 | ||
3b52fd1a | 686 | context.Replace(value_); |
029bc65b JF |
687 | return this; |
688 | } | |
689 | ||
6c093cce JF |
690 | CYExpression *CYRubyBlock::Replace(CYContext &context) { |
691 | // XXX: this needs to do something much more epic to handle return | |
692 | return call_->AddArgument(context, proc_->Replace(context)); | |
693 | } | |
694 | ||
695 | CYExpression *CYRubyProc::Replace(CYContext &context) { | |
61fe0c53 | 696 | return CYNonLocalize(context, $ CYFunctionExpression(NULL, parameters_, code_)); |
6c093cce JF |
697 | } |
698 | ||
61fe0c53 JF |
699 | CYScope::CYScope(bool transparent, CYContext &context, CYStatement *&statements) : |
700 | transparent_(transparent), | |
daf22a65 JF |
701 | context_(context), |
702 | statements_(statements), | |
703 | parent_(context.scope_) | |
704 | { | |
705 | context_.scope_ = this; | |
706 | } | |
707 | ||
e4676127 JF |
708 | CYScope::~CYScope() { |
709 | } | |
710 | ||
daf22a65 JF |
711 | void CYScope::Close() { |
712 | context_.scope_ = parent_; | |
713 | Scope(context_, statements_); | |
714 | } | |
715 | ||
a86e34d0 | 716 | void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) { |
61fe0c53 | 717 | if (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch) |
daf22a65 | 718 | internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags)); |
61fe0c53 JF |
719 | else if (parent_ != NULL) |
720 | parent_->Declare(context, identifier, flags); | |
a86e34d0 JF |
721 | } |
722 | ||
723 | CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) { | |
724 | std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier)); | |
725 | return *insert.first; | |
726 | } | |
727 | ||
6a981250 | 728 | void CYScope::Merge(CYContext &context, CYIdentifier *identifier) { |
10711823 | 729 | std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier)); |
6a981250 JF |
730 | if (!insert.second) { |
731 | if ((*insert.first)->offset_ < identifier->offset_) | |
732 | (*insert.first)->offset_ = identifier->offset_; | |
733 | identifier->replace_ = *insert.first; | |
e013809d | 734 | (*insert.first)->usage_ += identifier->usage_ + 1; |
029bc65b JF |
735 | } |
736 | } | |
737 | ||
10711823 JF |
738 | namespace { |
739 | struct IdentifierOffset { | |
740 | size_t offset_; | |
741 | CYIdentifierFlags flags_; | |
e013809d | 742 | size_t usage_; |
10711823 JF |
743 | CYIdentifier *identifier_; |
744 | ||
e013809d JF |
745 | IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) : |
746 | offset_(identifier->offset_), | |
10711823 | 747 | flags_(flags), |
e013809d | 748 | usage_(identifier->usage_), |
10711823 JF |
749 | identifier_(identifier) |
750 | { | |
751 | } | |
752 | }; | |
753 | ||
754 | struct IdentifierOffsetLess : | |
755 | std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool> | |
756 | { | |
757 | _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const { | |
758 | if (lhs.offset_ != rhs.offset_) | |
759 | return lhs.offset_ < rhs.offset_; | |
760 | if (lhs.flags_ != rhs.flags_) | |
761 | return lhs.flags_ < rhs.flags_; | |
e013809d | 762 | /*if (lhs.usage_ != rhs.usage_) |
a846a8cd | 763 | return lhs.usage_ < rhs.usage_;*/ |
10711823 JF |
764 | return lhs.identifier_ < rhs.identifier_; |
765 | } | |
766 | }; | |
767 | ||
768 | typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets; | |
769 | } | |
770 | ||
029bc65b | 771 | void CYScope::Scope(CYContext &context, CYStatement *&statements) { |
2c81c6df JF |
772 | if (parent_ == NULL) |
773 | return; | |
774 | ||
029bc65b | 775 | CYDeclarations *last(NULL), *curr(NULL); |
14ec9e00 | 776 | |
10711823 | 777 | IdentifierOffsets offsets; |
6a981250 | 778 | |
10711823 | 779 | for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) |
a846a8cd | 780 | if (i->second != CYIdentifierMagic) |
e013809d | 781 | offsets.insert(IdentifierOffset(i->first, i->second)); |
10711823 JF |
782 | |
783 | size_t offset(0); | |
784 | ||
785 | for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) { | |
786 | if (i->flags_ == CYIdentifierVariable) { | |
787 | CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_))); | |
029bc65b JF |
788 | if (last == NULL) |
789 | last = next; | |
790 | if (curr != NULL) | |
791 | curr->SetNext(next); | |
792 | curr = next; | |
793 | } | |
029bc65b | 794 | |
10711823 JF |
795 | if (offset < i->offset_) |
796 | offset = i->offset_; | |
a846a8cd JF |
797 | if (context.rename_.size() <= offset) |
798 | context.rename_.resize(offset + 1); | |
10711823 | 799 | |
a846a8cd | 800 | CYIdentifierUsage &rename(context.rename_[offset++]); |
e013809d JF |
801 | i->identifier_->SetNext(rename.identifier_); |
802 | rename.identifier_ = i->identifier_; | |
803 | rename.usage_ += i->identifier_->usage_ + 1; | |
6a981250 JF |
804 | } |
805 | ||
029bc65b JF |
806 | if (last != NULL) { |
807 | CYVar *var($ CYVar(last)); | |
808 | var->SetNext(statements); | |
809 | statements = var; | |
810 | } | |
811 | ||
6a981250 JF |
812 | for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) |
813 | if (internal_.find(*i) == internal_.end()) { | |
de9fc71b | 814 | //std::cout << *i << '=' << offset << std::endl; |
6a981250 JF |
815 | if ((*i)->offset_ < offset) |
816 | (*i)->offset_ = offset; | |
817 | parent_->Merge(context, *i); | |
818 | } | |
029bc65b JF |
819 | } |
820 | ||
4644480a JF |
821 | CYString *CYString::Concat(CYContext &context, CYString *rhs) const { |
822 | size_t size(size_ + rhs->size_); | |
2eb8215d | 823 | char *value($ char[size + 1]); |
4644480a JF |
824 | memcpy(value, value_, size_); |
825 | memcpy(value + size_, rhs->value_, rhs->size_); | |
826 | value[size] = '\0'; | |
a14eb702 | 827 | return $S(value, size); |
4644480a JF |
828 | } |
829 | ||
830 | CYNumber *CYString::Number(CYContext &context) { | |
831 | // XXX: there is a precise algorithm for this | |
832 | return NULL; | |
833 | } | |
834 | ||
835 | CYString *CYString::String(CYContext &context) { | |
836 | return this; | |
837 | } | |
838 | ||
3b52fd1a JF |
839 | CYStatement *CYSwitch::Replace(CYContext &context) { |
840 | context.Replace(value_); | |
841 | clauses_->Replace(context); | |
029bc65b | 842 | return this; |
3b52fd1a JF |
843 | } |
844 | ||
845 | CYExpression *CYThis::Replace(CYContext &context) { | |
a0be43fc JF |
846 | if (context.this_ != NULL) |
847 | return $V(context.this_->Identifier(context)); | |
029bc65b | 848 | return this; |
3b52fd1a JF |
849 | } |
850 | ||
37954781 JF |
851 | namespace cy { |
852 | namespace Syntax { | |
853 | ||
854 | CYStatement *Throw::Replace(CYContext &context) { | |
3b52fd1a | 855 | context.Replace(value_); |
029bc65b | 856 | return this; |
3b52fd1a JF |
857 | } |
858 | ||
37954781 JF |
859 | } } |
860 | ||
3b52fd1a | 861 | CYExpression *CYTrivial::Replace(CYContext &context) { |
029bc65b | 862 | return this; |
3b52fd1a JF |
863 | } |
864 | ||
4644480a JF |
865 | CYNumber *CYTrue::Number(CYContext &context) { |
866 | return $D(1); | |
867 | } | |
868 | ||
869 | CYString *CYTrue::String(CYContext &context) { | |
870 | return $S("true"); | |
871 | } | |
872 | ||
37954781 JF |
873 | namespace cy { |
874 | namespace Syntax { | |
875 | ||
876 | CYStatement *Try::Replace(CYContext &context) { | |
3b52fd1a JF |
877 | code_.Replace(context); |
878 | catch_->Replace(context); | |
879 | finally_->Replace(context); | |
029bc65b | 880 | return this; |
3b52fd1a JF |
881 | } |
882 | ||
37954781 JF |
883 | } } |
884 | ||
9a39f705 JF |
885 | CYExpression *CYTypeArrayOf::Replace_(CYContext &context, CYExpression *type) { |
886 | return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_))); | |
690cf1a8 JF |
887 | } |
888 | ||
3fe16be7 JF |
889 | CYExpression *CYTypeBlockWith::Replace_(CYContext &context, CYExpression *type) { |
890 | return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context))); | |
891 | } | |
892 | ||
9a39f705 JF |
893 | CYExpression *CYTypeConstant::Replace_(CYContext &context, CYExpression *type) { |
894 | return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant")))); | |
690cf1a8 JF |
895 | } |
896 | ||
60097023 | 897 | CYStatement *CYTypeDefinition::Replace(CYContext &context) { |
84759b5b | 898 | return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context))); |
60097023 JF |
899 | } |
900 | ||
03db6a67 JF |
901 | CYExpression *CYTypeError::Replace(CYContext &context) { |
902 | _assert(false); | |
903 | return NULL; | |
904 | } | |
905 | ||
9a39f705 JF |
906 | CYExpression *CYTypeModifier::Replace(CYContext &context, CYExpression *type) { $T(type) |
907 | return Replace_(context, type); | |
908 | } | |
909 | ||
910 | CYExpression *CYTypeFunctionWith::Replace_(CYContext &context, CYExpression *type) { | |
911 | return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context))); | |
912 | } | |
913 | ||
3fe283c5 JF |
914 | CYExpression *CYTypeLong::Replace(CYContext &context) { |
915 | return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long"))); | |
916 | } | |
917 | ||
9a39f705 JF |
918 | CYExpression *CYTypePointerTo::Replace_(CYContext &context, CYExpression *type) { |
919 | return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo")))); | |
663c538f JF |
920 | } |
921 | ||
3fe283c5 JF |
922 | CYExpression *CYTypeShort::Replace(CYContext &context) { |
923 | return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short"))); | |
924 | } | |
925 | ||
926 | CYExpression *CYTypeSigned::Replace(CYContext &context) { | |
927 | return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed"))); | |
928 | } | |
929 | ||
930 | CYExpression *CYTypeUnsigned::Replace(CYContext &context) { | |
931 | return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned"))); | |
932 | } | |
933 | ||
934 | CYExpression *CYTypeVariable::Replace(CYContext &context) { | |
935 | return $V(name_); | |
936 | } | |
937 | ||
938 | CYExpression *CYTypeVoid::Replace(CYContext &context) { | |
939 | return $N1($V("Type"), $ CYString("v")); | |
940 | } | |
941 | ||
9a39f705 JF |
942 | CYExpression *CYTypeVolatile::Replace_(CYContext &context, CYExpression *type) { |
943 | return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile")))); | |
690cf1a8 JF |
944 | } |
945 | ||
9a39f705 | 946 | CYExpression *CYTypedIdentifier::Replace(CYContext &context) { |
3fe283c5 | 947 | return modifier_->Replace(context, specifier_->Replace(context)); |
690cf1a8 JF |
948 | } |
949 | ||
663c538f | 950 | CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL) |
9a39f705 | 951 | return $ CYArgument(typed_->Replace(context), next_->Argument(context)); |
663c538f JF |
952 | } |
953 | ||
690cf1a8 JF |
954 | CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL) |
955 | return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context)); | |
956 | } | |
957 | ||
958 | CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix) | |
a2f3ecab | 959 | return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context))); |
690cf1a8 JF |
960 | } |
961 | ||
3b52fd1a | 962 | CYStatement *CYVar::Replace(CYContext &context) { |
15b88a33 JF |
963 | declarations_->Replace(context); |
964 | return $E(declarations_->Compound(context)); | |
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 | } |