]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Hide complicated hold manipulation behind CYHLD().
[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) {
5a6c975a
JF
104 CYForEach (element, elements_)
105 element->Replace(context);
029bc65b 106 return this;
3b52fd1a
JF
107}
108
7085e1ab 109CYTarget *CYArrayComprehension::Replace(CYContext &context) {
a0932196 110 CYIdentifier *cyv(context.Unique());
3b52fd1a 111
09fc3efb 112 return $C0($F(NULL, $P1($B(cyv), comprehensions_->Parameters(context)), $$
a0932196
JF
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
09fc3efb
JF
186 return $C1($ CYFunctionExpression(NULL, $P($B(super)), $$
187 ->* $ CYVar($B1($B(constructor, tail_->constructor_)))
188 ->* $ CYVar($B1($B(prototype, $ CYFunctionExpression(NULL, NULL, NULL))))
c5b15840
JF
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)))
09fc3efb 192 ->* $ CYVar(builder.bindings_)
c5b15840 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) {
09fc3efb 200 return $ CYVar($B1($B(name_, $ CYClassExpression(name_, tail_))));
c5b15840
JF
201}
202
3b52fd1a 203void CYClause::Replace(CYContext &context) { $T()
09fc3efb 204 context.Replace(value_);
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(
09fc3efb 268 $ CYVar($B1($B(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
09fc3efb 297CYTarget *CYBinding::Target(CYContext &context) {
7085e1ab
JF
298 return $V(identifier_);
299}
300
09fc3efb 301CYAssignment *CYBinding::Replace(CYContext &context, CYIdentifierKind kind) {
7085e1ab
JF
302 identifier_ = identifier_->Replace(context, kind);
303
09fc3efb 304 if (initializer_ == NULL)
15b88a33 305 return NULL;
127c1a9c 306
09fc3efb
JF
307 CYAssignment *value($ CYAssign(Target(context), initializer_));
308 initializer_ = NULL;
127c1a9c 309 return value;
15b88a33
JF
310}
311
09fc3efb
JF
312CYExpression *CYBindings::Replace(CYContext &context, CYIdentifierKind kind) { $T(NULL)
313 CYAssignment *assignment(binding_->Replace(context, kind));
7085e1ab 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
09fc3efb
JF
324CYFunctionParameter *CYBindings::Parameter(CYContext &context) { $T(NULL)
325 return $ CYFunctionParameter($ CYBinding(binding_->identifier_), next_->Parameter(context));
15b88a33
JF
326}
327
09fc3efb
JF
328CYArgument *CYBindings::Argument(CYContext &context) { $T(NULL)
329 return $ CYArgument(binding_->initializer_, 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_);
3b52fd1a
JF
350}
351
bfd79fae 352CYForInitializer *CYEmpty::Replace(CYContext &context) {
9cd63688 353 return NULL;
029bc65b
JF
354}
355
7085e1ab 356CYTarget *CYEncodedType::Replace(CYContext &context) {
9a39f705
JF
357 return typed_->Replace(context);
358}
359
7085e1ab
JF
360CYTarget *CYEval::Replace(CYContext &context) {
361 context.scope_->Damage();
362 if (arguments_ != NULL)
363 arguments_->value_ = $C1($M($V("Cycript"), $S("compile")), arguments_->value_);
364 return $C($V("eval"), arguments_);
365}
366
12e37ba3
JF
367CYStatement *CYExpress::Return() {
368 return $ CYReturn(expression_);
369}
370
bfd79fae 371CYForInitializer *CYExpress::Replace(CYContext &context) {
3b52fd1a 372 context.Replace(expression_);
029bc65b 373 return this;
3b52fd1a
JF
374}
375
7085e1ab 376CYTarget *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
6c093cce
JF
377 return $C1(this, value);
378}
379
0afc9ba3
JF
380CYFunctionParameter *CYExpression::Parameter() const {
381 return NULL;
382}
383
c5587ed7
JF
384CYStatement *CYExternal::Replace(CYContext &context) {
385 return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word())))));
386}
387
4644480a
JF
388CYNumber *CYFalse::Number(CYContext &context) {
389 return $D(0);
390}
391
392CYString *CYFalse::String(CYContext &context) {
393 return $S("false");
394}
395
a0be43fc
JF
396CYExpression *CYFatArrow::Replace(CYContext &context) {
397 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
398 function->this_.SetNext(context.this_);
399 return function;
400}
401
3b52fd1a 402void CYFinally::Replace(CYContext &context) { $T()
7085e1ab 403 CYScope scope(true, context);
b0385401 404 context.ReplaceAll(code_);
7085e1ab 405 scope.Close(context);
3b52fd1a
JF
406}
407
408CYStatement *CYFor::Replace(CYContext &context) {
7085e1ab 409 CYScope outer(true, context);
09fc3efb 410 context.Replace(initializer_);
7085e1ab 411
3b52fd1a 412 context.Replace(test_);
7085e1ab
JF
413
414 {
415 CYScope inner(true, context);
416 context.ReplaceAll(code_);
417 inner.Close(context);
418 }
419
3b52fd1a 420 context.Replace(increment_);
7085e1ab
JF
421
422 outer.Close(context);
029bc65b 423 return this;
3b52fd1a
JF
424}
425
7085e1ab
JF
426CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
427 if (value == NULL) {
09fc3efb 428 if (binding_->initializer_ == NULL)
7085e1ab 429 return NULL;
09fc3efb 430 value = binding_->initializer_;
7085e1ab
JF
431 }
432
09fc3efb 433 return $ CYLexical(constant_, $B1($ CYBinding(binding_->identifier_, value)));
7085e1ab
JF
434}
435
436CYTarget *CYForLexical::Replace(CYContext &context) {
09fc3efb
JF
437 _assert(binding_->Replace(context, CYIdentifierLexical) == NULL);
438 return binding_->Target(context);
7085e1ab 439}
ae65d594 440
7085e1ab
JF
441CYStatement *CYForIn::Replace(CYContext &context) {
442 CYScope scope(true, context);
09fc3efb
JF
443 context.Replace(initializer_);
444 context.Replace(iterable_);
b0385401 445 context.ReplaceAll(code_);
7085e1ab 446 scope.Close(context);
15b88a33 447 return this;
3b52fd1a
JF
448}
449
23111dca 450CYStatement *CYForInitialized::Replace(CYContext &context) {
09fc3efb 451 CYAssignment *assignment(binding_->Replace(context, CYIdentifierVariable));
23111dca
JF
452 return $ CYBlock($$
453 ->* (assignment == NULL ? NULL : $ CYExpress(assignment))
09fc3efb 454 ->* $ CYForIn(binding_->Target(context), iterable_, code_));
23111dca
JF
455}
456
3b52fd1a 457CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
09fc3efb 458 return $ CYFunctionParameter(binding_);
3b52fd1a
JF
459}
460
461CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
09fc3efb 462 return $ CYForIn(binding_->Target(context), iterable_, CYComprehension::Replace(context, statement));
3b52fd1a
JF
463}
464
d5618df7 465CYStatement *CYForOf::Replace(CYContext &context) {
7085e1ab 466 CYIdentifier *item(context.Unique()), *list(context.Unique());
3b52fd1a 467
ca6a1b2b 468 return $ CYBlock($$
09fc3efb
JF
469 ->* initializer_->Initialize(context, NULL)
470 ->* $ CYLexical(false, $B2($B(list, iterable_), $B(item)))
7085e1ab 471 ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
09fc3efb 472 ->* initializer_->Initialize(context, $M($V(list), $V(item)))
ca6a1b2b
JF
473 ->* code_
474 )));
3b52fd1a
JF
475}
476
d5618df7 477CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
09fc3efb 478 return $ CYFunctionParameter(binding_);
3b52fd1a
JF
479}
480
d5618df7 481CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
a0932196 482 CYIdentifier *cys(context.Unique());
3b52fd1a 483
a0932196 484 return $ CYBlock($$
09fc3efb
JF
485 ->* $ CYLexical(false, $B1($B(cys, iterable_)))
486 ->* $ CYForIn(binding_->Target(context), $V(cys), $ CYBlock($$
487 ->* $E($ CYAssign(binding_->Target(context), $M($V(cys), binding_->Target(context))))
8d970b62 488 ->* CYComprehension::Replace(context, statement)
3b52fd1a
JF
489 )));
490}
491
7085e1ab
JF
492CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
493 if (value == NULL) {
09fc3efb 494 if (binding_->initializer_ == NULL)
7085e1ab 495 return NULL;
09fc3efb 496 value = binding_->initializer_;
7085e1ab
JF
497 }
498
09fc3efb 499 return $ CYVar($B1($ CYBinding(binding_->identifier_, value)));
7085e1ab
JF
500}
501
502CYTarget *CYForVariable::Replace(CYContext &context) {
09fc3efb
JF
503 _assert(binding_->Replace(context, CYIdentifierVariable) == NULL);
504 return binding_->Target(context);
7085e1ab
JF
505}
506
507// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
508#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
509//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
510
c5b15840 511void CYFunction::Replace(CYContext &context) {
a0be43fc 512 CYThisScope *_this(context.this_);
12e37ba3
JF
513 context.this_ = &this_;
514 context.this_ = CYGetLast(context.this_);
a0be43fc 515
c5b15840
JF
516 CYIdentifier *super(context.super_);
517 context.super_ = super_;
518
06293152
JF
519 CYNonLocal *nonlocal(context.nonlocal_);
520 CYNonLocal *nextlocal(context.nextlocal_);
521
ab2aa221 522 bool localize;
06293152 523 if (nonlocal_ != NULL) {
ab2aa221 524 localize = false;
06293152
JF
525 context.nonlocal_ = nonlocal_;
526 } else {
ab2aa221
JF
527 localize = true;
528 nonlocal_ = $ CYNonLocal();
06293152 529 context.nextlocal_ = nonlocal_;
ab2aa221
JF
530 }
531
50a3d79f 532 CYScope scope(!localize, context);
61fe0c53 533
b026d136
JF
534 $I("arguments")->Replace(context, CYIdentifierMagic);
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()
09fc3efb 573 CYAssignment *assignment(binding_->Replace(context, CYIdentifierArgument));
c8a0500b 574
b0385401 575 next_->Replace(context, statements);
c8a0500b
JF
576
577 if (assignment != NULL)
8d970b62 578 statements = $$
09fc3efb 579 ->* $ CYIf($ CYIdentical($ CYTypeOf(binding_->Target(context)), $S("undefined")), $$
8d970b62
JF
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) {
09fc3efb 621 return $ CYVar($B1($B($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
7b750785
JF
622}
623
90dd6ff1
JF
624CYStatement *CYImportDeclaration::Replace(CYContext &context) {
625 CYIdentifier *module(context.Unique());
626
627 CYList<CYStatement> statements;
628 CYForEach (specifier, specifiers_)
629 statements->*specifier->Replace(context, module);
630
631 return $ CYBlock($$
632 ->* $ CYLexical(false, $B1($B(module, $C1($V("require"), module_))))
633 ->* statements);
634}
635
636CYStatement *CYImportSpecifier::Replace(CYContext &context, CYIdentifier *module) {
637 binding_ = binding_->Replace(context, CYIdentifierLexical);
638
639 CYExpression *import($V(module));
640 if (name_ != NULL)
641 import = $M(import, $S(name_));
642 return $E($ CYAssign($V(binding_), import));
643}
644
7085e1ab 645CYTarget *CYIndirect::Replace(CYContext &context) {
3b52fd1a
JF
646 return $M(rhs_, $S("$cyi"));
647}
648
7085e1ab 649CYTarget *CYIndirectMember::Replace(CYContext &context) {
3b52fd1a
JF
650 return $M($ CYIndirect(object_), property_);
651}
652
653CYExpression *CYInfix::Replace(CYContext &context) {
654 context.Replace(lhs_);
655 context.Replace(rhs_);
029bc65b 656 return this;
3b52fd1a
JF
657}
658
659CYStatement *CYLabel::Replace(CYContext &context) {
660 context.Replace(statement_);
029bc65b 661 return this;
3b52fd1a
JF
662}
663
7085e1ab 664CYTarget *CYLambda::Replace(CYContext &context) {
b0385401 665 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
690cf1a8
JF
666}
667
09fc3efb
JF
668CYForInitializer *CYLexical::Replace(CYContext &context) {
669 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierLexical))
ca6a1b2b
JF
670 return $E(expression);
671 return $ CYEmpty();
550ee46a
JF
672}
673
c5b15840
JF
674CYFunctionExpression *CYMethod::Constructor() {
675 return NULL;
676}
677
678void CYMethod::Replace(CYContext &context) {
679 CYFunction::Replace(context);
680}
681
7b750785
JF
682CYString *CYModule::Replace(CYContext &context, const char *separator) const {
683 if (next_ == NULL)
684 return $ CYString(part_);
685 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
686}
687
62f398e4
JF
688CYExpression *CYMultiply::Replace(CYContext &context) {
689 CYInfix::Replace(context);
690
fd5cdf97
JF
691 if (CYNumber *lhn = lhs_->Number(context))
692 if (CYNumber *rhn = rhs_->Number(context))
62f398e4
JF
693 return $D(lhn->Value() * rhn->Value());
694
695 return this;
696}
697
2eb8215d
JF
698namespace cy {
699namespace Syntax {
700
7085e1ab 701CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
bf45251b 702 CYSetLast(arguments_) = $ CYArgument(value);
6c093cce
JF
703 return this;
704}
705
7085e1ab 706CYTarget *New::Replace(CYContext &context) {
3b52fd1a
JF
707 context.Replace(constructor_);
708 arguments_->Replace(context);
029bc65b 709 return this;
3b52fd1a
JF
710}
711
2eb8215d
JF
712} }
713
4644480a
JF
714CYNumber *CYNull::Number(CYContext &context) {
715 return $D(0);
716}
717
718CYString *CYNull::String(CYContext &context) {
719 return $S("null");
720}
721
722CYNumber *CYNumber::Number(CYContext &context) {
723 return this;
724}
725
726CYString *CYNumber::String(CYContext &context) {
727 // XXX: there is a precise algorithm for this
0cbeddf8 728 return $S($pool.sprintf(24, "%.17g", Value()));
4644480a
JF
729}
730
c5b15840
JF
731CYExpression *CYNumber::PropertyName(CYContext &context) {
732 return String(context);
733}
734
7085e1ab 735CYTarget *CYObject::Replace(CYContext &context) {
c5b15840
JF
736 CYBuilder builder;
737 if (properties_ != NULL)
738 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
739
740 if (builder) {
09fc3efb 741 return $C1($M($ CYFunctionExpression(NULL, builder.bindings_->Parameter(context),
8d970b62
JF
742 builder.statements_
743 ->* $ CYReturn($ CYThis())
09fc3efb 744 ), $S("call")), this, builder.bindings_->Argument(context));
c5b15840
JF
745 }
746
747 CYForEach (property, properties_)
748 property->Replace(context);
029bc65b 749 return this;
3b52fd1a
JF
750}
751
7085e1ab
JF
752CYTarget *CYParenthetical::Replace(CYContext &context) {
753 // XXX: return expression_;
754 context.Replace(expression_);
755 return this;
b0385401
JF
756}
757
3b52fd1a
JF
758CYExpression *CYPostfix::Replace(CYContext &context) {
759 context.Replace(lhs_);
029bc65b 760 return this;
3b52fd1a
JF
761}
762
763CYExpression *CYPrefix::Replace(CYContext &context) {
764 context.Replace(rhs_);
029bc65b 765 return this;
3b52fd1a
JF
766}
767
c5b15840 768CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
7b87d205 769 update |= Update();
c5b15840 770 if (update)
a196a97a 771 Replace(context, builder, self, false);
c5b15840
JF
772 if (next_ != NULL)
773 next_ = next_->ReplaceAll(context, builder, self, update);
774 return update ? next_ : this;
775}
776
a196a97a 777void CYProperty::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect) {
c5b15840 778 CYExpression *name(name_->PropertyName(context));
a196a97a 779 if (name_->Computed()) {
c5b15840 780 CYIdentifier *unique(context.Unique());
09fc3efb
JF
781 builder.bindings_
782 ->* $B1($B(unique, name));
c5b15840
JF
783 name = $V(unique);
784 }
785
a196a97a 786 Replace(context, builder, self, name, protect);
c5b15840
JF
787}
788
7b87d205
JF
789bool CYProperty::Update() const {
790 return name_->Computed();
791}
792
a196a97a 793void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840 794 CYIdentifier *unique(context.Unique());
09fc3efb
JF
795 builder.bindings_
796 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
c5b15840 797 builder.statements_
a196a97a 798 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
c5b15840
JF
799}
800
801CYFunctionExpression *CYPropertyMethod::Constructor() {
802 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
803}
804
a196a97a 805void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840 806 CYIdentifier *unique(context.Unique());
09fc3efb
JF
807 builder.bindings_
808 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
c5b15840 809 builder.statements_
a196a97a
JF
810 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
811 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
c5b15840
JF
812}
813
7b87d205
JF
814bool CYPropertyMethod::Update() const {
815 return true;
816}
817
a196a97a 818void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840 819 CYIdentifier *unique(context.Unique());
09fc3efb
JF
820 builder.bindings_
821 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
c5b15840 822 builder.statements_
a196a97a 823 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
c5b15840
JF
824}
825
a196a97a
JF
826void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
827 _assert(!protect);
c5b15840 828 CYIdentifier *unique(context.Unique());
09fc3efb
JF
829 builder.bindings_
830 ->* $B1($B(unique, value_));
c5b15840
JF
831 builder.statements_
832 ->* $E($ CYAssign($M(self, name), $V(unique)));
833}
834
835void CYPropertyValue::Replace(CYContext &context) {
836 context.Replace(value_);
837}
838
a7d8b413 839void CYScript::Replace(CYContext &context) {
7085e1ab
JF
840 CYScope scope(false, context);
841 context.scope_->Damage();
ab2aa221 842
06293152 843 context.nextlocal_ = $ CYNonLocal();
b0385401
JF
844 context.ReplaceAll(code_);
845 context.NonLocal(code_);
ab2aa221 846
b0385401 847 scope.Close(context, code_);
14ec9e00 848
7085e1ab 849 unsigned offset(0);
de9fc71b 850
7085e1ab 851 for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
14ec9e00 852 const char *name;
14ec9e00 853 if (context.options_.verbose_)
7085e1ab 854 name = $pool.strcat("$", $pool.itoa(offset++), NULL);
14ec9e00
JF
855 else {
856 char id[8];
857 id[7] = '\0';
858
859 id:
7085e1ab 860 unsigned position(7), local(offset++ + 1);
14ec9e00
JF
861
862 do {
45d0c928
JF
863 unsigned index(local % (sizeof(MappingSet) - 1));
864 local /= sizeof(MappingSet) - 1;
865 id[--position] = MappingSet[index];
14ec9e00
JF
866 } while (local != 0);
867
7085e1ab 868 if (scope.Lookup(context, id + position) != NULL)
14ec9e00 869 goto id;
7085e1ab 870 // XXX: at some point, this could become a keyword
14ec9e00 871
b799113b 872 name = $pool.strmemdup(id + position, 7 - position);
14ec9e00
JF
873 }
874
7085e1ab
JF
875 CYIdentifier *identifier(*i);
876 _assert(identifier->next_ == identifier);
877 identifier->next_ = $I(name);
14ec9e00 878 }
3b52fd1a
JF
879}
880
2fad14e5
JF
881CYTarget *CYResolveMember::Replace(CYContext &context) {
882 return $M($M(object_, $S("$cyr")), property_);
883}
884
3b52fd1a 885CYStatement *CYReturn::Replace(CYContext &context) {
ab2aa221 886 if (context.nonlocal_ != NULL) {
c5b15840 887 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
ab2aa221 888 return $ cy::Syntax::Throw($ CYObject(
c5b15840 889 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
ab2aa221
JF
890 ));
891 }
892
3b52fd1a 893 context.Replace(value_);
029bc65b
JF
894 return this;
895}
896
7085e1ab 897CYTarget *CYRubyBlock::Replace(CYContext &context) {
6c093cce
JF
898 return call_->AddArgument(context, proc_->Replace(context));
899}
900
7085e1ab 901CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
1abe53bf
JF
902 return Replace(context)->AddArgument(context, value);
903}
904
7085e1ab 905CYTarget *CYRubyProc::Replace(CYContext &context) {
12e37ba3
JF
906 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
907 function = CYNonLocalize(context, function);
908 function->implicit_ = true;
909 return function;
6c093cce
JF
910}
911
50a3d79f 912CYScope::CYScope(bool transparent, CYContext &context) :
61fe0c53 913 transparent_(transparent),
7085e1ab
JF
914 parent_(context.scope_),
915 damaged_(false),
916 shadow_(NULL),
917 internal_(NULL)
daf22a65 918{
7085e1ab 919 _assert(!transparent_ || parent_ != NULL);
50a3d79f 920 context.scope_ = this;
daf22a65
JF
921}
922
7085e1ab
JF
923void CYScope::Damage() {
924 damaged_ = true;
925 if (parent_ != NULL)
926 parent_->Damage();
a86e34d0
JF
927}
928
7085e1ab
JF
929CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
930 CYForEach (i, internal_)
931 if (strcmp(i->identifier_->Word(), word) == 0)
932 return i;
933 return NULL;
a86e34d0
JF
934}
935
7085e1ab
JF
936CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
937 return Lookup(context, identifier->Word());
029bc65b
JF
938}
939
7085e1ab
JF
940CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
941 _assert(identifier->next_ == NULL || identifier->next_ == identifier);
10711823 942
7085e1ab
JF
943 CYIdentifierFlags *existing(Lookup(context, identifier));
944 if (existing == NULL)
945 internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
946 ++internal_->count_;
947 if (existing == NULL)
948 return internal_;
10711823 949
f5753712
JF
950 if (kind == CYIdentifierGlobal);
951 else if (existing->kind_ == CYIdentifierGlobal || existing->kind_ == CYIdentifierMagic)
7085e1ab 952 existing->kind_ = kind;
7085e1ab
JF
953 else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
954 _assert(false); // XXX: throw new SyntaxError()
10711823 955
7085e1ab
JF
956 return existing;
957}
958
959void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
960 _assert(flags->identifier_->next_ == flags->identifier_);
961 CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
962 flags->identifier_->next_ = existing->identifier_;
963
964 existing->count_ += flags->count_;
965 if (existing->offset_ < flags->offset_)
966 existing->offset_ = flags->offset_;
10711823
JF
967}
968
50a3d79f 969void CYScope::Close(CYContext &context, CYStatement *&statements) {
7085e1ab
JF
970 Close(context);
971
09fc3efb 972 CYList<CYBindings> bindings;
7085e1ab
JF
973
974 CYForEach (i, internal_)
975 if (i->kind_ == CYIdentifierVariable)
09fc3efb
JF
976 bindings
977 ->* $ CYBindings($ CYBinding(i->identifier_));
7085e1ab 978
09fc3efb
JF
979 if (bindings) {
980 CYVar *var($ CYVar(bindings));
7085e1ab
JF
981 var->SetNext(statements);
982 statements = var;
983 }
984}
985
986void CYScope::Close(CYContext &context) {
50a3d79f
JF
987 context.scope_ = parent_;
988
7085e1ab
JF
989 CYForEach (i, internal_) {
990 _assert(i->identifier_->next_ == i->identifier_);
991 switch (i->kind_) {
992 case CYIdentifierArgument: {
993 _assert(!transparent_);
994 } break;
995
996 case CYIdentifierLexical: {
997 if (!damaged_) {
998 CYIdentifier *replace(context.Unique());
999 replace->next_ = replace;
1000 i->identifier_->next_ = replace;
1001 i->identifier_ = replace;
1002 }
1003
1004 if (!transparent_)
1005 i->kind_ = CYIdentifierVariable;
1006 else
1007 parent_->Declare(context, i->identifier_, CYIdentifierVariable);
1008 } break;
2c81c6df 1009
7085e1ab
JF
1010 case CYIdentifierVariable: {
1011 if (transparent_) {
1012 parent_->Declare(context, i->identifier_, i->kind_);
1013 i->kind_ = CYIdentifierGlobal;
1014 }
1015 } break;
1016 default:; } }
14ec9e00 1017
7085e1ab
JF
1018 if (damaged_)
1019 return;
6a981250 1020
7085e1ab
JF
1021 typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
1022 CYIdentifierOffsetMap offsets;
10711823 1023
7085e1ab
JF
1024 CYForEach (i, internal_) {
1025 _assert(i->identifier_->next_ == i->identifier_);
1026 switch (i->kind_) {
1027 case CYIdentifierArgument:
1028 case CYIdentifierVariable:
1029 offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
1030 break;
1031 default:; } }
10711823 1032
7085e1ab 1033 unsigned offset(0);
029bc65b 1034
7085e1ab
JF
1035 for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
1036 if (offset < i->first)
1037 offset = i->first;
1038 CYIdentifier *identifier(i->second);
10711823 1039
7085e1ab
JF
1040 if (offset >= context.replace_.size())
1041 context.replace_.resize(offset + 1, NULL);
1042 CYIdentifier *&replace(context.replace_[offset++]);
6a981250 1043
7085e1ab
JF
1044 if (replace == NULL)
1045 replace = identifier;
1046 else {
1047 _assert(replace->next_ == replace);
1048 identifier->next_ = replace;
1049 }
029bc65b
JF
1050 }
1051
7085e1ab
JF
1052 if (parent_ == NULL)
1053 return;
1054
1055 CYForEach (i, internal_) {
1056 switch (i->kind_) {
1057 case CYIdentifierGlobal: {
1058 if (i->offset_ < offset)
1059 i->offset_ = offset;
1060 parent_->Merge(context, i);
1061 } break;
1062 default:; } }
029bc65b
JF
1063}
1064
fc8fc33d
JF
1065CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
1066 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
b900e1a4
JF
1067}
1068
12e37ba3
JF
1069CYStatement *CYStatement::Return() {
1070 return this;
1071}
1072
4644480a
JF
1073CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1074 size_t size(size_ + rhs->size_);
2eb8215d 1075 char *value($ char[size + 1]);
4644480a
JF
1076 memcpy(value, value_, size_);
1077 memcpy(value + size_, rhs->value_, rhs->size_);
1078 value[size] = '\0';
a14eb702 1079 return $S(value, size);
4644480a
JF
1080}
1081
1082CYNumber *CYString::Number(CYContext &context) {
1083 // XXX: there is a precise algorithm for this
1084 return NULL;
1085}
1086
c5b15840
JF
1087CYExpression *CYString::PropertyName(CYContext &context) {
1088 return this;
1089}
1090
4644480a
JF
1091CYString *CYString::String(CYContext &context) {
1092 return this;
1093}
1094
d8380373
JF
1095CYStatement *CYStructDefinition::Replace(CYContext &context) {
1096 CYTarget *target(tail_->Replace(context));
1097 if (name_ != NULL)
1098 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1099 return $ CYLexical(false, $B1($B($I($pool.strcat(name_->Word(), "$cy", NULL)), target)));
1100}
1101
1102CYTarget *CYStructTail::Replace(CYContext &context) {
1103 CYList<CYElementValue> types;
1104 CYList<CYElementValue> names;
1105
1106 CYForEach (field, fields_) {
1107 CYTypedIdentifier *typed(field->typed_);
1108 types->*$ CYElementValue(typed->Replace(context));
1109
1110 CYExpression *name;
1111 if (typed->identifier_ == NULL)
1112 name = NULL;
1113 else
1114 name = $S(typed->identifier_->Word());
1115 names->*$ CYElementValue(name);
1116 }
1117
1118 return $N2($V("Type"), $ CYArray(types), $ CYArray(names));
1119}
1120
7085e1ab 1121CYTarget *CYSuperAccess::Replace(CYContext &context) {
c5b15840
JF
1122 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1123}
1124
7085e1ab 1125CYTarget *CYSuperCall::Replace(CYContext &context) {
c5b15840
JF
1126 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1127}
1128
2fad14e5
JF
1129CYTarget *CYSymbol::Replace(CYContext &context) {
1130 return $C1($M($V("Symbol"), $S("for")), $S(name_));
1131}
1132
3b52fd1a
JF
1133CYStatement *CYSwitch::Replace(CYContext &context) {
1134 context.Replace(value_);
1135 clauses_->Replace(context);
029bc65b 1136 return this;
3b52fd1a
JF
1137}
1138
7085e1ab
JF
1139CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
1140 if (value == NULL)
1141 return NULL;
1142 return $E($ CYAssign(this, value));
1143}
1144
1145CYTarget *CYTemplate::Replace(CYContext &context) {
fc8fc33d 1146 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
b900e1a4
JF
1147}
1148
7085e1ab 1149CYTarget *CYThis::Replace(CYContext &context) {
a0be43fc
JF
1150 if (context.this_ != NULL)
1151 return $V(context.this_->Identifier(context));
029bc65b 1152 return this;
3b52fd1a
JF
1153}
1154
37954781
JF
1155namespace cy {
1156namespace Syntax {
1157
1158CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a 1159 context.Replace(value_);
029bc65b 1160 return this;
3b52fd1a
JF
1161}
1162
37954781
JF
1163} }
1164
7085e1ab 1165CYTarget *CYTrivial::Replace(CYContext &context) {
029bc65b 1166 return this;
3b52fd1a
JF
1167}
1168
4644480a
JF
1169CYNumber *CYTrue::Number(CYContext &context) {
1170 return $D(1);
1171}
1172
1173CYString *CYTrue::String(CYContext &context) {
1174 return $S("true");
1175}
1176
37954781
JF
1177namespace cy {
1178namespace Syntax {
1179
1180CYStatement *Try::Replace(CYContext &context) {
7085e1ab 1181 CYScope scope(true, context);
b0385401 1182 context.ReplaceAll(code_);
7085e1ab
JF
1183 scope.Close(context);
1184
3b52fd1a
JF
1185 catch_->Replace(context);
1186 finally_->Replace(context);
029bc65b 1187 return this;
3b52fd1a
JF
1188}
1189
37954781
JF
1190} }
1191
7085e1ab 1192CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1193 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
690cf1a8
JF
1194}
1195
7085e1ab 1196CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
3fe16be7
JF
1197 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1198}
1199
0559abf8
JF
1200CYTarget *CYTypeCharacter::Replace(CYContext &context) {
1201 switch (signing_) {
1202 case CYTypeNeutral: return $V("char");
1203 case CYTypeSigned: return $V("schar");
1204 case CYTypeUnsigned: return $V("uchar");
1205 default: _assert(false);
1206 }
1207}
1208
7085e1ab 1209CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1210 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
690cf1a8
JF
1211}
1212
60097023 1213CYStatement *CYTypeDefinition::Replace(CYContext &context) {
64a505ff
JF
1214 CYIdentifier *identifier(typed_->identifier_);
1215 typed_->identifier_ = NULL;
1216 return $ CYLexical(false, $B1($B(identifier, $ CYTypeExpression(typed_))));
60097023
JF
1217}
1218
7085e1ab 1219CYTarget *CYTypeError::Replace(CYContext &context) {
03db6a67
JF
1220 _assert(false);
1221 return NULL;
1222}
1223
64a505ff
JF
1224CYTarget *CYTypeExpression::Replace(CYContext &context) {
1225 return typed_->Replace(context);
1226}
1227
0559abf8
JF
1228CYTarget *CYTypeIntegral::Replace(CYContext &context) {
1229 bool u(signing_ == CYTypeUnsigned);
1230 switch (length_) {
1231 case 0: return $V(u ? "ushort" : "short");
1232 case 1: return $V(u ? "uint" : "int");
1233 case 2: return $V(u ? "ulong" : "long");
1234 case 3: return $V(u ? "ulonglong" : "longlong");
1235 default: _assert(false);
1236 }
1237}
1238
7085e1ab 1239CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
9a39f705
JF
1240 return Replace_(context, type);
1241}
1242
7085e1ab 1243CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
574d4720
JF
1244 CYList<CYArgument> arguments(parameters_->Argument(context));
1245 if (variadic_)
1246 arguments->*$C_($ CYNull());
1247 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), arguments));
9a39f705
JF
1248}
1249
7085e1ab 1250CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1251 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
663c538f
JF
1252}
1253
d8380373
JF
1254CYTarget *CYTypeReference::Replace(CYContext &context) {
1255 return $V($pool.strcat(name_->Word(), "$cy", NULL));
1256}
1257
b3c38c5f 1258CYTarget *CYTypeStruct::Replace(CYContext &context) {
d8380373 1259 CYTarget *target(tail_->Replace(context));
b3c38c5f
JF
1260 if (name_ != NULL)
1261 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1262 return target;
1263}
1264
7085e1ab 1265CYTarget *CYTypeVariable::Replace(CYContext &context) {
3fe283c5
JF
1266 return $V(name_);
1267}
1268
7085e1ab 1269CYTarget *CYTypeVoid::Replace(CYContext &context) {
3fe283c5
JF
1270 return $N1($V("Type"), $ CYString("v"));
1271}
1272
7085e1ab 1273CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1274 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
690cf1a8
JF
1275}
1276
7085e1ab 1277CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
3fe283c5 1278 return modifier_->Replace(context, specifier_->Replace(context));
690cf1a8
JF
1279}
1280
00b4cb83 1281CYTypeFunctionWith *CYTypedIdentifier::Function() {
574d4720
JF
1282 CYTypeModifier *&modifier(CYGetLast(modifier_));
1283 if (modifier == NULL)
00b4cb83 1284 return NULL;
574d4720
JF
1285
1286 CYTypeFunctionWith *function(modifier->Function());
00b4cb83
JF
1287 if (function == NULL)
1288 return NULL;
574d4720
JF
1289
1290 modifier = NULL;
00b4cb83
JF
1291 return function;
1292}
1293
663c538f 1294CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
9a39f705 1295 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
663c538f
JF
1296}
1297
690cf1a8 1298CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
09fc3efb 1299 return $ CYFunctionParameter($ CYBinding(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
690cf1a8
JF
1300}
1301
1302CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
a2f3ecab 1303 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
690cf1a8
JF
1304}
1305
bfd79fae 1306CYForInitializer *CYVar::Replace(CYContext &context) {
09fc3efb 1307 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierVariable))
b0385401 1308 return $E(expression);
fd5cdf97 1309 return $ CYEmpty();
3b52fd1a
JF
1310}
1311
7085e1ab
JF
1312CYTarget *CYVariable::Replace(CYContext &context) {
1313 name_ = name_->Replace(context, CYIdentifierGlobal);
029bc65b 1314 return this;
3b52fd1a
JF
1315}
1316
0afc9ba3 1317CYFunctionParameter *CYVariable::Parameter() const {
09fc3efb 1318 return $ CYFunctionParameter($ CYBinding(name_));
0afc9ba3
JF
1319}
1320
3b52fd1a
JF
1321CYStatement *CYWhile::Replace(CYContext &context) {
1322 context.Replace(test_);
b0385401 1323 context.ReplaceAll(code_);
029bc65b 1324 return this;
3b52fd1a
JF
1325}
1326
1327CYStatement *CYWith::Replace(CYContext &context) {
1328 context.Replace(scope_);
28ef79aa
JF
1329 CYScope scope(true, context);
1330 scope.Damage();
b0385401 1331 context.ReplaceAll(code_);
28ef79aa 1332 scope.Close(context);
029bc65b 1333 return this;
3b52fd1a
JF
1334}
1335
c5b15840
JF
1336CYExpression *CYWord::PropertyName(CYContext &context) {
1337 return $S(this);
3b52fd1a 1338}