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