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