]> git.saurik.com Git - cycript.git/blame - Replace.cpp
I don't think the Types_ TypeMap was ever used :/.
[cycript.git] / Replace.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
f95d2598 2 * Copyright (C) 2009-2014 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 "Parser.hpp"
6a981250 23#include "Replace.hpp"
3b52fd1a 24
3b52fd1a
JF
25#include <iomanip>
26
61fe0c53
JF
27CYFunctionExpression *CYNonLocalize(CYContext &context, CYFunctionExpression *function) {
28 function->nonlocal_ = context.nextlocal_;
29 return function;
30}
31
4644480a
JF
32CYExpression *CYAdd::Replace(CYContext &context) {
33 CYInfix::Replace(context);
34
fd5cdf97
JF
35 CYString *lhs(dynamic_cast<CYString *>(lhs_));
36 CYString *rhs(dynamic_cast<CYString *>(rhs_));
4644480a
JF
37
38 if (lhs != NULL || rhs != NULL) {
39 if (lhs == NULL) {
fd5cdf97 40 lhs = lhs_->String(context);
4644480a 41 if (lhs == NULL)
029bc65b 42 return this;
4644480a 43 } else if (rhs == NULL) {
fd5cdf97 44 rhs = rhs_->String(context);
4644480a 45 if (rhs == NULL)
029bc65b 46 return this;
4644480a
JF
47 }
48
49 return lhs->Concat(context, rhs);
50 }
51
fd5cdf97
JF
52 if (CYNumber *lhn = lhs_->Number(context))
53 if (CYNumber *rhn = rhs_->Number(context))
4644480a
JF
54 return $D(lhn->Value() + rhn->Value());
55
029bc65b 56 return this;
4644480a
JF
57}
58
3b52fd1a 59CYExpression *CYAddressOf::Replace(CYContext &context) {
3b52fd1a
JF
60 return $C0($M(rhs_, $S("$cya")));
61}
62
5192746c 63CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
3b52fd1a 64 context.Replace(value_);
5192746c
JF
65 next_ = next_->Replace(context);
66
67 if (value_ == NULL) {
68 if (next_ == NULL)
69 return NULL;
70 else
71 value_ = $U;
72 }
73
74 return this;
3b52fd1a
JF
75}
76
77CYExpression *CYArray::Replace(CYContext &context) {
78 elements_->Replace(context);
029bc65b 79 return this;
3b52fd1a
JF
80}
81
82CYExpression *CYArrayComprehension::Replace(CYContext &context) {
83 CYVariable *cyv($V("$cyv"));
84
c8a0500b 85 return $C0($F(NULL, $P1($L("$cyv"), comprehensions_->Parameters(context)), $$->*
3b52fd1a
JF
86 $E($ CYAssign(cyv, $ CYArray()))->*
87 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
88 $ CYReturn(cyv)
89 ));
90}
91
92CYExpression *CYAssignment::Replace(CYContext &context) {
93 context.Replace(lhs_);
94 context.Replace(rhs_);
029bc65b 95 return this;
3b52fd1a
JF
96}
97
98CYStatement *CYBlock::Replace(CYContext &context) {
cde20a5a 99 context.ReplaceAll(statements_);
029bc65b
JF
100 if (statements_ == NULL)
101 return $ CYEmpty();
102 return this;
3b52fd1a
JF
103}
104
105CYStatement *CYBreak::Replace(CYContext &context) {
029bc65b 106 return this;
3b52fd1a
JF
107}
108
6c093cce
JF
109CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
110 CYArgument **argument(&arguments_);
111 while (*argument != NULL)
112 argument = &(*argument)->next_;
113 *argument = $ CYArgument(value);
114 return this;
115}
116
3b52fd1a
JF
117CYExpression *CYCall::Replace(CYContext &context) {
118 context.Replace(function_);
119 arguments_->Replace(context);
029bc65b 120 return this;
3b52fd1a
JF
121}
122
37954781
JF
123namespace cy {
124namespace Syntax {
125
126void Catch::Replace(CYContext &context) { $T()
61fe0c53 127 CYScope scope(true, context, code_.statements_);
daf22a65
JF
128
129 context.Replace(name_);
130 context.scope_->Declare(context, name_, CYIdentifierCatch);
131
3b52fd1a 132 code_.Replace(context);
daf22a65 133 scope.Close();
3b52fd1a
JF
134}
135
37954781
JF
136} }
137
3b52fd1a
JF
138void CYClause::Replace(CYContext &context) { $T()
139 context.Replace(case_);
cde20a5a 140 context.ReplaceAll(statements_);
3b52fd1a
JF
141 next_->Replace(context);
142}
143
320ce753 144CYStatement *CYComment::Replace(CYContext &context) {
029bc65b 145 return this;
320ce753
JF
146}
147
3b52fd1a 148CYExpression *CYCompound::Replace(CYContext &context) {
fd5cdf97
JF
149 if (next_ == NULL)
150 return expression_;
151
152 context.Replace(expression_);
153 context.Replace(next_);
154
155 if (CYCompound *compound = dynamic_cast<CYCompound *>(expression_)) {
156 expression_ = compound->expression_;
157 compound->expression_ = compound->next_;
158 compound->next_ = next_;
159 next_ = compound;
160 }
161
9efd8b03 162 return this;
3b52fd1a
JF
163}
164
e06e5ee1 165CYExpression *CYCompound::Primitive(CYContext &context) {
fd5cdf97
JF
166 CYExpression *expression(expression_);
167 if (expression == NULL || next_ != NULL)
e06e5ee1 168 return NULL;
e06e5ee1
JF
169 return expression->Primitive(context);
170}
171
3b52fd1a
JF
172CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
173 CYFunctionParameter *next(next_->Parameters(context));
174 if (CYFunctionParameter *parameter = Parameter(context)) {
175 parameter->SetNext(next);
176 return parameter;
177 } else
178 return next;
179}
180
181CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
182 return next_ == NULL ? statement : next_->Replace(context, statement);
183}
184
185CYExpression *CYCondition::Replace(CYContext &context) {
186 context.Replace(test_);
187 context.Replace(true_);
188 context.Replace(false_);
029bc65b 189 return this;
3b52fd1a
JF
190}
191
ab2aa221
JF
192void CYContext::NonLocal(CYStatement *&statements) {
193 CYContext &context(*this);
194
06293152 195 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
daf22a65
JF
196 CYIdentifier *cye($I("$cye")->Replace(context));
197 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
198
199 CYStatement *declare(
c8a0500b 200 $ CYVar($L1($ CYDeclaration(unique, $ CYObject()))));
daf22a65
JF
201
202 cy::Syntax::Catch *rescue(
203 $ cy::Syntax::Catch(cye, $$->*
4e58e244
JF
204 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
205 $ CYReturn($M($V(cye), $S("$cyv"))))->*
206 $ cy::Syntax::Throw($V(cye))));
daf22a65 207
577bfbfa 208 context.Replace(declare);
daf22a65 209 rescue->Replace(context);
ab2aa221
JF
210
211 statements = $$->*
daf22a65
JF
212 declare->*
213 $ cy::Syntax::Try(statements, rescue, NULL);
ab2aa221
JF
214 }
215}
216
217CYIdentifier *CYContext::Unique() {
0cbeddf8 218 return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL));
ab2aa221
JF
219}
220
3b52fd1a 221CYStatement *CYContinue::Replace(CYContext &context) {
029bc65b
JF
222 return this;
223}
224
c8a0500b
JF
225CYStatement *CYDebugger::Replace(CYContext &context) {
226 return this;
227}
228
029bc65b 229CYAssignment *CYDeclaration::Assignment(CYContext &context) {
15b88a33
JF
230 if (initialiser_ == NULL)
231 return NULL;
127c1a9c
JF
232
233 CYAssignment *value($ CYAssign(Variable(context), initialiser_));
234 initialiser_ = NULL;
235 return value;
15b88a33
JF
236}
237
238CYVariable *CYDeclaration::Variable(CYContext &context) {
239 return $V(identifier_);
3b52fd1a
JF
240}
241
ad3b38bb 242CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
c8a0500b 243 return $ CYVar($L1($ CYDeclaration(identifier_, value)));
3b52fd1a
JF
244}
245
029bc65b 246CYExpression *CYDeclaration::Replace(CYContext &context) {
15b88a33
JF
247 context.Replace(identifier_);
248 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
249 return Variable(context);
250}
251
252void CYDeclarations::Replace(CYContext &context) { $T()
253 declaration_->Replace(context);
254 next_->Replace(context);
3b52fd1a
JF
255}
256
550ee46a 257CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
5192746c 258 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_, next_->Property(context));
550ee46a
JF
259}
260
15b88a33 261CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
c8a0500b 262 return $ CYFunctionParameter($ CYDeclaration(declaration_->identifier_), next_->Parameter(context));
15b88a33
JF
263}
264
265CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
5192746c 266 return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
15b88a33
JF
267}
268
269CYCompound *CYDeclarations::Compound(CYContext &context) { $T(NULL)
fd5cdf97 270 CYCompound *compound(next_->Compound(context));
029bc65b 271 if (CYAssignment *assignment = declaration_->Assignment(context))
fd5cdf97 272 compound = $ CYCompound(assignment, compound);
029bc65b 273 return compound;
3b52fd1a
JF
274}
275
276CYExpression *CYDirectMember::Replace(CYContext &context) {
ca0f097f
JF
277 context.Replace(object_);
278 context.Replace(property_);
029bc65b 279 return this;
3b52fd1a
JF
280}
281
282CYStatement *CYDoWhile::Replace(CYContext &context) {
283 context.Replace(test_);
284 context.Replace(code_);
029bc65b 285 return this;
3b52fd1a
JF
286}
287
288void CYElement::Replace(CYContext &context) { $T()
289 context.Replace(value_);
290 next_->Replace(context);
291}
292
293CYStatement *CYEmpty::Replace(CYContext &context) {
9cd63688 294 return NULL;
029bc65b
JF
295}
296
9a39f705
JF
297CYExpression *CYEncodedType::Replace(CYContext &context) {
298 return typed_->Replace(context);
299}
300
720a57a8
JF
301CYStatement *CYExpress::Replace(CYContext &context) {
302 while (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
fd5cdf97 303 expression_ = $ CYCompound(expression_, express->expression_);
029bc65b
JF
304 SetNext(express->next_);
305 }
306
3b52fd1a 307 context.Replace(expression_);
029bc65b
JF
308 if (expression_ == NULL)
309 return $ CYEmpty();
720a57a8 310
029bc65b 311 return this;
3b52fd1a
JF
312}
313
6c093cce
JF
314CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
315 return $C1(this, value);
316}
317
3b52fd1a
JF
318CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
319 return this;
320}
321
ad3b38bb
JF
322CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
323 return $E($ CYAssign(this, value));
3b52fd1a
JF
324}
325
ae65d594
JF
326CYAssignment *CYExpression::Assignment(CYContext &context) {
327 return NULL;
328}
329
c5587ed7
JF
330CYStatement *CYExternal::Replace(CYContext &context) {
331 return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word())))));
332}
333
4644480a
JF
334CYNumber *CYFalse::Number(CYContext &context) {
335 return $D(0);
336}
337
338CYString *CYFalse::String(CYContext &context) {
339 return $S("false");
340}
341
a0be43fc
JF
342CYExpression *CYFatArrow::Replace(CYContext &context) {
343 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
344 function->this_.SetNext(context.this_);
345 return function;
346}
347
3b52fd1a
JF
348void CYFinally::Replace(CYContext &context) { $T()
349 code_.Replace(context);
350}
351
352CYStatement *CYFor::Replace(CYContext &context) {
029bc65b 353 context.Replace(initialiser_);
3b52fd1a
JF
354 context.Replace(test_);
355 context.Replace(increment_);
356 context.Replace(code_);
029bc65b 357 return this;
3b52fd1a
JF
358}
359
15b88a33
JF
360CYCompound *CYForDeclarations::Replace(CYContext &context) {
361 declarations_->Replace(context);
362 return declarations_->Compound(context);
363}
364
365// XXX: this still feels highly suboptimal
3b52fd1a 366CYStatement *CYForIn::Replace(CYContext &context) {
127c1a9c 367 if (CYAssignment *assignment = initialiser_->Assignment(context))
15b88a33
JF
368 return $ CYBlock($$->*
369 $E(assignment)->*
370 this
371 );
ae65d594 372
127c1a9c
JF
373 context.Replace(initialiser_);
374 context.Replace(set_);
375 context.Replace(code_);
15b88a33 376 return this;
3b52fd1a
JF
377}
378
379CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
c8a0500b 380 return $ CYFunctionParameter($ CYDeclaration(name_));
3b52fd1a
JF
381}
382
383CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
4e58e244 384 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
3b52fd1a
JF
385}
386
d5618df7 387CYStatement *CYForOf::Replace(CYContext &context) {
04ea132d
JF
388 if (CYAssignment *assignment = initialiser_->Assignment(context))
389 return $ CYBlock($$->*
390 $E(assignment)->*
391 this
392 );
393
2eb8215d 394 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
3b52fd1a 395
c8a0500b 396 return $ CYLetStatement($L2($ CYDeclaration(cys, set_), $ CYDeclaration(cyt)), $$->*
2eb8215d 397 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
ad3b38bb 398 initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))->*
3b52fd1a
JF
399 code_
400 ))
550ee46a 401 );
3b52fd1a
JF
402}
403
d5618df7 404CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
c8a0500b 405 return $ CYFunctionParameter($ CYDeclaration(name_));
3b52fd1a
JF
406}
407
d5618df7 408CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
2eb8215d 409 CYIdentifier *cys($I("cys"));
3b52fd1a 410
c8a0500b 411 return $E($C0($F(NULL, $P1($L("$cys")), $$->*
2eb8215d
JF
412 $E($ CYAssign($V(cys), set_))->*
413 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
414 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
3b52fd1a
JF
415 CYComprehension::Replace(context, statement)
416 ))
417 )));
418}
419
14ec9e00 420void CYFunction::Inject(CYContext &context) {
6a981250 421 context.Replace(name_);
a86e34d0 422 context.scope_->Declare(context, name_, CYIdentifierOther);
14ec9e00
JF
423}
424
425void CYFunction::Replace_(CYContext &context, bool outer) {
426 if (outer)
427 Inject(context);
428
a0be43fc
JF
429 CYThisScope *_this(context.this_);
430 context.this_ = CYGetLast(&this_);
431
06293152
JF
432 CYNonLocal *nonlocal(context.nonlocal_);
433 CYNonLocal *nextlocal(context.nextlocal_);
434
ab2aa221 435 bool localize;
06293152 436 if (nonlocal_ != NULL) {
ab2aa221 437 localize = false;
06293152
JF
438 context.nonlocal_ = nonlocal_;
439 } else {
ab2aa221
JF
440 localize = true;
441 nonlocal_ = $ CYNonLocal();
06293152 442 context.nextlocal_ = nonlocal_;
ab2aa221
JF
443 }
444
61fe0c53
JF
445 CYScope scope(!localize, context, code_.statements_);
446
14ec9e00
JF
447 if (!outer && name_ != NULL)
448 Inject(context);
449
c8a0500b 450 parameters_->Replace(context, code_);
3b52fd1a 451 code_.Replace(context);
029bc65b 452
a0be43fc
JF
453 if (CYIdentifier *identifier = this_.identifier_)
454 code_.statements_ = $$->*
455 $ CYVar($L1($ CYDeclaration(identifier, $ CYThis())))->*
456 code_.statements_;
457
ab2aa221
JF
458 if (localize)
459 context.NonLocal(code_.statements_);
06293152
JF
460
461 context.nextlocal_ = nextlocal;
ab2aa221
JF
462 context.nonlocal_ = nonlocal;
463
a0be43fc
JF
464 context.this_ = _this;
465
daf22a65 466 scope.Close();
3b52fd1a
JF
467}
468
469CYExpression *CYFunctionExpression::Replace(CYContext &context) {
14ec9e00 470 Replace_(context, false);
029bc65b
JF
471 return this;
472}
473
c8a0500b
JF
474void CYFunctionParameter::Replace(CYContext &context, CYBlock &code) { $T()
475 CYAssignment *assignment(initialiser_->Assignment(context));
476 context.Replace(initialiser_);
477
478 next_->Replace(context, code);
479
480 if (assignment != NULL)
481 // XXX: this cast is quite incorrect
482 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(dynamic_cast<CYExpression *>(initialiser_)), $S("undefined")), $$->*
483 $E(assignment)
484 ));
3b52fd1a
JF
485}
486
487CYStatement *CYFunctionStatement::Replace(CYContext &context) {
14ec9e00 488 Replace_(context, true);
029bc65b
JF
489 return this;
490}
491
492CYIdentifier *CYIdentifier::Replace(CYContext &context) {
a846a8cd 493 if (replace_ != NULL && replace_ != this)
de9fc71b 494 return replace_->Replace(context);
a846a8cd 495 replace_ = context.scope_->Lookup(context, this);
a86e34d0 496 return replace_;
3b52fd1a
JF
497}
498
499CYStatement *CYIf::Replace(CYContext &context) {
500 context.Replace(test_);
501 context.Replace(true_);
502 context.Replace(false_);
029bc65b 503 return this;
3b52fd1a
JF
504}
505
506CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
507 return NULL;
508}
509
510CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
511 return $ CYIf(test_, CYComprehension::Replace(context, statement));
512}
513
7b750785
JF
514CYStatement *CYImport::Replace(CYContext &context) {
515 return $ CYVar($L1($L(module_->part_->Word(), $C1($V("require"), module_->Replace(context, "/")))));
516}
517
3b52fd1a 518CYExpression *CYIndirect::Replace(CYContext &context) {
3b52fd1a
JF
519 return $M(rhs_, $S("$cyi"));
520}
521
522CYExpression *CYIndirectMember::Replace(CYContext &context) {
3b52fd1a
JF
523 return $M($ CYIndirect(object_), property_);
524}
525
526CYExpression *CYInfix::Replace(CYContext &context) {
527 context.Replace(lhs_);
528 context.Replace(rhs_);
029bc65b 529 return this;
3b52fd1a
JF
530}
531
532CYStatement *CYLabel::Replace(CYContext &context) {
533 context.Replace(statement_);
029bc65b 534 return this;
3b52fd1a
JF
535}
536
690cf1a8 537CYExpression *CYLambda::Replace(CYContext &context) {
9a39f705 538 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), statements_), parameters_->TypeSignature(context, typed_->Replace(context)));
690cf1a8
JF
539}
540
c8a0500b 541CYStatement *CYLetStatement::Replace(CYContext &context) {
61fe0c53 542 return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context)));
550ee46a
JF
543}
544
7b750785
JF
545CYString *CYModule::Replace(CYContext &context, const char *separator) const {
546 if (next_ == NULL)
547 return $ CYString(part_);
548 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
549}
550
62f398e4
JF
551CYExpression *CYMultiply::Replace(CYContext &context) {
552 CYInfix::Replace(context);
553
fd5cdf97
JF
554 if (CYNumber *lhn = lhs_->Number(context))
555 if (CYNumber *rhn = rhs_->Number(context))
62f398e4
JF
556 return $D(lhn->Value() * rhn->Value());
557
558 return this;
559}
560
2eb8215d
JF
561namespace cy {
562namespace Syntax {
563
564CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
bf45251b 565 CYSetLast(arguments_) = $ CYArgument(value);
6c093cce
JF
566 return this;
567}
568
2eb8215d 569CYExpression *New::Replace(CYContext &context) {
3b52fd1a
JF
570 context.Replace(constructor_);
571 arguments_->Replace(context);
029bc65b 572 return this;
3b52fd1a
JF
573}
574
2eb8215d
JF
575} }
576
4644480a
JF
577CYNumber *CYNull::Number(CYContext &context) {
578 return $D(0);
579}
580
581CYString *CYNull::String(CYContext &context) {
582 return $S("null");
583}
584
585CYNumber *CYNumber::Number(CYContext &context) {
586 return this;
587}
588
589CYString *CYNumber::String(CYContext &context) {
590 // XXX: there is a precise algorithm for this
0cbeddf8 591 return $S($pool.sprintf(24, "%.17g", Value()));
4644480a
JF
592}
593
3b52fd1a
JF
594CYExpression *CYObject::Replace(CYContext &context) {
595 properties_->Replace(context);
029bc65b 596 return this;
3b52fd1a
JF
597}
598
599CYExpression *CYPostfix::Replace(CYContext &context) {
600 context.Replace(lhs_);
029bc65b 601 return this;
3b52fd1a
JF
602}
603
604CYExpression *CYPrefix::Replace(CYContext &context) {
605 context.Replace(rhs_);
029bc65b 606 return this;
3b52fd1a
JF
607}
608
10711823 609// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
45d0c928 610#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
10711823 611//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
45d0c928 612
e013809d
JF
613namespace {
614 struct IdentifierUsageLess :
615 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
616 {
617 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
618 if (lhs->usage_ != rhs->usage_)
619 return lhs->usage_ > rhs->usage_;
620 return lhs < rhs;
621 }
622 };
623
624 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
625}
626
3b52fd1a 627void CYProgram::Replace(CYContext &context) {
61fe0c53 628 CYScope scope(true, context, statements_);
ab2aa221 629
06293152 630 context.nextlocal_ = $ CYNonLocal();
cde20a5a 631 context.ReplaceAll(statements_);
ab2aa221
JF
632 context.NonLocal(statements_);
633
daf22a65 634 scope.Close();
14ec9e00
JF
635
636 size_t offset(0);
637
6a981250 638 CYCStringSet external;
a846a8cd 639 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
6a981250
JF
640 external.insert((*i)->Word());
641
e013809d
JF
642 IdentifierUsages usages;
643
a846a8cd 644 if (offset < context.rename_.size())
c2c9f509
JF
645 CYForEach (i, context.rename_[offset].identifier_)
646 usages.insert(i);
e013809d 647
14ec9e00 648 // XXX: totalling the probable occurrences and sorting by them would improve the result
a846a8cd 649 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
de9fc71b
JF
650 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
651
14ec9e00
JF
652 const char *name;
653
654 if (context.options_.verbose_)
0cbeddf8 655 name = $pool.strcat("$", $pool.itoa(offset), NULL);
14ec9e00
JF
656 else {
657 char id[8];
658 id[7] = '\0';
659
660 id:
661 unsigned position(7), local(offset + 1);
662
663 do {
45d0c928
JF
664 unsigned index(local % (sizeof(MappingSet) - 1));
665 local /= sizeof(MappingSet) - 1;
666 id[--position] = MappingSet[index];
14ec9e00
JF
667 } while (local != 0);
668
6a981250 669 if (external.find(id + position) != external.end()) {
14ec9e00
JF
670 ++offset;
671 goto id;
672 }
673
b799113b 674 name = $pool.strmemdup(id + position, 7 - position);
14ec9e00
JF
675 // XXX: at some point, this could become a keyword
676 }
677
c2c9f509 678 CYForEach (identifier, i->identifier_)
14ec9e00
JF
679 identifier->Set(name);
680 }
3b52fd1a
JF
681}
682
683void CYProperty::Replace(CYContext &context) { $T()
684 context.Replace(value_);
685 next_->Replace(context);
5192746c
JF
686 if (value_ == NULL)
687 value_ = $U;
3b52fd1a
JF
688}
689
690CYStatement *CYReturn::Replace(CYContext &context) {
ab2aa221
JF
691 if (context.nonlocal_ != NULL) {
692 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
693 return $ cy::Syntax::Throw($ CYObject(
4e58e244 694 $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
ab2aa221
JF
695 ));
696 }
697
3b52fd1a 698 context.Replace(value_);
029bc65b
JF
699 return this;
700}
701
6c093cce
JF
702CYExpression *CYRubyBlock::Replace(CYContext &context) {
703 // XXX: this needs to do something much more epic to handle return
704 return call_->AddArgument(context, proc_->Replace(context));
705}
706
707CYExpression *CYRubyProc::Replace(CYContext &context) {
61fe0c53 708 return CYNonLocalize(context, $ CYFunctionExpression(NULL, parameters_, code_));
6c093cce
JF
709}
710
61fe0c53
JF
711CYScope::CYScope(bool transparent, CYContext &context, CYStatement *&statements) :
712 transparent_(transparent),
daf22a65
JF
713 context_(context),
714 statements_(statements),
715 parent_(context.scope_)
716{
717 context_.scope_ = this;
718}
719
e4676127
JF
720CYScope::~CYScope() {
721}
722
daf22a65
JF
723void CYScope::Close() {
724 context_.scope_ = parent_;
725 Scope(context_, statements_);
726}
727
a86e34d0 728void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
61fe0c53 729 if (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch)
daf22a65 730 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
61fe0c53
JF
731 else if (parent_ != NULL)
732 parent_->Declare(context, identifier, flags);
a86e34d0
JF
733}
734
735CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
736 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
737 return *insert.first;
738}
739
6a981250 740void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
10711823 741 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
6a981250
JF
742 if (!insert.second) {
743 if ((*insert.first)->offset_ < identifier->offset_)
744 (*insert.first)->offset_ = identifier->offset_;
745 identifier->replace_ = *insert.first;
e013809d 746 (*insert.first)->usage_ += identifier->usage_ + 1;
029bc65b
JF
747 }
748}
749
10711823
JF
750namespace {
751 struct IdentifierOffset {
752 size_t offset_;
753 CYIdentifierFlags flags_;
e013809d 754 size_t usage_;
10711823
JF
755 CYIdentifier *identifier_;
756
e013809d
JF
757 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
758 offset_(identifier->offset_),
10711823 759 flags_(flags),
e013809d 760 usage_(identifier->usage_),
10711823
JF
761 identifier_(identifier)
762 {
763 }
764 };
765
766 struct IdentifierOffsetLess :
767 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
768 {
769 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
770 if (lhs.offset_ != rhs.offset_)
771 return lhs.offset_ < rhs.offset_;
772 if (lhs.flags_ != rhs.flags_)
773 return lhs.flags_ < rhs.flags_;
e013809d 774 /*if (lhs.usage_ != rhs.usage_)
a846a8cd 775 return lhs.usage_ < rhs.usage_;*/
10711823
JF
776 return lhs.identifier_ < rhs.identifier_;
777 }
778 };
779
780 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
781}
782
029bc65b 783void CYScope::Scope(CYContext &context, CYStatement *&statements) {
2c81c6df
JF
784 if (parent_ == NULL)
785 return;
786
029bc65b 787 CYDeclarations *last(NULL), *curr(NULL);
14ec9e00 788
10711823 789 IdentifierOffsets offsets;
6a981250 790
10711823 791 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
a846a8cd 792 if (i->second != CYIdentifierMagic)
e013809d 793 offsets.insert(IdentifierOffset(i->first, i->second));
10711823
JF
794
795 size_t offset(0);
796
797 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
798 if (i->flags_ == CYIdentifierVariable) {
799 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
029bc65b
JF
800 if (last == NULL)
801 last = next;
802 if (curr != NULL)
803 curr->SetNext(next);
804 curr = next;
805 }
029bc65b 806
10711823
JF
807 if (offset < i->offset_)
808 offset = i->offset_;
a846a8cd
JF
809 if (context.rename_.size() <= offset)
810 context.rename_.resize(offset + 1);
10711823 811
a846a8cd 812 CYIdentifierUsage &rename(context.rename_[offset++]);
e013809d
JF
813 i->identifier_->SetNext(rename.identifier_);
814 rename.identifier_ = i->identifier_;
815 rename.usage_ += i->identifier_->usage_ + 1;
6a981250
JF
816 }
817
029bc65b
JF
818 if (last != NULL) {
819 CYVar *var($ CYVar(last));
820 var->SetNext(statements);
821 statements = var;
822 }
823
6a981250
JF
824 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
825 if (internal_.find(*i) == internal_.end()) {
de9fc71b 826 //std::cout << *i << '=' << offset << std::endl;
6a981250
JF
827 if ((*i)->offset_ < offset)
828 (*i)->offset_ = offset;
829 parent_->Merge(context, *i);
830 }
029bc65b
JF
831}
832
4644480a
JF
833CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
834 size_t size(size_ + rhs->size_);
2eb8215d 835 char *value($ char[size + 1]);
4644480a
JF
836 memcpy(value, value_, size_);
837 memcpy(value + size_, rhs->value_, rhs->size_);
838 value[size] = '\0';
a14eb702 839 return $S(value, size);
4644480a
JF
840}
841
842CYNumber *CYString::Number(CYContext &context) {
843 // XXX: there is a precise algorithm for this
844 return NULL;
845}
846
847CYString *CYString::String(CYContext &context) {
848 return this;
849}
850
3b52fd1a
JF
851CYStatement *CYSwitch::Replace(CYContext &context) {
852 context.Replace(value_);
853 clauses_->Replace(context);
029bc65b 854 return this;
3b52fd1a
JF
855}
856
857CYExpression *CYThis::Replace(CYContext &context) {
a0be43fc
JF
858 if (context.this_ != NULL)
859 return $V(context.this_->Identifier(context));
029bc65b 860 return this;
3b52fd1a
JF
861}
862
37954781
JF
863namespace cy {
864namespace Syntax {
865
866CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a 867 context.Replace(value_);
029bc65b 868 return this;
3b52fd1a
JF
869}
870
37954781
JF
871} }
872
3b52fd1a 873CYExpression *CYTrivial::Replace(CYContext &context) {
029bc65b 874 return this;
3b52fd1a
JF
875}
876
4644480a
JF
877CYNumber *CYTrue::Number(CYContext &context) {
878 return $D(1);
879}
880
881CYString *CYTrue::String(CYContext &context) {
882 return $S("true");
883}
884
37954781
JF
885namespace cy {
886namespace Syntax {
887
888CYStatement *Try::Replace(CYContext &context) {
3b52fd1a
JF
889 code_.Replace(context);
890 catch_->Replace(context);
891 finally_->Replace(context);
029bc65b 892 return this;
3b52fd1a
JF
893}
894
37954781
JF
895} }
896
9a39f705
JF
897CYExpression *CYTypeArrayOf::Replace_(CYContext &context, CYExpression *type) {
898 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
690cf1a8
JF
899}
900
3fe16be7
JF
901CYExpression *CYTypeBlockWith::Replace_(CYContext &context, CYExpression *type) {
902 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
903}
904
9a39f705
JF
905CYExpression *CYTypeConstant::Replace_(CYContext &context, CYExpression *type) {
906 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
690cf1a8
JF
907}
908
60097023 909CYStatement *CYTypeDefinition::Replace(CYContext &context) {
84759b5b 910 return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
60097023
JF
911}
912
03db6a67
JF
913CYExpression *CYTypeError::Replace(CYContext &context) {
914 _assert(false);
915 return NULL;
916}
917
9a39f705
JF
918CYExpression *CYTypeModifier::Replace(CYContext &context, CYExpression *type) { $T(type)
919 return Replace_(context, type);
920}
921
922CYExpression *CYTypeFunctionWith::Replace_(CYContext &context, CYExpression *type) {
923 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
924}
925
3fe283c5
JF
926CYExpression *CYTypeLong::Replace(CYContext &context) {
927 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
928}
929
9a39f705
JF
930CYExpression *CYTypePointerTo::Replace_(CYContext &context, CYExpression *type) {
931 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
663c538f
JF
932}
933
3fe283c5
JF
934CYExpression *CYTypeShort::Replace(CYContext &context) {
935 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
936}
937
938CYExpression *CYTypeSigned::Replace(CYContext &context) {
939 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
940}
941
942CYExpression *CYTypeUnsigned::Replace(CYContext &context) {
943 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
944}
945
946CYExpression *CYTypeVariable::Replace(CYContext &context) {
947 return $V(name_);
948}
949
950CYExpression *CYTypeVoid::Replace(CYContext &context) {
951 return $N1($V("Type"), $ CYString("v"));
952}
953
9a39f705
JF
954CYExpression *CYTypeVolatile::Replace_(CYContext &context, CYExpression *type) {
955 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
690cf1a8
JF
956}
957
9a39f705 958CYExpression *CYTypedIdentifier::Replace(CYContext &context) {
3fe283c5 959 return modifier_->Replace(context, specifier_->Replace(context));
690cf1a8
JF
960}
961
663c538f 962CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
9a39f705 963 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
663c538f
JF
964}
965
690cf1a8
JF
966CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
967 return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
968}
969
970CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
a2f3ecab 971 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
690cf1a8
JF
972}
973
3b52fd1a 974CYStatement *CYVar::Replace(CYContext &context) {
15b88a33 975 declarations_->Replace(context);
fd5cdf97
JF
976 if (CYCompound *compound = declarations_->Compound(context))
977 return $E(compound);
978 return $ CYEmpty();
3b52fd1a
JF
979}
980
981CYExpression *CYVariable::Replace(CYContext &context) {
577bfbfa 982 context.Replace(name_);
029bc65b 983 return this;
3b52fd1a
JF
984}
985
986CYStatement *CYWhile::Replace(CYContext &context) {
987 context.Replace(test_);
988 context.Replace(code_);
029bc65b 989 return this;
3b52fd1a
JF
990}
991
992CYStatement *CYWith::Replace(CYContext &context) {
993 context.Replace(scope_);
994 context.Replace(code_);
029bc65b 995 return this;
3b52fd1a
JF
996}
997
998CYExpression *CYWord::ClassName(CYContext &context, bool object) {
999 CYString *name($S(this));
1000 if (object)
1001 return $C1($V("objc_getClass"), name);
1002 else
1003 return name;
1004}