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