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