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