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