]>
git.saurik.com Git - cycript.git/blob - Output.cpp
a5d97430c3e2b0cc9a2601099082b5374ecc1e56
6 #include <objc/runtime.h>
9 _finline CYFlags
operator ~(CYFlags rhs
) {
10 return static_cast<CYFlags
>(~static_cast<unsigned>(rhs
));
13 _finline CYFlags
operator &(CYFlags lhs
, CYFlags rhs
) {
14 return static_cast<CYFlags
>(static_cast<unsigned>(lhs
) & static_cast<unsigned>(rhs
));
17 _finline CYFlags
operator |(CYFlags lhs
, CYFlags rhs
) {
18 return static_cast<CYFlags
>(static_cast<unsigned>(lhs
) | static_cast<unsigned>(rhs
));
21 _finline CYFlags
&operator |=(CYFlags
&lhs
, CYFlags rhs
) {
22 return lhs
= lhs
| rhs
;
25 _finline CYFlags
CYLeft(CYFlags flags
) {
26 return flags
& ~CYNoTrailer
;
29 _finline CYFlags
CYCenter(CYFlags flags
) {
30 return flags
& CYNoIn
;
33 _finline CYFlags
CYRight(CYFlags flags
) {
34 return flags
& (CYNoIn
| CYNoTrailer
);
37 bool CYFalse::Value() const {
41 bool CYTrue::Value() const {
47 void CYAddressOf::Output(std::ostream
&out
, CYFlags flags
) const {
48 rhs_
->Output(out
, 1, CYLeft(flags
));
52 void CYArgument::Output(std::ostream
&out
) const {
59 value_
->Output(out
, CYPA
, CYNoFlags
);
61 if (next_
->name_
== NULL
)
69 void CYArray::Output(std::ostream
&out
, CYFlags flags
) const {
71 if (elements_
!= NULL
)
72 elements_
->Output(out
);
76 void CYArrayComprehension::Output(std::ostream
&out
, CYFlags flags
) const {
77 // XXX: I don't necc. need the ()s
78 out
<< "(function($cyv";
79 for (CYComprehension
*comprehension(comprehensions_
); comprehension
!= NULL
; comprehension
= comprehension
->next_
)
80 if (const char *name
= comprehension
->Name())
84 comprehensions_
->Output(out
);
86 expression_
->Output(out
, CYPA
, CYNoFlags
);
88 for (CYComprehension
*comprehension(comprehensions_
); comprehension
!= NULL
; comprehension
= comprehension
->next_
)
89 comprehension
->End_(out
);
90 out
<< "return $cyv;";
94 void CYAssignment::Output(std::ostream
&out
, CYFlags flags
) const {
95 lhs_
->Output(out
, Precedence() - 1, CYLeft(flags
));
97 rhs_
->Output(out
, Precedence(), CYRight(flags
));
100 void CYBlock::Output(std::ostream
&out
) const {
101 for (CYSource
*statement(statements_
); statement
!= NULL
; statement
= statement
->next_
)
102 statement
->Output(out
);
105 void CYBoolean::Output(std::ostream
&out
, CYFlags flags
) const {
106 if ((flags
& CYNoLeader
) != 0)
108 out
<< (Value() ? "true" : "false");
109 if ((flags
& CYNoTrailer
) != 0)
113 void CYBreak::Output(std::ostream
&out
) const {
116 out
<< ' ' << *label_
;
120 void CYCall::Output(std::ostream
&out
, CYFlags flags
) const {
121 function_
->Output(out
, Precedence(), CYLeft(flags
));
123 if (arguments_
!= NULL
)
124 arguments_
->Output(out
);
128 void CYCatch::Output(std::ostream
&out
) const {
129 out
<< "catch(" << *name_
<< ')';
130 code_
->Output(out
, true);
133 void CYCategory::Output(std::ostream
&out
) const {
134 out
<< "(function($cys,$cyp,$cyc,$cyn,$cyt){";
135 out
<< "$cyp=object_getClass($cys);";
137 if (messages_
!= NULL
)
138 messages_
->Output(out
, true);
140 name_
->ClassName(out
, true);
144 void CYClass::Output(std::ostream
&out
) const {
149 void CYClass::Output(std::ostream
&out
, CYFlags flags
) const {
150 // XXX: I don't necc. need the ()s
151 out
<< "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
152 out
<< "$cyp=object_getClass($cys);";
153 out
<< "$cyc=objc_allocateClassPair($cys,";
155 name_
->ClassName(out
, false);
157 out
<< "$cyq(\"CY$\")";
159 out
<< "$cym=object_getClass($cyc);";
161 fields_
->Output(out
);
162 if (messages_
!= NULL
)
163 messages_
->Output(out
, false);
164 out
<< "objc_registerClassPair($cyc);";
165 out
<< "return $cyc;";
168 super_
->Output(out
, CYPA
, CYNoFlags
);
174 void CYCompound::Output(std::ostream
&out
, CYFlags flags
) const {
175 if (CYExpression
*expression
= expressions_
)
176 if (CYExpression
*next
= expression
->next_
) {
177 expression
->Output(out
, CYLeft(flags
));
178 CYFlags
center(CYCenter(flags
));
179 while (next
!= NULL
) {
182 next
= expression
->next_
;
183 CYFlags
right(next
!= NULL
? center
: CYRight(flags
));
184 expression
->Output(out
, right
);
187 expression
->Output(out
, flags
);
190 void CYComprehension::Output(std::ostream
&out
) const {
196 void CYCondition::Output(std::ostream
&out
, CYFlags flags
) const {
197 test_
->Output(out
, Precedence() - 1, CYLeft(flags
));
200 true_
->Output(out
, CYPA
, CYNoFlags
);
202 false_
->Output(out
, CYPA
, CYRight(flags
));
205 void CYContinue::Output(std::ostream
&out
) const {
208 out
<< ' ' << *label_
;
212 void CYClause::Output(std::ostream
&out
) const {
215 case_
->Output(out
, CYNoLeader
);
220 code_
->Output(out
, false);
225 const char *CYDeclaration::ForEachIn() const {
226 return identifier_
->Value();
229 void CYDeclaration::ForIn(std::ostream
&out
, CYFlags flags
) const {
230 if ((flags
& CYNoLeader
) != 0)
233 Output(out
, CYRight(flags
));
236 void CYDeclaration::ForEachIn(std::ostream
&out
) const {
240 void CYDeclaration::Output(std::ostream
&out
, CYFlags flags
) const {
242 if (initialiser_
!= NULL
) {
244 initialiser_
->Output(out
, CYPA
, CYRight(flags
));
245 } else if ((flags
& CYNoTrailer
) != 0)
249 void CYDeclarations::For(std::ostream
&out
) const {
254 void CYDeclarations::Output(std::ostream
&out
, CYFlags flags
) const {
255 const CYDeclarations
*declaration(this);
257 CYDeclarations
*next(declaration
->next_
);
258 CYFlags
right(next
== NULL
? CYRight(flags
) : CYCenter(flags
));
259 declaration
->declaration_
->Output(out
, right
);
268 void CYDirectMember::Output(std::ostream
&out
, CYFlags flags
) const {
269 object_
->Output(out
, Precedence(), CYLeft(flags
));
270 if (const char *word
= property_
->Word())
274 property_
->Output(out
, CYNoFlags
);
279 void CYDoWhile::Output(std::ostream
&out
) const {
280 // XXX: extra space character!
282 code_
->Output(out
, false);
284 test_
->Output(out
, CYNoFlags
);
288 void CYElement::Output(std::ostream
&out
) const {
290 value_
->Output(out
, CYPA
, CYNoFlags
);
291 if (next_
!= NULL
|| value_
== NULL
)
297 void CYEmpty::Output(std::ostream
&out
) const {
301 void CYEmpty::Output(std::ostream
&out
, bool block
) const {
303 CYSource::Output(out
, block
);
308 void CYExpress::Output(std::ostream
&out
) const {
309 expression_
->Output(out
, CYNoBF
);
313 void CYExpression::ClassName(std::ostream
&out
, bool object
) const {
314 Output(out
, CYPA
, CYNoFlags
);
317 const char *CYExpression::ForEachIn() const {
321 void CYExpression::For(std::ostream
&out
) const {
325 void CYExpression::ForEachIn(std::ostream
&out
) const {
326 // XXX: this should handle LeftHandSideExpression
327 Output(out
, CYPA
, CYNoFlags
);
330 void CYExpression::ForIn(std::ostream
&out
, CYFlags flags
) const {
331 // XXX: this should handle LeftHandSideExpression
335 void CYExpression::Output(std::ostream
&out
, unsigned precedence
, CYFlags flags
) const {
336 if (precedence
< Precedence()) {
338 Output(out
, CYNoFlags
);
344 void CYField::Output(std::ostream
&out
) const {
348 void CYFor::Output(std::ostream
&out
) const {
350 if (initialiser_
!= NULL
)
351 initialiser_
->For(out
);
354 test_
->Output(out
, CYNoFlags
);
356 if (increment_
!= NULL
)
357 increment_
->Output(out
, CYNoFlags
);
359 code_
->Output(out
, false);
362 void CYForEachIn::Output(std::ostream
&out
) const {
363 out
<< "with({$cys:0,$cyt:0}){";
366 set_
->Output(out
, CYPA
, CYNoFlags
);
369 out
<< "for($cyt in $cys){";
371 initialiser_
->ForEachIn(out
);
372 out
<< "=$cys[$cyt];";
381 void CYForEachInComprehension::Begin_(std::ostream
&out
) const {
382 out
<< "(function($cys){";
384 set_
->Output(out
, CYPA
, CYNoFlags
);
387 out
<< "for(" << *name_
<< " in $cys){";
388 out
<< *name_
<< "=$cys[" << *name_
<< "];";
391 void CYForEachInComprehension::End_(std::ostream
&out
) const {
395 void CYForIn::Output(std::ostream
&out
) const {
397 initialiser_
->ForIn(out
, CYNoIn
| CYNoTrailer
);
399 set_
->Output(out
, CYNoLeader
);
401 code_
->Output(out
, false);
404 void CYForInComprehension::Begin_(std::ostream
&out
) const {
405 out
<< "for(" << *name_
<< " in";
406 set_
->Output(out
, CYNoLeader
);
410 void CYFunction::Output(std::ostream
&out
) const {
411 CYLambda::Output(out
, CYNoFlags
);
414 void CYFunctionParameter::Output(std::ostream
&out
) const {
422 void CYIf::Output(std::ostream
&out
) const {
424 test_
->Output(out
, CYNoFlags
);
426 true_
->Output(out
, true);
427 if (false_
!= NULL
) {
429 false_
->Output(out
, false);
433 void CYIfComprehension::Begin_(std::ostream
&out
) const {
435 test_
->Output(out
, CYNoFlags
);
439 void CYIndirect::Output(std::ostream
&out
, CYFlags flags
) const {
440 rhs_
->Output(out
, 1, CYLeft(flags
));
444 void CYIndirectMember::Output(std::ostream
&out
, CYFlags flags
) const {
445 object_
->Output(out
, Precedence(), CYLeft(flags
));
447 if (const char *word
= property_
->Word())
451 property_
->Output(out
, CYNoFlags
);
456 void CYInfix::Output(std::ostream
&out
, CYFlags flags
) const {
457 const char *name(Operator());
458 bool protect((flags
& CYNoIn
) != 0 && strcmp(name
, "in"));
461 bool alphabetic(Alphabetic());
462 CYFlags
left(protect
? CYNoFlags
: CYLeft(flags
));
465 lhs_
->Output(out
, Precedence(), left
);
467 CYFlags
right(protect
? CYNoFlags
: CYRight(flags
));
470 if (strcmp(name
, "-") == 0)
472 rhs_
->Output(out
, Precedence() - 1, right
);
477 void CYLambda::Output(std::ostream
&out
, CYFlags flags
) const {
478 bool protect((flags
& CYNoFunction
) != 0);
481 else if ((flags
& CYNoLeader
) != 0)
485 out
<< ' ' << *name_
;
487 if (parameters_
!= NULL
)
497 void CYLet::Output(std::ostream
&out
) const {
499 declarations_
->Output(out
, CYNoFlags
);
501 if (statements_
!= NULL
)
502 statements_
->Show(out
);
506 void CYMessage::Output(std::ostream
&out
, bool replace
) const {
508 next_
->Output(out
, replace
);
509 out
<< "$cyn=new Selector(\"";
510 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
511 if (parameter
->tag_
!= NULL
) {
512 out
<< *parameter
->tag_
;
513 if (parameter
->name_
!= NULL
)
517 out
<< "$cyt=$cyn.type($cy" << (instance_
? 's' : 'p') << ");";
518 out
<< "class_" << (replace
? "replace" : "add") << "Method($cy" << (instance_
? 'c' : 'm') << ",$cyn,";
519 out
<< "new Functor(function(self,_cmd";
520 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
521 if (parameter
->name_
!= NULL
)
522 out
<< ',' << *parameter
->name_
;
523 out
<< "){return function(){";
526 out
<< "}.call(self);},$cyt),$cyt);";
529 void CYNew::Output(std::ostream
&out
, CYFlags flags
) const {
530 if ((flags
& CYNoLeader
) != 0)
533 constructor_
->Output(out
, Precedence(), CYCenter(flags
) | CYNoLeader
);
535 if (arguments_
!= NULL
)
536 arguments_
->Output(out
);
540 void CYNull::Output(std::ostream
&out
, CYFlags flags
) const {
541 if ((flags
& CYNoLeader
) != 0)
544 if ((flags
& CYNoTrailer
) != 0)
548 void CYNumber::Output(std::ostream
&out
, CYFlags flags
) const {
549 double value(Value());
550 if ((flags
& CYNoLeader
) != 0 || value
< 0 && (flags
& CYNoHyphen
) != 0)
552 // XXX: decide on correct precision
553 out
<< std::setprecision(9) << value
;
554 if ((flags
& CYNoTrailer
) != 0)
558 void CYNumber::PropertyName(std::ostream
&out
) const {
562 void CYObject::Output(std::ostream
&out
, CYFlags flags
) const {
563 bool protect((flags
& CYNoBrace
) != 0);
567 if (property_
!= NULL
)
568 property_
->Output(out
);
574 void CYPostfix::Output(std::ostream
&out
, CYFlags flags
) const {
575 lhs_
->Output(out
, Precedence(), CYLeft(flags
));
579 void CYPrefix::Output(std::ostream
&out
, CYFlags flags
) const {
580 const char *name(Operator());
581 bool alphabetic(Alphabetic());
582 if (alphabetic
&& (flags
& CYNoLeader
) != 0 || name
[0] == '-' && (flags
& CYNoHyphen
) != 0)
585 CYFlags
right(CYRight(flags
));
588 rhs_
->Output(out
, Precedence(), right
);
591 void CYProperty::Output(std::ostream
&out
) const {
592 name_
->PropertyName(out
);
594 value_
->Output(out
, CYPA
, CYNoFlags
);
601 void CYReturn::Output(std::ostream
&out
) const {
604 value_
->Output(out
, CYNoLeader
);
608 void CYSelector::Output(std::ostream
&out
, CYFlags flags
) const {
609 if ((flags
& CYNoLeader
) != 0)
611 out
<< "new Selector(\"";
617 void CYSelectorPart::Output(std::ostream
&out
) const {
626 void CYSend::Output(std::ostream
&out
, CYFlags flags
) const {
627 if ((flags
& CYNoLeader
) != 0)
629 out
<< "objc_msgSend(";
630 self_
->Output(out
, CYPA
, CYNoFlags
);
632 std::ostringstream name
;
633 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
634 if (argument
->name_
!= NULL
) {
635 name
<< *argument
->name_
;
636 if (argument
->value_
!= NULL
)
639 out
<< reinterpret_cast<void *>(sel_registerName(name
.str().c_str()));
640 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
641 if (argument
->value_
!= NULL
) {
643 argument
->value_
->Output(out
, CYPA
, CYNoFlags
);
648 void CYSource::Show(std::ostream
&out
) const {
649 for (const CYSource
*next(this); next
!= NULL
; next
= next
->next_
)
653 void CYSource::Output(std::ostream
&out
, bool block
) const {
654 if (!block
&& !IsBlock())
663 void CYSource::Output_(std::ostream
&out
) const {
667 void CYStatement::Output_(std::ostream
&out
) const {
668 for (CYLabel
*label(labels_
); label
!= NULL
; label
= label
->next_
)
669 out
<< *label
->name_
<< ':';
673 void CYString::Output(std::ostream
&out
, CYFlags flags
) const {
674 unsigned quot(0), apos(0);
675 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
678 else if (*value
== '\'')
681 bool single(quot
> apos
);
683 out
<< (single
? '\'' : '"');
684 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
686 case '\\': out
<< "\\\\"; break;
687 case '\b': out
<< "\\b"; break;
688 case '\f': out
<< "\\f"; break;
689 case '\n': out
<< "\\n"; break;
690 case '\r': out
<< "\\r"; break;
691 case '\t': out
<< "\\t"; break;
692 case '\v': out
<< "\\v"; break;
707 if (*value
< 0x20 || *value
>= 0x7f)
708 out
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value
);
712 out
<< (single
? '\'' : '"');
715 void CYString::PropertyName(std::ostream
&out
) const {
716 if (const char *word
= Word())
722 void CYSwitch::Output(std::ostream
&out
) const {
724 value_
->Output(out
, CYNoFlags
);
726 if (clauses_
!= NULL
)
731 void CYThis::Output(std::ostream
&out
, CYFlags flags
) const {
732 if ((flags
& CYNoLeader
) != 0)
735 if ((flags
& CYNoTrailer
) != 0)
739 void CYThrow::Output(std::ostream
&out
) const {
742 value_
->Output(out
, CYNoLeader
);
746 void CYTry::Output(std::ostream
&out
) const {
748 try_
->Output(out
, true);
751 if (finally_
!= NULL
) {
753 finally_
->Output(out
, true);
757 void CYVar::Output(std::ostream
&out
) const {
759 declarations_
->Output(out
, CYNoFlags
);
763 void CYVariable::Output(std::ostream
&out
, CYFlags flags
) const {
764 if ((flags
& CYNoLeader
) != 0)
767 if ((flags
& CYNoTrailer
) != 0)
771 void CYWhile::Output(std::ostream
&out
) const {
773 test_
->Output(out
, CYNoFlags
);
775 code_
->Output(out
, false);
778 void CYWith::Output(std::ostream
&out
) const {
780 scope_
->Output(out
, CYNoFlags
);
782 code_
->Output(out
, false);
785 void CYWord::ClassName(std::ostream
&out
, bool object
) const {
787 out
<< "objc_getClass(";
788 out
<< '"' << Value() << '"';
793 void CYWord::Output(std::ostream
&out
) const {
797 void CYWord::PropertyName(std::ostream
&out
) const {