]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Remove "!super" hack: "super" is always a keyword.
[cycript.git] / Replace.cpp
CommitLineData
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 <iomanip>
7085e1ab 23#include <map>
3b52fd1a 24
20052ff7
JF
25#include "Replace.hpp"
26#include "Syntax.hpp"
27
61fe0c53
JF
28CYFunctionExpression *CYNonLocalize(CYContext &context, CYFunctionExpression *function) {
29 function->nonlocal_ = context.nextlocal_;
30 return function;
31}
32
c5b15840
JF
33CYFunctionExpression *CYSuperize(CYContext &context, CYFunctionExpression *function) {
34 function->super_ = context.super_;
35 return function;
36}
37
a196a97a
JF
38CYStatement *CYDefineProperty(CYExpression *object, CYExpression *name, bool configurable, bool enumerable, CYProperty *descriptor) {
39 return $E($C3($M($V("Object"), $S("defineProperty")), object, name, $ CYObject(CYList<CYProperty>()
40 ->* (configurable ? $ CYPropertyValue($S("configurable"), $ CYTrue()) : NULL)
41 ->* (enumerable ? $ CYPropertyValue($S("enumerable"), $ CYTrue()) : NULL)
42 ->* descriptor)));
43}
44
12e37ba3
JF
45static void CYImplicitReturn(CYStatement *&code) {
46 if (CYStatement *&last = CYGetLast(code))
47 last = last->Return();
48}
49
4644480a
JF
50CYExpression *CYAdd::Replace(CYContext &context) {
51 CYInfix::Replace(context);
52
fd5cdf97
JF
53 CYString *lhs(dynamic_cast<CYString *>(lhs_));
54 CYString *rhs(dynamic_cast<CYString *>(rhs_));
4644480a
JF
55
56 if (lhs != NULL || rhs != NULL) {
57 if (lhs == NULL) {
fd5cdf97 58 lhs = lhs_->String(context);
4644480a 59 if (lhs == NULL)
029bc65b 60 return this;
4644480a 61 } else if (rhs == NULL) {
fd5cdf97 62 rhs = rhs_->String(context);
4644480a 63 if (rhs == NULL)
029bc65b 64 return this;
4644480a
JF
65 }
66
67 return lhs->Concat(context, rhs);
68 }
69
fd5cdf97
JF
70 if (CYNumber *lhn = lhs_->Number(context))
71 if (CYNumber *rhn = rhs_->Number(context))
4644480a
JF
72 return $D(lhn->Value() + rhn->Value());
73
029bc65b 74 return this;
4644480a
JF
75}
76
3b52fd1a 77CYExpression *CYAddressOf::Replace(CYContext &context) {
3b52fd1a
JF
78 return $C0($M(rhs_, $S("$cya")));
79}
80
7085e1ab
JF
81CYTarget *CYApply::AddArgument(CYContext &context, CYExpression *value) {
82 CYArgument **argument(&arguments_);
83 while (*argument != NULL)
84 argument = &(*argument)->next_;
85 *argument = $ CYArgument(value);
86 return this;
87}
88
5192746c 89CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
3b52fd1a 90 context.Replace(value_);
5192746c
JF
91 next_ = next_->Replace(context);
92
93 if (value_ == NULL) {
94 if (next_ == NULL)
95 return NULL;
96 else
97 value_ = $U;
98 }
99
100 return this;
3b52fd1a
JF
101}
102
7085e1ab 103CYTarget *CYArray::Replace(CYContext &context) {
fc8fc33d
JF
104 if (elements_ != NULL)
105 elements_->Replace(context);
029bc65b 106 return this;
3b52fd1a
JF
107}
108
7085e1ab 109CYTarget *CYArrayComprehension::Replace(CYContext &context) {
a0932196 110 CYIdentifier *cyv(context.Unique());
3b52fd1a 111
a0932196
JF
112 return $C0($F(NULL, $P1($L(cyv), comprehensions_->Parameters(context)), $$
113 ->* $E($ CYAssign($V(cyv), $ CYArray()))
114 ->* comprehensions_->Replace(context, $E($C1($M($V(cyv), $S("push")), expression_)))
115 ->* $ CYReturn($V(cyv))
3b52fd1a
JF
116 ));
117}
118
119CYExpression *CYAssignment::Replace(CYContext &context) {
120 context.Replace(lhs_);
121 context.Replace(rhs_);
029bc65b 122 return this;
3b52fd1a
JF
123}
124
12e37ba3
JF
125CYStatement *CYBlock::Return() {
126 CYImplicitReturn(code_);
127 return this;
128}
129
3b52fd1a 130CYStatement *CYBlock::Replace(CYContext &context) {
7085e1ab 131 CYScope scope(true, context);
b0385401 132 context.ReplaceAll(code_);
7085e1ab
JF
133 scope.Close(context);
134
b0385401 135 if (code_ == NULL)
029bc65b
JF
136 return $ CYEmpty();
137 return this;
3b52fd1a
JF
138}
139
140CYStatement *CYBreak::Replace(CYContext &context) {
029bc65b 141 return this;
3b52fd1a
JF
142}
143
7085e1ab 144CYTarget *CYCall::Replace(CYContext &context) {
3b52fd1a
JF
145 context.Replace(function_);
146 arguments_->Replace(context);
029bc65b 147 return this;
3b52fd1a
JF
148}
149
37954781
JF
150namespace cy {
151namespace Syntax {
152
153void Catch::Replace(CYContext &context) { $T()
50a3d79f 154 CYScope scope(true, context);
daf22a65 155
7085e1ab 156 name_ = name_->Replace(context, CYIdentifierCatch);
daf22a65 157
b0385401 158 context.ReplaceAll(code_);
7085e1ab 159 scope.Close(context);
3b52fd1a
JF
160}
161
37954781
JF
162} }
163
7085e1ab 164CYTarget *CYClassExpression::Replace(CYContext &context) {
c5b15840
JF
165 CYBuilder builder;
166
167 CYIdentifier *super(context.Unique());
168
169 CYIdentifier *old(context.super_);
170 context.super_ = super;
171
172 CYIdentifier *constructor(context.Unique());
173 CYForEach (member, tail_->static_)
174 member->Replace(context, builder, $V(constructor), true);
175
176 CYIdentifier *prototype(context.Unique());
177 CYForEach (member, tail_->instance_)
178 member->Replace(context, builder, $V(prototype), true);
179
180 if (tail_->constructor_ == NULL)
181 tail_->constructor_ = $ CYFunctionExpression(NULL, NULL, NULL);
182 tail_->constructor_ = CYSuperize(context, tail_->constructor_);
183
184 context.super_ = old;
185
186 return $C1($ CYFunctionExpression(NULL, $P($L(super)), $$
187 ->* $ CYVar($L1($L(constructor, tail_->constructor_)))
188 ->* $ CYVar($L1($L(prototype, $ CYFunctionExpression(NULL, NULL, NULL))))
189 ->* $E($ CYAssign($M($V(prototype), $S("prototype")), $M($V(super), $S("prototype"))))
190 ->* $E($ CYAssign($V(prototype), $N($V(prototype))))
a196a97a 191 ->* CYDefineProperty($V(prototype), $S("constructor"), false, false, $ CYPropertyValue($S("value"), $V(constructor)))
c5b15840
JF
192 ->* $ CYVar(builder.declarations_)
193 ->* builder.statements_
a196a97a 194 ->* CYDefineProperty($V(constructor), $S("prototype"), false, false, $ CYPropertyValue($S("value"), $V(prototype)))
c5b15840
JF
195 ->* $ CYReturn($V(constructor))
196 ), tail_->extends_ ?: $V($I("Object")));
197}
198
199CYStatement *CYClassStatement::Replace(CYContext &context) {
200 return $ CYVar($L1($L(name_, $ CYClassExpression(name_, tail_))));
201}
202
3b52fd1a
JF
203void CYClause::Replace(CYContext &context) { $T()
204 context.Replace(case_);
b0385401 205 context.ReplaceAll(code_);
3b52fd1a
JF
206 next_->Replace(context);
207}
208
209CYExpression *CYCompound::Replace(CYContext &context) {
fd5cdf97
JF
210 context.Replace(expression_);
211 context.Replace(next_);
212
213 if (CYCompound *compound = dynamic_cast<CYCompound *>(expression_)) {
214 expression_ = compound->expression_;
215 compound->expression_ = compound->next_;
216 compound->next_ = next_;
217 next_ = compound;
218 }
219
9efd8b03 220 return this;
3b52fd1a
JF
221}
222
b0385401
JF
223CYFunctionParameter *CYCompound::Parameter() const {
224 CYFunctionParameter *next(next_->Parameter());
225 if (next == NULL)
e06e5ee1 226 return NULL;
0afc9ba3
JF
227
228 CYFunctionParameter *parameter(expression_->Parameter());
229 if (parameter == NULL)
230 return NULL;
231
232 parameter->SetNext(next);
233 return parameter;
234}
235
3b52fd1a
JF
236CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
237 CYFunctionParameter *next(next_->Parameters(context));
238 if (CYFunctionParameter *parameter = Parameter(context)) {
239 parameter->SetNext(next);
240 return parameter;
241 } else
242 return next;
243}
244
245CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
246 return next_ == NULL ? statement : next_->Replace(context, statement);
247}
248
c5b15840
JF
249CYExpression *CYComputed::PropertyName(CYContext &context) {
250 return expression_;
251}
252
3b52fd1a
JF
253CYExpression *CYCondition::Replace(CYContext &context) {
254 context.Replace(test_);
255 context.Replace(true_);
256 context.Replace(false_);
029bc65b 257 return this;
3b52fd1a
JF
258}
259
ab2aa221
JF
260void CYContext::NonLocal(CYStatement *&statements) {
261 CYContext &context(*this);
262
06293152 263 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
7085e1ab
JF
264 CYIdentifier *cye($I("$cye")->Replace(context, CYIdentifierGlobal));
265 CYIdentifier *unique(nextlocal_->identifier_->Replace(context, CYIdentifierGlobal));
daf22a65
JF
266
267 CYStatement *declare(
c5b15840 268 $ CYVar($L1($L(unique, $ CYObject()))));
daf22a65
JF
269
270 cy::Syntax::Catch *rescue(
8d970b62
JF
271 $ cy::Syntax::Catch(cye, $$
272 ->* $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$
273 ->* $ CYReturn($M($V(cye), $S("$cyv"))))
274 ->* $ cy::Syntax::Throw($V(cye))));
daf22a65 275
577bfbfa 276 context.Replace(declare);
daf22a65 277 rescue->Replace(context);
ab2aa221 278
8d970b62
JF
279 statements = $$
280 ->* declare
281 ->* $ cy::Syntax::Try(statements, rescue, NULL);
ab2aa221
JF
282 }
283}
284
285CYIdentifier *CYContext::Unique() {
0cbeddf8 286 return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL));
ab2aa221
JF
287}
288
3b52fd1a 289CYStatement *CYContinue::Replace(CYContext &context) {
029bc65b
JF
290 return this;
291}
292
c8a0500b
JF
293CYStatement *CYDebugger::Replace(CYContext &context) {
294 return this;
295}
296
7085e1ab
JF
297CYTarget *CYDeclaration::Target(CYContext &context) {
298 return $V(identifier_);
299}
300
301CYAssignment *CYDeclaration::Replace(CYContext &context, CYIdentifierKind kind) {
302 identifier_ = identifier_->Replace(context, kind);
303
15b88a33
JF
304 if (initialiser_ == NULL)
305 return NULL;
127c1a9c 306
7085e1ab 307 CYAssignment *value($ CYAssign(Target(context), initialiser_));
127c1a9c
JF
308 initialiser_ = NULL;
309 return value;
15b88a33
JF
310}
311
7085e1ab
JF
312CYExpression *CYDeclarations::Replace(CYContext &context, CYIdentifierKind kind) { $T(NULL)
313 CYAssignment *assignment(declaration_->Replace(context, kind));
314 CYExpression *compound(next_->Replace(context, kind));
3b52fd1a 315
7085e1ab
JF
316 if (assignment != NULL)
317 if (compound == NULL)
318 compound = assignment;
319 else
320 compound = $ CYCompound(assignment, compound);
321 return compound;
3b52fd1a
JF
322}
323
15b88a33 324CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
c8a0500b 325 return $ CYFunctionParameter($ CYDeclaration(declaration_->identifier_), next_->Parameter(context));
15b88a33
JF
326}
327
328CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
5192746c 329 return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
15b88a33
JF
330}
331
7085e1ab 332CYTarget *CYDirectMember::Replace(CYContext &context) {
ca0f097f
JF
333 context.Replace(object_);
334 context.Replace(property_);
029bc65b 335 return this;
3b52fd1a
JF
336}
337
338CYStatement *CYDoWhile::Replace(CYContext &context) {
339 context.Replace(test_);
b0385401 340 context.ReplaceAll(code_);
029bc65b 341 return this;
3b52fd1a
JF
342}
343
fc8fc33d 344void CYElementSpread::Replace(CYContext &context) {
3b52fd1a 345 context.Replace(value_);
fc8fc33d
JF
346}
347
348void CYElementValue::Replace(CYContext &context) {
349 context.Replace(value_);
350 if (next_ != NULL)
351 next_->Replace(context);
3b52fd1a
JF
352}
353
bfd79fae 354CYForInitializer *CYEmpty::Replace(CYContext &context) {
9cd63688 355 return NULL;
029bc65b
JF
356}
357
7085e1ab 358CYTarget *CYEncodedType::Replace(CYContext &context) {
9a39f705
JF
359 return typed_->Replace(context);
360}
361
7085e1ab
JF
362CYTarget *CYEval::Replace(CYContext &context) {
363 context.scope_->Damage();
364 if (arguments_ != NULL)
365 arguments_->value_ = $C1($M($V("Cycript"), $S("compile")), arguments_->value_);
366 return $C($V("eval"), arguments_);
367}
368
12e37ba3
JF
369CYStatement *CYExpress::Return() {
370 return $ CYReturn(expression_);
371}
372
bfd79fae 373CYForInitializer *CYExpress::Replace(CYContext &context) {
3b52fd1a 374 context.Replace(expression_);
029bc65b 375 return this;
3b52fd1a
JF
376}
377
7085e1ab 378CYTarget *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
6c093cce
JF
379 return $C1(this, value);
380}
381
0afc9ba3
JF
382CYFunctionParameter *CYExpression::Parameter() const {
383 return NULL;
384}
385
c5587ed7
JF
386CYStatement *CYExternal::Replace(CYContext &context) {
387 return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word())))));
388}
389
4644480a
JF
390CYNumber *CYFalse::Number(CYContext &context) {
391 return $D(0);
392}
393
394CYString *CYFalse::String(CYContext &context) {
395 return $S("false");
396}
397
a0be43fc
JF
398CYExpression *CYFatArrow::Replace(CYContext &context) {
399 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
400 function->this_.SetNext(context.this_);
401 return function;
402}
403
3b52fd1a 404void CYFinally::Replace(CYContext &context) { $T()
7085e1ab 405 CYScope scope(true, context);
b0385401 406 context.ReplaceAll(code_);
7085e1ab 407 scope.Close(context);
3b52fd1a
JF
408}
409
410CYStatement *CYFor::Replace(CYContext &context) {
7085e1ab 411 CYScope outer(true, context);
029bc65b 412 context.Replace(initialiser_);
7085e1ab 413
3b52fd1a 414 context.Replace(test_);
7085e1ab
JF
415
416 {
417 CYScope inner(true, context);
418 context.ReplaceAll(code_);
419 inner.Close(context);
420 }
421
3b52fd1a 422 context.Replace(increment_);
7085e1ab
JF
423
424 outer.Close(context);
029bc65b 425 return this;
3b52fd1a
JF
426}
427
7085e1ab
JF
428CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
429 if (value == NULL) {
430 if (declaration_->initialiser_ == NULL)
431 return NULL;
432 value = declaration_->initialiser_;
433 }
434
435 return $ CYLet(constant_, $L1($ CYDeclaration(declaration_->identifier_, value)));
436}
437
438CYTarget *CYForLexical::Replace(CYContext &context) {
439 _assert(declaration_->Replace(context, CYIdentifierLexical) == NULL);
440 return declaration_->Target(context);
441}
ae65d594 442
7085e1ab
JF
443CYStatement *CYForIn::Replace(CYContext &context) {
444 CYScope scope(true, context);
127c1a9c
JF
445 context.Replace(initialiser_);
446 context.Replace(set_);
b0385401 447 context.ReplaceAll(code_);
7085e1ab 448 scope.Close(context);
15b88a33 449 return this;
3b52fd1a
JF
450}
451
23111dca
JF
452CYStatement *CYForInitialized::Replace(CYContext &context) {
453 CYAssignment *assignment(declaration_->Replace(context, CYIdentifierVariable));
454 return $ CYBlock($$
455 ->* (assignment == NULL ? NULL : $ CYExpress(assignment))
456 ->* $ CYForIn(declaration_->Target(context), set_, code_));
457}
458
3b52fd1a 459CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
4e3c9056 460 return $ CYFunctionParameter(declaration_);
3b52fd1a
JF
461}
462
463CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
7085e1ab 464 return $ CYForIn(declaration_->Target(context), set_, CYComprehension::Replace(context, statement));
3b52fd1a
JF
465}
466
d5618df7 467CYStatement *CYForOf::Replace(CYContext &context) {
7085e1ab 468 CYIdentifier *item(context.Unique()), *list(context.Unique());
3b52fd1a 469
ca6a1b2b 470 return $ CYBlock($$
7085e1ab
JF
471 ->* initialiser_->Initialize(context, NULL)
472 ->* $ CYLet(false, $L2($L(list, set_), $L(item)))
473 ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
474 ->* initialiser_->Initialize(context, $M($V(list), $V(item)))
ca6a1b2b
JF
475 ->* code_
476 )));
3b52fd1a
JF
477}
478
d5618df7 479CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
4e3c9056 480 return $ CYFunctionParameter(declaration_);
3b52fd1a
JF
481}
482
d5618df7 483CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
a0932196 484 CYIdentifier *cys(context.Unique());
3b52fd1a 485
a0932196
JF
486 return $ CYBlock($$
487 ->* $ CYLet(false, $L1($L(cys, set_)))
8d970b62
JF
488 ->* $ CYForIn(declaration_->Target(context), $V(cys), $ CYBlock($$
489 ->* $E($ CYAssign(declaration_->Target(context), $M($V(cys), declaration_->Target(context))))
490 ->* CYComprehension::Replace(context, statement)
3b52fd1a
JF
491 )));
492}
493
7085e1ab
JF
494CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
495 if (value == NULL) {
496 if (declaration_->initialiser_ == NULL)
497 return NULL;
498 value = declaration_->initialiser_;
499 }
500
501 return $ CYVar($L1($ CYDeclaration(declaration_->identifier_, value)));
502}
503
504CYTarget *CYForVariable::Replace(CYContext &context) {
505 _assert(declaration_->Replace(context, CYIdentifierVariable) == NULL);
506 return declaration_->Target(context);
507}
508
509// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
510#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
511//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
512
c5b15840 513void CYFunction::Replace(CYContext &context) {
a0be43fc 514 CYThisScope *_this(context.this_);
12e37ba3
JF
515 context.this_ = &this_;
516 context.this_ = CYGetLast(context.this_);
a0be43fc 517
c5b15840
JF
518 CYIdentifier *super(context.super_);
519 context.super_ = super_;
520
06293152
JF
521 CYNonLocal *nonlocal(context.nonlocal_);
522 CYNonLocal *nextlocal(context.nextlocal_);
523
ab2aa221 524 bool localize;
06293152 525 if (nonlocal_ != NULL) {
ab2aa221 526 localize = false;
06293152
JF
527 context.nonlocal_ = nonlocal_;
528 } else {
ab2aa221
JF
529 localize = true;
530 nonlocal_ = $ CYNonLocal();
06293152 531 context.nextlocal_ = nonlocal_;
ab2aa221
JF
532 }
533
50a3d79f 534 CYScope scope(!localize, context);
61fe0c53 535
c8a0500b 536 parameters_->Replace(context, code_);
c5b15840 537
b0385401 538 context.ReplaceAll(code_);
029bc65b 539
12e37ba3
JF
540 if (implicit_)
541 CYImplicitReturn(code_);
542
c5b15840
JF
543 if (CYIdentifier *identifier = this_.identifier_) {
544 context.scope_->Declare(context, identifier, CYIdentifierVariable);
545 code_ = $$
8d970b62
JF
546 ->* $E($ CYAssign($V(identifier), $ CYThis()))
547 ->* code_;
c5b15840 548 }
a0be43fc 549
ab2aa221 550 if (localize)
b0385401 551 context.NonLocal(code_);
06293152
JF
552
553 context.nextlocal_ = nextlocal;
ab2aa221
JF
554 context.nonlocal_ = nonlocal;
555
c5b15840 556 context.super_ = super;
a0be43fc
JF
557 context.this_ = _this;
558
b0385401 559 scope.Close(context, code_);
3b52fd1a
JF
560}
561
7085e1ab 562CYTarget *CYFunctionExpression::Replace(CYContext &context) {
c5b15840
JF
563 CYScope scope(false, context);
564 if (name_ != NULL)
7085e1ab
JF
565 name_ = name_->Replace(context, CYIdentifierOther);
566
c5b15840 567 CYFunction::Replace(context);
7085e1ab 568 scope.Close(context);
029bc65b
JF
569 return this;
570}
571
b0385401 572void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
7085e1ab 573 CYAssignment *assignment(initialiser_->Replace(context, CYIdentifierArgument));
c8a0500b 574
b0385401 575 next_->Replace(context, statements);
c8a0500b
JF
576
577 if (assignment != NULL)
8d970b62
JF
578 statements = $$
579 ->* $ CYIf($ CYIdentical($ CYTypeOf(initialiser_->Target(context)), $S("undefined")), $$
580 ->* $E(assignment))
581 ->* statements;
3b52fd1a
JF
582}
583
584CYStatement *CYFunctionStatement::Replace(CYContext &context) {
7085e1ab 585 name_ = name_->Replace(context, CYIdentifierOther);
c5b15840 586 CYFunction::Replace(context);
029bc65b
JF
587 return this;
588}
589
7085e1ab
JF
590CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
591 if (next_ == this)
592 return this;
593 if (next_ != NULL)
594 return next_->Replace(context, kind);
595 next_ = context.scope_->Declare(context, this, kind)->identifier_;
596 return next_;
3b52fd1a
JF
597}
598
12e37ba3
JF
599CYStatement *CYIf::Return() {
600 CYImplicitReturn(true_);
601 CYImplicitReturn(false_);
602 return this;
603}
604
3b52fd1a
JF
605CYStatement *CYIf::Replace(CYContext &context) {
606 context.Replace(test_);
b0385401
JF
607 context.ReplaceAll(true_);
608 context.ReplaceAll(false_);
029bc65b 609 return this;
3b52fd1a
JF
610}
611
612CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
613 return NULL;
614}
615
616CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
617 return $ CYIf(test_, CYComprehension::Replace(context, statement));
618}
619
7b750785 620CYStatement *CYImport::Replace(CYContext &context) {
c5b15840 621 return $ CYVar($L1($L($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
7b750785
JF
622}
623
7085e1ab 624CYTarget *CYIndirect::Replace(CYContext &context) {
3b52fd1a
JF
625 return $M(rhs_, $S("$cyi"));
626}
627
7085e1ab 628CYTarget *CYIndirectMember::Replace(CYContext &context) {
3b52fd1a
JF
629 return $M($ CYIndirect(object_), property_);
630}
631
632CYExpression *CYInfix::Replace(CYContext &context) {
633 context.Replace(lhs_);
634 context.Replace(rhs_);
029bc65b 635 return this;
3b52fd1a
JF
636}
637
638CYStatement *CYLabel::Replace(CYContext &context) {
639 context.Replace(statement_);
029bc65b 640 return this;
3b52fd1a
JF
641}
642
7085e1ab 643CYTarget *CYLambda::Replace(CYContext &context) {
b0385401 644 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
690cf1a8
JF
645}
646
bfd79fae 647CYForInitializer *CYLet::Replace(CYContext &context) {
7085e1ab 648 if (CYExpression *expression = declarations_->Replace(context, CYIdentifierLexical))
ca6a1b2b
JF
649 return $E(expression);
650 return $ CYEmpty();
550ee46a
JF
651}
652
c5b15840
JF
653CYFunctionExpression *CYMethod::Constructor() {
654 return NULL;
655}
656
657void CYMethod::Replace(CYContext &context) {
658 CYFunction::Replace(context);
659}
660
7b750785
JF
661CYString *CYModule::Replace(CYContext &context, const char *separator) const {
662 if (next_ == NULL)
663 return $ CYString(part_);
664 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
665}
666
62f398e4
JF
667CYExpression *CYMultiply::Replace(CYContext &context) {
668 CYInfix::Replace(context);
669
fd5cdf97
JF
670 if (CYNumber *lhn = lhs_->Number(context))
671 if (CYNumber *rhn = rhs_->Number(context))
62f398e4
JF
672 return $D(lhn->Value() * rhn->Value());
673
674 return this;
675}
676
2eb8215d
JF
677namespace cy {
678namespace Syntax {
679
7085e1ab 680CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
bf45251b 681 CYSetLast(arguments_) = $ CYArgument(value);
6c093cce
JF
682 return this;
683}
684
7085e1ab 685CYTarget *New::Replace(CYContext &context) {
3b52fd1a
JF
686 context.Replace(constructor_);
687 arguments_->Replace(context);
029bc65b 688 return this;
3b52fd1a
JF
689}
690
2eb8215d
JF
691} }
692
4644480a
JF
693CYNumber *CYNull::Number(CYContext &context) {
694 return $D(0);
695}
696
697CYString *CYNull::String(CYContext &context) {
698 return $S("null");
699}
700
701CYNumber *CYNumber::Number(CYContext &context) {
702 return this;
703}
704
705CYString *CYNumber::String(CYContext &context) {
706 // XXX: there is a precise algorithm for this
0cbeddf8 707 return $S($pool.sprintf(24, "%.17g", Value()));
4644480a
JF
708}
709
c5b15840
JF
710CYExpression *CYNumber::PropertyName(CYContext &context) {
711 return String(context);
712}
713
7085e1ab 714CYTarget *CYObject::Replace(CYContext &context) {
c5b15840
JF
715 CYBuilder builder;
716 if (properties_ != NULL)
717 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
718
719 if (builder) {
720 return $C1($M($ CYFunctionExpression(NULL, builder.declarations_->Parameter(context),
8d970b62
JF
721 builder.statements_
722 ->* $ CYReturn($ CYThis())
c5b15840
JF
723 ), $S("call")), this, builder.declarations_->Argument(context));
724 }
725
726 CYForEach (property, properties_)
727 property->Replace(context);
029bc65b 728 return this;
3b52fd1a
JF
729}
730
7085e1ab
JF
731CYTarget *CYParenthetical::Replace(CYContext &context) {
732 // XXX: return expression_;
733 context.Replace(expression_);
734 return this;
b0385401
JF
735}
736
3b52fd1a
JF
737CYExpression *CYPostfix::Replace(CYContext &context) {
738 context.Replace(lhs_);
029bc65b 739 return this;
3b52fd1a
JF
740}
741
742CYExpression *CYPrefix::Replace(CYContext &context) {
743 context.Replace(rhs_);
029bc65b 744 return this;
3b52fd1a
JF
745}
746
c5b15840 747CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
7b87d205 748 update |= Update();
c5b15840 749 if (update)
a196a97a 750 Replace(context, builder, self, false);
c5b15840
JF
751 if (next_ != NULL)
752 next_ = next_->ReplaceAll(context, builder, self, update);
753 return update ? next_ : this;
754}
755
a196a97a 756void CYProperty::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect) {
c5b15840 757 CYExpression *name(name_->PropertyName(context));
a196a97a 758 if (name_->Computed()) {
c5b15840 759 CYIdentifier *unique(context.Unique());
8d970b62
JF
760 builder.declarations_
761 ->* $L1($L(unique, name));
c5b15840
JF
762 name = $V(unique);
763 }
764
a196a97a 765 Replace(context, builder, self, name, protect);
c5b15840
JF
766}
767
7b87d205
JF
768bool CYProperty::Update() const {
769 return name_->Computed();
770}
771
a196a97a 772void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840
JF
773 CYIdentifier *unique(context.Unique());
774 builder.declarations_
775 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
776 builder.statements_
a196a97a 777 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
c5b15840
JF
778}
779
780CYFunctionExpression *CYPropertyMethod::Constructor() {
781 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
782}
783
a196a97a 784void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840
JF
785 CYIdentifier *unique(context.Unique());
786 builder.declarations_
787 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
788 builder.statements_
a196a97a
JF
789 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
790 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
c5b15840
JF
791}
792
7b87d205
JF
793bool CYPropertyMethod::Update() const {
794 return true;
795}
796
a196a97a 797void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840
JF
798 CYIdentifier *unique(context.Unique());
799 builder.declarations_
800 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
801 builder.statements_
a196a97a 802 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
c5b15840
JF
803}
804
a196a97a
JF
805void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
806 _assert(!protect);
c5b15840
JF
807 CYIdentifier *unique(context.Unique());
808 builder.declarations_
809 ->* $L1($L(unique, value_));
810 builder.statements_
811 ->* $E($ CYAssign($M(self, name), $V(unique)));
812}
813
814void CYPropertyValue::Replace(CYContext &context) {
815 context.Replace(value_);
816}
817
a7d8b413 818void CYScript::Replace(CYContext &context) {
7085e1ab
JF
819 CYScope scope(false, context);
820 context.scope_->Damage();
ab2aa221 821
06293152 822 context.nextlocal_ = $ CYNonLocal();
b0385401
JF
823 context.ReplaceAll(code_);
824 context.NonLocal(code_);
ab2aa221 825
b0385401 826 scope.Close(context, code_);
14ec9e00 827
7085e1ab 828 unsigned offset(0);
de9fc71b 829
7085e1ab 830 for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
14ec9e00 831 const char *name;
14ec9e00 832 if (context.options_.verbose_)
7085e1ab 833 name = $pool.strcat("$", $pool.itoa(offset++), NULL);
14ec9e00
JF
834 else {
835 char id[8];
836 id[7] = '\0';
837
838 id:
7085e1ab 839 unsigned position(7), local(offset++ + 1);
14ec9e00
JF
840
841 do {
45d0c928
JF
842 unsigned index(local % (sizeof(MappingSet) - 1));
843 local /= sizeof(MappingSet) - 1;
844 id[--position] = MappingSet[index];
14ec9e00
JF
845 } while (local != 0);
846
7085e1ab 847 if (scope.Lookup(context, id + position) != NULL)
14ec9e00 848 goto id;
7085e1ab 849 // XXX: at some point, this could become a keyword
14ec9e00 850
b799113b 851 name = $pool.strmemdup(id + position, 7 - position);
14ec9e00
JF
852 }
853
7085e1ab
JF
854 CYIdentifier *identifier(*i);
855 _assert(identifier->next_ == identifier);
856 identifier->next_ = $I(name);
14ec9e00 857 }
3b52fd1a
JF
858}
859
3b52fd1a 860CYStatement *CYReturn::Replace(CYContext &context) {
ab2aa221 861 if (context.nonlocal_ != NULL) {
c5b15840 862 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
ab2aa221 863 return $ cy::Syntax::Throw($ CYObject(
c5b15840 864 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
ab2aa221
JF
865 ));
866 }
867
3b52fd1a 868 context.Replace(value_);
029bc65b
JF
869 return this;
870}
871
7085e1ab 872CYTarget *CYRubyBlock::Replace(CYContext &context) {
6c093cce
JF
873 return call_->AddArgument(context, proc_->Replace(context));
874}
875
7085e1ab 876CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
1abe53bf
JF
877 return Replace(context)->AddArgument(context, value);
878}
879
7085e1ab 880CYTarget *CYRubyProc::Replace(CYContext &context) {
12e37ba3
JF
881 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
882 function = CYNonLocalize(context, function);
883 function->implicit_ = true;
884 return function;
6c093cce
JF
885}
886
50a3d79f 887CYScope::CYScope(bool transparent, CYContext &context) :
61fe0c53 888 transparent_(transparent),
7085e1ab
JF
889 parent_(context.scope_),
890 damaged_(false),
891 shadow_(NULL),
892 internal_(NULL)
daf22a65 893{
7085e1ab 894 _assert(!transparent_ || parent_ != NULL);
50a3d79f 895 context.scope_ = this;
daf22a65
JF
896}
897
7085e1ab
JF
898void CYScope::Damage() {
899 damaged_ = true;
900 if (parent_ != NULL)
901 parent_->Damage();
a86e34d0
JF
902}
903
7085e1ab
JF
904CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
905 CYForEach (i, internal_)
906 if (strcmp(i->identifier_->Word(), word) == 0)
907 return i;
908 return NULL;
a86e34d0
JF
909}
910
7085e1ab
JF
911CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
912 return Lookup(context, identifier->Word());
029bc65b
JF
913}
914
7085e1ab
JF
915CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
916 _assert(identifier->next_ == NULL || identifier->next_ == identifier);
10711823 917
7085e1ab
JF
918 CYIdentifierFlags *existing(Lookup(context, identifier));
919 if (existing == NULL)
920 internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
921 ++internal_->count_;
922 if (existing == NULL)
923 return internal_;
10711823 924
7085e1ab
JF
925 switch (kind) {
926 case CYIdentifierArgument:
927 case CYIdentifierCatch:
928 case CYIdentifierMagic:
929 _assert(false);
930 default:
931 break;
932 }
933
934 if (existing->kind_ == CYIdentifierGlobal)
935 existing->kind_ = kind;
936 else if (kind == CYIdentifierGlobal)
937 ;
938 else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
939 _assert(false); // XXX: throw new SyntaxError()
10711823 940
7085e1ab
JF
941 return existing;
942}
943
944void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
945 _assert(flags->identifier_->next_ == flags->identifier_);
946 CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
947 flags->identifier_->next_ = existing->identifier_;
948
949 existing->count_ += flags->count_;
950 if (existing->offset_ < flags->offset_)
951 existing->offset_ = flags->offset_;
10711823
JF
952}
953
50a3d79f 954void CYScope::Close(CYContext &context, CYStatement *&statements) {
7085e1ab
JF
955 Close(context);
956
957 CYList<CYDeclarations> declarations;
958
959 CYForEach (i, internal_)
960 if (i->kind_ == CYIdentifierVariable)
8d970b62
JF
961 declarations
962 ->* $ CYDeclarations($ CYDeclaration(i->identifier_));
7085e1ab
JF
963
964 if (declarations) {
965 CYVar *var($ CYVar(declarations));
966 var->SetNext(statements);
967 statements = var;
968 }
969}
970
971void CYScope::Close(CYContext &context) {
50a3d79f
JF
972 context.scope_ = parent_;
973
7085e1ab
JF
974 CYForEach (i, internal_) {
975 _assert(i->identifier_->next_ == i->identifier_);
976 switch (i->kind_) {
977 case CYIdentifierArgument: {
978 _assert(!transparent_);
979 } break;
980
981 case CYIdentifierLexical: {
982 if (!damaged_) {
983 CYIdentifier *replace(context.Unique());
984 replace->next_ = replace;
985 i->identifier_->next_ = replace;
986 i->identifier_ = replace;
987 }
988
989 if (!transparent_)
990 i->kind_ = CYIdentifierVariable;
991 else
992 parent_->Declare(context, i->identifier_, CYIdentifierVariable);
993 } break;
2c81c6df 994
7085e1ab
JF
995 case CYIdentifierVariable: {
996 if (transparent_) {
997 parent_->Declare(context, i->identifier_, i->kind_);
998 i->kind_ = CYIdentifierGlobal;
999 }
1000 } break;
1001 default:; } }
14ec9e00 1002
7085e1ab
JF
1003 if (damaged_)
1004 return;
6a981250 1005
7085e1ab
JF
1006 typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
1007 CYIdentifierOffsetMap offsets;
10711823 1008
7085e1ab
JF
1009 CYForEach (i, internal_) {
1010 _assert(i->identifier_->next_ == i->identifier_);
1011 switch (i->kind_) {
1012 case CYIdentifierArgument:
1013 case CYIdentifierVariable:
1014 offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
1015 break;
1016 default:; } }
10711823 1017
7085e1ab 1018 unsigned offset(0);
029bc65b 1019
7085e1ab
JF
1020 for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
1021 if (offset < i->first)
1022 offset = i->first;
1023 CYIdentifier *identifier(i->second);
10711823 1024
7085e1ab
JF
1025 if (offset >= context.replace_.size())
1026 context.replace_.resize(offset + 1, NULL);
1027 CYIdentifier *&replace(context.replace_[offset++]);
6a981250 1028
7085e1ab
JF
1029 if (replace == NULL)
1030 replace = identifier;
1031 else {
1032 _assert(replace->next_ == replace);
1033 identifier->next_ = replace;
1034 }
029bc65b
JF
1035 }
1036
7085e1ab
JF
1037 if (parent_ == NULL)
1038 return;
1039
1040 CYForEach (i, internal_) {
1041 switch (i->kind_) {
1042 case CYIdentifierGlobal: {
1043 if (i->offset_ < offset)
1044 i->offset_ = offset;
1045 parent_->Merge(context, i);
1046 } break;
1047 default:; } }
029bc65b
JF
1048}
1049
fc8fc33d
JF
1050CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
1051 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
b900e1a4
JF
1052}
1053
12e37ba3
JF
1054CYStatement *CYStatement::Return() {
1055 return this;
1056}
1057
4644480a
JF
1058CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1059 size_t size(size_ + rhs->size_);
2eb8215d 1060 char *value($ char[size + 1]);
4644480a
JF
1061 memcpy(value, value_, size_);
1062 memcpy(value + size_, rhs->value_, rhs->size_);
1063 value[size] = '\0';
a14eb702 1064 return $S(value, size);
4644480a
JF
1065}
1066
1067CYNumber *CYString::Number(CYContext &context) {
1068 // XXX: there is a precise algorithm for this
1069 return NULL;
1070}
1071
c5b15840
JF
1072CYExpression *CYString::PropertyName(CYContext &context) {
1073 return this;
1074}
1075
4644480a
JF
1076CYString *CYString::String(CYContext &context) {
1077 return this;
1078}
1079
7085e1ab 1080CYTarget *CYSuperAccess::Replace(CYContext &context) {
c5b15840
JF
1081 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1082}
1083
7085e1ab 1084CYTarget *CYSuperCall::Replace(CYContext &context) {
c5b15840
JF
1085 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1086}
1087
3b52fd1a
JF
1088CYStatement *CYSwitch::Replace(CYContext &context) {
1089 context.Replace(value_);
1090 clauses_->Replace(context);
029bc65b 1091 return this;
3b52fd1a
JF
1092}
1093
7085e1ab
JF
1094CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
1095 if (value == NULL)
1096 return NULL;
1097 return $E($ CYAssign(this, value));
1098}
1099
1100CYTarget *CYTemplate::Replace(CYContext &context) {
fc8fc33d 1101 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
b900e1a4
JF
1102}
1103
7085e1ab 1104CYTarget *CYThis::Replace(CYContext &context) {
a0be43fc
JF
1105 if (context.this_ != NULL)
1106 return $V(context.this_->Identifier(context));
029bc65b 1107 return this;
3b52fd1a
JF
1108}
1109
37954781
JF
1110namespace cy {
1111namespace Syntax {
1112
1113CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a 1114 context.Replace(value_);
029bc65b 1115 return this;
3b52fd1a
JF
1116}
1117
37954781
JF
1118} }
1119
7085e1ab 1120CYTarget *CYTrivial::Replace(CYContext &context) {
029bc65b 1121 return this;
3b52fd1a
JF
1122}
1123
4644480a
JF
1124CYNumber *CYTrue::Number(CYContext &context) {
1125 return $D(1);
1126}
1127
1128CYString *CYTrue::String(CYContext &context) {
1129 return $S("true");
1130}
1131
37954781
JF
1132namespace cy {
1133namespace Syntax {
1134
1135CYStatement *Try::Replace(CYContext &context) {
7085e1ab 1136 CYScope scope(true, context);
b0385401 1137 context.ReplaceAll(code_);
7085e1ab
JF
1138 scope.Close(context);
1139
3b52fd1a
JF
1140 catch_->Replace(context);
1141 finally_->Replace(context);
029bc65b 1142 return this;
3b52fd1a
JF
1143}
1144
37954781
JF
1145} }
1146
7085e1ab 1147CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1148 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
690cf1a8
JF
1149}
1150
7085e1ab 1151CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
3fe16be7
JF
1152 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1153}
1154
7085e1ab 1155CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1156 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
690cf1a8
JF
1157}
1158
60097023 1159CYStatement *CYTypeDefinition::Replace(CYContext &context) {
84759b5b 1160 return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
60097023
JF
1161}
1162
7085e1ab 1163CYTarget *CYTypeError::Replace(CYContext &context) {
03db6a67
JF
1164 _assert(false);
1165 return NULL;
1166}
1167
7085e1ab 1168CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
9a39f705
JF
1169 return Replace_(context, type);
1170}
1171
7085e1ab 1172CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
9a39f705
JF
1173 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
1174}
1175
7085e1ab 1176CYTarget *CYTypeLong::Replace(CYContext &context) {
3fe283c5
JF
1177 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
1178}
1179
7085e1ab 1180CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1181 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
663c538f
JF
1182}
1183
7085e1ab 1184CYTarget *CYTypeShort::Replace(CYContext &context) {
3fe283c5
JF
1185 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
1186}
1187
7085e1ab 1188CYTarget *CYTypeSigned::Replace(CYContext &context) {
3fe283c5
JF
1189 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
1190}
1191
7085e1ab 1192CYTarget *CYTypeUnsigned::Replace(CYContext &context) {
3fe283c5
JF
1193 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
1194}
1195
7085e1ab 1196CYTarget *CYTypeVariable::Replace(CYContext &context) {
3fe283c5
JF
1197 return $V(name_);
1198}
1199
7085e1ab 1200CYTarget *CYTypeVoid::Replace(CYContext &context) {
3fe283c5
JF
1201 return $N1($V("Type"), $ CYString("v"));
1202}
1203
7085e1ab 1204CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1205 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
690cf1a8
JF
1206}
1207
7085e1ab 1208CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
3fe283c5 1209 return modifier_->Replace(context, specifier_->Replace(context));
690cf1a8
JF
1210}
1211
00b4cb83
JF
1212CYTypeFunctionWith *CYTypedIdentifier::Function() {
1213 CYTypeModifier **modifier(&modifier_);
1214 if (*modifier == NULL)
1215 return NULL;
1216 while ((*modifier)->next_ != NULL)
1217 modifier = &(*modifier)->next_;
1218 CYTypeFunctionWith *function((*modifier)->Function());
1219 if (function == NULL)
1220 return NULL;
1221 *modifier = NULL;
1222 return function;
1223}
1224
663c538f 1225CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
9a39f705 1226 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
663c538f
JF
1227}
1228
690cf1a8
JF
1229CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1230 return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
1231}
1232
1233CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
a2f3ecab 1234 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
690cf1a8
JF
1235}
1236
bfd79fae 1237CYForInitializer *CYVar::Replace(CYContext &context) {
7085e1ab 1238 if (CYExpression *expression = declarations_->Replace(context, CYIdentifierVariable))
b0385401 1239 return $E(expression);
fd5cdf97 1240 return $ CYEmpty();
3b52fd1a
JF
1241}
1242
7085e1ab
JF
1243CYTarget *CYVariable::Replace(CYContext &context) {
1244 name_ = name_->Replace(context, CYIdentifierGlobal);
029bc65b 1245 return this;
3b52fd1a
JF
1246}
1247
0afc9ba3
JF
1248CYFunctionParameter *CYVariable::Parameter() const {
1249 return $ CYFunctionParameter($ CYDeclaration(name_));
1250}
1251
3b52fd1a
JF
1252CYStatement *CYWhile::Replace(CYContext &context) {
1253 context.Replace(test_);
b0385401 1254 context.ReplaceAll(code_);
029bc65b 1255 return this;
3b52fd1a
JF
1256}
1257
1258CYStatement *CYWith::Replace(CYContext &context) {
1259 context.Replace(scope_);
b0385401 1260 context.ReplaceAll(code_);
029bc65b 1261 return this;
3b52fd1a
JF
1262}
1263
c5b15840
JF
1264CYExpression *CYWord::PropertyName(CYContext &context) {
1265 return $S(this);
3b52fd1a 1266}