]> git.saurik.com Git - cycript.git/blame - Output.cpp
Drastic improvements to memory management, handle many more minimization corner cases...
[cycript.git] / Output.cpp
CommitLineData
5999c315
JF
1#include "Parser.hpp"
2
3#include <iostream>
4#include <iomanip>
5
b09da87b
JF
6_finline CYFlags operator ~(CYFlags rhs) {
7 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
8}
9
10_finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
11 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
12}
13
14_finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
15 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
16}
17
18_finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
19 return lhs = lhs | rhs;
20}
21
22_finline CYFlags CYLeft(CYFlags flags) {
23 return flags & ~CYNoTrailer;
24}
25
26_finline CYFlags CYCenter(CYFlags flags) {
27 return flags & CYNoIn;
28}
29
30_finline CYFlags CYRight(CYFlags flags) {
31 return flags & (CYNoIn | CYNoTrailer);
32}
33
34bool CYFalse::Value() const {
35 return false;
36}
37
38bool CYTrue::Value() const {
39 return true;
40}
41
d35a3b07
JF
42#define CYPA 16
43
b09da87b
JF
44void CYAddressOf::Output(std::ostream &out, CYFlags flags) const {
45 rhs_->Output(out, 1, CYLeft(flags));
d35a3b07 46 out << ".$()";
5999c315
JF
47}
48
d35a3b07
JF
49void CYArgument::Output(std::ostream &out) const {
50 if (name_ != NULL) {
5999c315
JF
51 out << *name_;
52 if (value_ != NULL)
53 out << ":";
54 }
d35a3b07 55 if (value_ != NULL)
b09da87b 56 value_->Output(out, CYPA, CYNoFlags);
5999c315 57 if (next_ != NULL) {
d35a3b07
JF
58 if (next_->name_ == NULL)
59 out << ',';
60 else
61 out << ' ';
62 next_->Output(out);
5999c315
JF
63 }
64}
65
b09da87b 66void CYArray::Output(std::ostream &out, CYFlags flags) const {
5befe15e
JF
67 out << '[';
68 if (elements_ != NULL)
69 elements_->Output(out);
70 out << ']';
71}
72
b09da87b
JF
73void CYAssignment::Output(std::ostream &out, CYFlags flags) const {
74 lhs_->Output(out, Precedence() - 1, CYLeft(flags));
d35a3b07 75 out << Operator();
b09da87b 76 rhs_->Output(out, Precedence(), CYRight(flags));
d35a3b07
JF
77}
78
b09da87b
JF
79void CYBoolean::Output(std::ostream &out, CYFlags flags) const {
80 if ((flags & CYNoLeader) != 0)
81 out << ' ';
5999c315 82 out << (Value() ? "true" : "false");
b09da87b
JF
83 if ((flags & CYNoTrailer) != 0)
84 out << ' ';
5999c315
JF
85}
86
87void CYBreak::Output(std::ostream &out) const {
88 out << "break";
89 if (label_ != NULL)
90 out << ' ' << *label_;
91 out << ';';
92}
93
b09da87b
JF
94void CYCall::Output(std::ostream &out, CYFlags flags) const {
95 function_->Output(out, Precedence(), CYLeft(flags));
d35a3b07 96 out << '(';
5999c315 97 if (arguments_ != NULL)
d35a3b07 98 arguments_->Output(out);
5999c315
JF
99 out << ')';
100}
101
102void CYCatch::Output(std::ostream &out) const {
103 out << "catch(" << *name_ << ')';
104 code_->Output(out, true);
105}
106
b09da87b
JF
107void CYClass::Output(std::ostream &out) const {
108 out << "(function($cys,$cyc,$cym,$cyn,$cyt){";
109 out << "$cyc=objc_allocateClassPair($cys,\"" << *name_ << "\",0);";
110 out << "$cym=object_getClass($cyc);";
111 if (fields_ != NULL)
112 fields_->Output(out);
113 if (messages_ != NULL)
114 messages_->Output(out);
115 out << "objc_registerClassPair($cyc);";
116 out << "})(";
117 if (super_ != NULL)
118 super_->Output(out, CYPA, CYNoFlags);
119 else
120 out << "null";
121 out << ");";
122}
123
124void CYCondition::Output(std::ostream &out, CYFlags flags) const {
125 test_->Output(out, Precedence() - 1, CYLeft(flags));
d35a3b07 126 out << '?';
5999c315 127 if (true_ != NULL)
b09da87b 128 true_->Output(out, CYPA, CYNoFlags);
d35a3b07 129 out << ':';
b09da87b 130 false_->Output(out, CYPA, CYRight(flags));
5999c315
JF
131}
132
133void CYContinue::Output(std::ostream &out) const {
134 out << "continue";
135 if (label_ != NULL)
136 out << ' ' << *label_;
137 out << ';';
138}
139
140void CYClause::Output(std::ostream &out) const {
d35a3b07
JF
141 if (case_ != NULL) {
142 out << "case";
b09da87b 143 case_->Output(out, CYNoFlags);
d35a3b07 144 } else
5999c315
JF
145 out << "default";
146 out << ':';
147 if (code_ != NULL)
148 code_->Output(out, false);
149 out << *next_;
150}
151
b09da87b 152// XXX: deal with NoIn
5999c315
JF
153void CYDeclaration::Part(std::ostream &out) const {
154 out << "var ";
155 Output(out);
156}
157
158void CYDeclaration::Output(std::ostream &out) const {
159 out << *identifier_;
d35a3b07
JF
160 if (initialiser_ != NULL) {
161 out << '=';
b09da87b 162 initialiser_->Output(out, CYPA, CYNoFlags);
d35a3b07 163 }
5999c315
JF
164}
165
b09da87b 166// XXX: deal with NoIn
5999c315
JF
167void CYDeclarations::Part(std::ostream &out) const {
168 out << "var ";
d35a3b07 169
5999c315 170 const CYDeclarations *declaration(this);
d35a3b07
JF
171 output:
172 out << *declaration->declaration_;
173 declaration = declaration->next_;
174
175 if (declaration != NULL) {
176 out << ',';
177 goto output;
178 }
5999c315
JF
179}
180
181void CYDeclarations::Output(std::ostream &out) const {
182 Part(out);
183 out << ';';
184}
185
186void CYDoWhile::Output(std::ostream &out) const {
b09da87b 187 // XXX: extra space character!
5999c315
JF
188 out << "do ";
189 code_->Output(out, false);
d35a3b07 190 out << "while(";
b09da87b
JF
191 test_->Output(out, CYNoFlags);
192 out << ')';
5999c315
JF
193}
194
5befe15e 195void CYElement::Output(std::ostream &out) const {
5999c315 196 if (value_ != NULL)
b09da87b 197 value_->Output(out, CYPA, CYNoFlags);
5befe15e 198 if (next_ != NULL || value_ == NULL)
5999c315 199 out << ',';
5befe15e
JF
200 if (next_ != NULL)
201 next_->Output(out);
5999c315
JF
202}
203
204void CYEmpty::Output(std::ostream &out) const {
205 out << ';';
206}
207
208void CYEmpty::Output(std::ostream &out, bool block) const {
209 if (next_ != NULL)
210 CYSource::Output(out, block);
211 else
212 out << "{}";
213}
214
215void CYExpress::Output(std::ostream &out) const {
b09da87b 216 expression_->Output(out, CYNoFunction | CYNoBrace);
5999c315
JF
217 out << ';';
218}
219
220void CYExpression::Part(std::ostream &out) const {
d35a3b07 221 // XXX: this should handle LeftHandSideExpression
b09da87b 222 Output(out, CYNoIn);
d35a3b07
JF
223}
224
b09da87b 225void CYCompound::Output(std::ostream &out, CYFlags flags) const {
d35a3b07 226 if (CYExpression *expression = expressions_)
b09da87b
JF
227 if (CYExpression *next = expression->next_)
228 expression->Output(out, flags);
229 else {
230 expression->Output(out, CYLeft(flags));
231 CYFlags center(CYCenter(flags));
232 while (next != NULL) {
233 out << ',';
234 next = expression->next_;
235 CYFlags right(next != NULL ? center : CYRight(flags));
236 expression->Output(out, right);
237 }
d35a3b07 238 }
5999c315
JF
239}
240
b09da87b
JF
241void CYExpression::Output(std::ostream &out, unsigned precedence, CYFlags flags) const {
242 if (precedence < Precedence()) {
5999c315 243 out << '(';
b09da87b 244 Output(out, CYNoFlags);
5999c315 245 out << ')';
b09da87b
JF
246 } else
247 Output(out, flags);
248}
249
250void CYField::Output(std::ostream &out) const {
251 // XXX: implement!
5999c315
JF
252}
253
254void CYFor::Output(std::ostream &out) const {
255 out << "for(";
256 if (initialiser_ != NULL)
257 initialiser_->Part(out);
258 out << ';';
259 if (test_ != NULL)
b09da87b 260 test_->Output(out, CYNoFlags);
5999c315
JF
261 out << ';';
262 if (increment_ != NULL)
b09da87b 263 increment_->Output(out, CYNoFlags);
5999c315
JF
264 out << ')';
265 code_->Output(out, false);
266}
267
268void CYForIn::Output(std::ostream &out) const {
269 out << "for(";
270 initialiser_->Part(out);
b09da87b
JF
271 // XXX: deal with this space character!
272 out << ' ';
273 out << "in";
274 set_->Output(out, CYNoLeader);
5999c315
JF
275 out << ')';
276 code_->Output(out, false);
277}
278
279void CYFunction::Output(std::ostream &out) const {
b09da87b
JF
280 CYLambda::Output(out, CYNoFlags);
281}
282
283void CYFunctionParameter::Output(std::ostream &out) const {
284 out << *name_;
285 if (next_ != NULL) {
286 out << ',';
287 out << *next_;
288 }
5999c315
JF
289}
290
291void CYIf::Output(std::ostream &out) const {
d35a3b07 292 out << "if(";
b09da87b 293 test_->Output(out, CYNoFlags);
d35a3b07 294 out << ')';
5999c315
JF
295 true_->Output(out, true);
296 if (false_ != NULL) {
297 out << "else ";
298 false_->Output(out, false);
299 }
300}
301
b09da87b
JF
302void CYIndirect::Output(std::ostream &out, CYFlags flags) const {
303 rhs_->Output(out, 1, CYLeft(flags));
d35a3b07 304 out << "[0]";
5999c315
JF
305}
306
b09da87b
JF
307void CYInfix::Output(std::ostream &out, CYFlags flags) const {
308 const char *name(Operator());
309 bool protect((flags & CYNoIn) != 0 && strcmp(name, "in"));
310 if (protect)
311 out << '(';
312 bool alphabetic(Alphabetic());
313 CYFlags left(protect ? CYNoFlags : CYLeft(flags));
314 if (alphabetic)
315 left |= CYNoTrailer;
316 lhs_->Output(out, Precedence(), left);
317 out << name;
318 CYFlags right(protect ? CYNoFlags : CYRight(flags));
319 if (alphabetic)
320 right |= CYNoLeader;
321 rhs_->Output(out, Precedence() - 1, right);
322 if (protect)
323 out << ')';
5999c315
JF
324}
325
b09da87b
JF
326void CYLambda::Output(std::ostream &out, CYFlags flags) const {
327 bool protect((flags & CYNoFunction) != 0);
328 if (protect)
329 out << '(';
5999c315
JF
330 out << "function";
331 if (name_ != NULL)
332 out << ' ' << *name_;
333 out << '(';
334 if (parameters_ != NULL)
335 out << *parameters_;
b09da87b
JF
336 out << "){";
337 if (body_ != NULL)
338 body_->Show(out);
339 out << '}';
340 if (protect)
341 out << ')';
5999c315
JF
342}
343
b09da87b
JF
344void CYMember::Output(std::ostream &out, CYFlags flags) const {
345 object_->Output(out, Precedence(), CYLeft(flags));
dea834b0
JF
346 if (const char *word = property_->Word())
347 out << '.' << word;
348 else {
349 out << '[';
b09da87b 350 property_->Output(out, CYNoFlags);
dea834b0
JF
351 out << ']';
352 }
5999c315
JF
353}
354
355void CYMessage::Output(std::ostream &out) const {
b09da87b
JF
356 out << "$cyn=new Selector(\"";
357 for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
358 if (parameter->tag_ != NULL) {
359 out << *parameter->tag_;
360 if (parameter->name_ != NULL)
5999c315
JF
361 out << ':';
362 }
b09da87b
JF
363 out << "\");";
364 out << "$cyt=$cyn.type($cys," << (instance_ ? "true" : "false") << ");";
365 out << "class_addMethod($cy" << (instance_ ? 'c' : 'm') << ",$cyn,";
366 out << "new Functor(function(";
367 bool comma(false);
368 for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
369 if (parameter->name_ != NULL) {
370 if (comma)
371 out << ',';
372 else
373 comma = true;
374 out << *parameter->name_;
d35a3b07 375 }
b09da87b
JF
376 out << "){";
377 if (body_ != NULL)
378 body_->Show(out);
379 out << "},$cyt),$cyt);";
5999c315
JF
380}
381
b09da87b 382void CYNew::Output(std::ostream &out, CYFlags flags) const {
d35a3b07 383 out << "new";
b09da87b 384 constructor_->Output(out, Precedence(), CYCenter(flags) | CYNoLeader);
d35a3b07 385 out << '(';
5999c315 386 if (arguments_ != NULL)
d35a3b07 387 arguments_->Output(out);
5999c315
JF
388 out << ')';
389}
390
b09da87b
JF
391void CYNull::Output(std::ostream &out, CYFlags flags) const {
392 if ((flags & CYNoLeader) != 0)
393 out << ' ';
5999c315 394 CYWord::Output(out);
b09da87b
JF
395 if ((flags & CYNoTrailer) != 0)
396 out << ' ';
5999c315
JF
397}
398
b09da87b
JF
399void CYNumber::Output(std::ostream &out, CYFlags flags) const {
400 if ((flags & CYNoLeader) != 0)
401 out << ' ';
5999c315
JF
402 // XXX: this is not a useful formatting
403 out << Value();
b09da87b
JF
404 if ((flags & CYNoTrailer) != 0)
405 out << ' ';
5999c315
JF
406}
407
b09da87b
JF
408void CYObject::Output(std::ostream &out, CYFlags flags) const {
409 bool protect((flags & CYNoBrace) != 0);
410 if (protect)
411 out << '(';
693d501b
JF
412 out << '{';
413 if (property_ != NULL)
414 property_->Output(out);
415 out << '}';
b09da87b
JF
416 if (protect)
417 out << ')';
693d501b
JF
418}
419
b09da87b
JF
420void CYPostfix::Output(std::ostream &out, CYFlags flags) const {
421 lhs_->Output(out, Precedence(), CYLeft(flags));
d35a3b07 422 out << Operator();
5999c315
JF
423}
424
b09da87b
JF
425void CYPrefix::Output(std::ostream &out, CYFlags flags) const {
426 bool alphabetic(Alphabetic());
d35a3b07 427 out << Operator();
b09da87b
JF
428 CYFlags right(CYRight(flags));
429 if (alphabetic)
430 right |= CYNoLeader;
431 rhs_->Output(out, Precedence(), right);
5999c315
JF
432}
433
693d501b 434void CYProperty::Output(std::ostream &out) const {
d35a3b07 435 out << *name_ << ':';
b09da87b 436 value_->Output(out, CYPA, CYNoFlags);
5999c315
JF
437 if (next_ != NULL) {
438 out << ',';
693d501b 439 next_->Output(out);
5999c315 440 }
5999c315
JF
441}
442
443void CYReturn::Output(std::ostream &out) const {
444 out << "return";
b09da87b
JF
445 if (value_ != NULL)
446 value_->Output(out, CYNoLeader);
5999c315
JF
447 out << ';';
448}
449
b09da87b
JF
450void CYSelector::Output(std::ostream &out, CYFlags flags) const {
451 out << "new Selector(\"";
62014ea9
JF
452 if (name_ != NULL)
453 name_->Output(out);
dea834b0 454 out << "\")";
e7ed5354
JF
455}
456
62014ea9
JF
457void CYSelectorPart::Output(std::ostream &out) const {
458 if (name_ != NULL)
459 out << *name_;
460 if (value_)
461 out << ':';
462 if (next_ != NULL)
463 next_->Output(out);
464}
465
b09da87b
JF
466void CYSend::Output(std::ostream &out, CYFlags flags) const {
467 out << "objc_msgSend(";
468 self_->Output(out, CYPA, CYNoFlags);
469 out << ",\"";
470 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
471 if (argument->name_ != NULL) {
472 out << *argument->name_;
473 if (argument->value_ != NULL)
474 out << ':';
475 }
476 out << "\"";
477 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
478 if (argument->value_ != NULL) {
479 out << ",";
480 argument->value_->Output(out, CYPA, CYNoFlags);
481 }
482 out << ')';
483}
484
b1ff2d78 485void CYSource::Show(std::ostream &out) const {
5999c315 486 for (const CYSource *next(this); next != NULL; next = next->next_)
b09da87b 487 next->Output(out);
5999c315
JF
488}
489
490void CYSource::Output(std::ostream &out, bool block) const {
491 if (!block && next_ == NULL)
492 Output(out);
493 else {
494 out << '{';
b1ff2d78 495 Show(out);
5999c315
JF
496 out << '}';
497 }
498}
499
b09da87b 500void CYString::Output(std::ostream &out, CYFlags flags) const {
5999c315
JF
501 out << '\"';
502 for (const char *value(value_), *end(value_ + size_); value != end; ++value)
503 switch (*value) {
504 case '"': out << "\\\""; break;
505 case '\\': out << "\\\\"; break;
506 case '\b': out << "\\b"; break;
507 case '\f': out << "\\f"; break;
508 case '\n': out << "\\n"; break;
509 case '\r': out << "\\r"; break;
510 case '\t': out << "\\t"; break;
511 case '\v': out << "\\v"; break;
512
513 default:
514 if (*value < 0x20 || *value >= 0x7f)
515 out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
516 else
517 out << *value;
518 }
519 out << '\"';
520}
521
522void CYSwitch::Output(std::ostream &out) const {
d35a3b07 523 out << "switch(";
b09da87b 524 value_->Output(out, CYNoFlags);
d35a3b07 525 out << "){";
5999c315
JF
526 if (clauses_ != NULL)
527 out << *clauses_;
528 out << '}';
529}
530
b09da87b
JF
531void CYThis::Output(std::ostream &out, CYFlags flags) const {
532 if ((flags & CYNoLeader) != 0)
533 out << ' ';
5999c315 534 CYWord::Output(out);
b09da87b
JF
535 if ((flags & CYNoTrailer) != 0)
536 out << ' ';
5999c315
JF
537}
538
539void CYThrow::Output(std::ostream &out) const {
d35a3b07 540 out << "throw";
b09da87b
JF
541 if (value_ != NULL)
542 value_->Output(out, CYNoLeader);
5999c315
JF
543 out << ';';
544}
545
546void CYTry::Output(std::ostream &out) const {
547 out << "try";
548 try_->Output(out, true);
549 if (catch_ != NULL)
550 out << catch_;
551 if (finally_ != NULL) {
552 out << "finally";
553 finally_->Output(out, true);
554 }
555}
556
b09da87b
JF
557void CYVariable::Output(std::ostream &out, CYFlags flags) const {
558 bool protect((flags & CYNoLeader) != 0);
559 if (protect)
560 out << '(';
5999c315 561 out << *name_;
b09da87b
JF
562 if (protect)
563 out << ')';
5999c315
JF
564}
565
566void CYWhile::Output(std::ostream &out) const {
d35a3b07 567 out << "while(";
b09da87b 568 test_->Output(out, CYNoFlags);
d35a3b07 569 out << ')';
5999c315
JF
570 code_->Output(out, false);
571}
572
573void CYWith::Output(std::ostream &out) const {
d35a3b07 574 out << "with(";
b09da87b 575 scope_->Output(out, CYNoFlags);
d35a3b07 576 out << ')';
5999c315
JF
577 code_->Output(out, false);
578}
579
580void CYWord::Output(std::ostream &out) const {
581 out << Value();
582}