]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Struct field access must be mindful of alignments.
[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
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_);
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);
09fc3efb 412 context.Replace(initializer_);
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) {
09fc3efb 430 if (binding_->initializer_ == NULL)
7085e1ab 431 return NULL;
09fc3efb 432 value = binding_->initializer_;
7085e1ab
JF
433 }
434
09fc3efb 435 return $ CYLexical(constant_, $B1($ CYBinding(binding_->identifier_, value)));
7085e1ab
JF
436}
437
438CYTarget *CYForLexical::Replace(CYContext &context) {
09fc3efb
JF
439 _assert(binding_->Replace(context, CYIdentifierLexical) == NULL);
440 return binding_->Target(context);
7085e1ab 441}
ae65d594 442
7085e1ab
JF
443CYStatement *CYForIn::Replace(CYContext &context) {
444 CYScope scope(true, context);
09fc3efb
JF
445 context.Replace(initializer_);
446 context.Replace(iterable_);
b0385401 447 context.ReplaceAll(code_);
7085e1ab 448 scope.Close(context);
15b88a33 449 return this;
3b52fd1a
JF
450}
451
23111dca 452CYStatement *CYForInitialized::Replace(CYContext &context) {
09fc3efb 453 CYAssignment *assignment(binding_->Replace(context, CYIdentifierVariable));
23111dca
JF
454 return $ CYBlock($$
455 ->* (assignment == NULL ? NULL : $ CYExpress(assignment))
09fc3efb 456 ->* $ CYForIn(binding_->Target(context), iterable_, code_));
23111dca
JF
457}
458
3b52fd1a 459CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
09fc3efb 460 return $ CYFunctionParameter(binding_);
3b52fd1a
JF
461}
462
463CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
09fc3efb 464 return $ CYForIn(binding_->Target(context), iterable_, 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($$
09fc3efb
JF
471 ->* initializer_->Initialize(context, NULL)
472 ->* $ CYLexical(false, $B2($B(list, iterable_), $B(item)))
7085e1ab 473 ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
09fc3efb 474 ->* initializer_->Initialize(context, $M($V(list), $V(item)))
ca6a1b2b
JF
475 ->* code_
476 )));
3b52fd1a
JF
477}
478
d5618df7 479CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
09fc3efb 480 return $ CYFunctionParameter(binding_);
3b52fd1a
JF
481}
482
d5618df7 483CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
a0932196 484 CYIdentifier *cys(context.Unique());
3b52fd1a 485
a0932196 486 return $ CYBlock($$
09fc3efb
JF
487 ->* $ CYLexical(false, $B1($B(cys, iterable_)))
488 ->* $ CYForIn(binding_->Target(context), $V(cys), $ CYBlock($$
489 ->* $E($ CYAssign(binding_->Target(context), $M($V(cys), binding_->Target(context))))
8d970b62 490 ->* CYComprehension::Replace(context, statement)
3b52fd1a
JF
491 )));
492}
493
7085e1ab
JF
494CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
495 if (value == NULL) {
09fc3efb 496 if (binding_->initializer_ == NULL)
7085e1ab 497 return NULL;
09fc3efb 498 value = binding_->initializer_;
7085e1ab
JF
499 }
500
09fc3efb 501 return $ CYVar($B1($ CYBinding(binding_->identifier_, value)));
7085e1ab
JF
502}
503
504CYTarget *CYForVariable::Replace(CYContext &context) {
09fc3efb
JF
505 _assert(binding_->Replace(context, CYIdentifierVariable) == NULL);
506 return binding_->Target(context);
7085e1ab
JF
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
b026d136
JF
536 $I("arguments")->Replace(context, CYIdentifierMagic);
537
c8a0500b 538 parameters_->Replace(context, code_);
c5b15840 539
b0385401 540 context.ReplaceAll(code_);
029bc65b 541
12e37ba3
JF
542 if (implicit_)
543 CYImplicitReturn(code_);
544
c5b15840
JF
545 if (CYIdentifier *identifier = this_.identifier_) {
546 context.scope_->Declare(context, identifier, CYIdentifierVariable);
547 code_ = $$
8d970b62
JF
548 ->* $E($ CYAssign($V(identifier), $ CYThis()))
549 ->* code_;
c5b15840 550 }
a0be43fc 551
ab2aa221 552 if (localize)
b0385401 553 context.NonLocal(code_);
06293152
JF
554
555 context.nextlocal_ = nextlocal;
ab2aa221
JF
556 context.nonlocal_ = nonlocal;
557
c5b15840 558 context.super_ = super;
a0be43fc
JF
559 context.this_ = _this;
560
b0385401 561 scope.Close(context, code_);
3b52fd1a
JF
562}
563
7085e1ab 564CYTarget *CYFunctionExpression::Replace(CYContext &context) {
c5b15840
JF
565 CYScope scope(false, context);
566 if (name_ != NULL)
7085e1ab
JF
567 name_ = name_->Replace(context, CYIdentifierOther);
568
c5b15840 569 CYFunction::Replace(context);
7085e1ab 570 scope.Close(context);
029bc65b
JF
571 return this;
572}
573
b0385401 574void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
09fc3efb 575 CYAssignment *assignment(binding_->Replace(context, CYIdentifierArgument));
c8a0500b 576
b0385401 577 next_->Replace(context, statements);
c8a0500b
JF
578
579 if (assignment != NULL)
8d970b62 580 statements = $$
09fc3efb 581 ->* $ CYIf($ CYIdentical($ CYTypeOf(binding_->Target(context)), $S("undefined")), $$
8d970b62
JF
582 ->* $E(assignment))
583 ->* statements;
3b52fd1a
JF
584}
585
586CYStatement *CYFunctionStatement::Replace(CYContext &context) {
7085e1ab 587 name_ = name_->Replace(context, CYIdentifierOther);
c5b15840 588 CYFunction::Replace(context);
029bc65b
JF
589 return this;
590}
591
7085e1ab
JF
592CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
593 if (next_ == this)
594 return this;
595 if (next_ != NULL)
596 return next_->Replace(context, kind);
597 next_ = context.scope_->Declare(context, this, kind)->identifier_;
598 return next_;
3b52fd1a
JF
599}
600
12e37ba3
JF
601CYStatement *CYIf::Return() {
602 CYImplicitReturn(true_);
603 CYImplicitReturn(false_);
604 return this;
605}
606
3b52fd1a
JF
607CYStatement *CYIf::Replace(CYContext &context) {
608 context.Replace(test_);
b0385401
JF
609 context.ReplaceAll(true_);
610 context.ReplaceAll(false_);
029bc65b 611 return this;
3b52fd1a
JF
612}
613
614CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
615 return NULL;
616}
617
618CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
619 return $ CYIf(test_, CYComprehension::Replace(context, statement));
620}
621
7b750785 622CYStatement *CYImport::Replace(CYContext &context) {
09fc3efb 623 return $ CYVar($B1($B($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
7b750785
JF
624}
625
90dd6ff1
JF
626CYStatement *CYImportDeclaration::Replace(CYContext &context) {
627 CYIdentifier *module(context.Unique());
628
629 CYList<CYStatement> statements;
630 CYForEach (specifier, specifiers_)
631 statements->*specifier->Replace(context, module);
632
633 return $ CYBlock($$
634 ->* $ CYLexical(false, $B1($B(module, $C1($V("require"), module_))))
635 ->* statements);
636}
637
638CYStatement *CYImportSpecifier::Replace(CYContext &context, CYIdentifier *module) {
639 binding_ = binding_->Replace(context, CYIdentifierLexical);
640
641 CYExpression *import($V(module));
642 if (name_ != NULL)
643 import = $M(import, $S(name_));
644 return $E($ CYAssign($V(binding_), import));
645}
646
7085e1ab 647CYTarget *CYIndirect::Replace(CYContext &context) {
3b52fd1a
JF
648 return $M(rhs_, $S("$cyi"));
649}
650
7085e1ab 651CYTarget *CYIndirectMember::Replace(CYContext &context) {
3b52fd1a
JF
652 return $M($ CYIndirect(object_), property_);
653}
654
655CYExpression *CYInfix::Replace(CYContext &context) {
656 context.Replace(lhs_);
657 context.Replace(rhs_);
029bc65b 658 return this;
3b52fd1a
JF
659}
660
661CYStatement *CYLabel::Replace(CYContext &context) {
662 context.Replace(statement_);
029bc65b 663 return this;
3b52fd1a
JF
664}
665
7085e1ab 666CYTarget *CYLambda::Replace(CYContext &context) {
b0385401 667 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
690cf1a8
JF
668}
669
09fc3efb
JF
670CYForInitializer *CYLexical::Replace(CYContext &context) {
671 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierLexical))
ca6a1b2b
JF
672 return $E(expression);
673 return $ CYEmpty();
550ee46a
JF
674}
675
c5b15840
JF
676CYFunctionExpression *CYMethod::Constructor() {
677 return NULL;
678}
679
680void CYMethod::Replace(CYContext &context) {
681 CYFunction::Replace(context);
682}
683
7b750785
JF
684CYString *CYModule::Replace(CYContext &context, const char *separator) const {
685 if (next_ == NULL)
686 return $ CYString(part_);
687 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
688}
689
62f398e4
JF
690CYExpression *CYMultiply::Replace(CYContext &context) {
691 CYInfix::Replace(context);
692
fd5cdf97
JF
693 if (CYNumber *lhn = lhs_->Number(context))
694 if (CYNumber *rhn = rhs_->Number(context))
62f398e4
JF
695 return $D(lhn->Value() * rhn->Value());
696
697 return this;
698}
699
2eb8215d
JF
700namespace cy {
701namespace Syntax {
702
7085e1ab 703CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
bf45251b 704 CYSetLast(arguments_) = $ CYArgument(value);
6c093cce
JF
705 return this;
706}
707
7085e1ab 708CYTarget *New::Replace(CYContext &context) {
3b52fd1a
JF
709 context.Replace(constructor_);
710 arguments_->Replace(context);
029bc65b 711 return this;
3b52fd1a
JF
712}
713
2eb8215d
JF
714} }
715
4644480a
JF
716CYNumber *CYNull::Number(CYContext &context) {
717 return $D(0);
718}
719
720CYString *CYNull::String(CYContext &context) {
721 return $S("null");
722}
723
724CYNumber *CYNumber::Number(CYContext &context) {
725 return this;
726}
727
728CYString *CYNumber::String(CYContext &context) {
729 // XXX: there is a precise algorithm for this
0cbeddf8 730 return $S($pool.sprintf(24, "%.17g", Value()));
4644480a
JF
731}
732
c5b15840
JF
733CYExpression *CYNumber::PropertyName(CYContext &context) {
734 return String(context);
735}
736
7085e1ab 737CYTarget *CYObject::Replace(CYContext &context) {
c5b15840
JF
738 CYBuilder builder;
739 if (properties_ != NULL)
740 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
741
742 if (builder) {
09fc3efb 743 return $C1($M($ CYFunctionExpression(NULL, builder.bindings_->Parameter(context),
8d970b62
JF
744 builder.statements_
745 ->* $ CYReturn($ CYThis())
09fc3efb 746 ), $S("call")), this, builder.bindings_->Argument(context));
c5b15840
JF
747 }
748
749 CYForEach (property, properties_)
750 property->Replace(context);
029bc65b 751 return this;
3b52fd1a
JF
752}
753
7085e1ab
JF
754CYTarget *CYParenthetical::Replace(CYContext &context) {
755 // XXX: return expression_;
756 context.Replace(expression_);
757 return this;
b0385401
JF
758}
759
3b52fd1a
JF
760CYExpression *CYPostfix::Replace(CYContext &context) {
761 context.Replace(lhs_);
029bc65b 762 return this;
3b52fd1a
JF
763}
764
765CYExpression *CYPrefix::Replace(CYContext &context) {
766 context.Replace(rhs_);
029bc65b 767 return this;
3b52fd1a
JF
768}
769
c5b15840 770CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
7b87d205 771 update |= Update();
c5b15840 772 if (update)
a196a97a 773 Replace(context, builder, self, false);
c5b15840
JF
774 if (next_ != NULL)
775 next_ = next_->ReplaceAll(context, builder, self, update);
776 return update ? next_ : this;
777}
778
a196a97a 779void CYProperty::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect) {
c5b15840 780 CYExpression *name(name_->PropertyName(context));
a196a97a 781 if (name_->Computed()) {
c5b15840 782 CYIdentifier *unique(context.Unique());
09fc3efb
JF
783 builder.bindings_
784 ->* $B1($B(unique, name));
c5b15840
JF
785 name = $V(unique);
786 }
787
a196a97a 788 Replace(context, builder, self, name, protect);
c5b15840
JF
789}
790
7b87d205
JF
791bool CYProperty::Update() const {
792 return name_->Computed();
793}
794
a196a97a 795void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840 796 CYIdentifier *unique(context.Unique());
09fc3efb
JF
797 builder.bindings_
798 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
c5b15840 799 builder.statements_
a196a97a 800 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
c5b15840
JF
801}
802
803CYFunctionExpression *CYPropertyMethod::Constructor() {
804 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
805}
806
a196a97a 807void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840 808 CYIdentifier *unique(context.Unique());
09fc3efb
JF
809 builder.bindings_
810 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
c5b15840 811 builder.statements_
a196a97a
JF
812 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
813 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
c5b15840
JF
814}
815
7b87d205
JF
816bool CYPropertyMethod::Update() const {
817 return true;
818}
819
a196a97a 820void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
c5b15840 821 CYIdentifier *unique(context.Unique());
09fc3efb
JF
822 builder.bindings_
823 ->* $B1($B(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
c5b15840 824 builder.statements_
a196a97a 825 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
c5b15840
JF
826}
827
a196a97a
JF
828void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
829 _assert(!protect);
c5b15840 830 CYIdentifier *unique(context.Unique());
09fc3efb
JF
831 builder.bindings_
832 ->* $B1($B(unique, value_));
c5b15840
JF
833 builder.statements_
834 ->* $E($ CYAssign($M(self, name), $V(unique)));
835}
836
837void CYPropertyValue::Replace(CYContext &context) {
838 context.Replace(value_);
839}
840
a7d8b413 841void CYScript::Replace(CYContext &context) {
7085e1ab
JF
842 CYScope scope(false, context);
843 context.scope_->Damage();
ab2aa221 844
06293152 845 context.nextlocal_ = $ CYNonLocal();
b0385401
JF
846 context.ReplaceAll(code_);
847 context.NonLocal(code_);
ab2aa221 848
b0385401 849 scope.Close(context, code_);
14ec9e00 850
7085e1ab 851 unsigned offset(0);
de9fc71b 852
7085e1ab 853 for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
14ec9e00 854 const char *name;
14ec9e00 855 if (context.options_.verbose_)
7085e1ab 856 name = $pool.strcat("$", $pool.itoa(offset++), NULL);
14ec9e00
JF
857 else {
858 char id[8];
859 id[7] = '\0';
860
861 id:
7085e1ab 862 unsigned position(7), local(offset++ + 1);
14ec9e00
JF
863
864 do {
45d0c928
JF
865 unsigned index(local % (sizeof(MappingSet) - 1));
866 local /= sizeof(MappingSet) - 1;
867 id[--position] = MappingSet[index];
14ec9e00
JF
868 } while (local != 0);
869
7085e1ab 870 if (scope.Lookup(context, id + position) != NULL)
14ec9e00 871 goto id;
7085e1ab 872 // XXX: at some point, this could become a keyword
14ec9e00 873
b799113b 874 name = $pool.strmemdup(id + position, 7 - position);
14ec9e00
JF
875 }
876
7085e1ab
JF
877 CYIdentifier *identifier(*i);
878 _assert(identifier->next_ == identifier);
879 identifier->next_ = $I(name);
14ec9e00 880 }
3b52fd1a
JF
881}
882
3b52fd1a 883CYStatement *CYReturn::Replace(CYContext &context) {
ab2aa221 884 if (context.nonlocal_ != NULL) {
c5b15840 885 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
ab2aa221 886 return $ cy::Syntax::Throw($ CYObject(
c5b15840 887 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
ab2aa221
JF
888 ));
889 }
890
3b52fd1a 891 context.Replace(value_);
029bc65b
JF
892 return this;
893}
894
7085e1ab 895CYTarget *CYRubyBlock::Replace(CYContext &context) {
6c093cce
JF
896 return call_->AddArgument(context, proc_->Replace(context));
897}
898
7085e1ab 899CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
1abe53bf
JF
900 return Replace(context)->AddArgument(context, value);
901}
902
7085e1ab 903CYTarget *CYRubyProc::Replace(CYContext &context) {
12e37ba3
JF
904 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
905 function = CYNonLocalize(context, function);
906 function->implicit_ = true;
907 return function;
6c093cce
JF
908}
909
50a3d79f 910CYScope::CYScope(bool transparent, CYContext &context) :
61fe0c53 911 transparent_(transparent),
7085e1ab
JF
912 parent_(context.scope_),
913 damaged_(false),
914 shadow_(NULL),
915 internal_(NULL)
daf22a65 916{
7085e1ab 917 _assert(!transparent_ || parent_ != NULL);
50a3d79f 918 context.scope_ = this;
daf22a65
JF
919}
920
7085e1ab
JF
921void CYScope::Damage() {
922 damaged_ = true;
923 if (parent_ != NULL)
924 parent_->Damage();
a86e34d0
JF
925}
926
7085e1ab
JF
927CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
928 CYForEach (i, internal_)
929 if (strcmp(i->identifier_->Word(), word) == 0)
930 return i;
931 return NULL;
a86e34d0
JF
932}
933
7085e1ab
JF
934CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
935 return Lookup(context, identifier->Word());
029bc65b
JF
936}
937
7085e1ab
JF
938CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
939 _assert(identifier->next_ == NULL || identifier->next_ == identifier);
10711823 940
7085e1ab
JF
941 CYIdentifierFlags *existing(Lookup(context, identifier));
942 if (existing == NULL)
943 internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
944 ++internal_->count_;
945 if (existing == NULL)
946 return internal_;
10711823 947
f5753712
JF
948 if (kind == CYIdentifierGlobal);
949 else if (existing->kind_ == CYIdentifierGlobal || existing->kind_ == CYIdentifierMagic)
7085e1ab 950 existing->kind_ = kind;
7085e1ab
JF
951 else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
952 _assert(false); // XXX: throw new SyntaxError()
10711823 953
7085e1ab
JF
954 return existing;
955}
956
957void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
958 _assert(flags->identifier_->next_ == flags->identifier_);
959 CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
960 flags->identifier_->next_ = existing->identifier_;
961
962 existing->count_ += flags->count_;
963 if (existing->offset_ < flags->offset_)
964 existing->offset_ = flags->offset_;
10711823
JF
965}
966
50a3d79f 967void CYScope::Close(CYContext &context, CYStatement *&statements) {
7085e1ab
JF
968 Close(context);
969
09fc3efb 970 CYList<CYBindings> bindings;
7085e1ab
JF
971
972 CYForEach (i, internal_)
973 if (i->kind_ == CYIdentifierVariable)
09fc3efb
JF
974 bindings
975 ->* $ CYBindings($ CYBinding(i->identifier_));
7085e1ab 976
09fc3efb
JF
977 if (bindings) {
978 CYVar *var($ CYVar(bindings));
7085e1ab
JF
979 var->SetNext(statements);
980 statements = var;
981 }
982}
983
984void CYScope::Close(CYContext &context) {
50a3d79f
JF
985 context.scope_ = parent_;
986
7085e1ab
JF
987 CYForEach (i, internal_) {
988 _assert(i->identifier_->next_ == i->identifier_);
989 switch (i->kind_) {
990 case CYIdentifierArgument: {
991 _assert(!transparent_);
992 } break;
993
994 case CYIdentifierLexical: {
995 if (!damaged_) {
996 CYIdentifier *replace(context.Unique());
997 replace->next_ = replace;
998 i->identifier_->next_ = replace;
999 i->identifier_ = replace;
1000 }
1001
1002 if (!transparent_)
1003 i->kind_ = CYIdentifierVariable;
1004 else
1005 parent_->Declare(context, i->identifier_, CYIdentifierVariable);
1006 } break;
2c81c6df 1007
7085e1ab
JF
1008 case CYIdentifierVariable: {
1009 if (transparent_) {
1010 parent_->Declare(context, i->identifier_, i->kind_);
1011 i->kind_ = CYIdentifierGlobal;
1012 }
1013 } break;
1014 default:; } }
14ec9e00 1015
7085e1ab
JF
1016 if (damaged_)
1017 return;
6a981250 1018
7085e1ab
JF
1019 typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
1020 CYIdentifierOffsetMap offsets;
10711823 1021
7085e1ab
JF
1022 CYForEach (i, internal_) {
1023 _assert(i->identifier_->next_ == i->identifier_);
1024 switch (i->kind_) {
1025 case CYIdentifierArgument:
1026 case CYIdentifierVariable:
1027 offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
1028 break;
1029 default:; } }
10711823 1030
7085e1ab 1031 unsigned offset(0);
029bc65b 1032
7085e1ab
JF
1033 for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
1034 if (offset < i->first)
1035 offset = i->first;
1036 CYIdentifier *identifier(i->second);
10711823 1037
7085e1ab
JF
1038 if (offset >= context.replace_.size())
1039 context.replace_.resize(offset + 1, NULL);
1040 CYIdentifier *&replace(context.replace_[offset++]);
6a981250 1041
7085e1ab
JF
1042 if (replace == NULL)
1043 replace = identifier;
1044 else {
1045 _assert(replace->next_ == replace);
1046 identifier->next_ = replace;
1047 }
029bc65b
JF
1048 }
1049
7085e1ab
JF
1050 if (parent_ == NULL)
1051 return;
1052
1053 CYForEach (i, internal_) {
1054 switch (i->kind_) {
1055 case CYIdentifierGlobal: {
1056 if (i->offset_ < offset)
1057 i->offset_ = offset;
1058 parent_->Merge(context, i);
1059 } break;
1060 default:; } }
029bc65b
JF
1061}
1062
fc8fc33d
JF
1063CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
1064 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
b900e1a4
JF
1065}
1066
12e37ba3
JF
1067CYStatement *CYStatement::Return() {
1068 return this;
1069}
1070
4644480a
JF
1071CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1072 size_t size(size_ + rhs->size_);
2eb8215d 1073 char *value($ char[size + 1]);
4644480a
JF
1074 memcpy(value, value_, size_);
1075 memcpy(value + size_, rhs->value_, rhs->size_);
1076 value[size] = '\0';
a14eb702 1077 return $S(value, size);
4644480a
JF
1078}
1079
1080CYNumber *CYString::Number(CYContext &context) {
1081 // XXX: there is a precise algorithm for this
1082 return NULL;
1083}
1084
c5b15840
JF
1085CYExpression *CYString::PropertyName(CYContext &context) {
1086 return this;
1087}
1088
4644480a
JF
1089CYString *CYString::String(CYContext &context) {
1090 return this;
1091}
1092
d8380373
JF
1093CYStatement *CYStructDefinition::Replace(CYContext &context) {
1094 CYTarget *target(tail_->Replace(context));
1095 if (name_ != NULL)
1096 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1097 return $ CYLexical(false, $B1($B($I($pool.strcat(name_->Word(), "$cy", NULL)), target)));
1098}
1099
1100CYTarget *CYStructTail::Replace(CYContext &context) {
1101 CYList<CYElementValue> types;
1102 CYList<CYElementValue> names;
1103
1104 CYForEach (field, fields_) {
1105 CYTypedIdentifier *typed(field->typed_);
1106 types->*$ CYElementValue(typed->Replace(context));
1107
1108 CYExpression *name;
1109 if (typed->identifier_ == NULL)
1110 name = NULL;
1111 else
1112 name = $S(typed->identifier_->Word());
1113 names->*$ CYElementValue(name);
1114 }
1115
1116 return $N2($V("Type"), $ CYArray(types), $ CYArray(names));
1117}
1118
7085e1ab 1119CYTarget *CYSuperAccess::Replace(CYContext &context) {
c5b15840
JF
1120 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1121}
1122
7085e1ab 1123CYTarget *CYSuperCall::Replace(CYContext &context) {
c5b15840
JF
1124 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1125}
1126
3b52fd1a
JF
1127CYStatement *CYSwitch::Replace(CYContext &context) {
1128 context.Replace(value_);
1129 clauses_->Replace(context);
029bc65b 1130 return this;
3b52fd1a
JF
1131}
1132
7085e1ab
JF
1133CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
1134 if (value == NULL)
1135 return NULL;
1136 return $E($ CYAssign(this, value));
1137}
1138
1139CYTarget *CYTemplate::Replace(CYContext &context) {
fc8fc33d 1140 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
b900e1a4
JF
1141}
1142
7085e1ab 1143CYTarget *CYThis::Replace(CYContext &context) {
a0be43fc
JF
1144 if (context.this_ != NULL)
1145 return $V(context.this_->Identifier(context));
029bc65b 1146 return this;
3b52fd1a
JF
1147}
1148
37954781
JF
1149namespace cy {
1150namespace Syntax {
1151
1152CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a 1153 context.Replace(value_);
029bc65b 1154 return this;
3b52fd1a
JF
1155}
1156
37954781
JF
1157} }
1158
7085e1ab 1159CYTarget *CYTrivial::Replace(CYContext &context) {
029bc65b 1160 return this;
3b52fd1a
JF
1161}
1162
4644480a
JF
1163CYNumber *CYTrue::Number(CYContext &context) {
1164 return $D(1);
1165}
1166
1167CYString *CYTrue::String(CYContext &context) {
1168 return $S("true");
1169}
1170
37954781
JF
1171namespace cy {
1172namespace Syntax {
1173
1174CYStatement *Try::Replace(CYContext &context) {
7085e1ab 1175 CYScope scope(true, context);
b0385401 1176 context.ReplaceAll(code_);
7085e1ab
JF
1177 scope.Close(context);
1178
3b52fd1a
JF
1179 catch_->Replace(context);
1180 finally_->Replace(context);
029bc65b 1181 return this;
3b52fd1a
JF
1182}
1183
37954781
JF
1184} }
1185
7085e1ab 1186CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1187 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
690cf1a8
JF
1188}
1189
7085e1ab 1190CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
3fe16be7
JF
1191 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1192}
1193
7085e1ab 1194CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1195 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
690cf1a8
JF
1196}
1197
60097023 1198CYStatement *CYTypeDefinition::Replace(CYContext &context) {
64a505ff
JF
1199 CYIdentifier *identifier(typed_->identifier_);
1200 typed_->identifier_ = NULL;
1201 return $ CYLexical(false, $B1($B(identifier, $ CYTypeExpression(typed_))));
60097023
JF
1202}
1203
7085e1ab 1204CYTarget *CYTypeError::Replace(CYContext &context) {
03db6a67
JF
1205 _assert(false);
1206 return NULL;
1207}
1208
64a505ff
JF
1209CYTarget *CYTypeExpression::Replace(CYContext &context) {
1210 return typed_->Replace(context);
1211}
1212
7085e1ab 1213CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
9a39f705
JF
1214 return Replace_(context, type);
1215}
1216
7085e1ab 1217CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
9a39f705
JF
1218 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
1219}
1220
7085e1ab 1221CYTarget *CYTypeLong::Replace(CYContext &context) {
3fe283c5
JF
1222 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
1223}
1224
7085e1ab 1225CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1226 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
663c538f
JF
1227}
1228
d8380373
JF
1229CYTarget *CYTypeReference::Replace(CYContext &context) {
1230 return $V($pool.strcat(name_->Word(), "$cy", NULL));
1231}
1232
7085e1ab 1233CYTarget *CYTypeShort::Replace(CYContext &context) {
3fe283c5
JF
1234 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
1235}
1236
7085e1ab 1237CYTarget *CYTypeSigned::Replace(CYContext &context) {
3fe283c5
JF
1238 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
1239}
1240
b3c38c5f 1241CYTarget *CYTypeStruct::Replace(CYContext &context) {
d8380373 1242 CYTarget *target(tail_->Replace(context));
b3c38c5f
JF
1243 if (name_ != NULL)
1244 target = $C1($M(target, $S("withName")), $S(name_->Word()));
1245 return target;
1246}
1247
7085e1ab 1248CYTarget *CYTypeUnsigned::Replace(CYContext &context) {
3fe283c5
JF
1249 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
1250}
1251
7085e1ab 1252CYTarget *CYTypeVariable::Replace(CYContext &context) {
3fe283c5
JF
1253 return $V(name_);
1254}
1255
7085e1ab 1256CYTarget *CYTypeVoid::Replace(CYContext &context) {
3fe283c5
JF
1257 return $N1($V("Type"), $ CYString("v"));
1258}
1259
7085e1ab 1260CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
9a39f705 1261 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
690cf1a8
JF
1262}
1263
7085e1ab 1264CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
3fe283c5 1265 return modifier_->Replace(context, specifier_->Replace(context));
690cf1a8
JF
1266}
1267
00b4cb83
JF
1268CYTypeFunctionWith *CYTypedIdentifier::Function() {
1269 CYTypeModifier **modifier(&modifier_);
1270 if (*modifier == NULL)
1271 return NULL;
1272 while ((*modifier)->next_ != NULL)
1273 modifier = &(*modifier)->next_;
1274 CYTypeFunctionWith *function((*modifier)->Function());
1275 if (function == NULL)
1276 return NULL;
1277 *modifier = NULL;
1278 return function;
1279}
1280
663c538f 1281CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
9a39f705 1282 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
663c538f
JF
1283}
1284
690cf1a8 1285CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
09fc3efb 1286 return $ CYFunctionParameter($ CYBinding(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
690cf1a8
JF
1287}
1288
1289CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
a2f3ecab 1290 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
690cf1a8
JF
1291}
1292
bfd79fae 1293CYForInitializer *CYVar::Replace(CYContext &context) {
09fc3efb 1294 if (CYExpression *expression = bindings_->Replace(context, CYIdentifierVariable))
b0385401 1295 return $E(expression);
fd5cdf97 1296 return $ CYEmpty();
3b52fd1a
JF
1297}
1298
7085e1ab
JF
1299CYTarget *CYVariable::Replace(CYContext &context) {
1300 name_ = name_->Replace(context, CYIdentifierGlobal);
029bc65b 1301 return this;
3b52fd1a
JF
1302}
1303
0afc9ba3 1304CYFunctionParameter *CYVariable::Parameter() const {
09fc3efb 1305 return $ CYFunctionParameter($ CYBinding(name_));
0afc9ba3
JF
1306}
1307
3b52fd1a
JF
1308CYStatement *CYWhile::Replace(CYContext &context) {
1309 context.Replace(test_);
b0385401 1310 context.ReplaceAll(code_);
029bc65b 1311 return this;
3b52fd1a
JF
1312}
1313
1314CYStatement *CYWith::Replace(CYContext &context) {
1315 context.Replace(scope_);
28ef79aa
JF
1316 CYScope scope(true, context);
1317 scope.Damage();
b0385401 1318 context.ReplaceAll(code_);
28ef79aa 1319 scope.Close(context);
029bc65b 1320 return this;
3b52fd1a
JF
1321}
1322
c5b15840
JF
1323CYExpression *CYWord::PropertyName(CYContext &context) {
1324 return $S(this);
3b52fd1a 1325}