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