]>
git.saurik.com Git - cycript.git/blob - Output.cpp
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 CYAssignment::Output(std::ostream
&out
, CYFlags flags
) const {
77 lhs_
->Output(out
, Precedence() - 1, CYLeft(flags
));
79 rhs_
->Output(out
, Precedence(), CYRight(flags
));
82 void CYBoolean::Output(std::ostream
&out
, CYFlags flags
) const {
83 if ((flags
& CYNoLeader
) != 0)
85 out
<< (Value() ? "true" : "false");
86 if ((flags
& CYNoTrailer
) != 0)
90 void CYBreak::Output(std::ostream
&out
) const {
93 out
<< ' ' << *label_
;
97 void CYCall::Output(std::ostream
&out
, CYFlags flags
) const {
98 function_
->Output(out
, Precedence(), CYLeft(flags
));
100 if (arguments_
!= NULL
)
101 arguments_
->Output(out
);
105 void CYCatch::Output(std::ostream
&out
) const {
106 out
<< "catch(" << *name_
<< ')';
107 code_
->Output(out
, true);
110 void CYCategory::Output(std::ostream
&out
) const {
111 out
<< "(function($cys,$cyp,$cyc,$cyn,$cyt){";
112 out
<< "$cyp=object_getClass($cys);";
114 if (messages_
!= NULL
)
115 messages_
->Output(out
, true);
117 name_
->ClassName(out
);
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);";
127 fields_
->Output(out
);
128 if (messages_
!= NULL
)
129 messages_
->Output(out
, false);
130 out
<< "objc_registerClassPair($cyc);";
133 super_
->Output(out
, CYPA
, CYNoFlags
);
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
) {
147 next
= expression
->next_
;
148 CYFlags
right(next
!= NULL
? center
: CYRight(flags
));
149 expression
->Output(out
, right
);
152 expression
->Output(out
, flags
);
155 void CYCondition::Output(std::ostream
&out
, CYFlags flags
) const {
156 test_
->Output(out
, Precedence() - 1, CYLeft(flags
));
159 true_
->Output(out
, CYPA
, CYNoFlags
);
161 false_
->Output(out
, CYPA
, CYRight(flags
));
164 void CYContinue::Output(std::ostream
&out
) const {
167 out
<< ' ' << *label_
;
171 void CYClause::Output(std::ostream
&out
) const {
174 case_
->Output(out
, CYNoFlags
);
179 code_
->Output(out
, false);
183 // XXX: deal with NoIn
184 void CYDeclaration::Part(std::ostream
&out
) const {
189 void CYDeclaration::Output(std::ostream
&out
) const {
191 if (initialiser_
!= NULL
) {
193 initialiser_
->Output(out
, CYPA
, CYNoFlags
);
197 // XXX: deal with NoIn
198 void CYDeclarations::Part(std::ostream
&out
) const {
201 const CYDeclarations
*declaration(this);
203 out
<< *declaration
->declaration_
;
204 declaration
= declaration
->next_
;
206 if (declaration
!= NULL
) {
212 void CYDeclarations::Output(std::ostream
&out
) const {
217 void CYDirectMember::Output(std::ostream
&out
, CYFlags flags
) const {
218 object_
->Output(out
, Precedence(), CYLeft(flags
));
219 if (const char *word
= property_
->Word())
223 property_
->Output(out
, CYNoFlags
);
228 void CYDoWhile::Output(std::ostream
&out
) const {
229 // XXX: extra space character!
231 code_
->Output(out
, false);
233 test_
->Output(out
, CYNoFlags
);
237 void CYElement::Output(std::ostream
&out
) const {
239 value_
->Output(out
, CYPA
, CYNoFlags
);
240 if (next_
!= NULL
|| value_
== NULL
)
246 void CYEmpty::Output(std::ostream
&out
) const {
250 void CYEmpty::Output(std::ostream
&out
, bool block
) const {
252 CYSource::Output(out
, block
);
257 void CYExpress::Output(std::ostream
&out
) const {
258 expression_
->Output(out
, CYNoFunction
| CYNoBrace
);
262 void CYExpression::ClassName(std::ostream
&out
) const {
263 Output(out
, CYPA
, CYNoFlags
);
266 void CYExpression::Part(std::ostream
&out
) const {
267 // XXX: this should handle LeftHandSideExpression
271 void CYExpression::Output(std::ostream
&out
, unsigned precedence
, CYFlags flags
) const {
272 if (precedence
< Precedence()) {
274 Output(out
, CYNoFlags
);
280 void CYField::Output(std::ostream
&out
) const {
284 void CYFor::Output(std::ostream
&out
) const {
286 if (initialiser_
!= NULL
)
287 initialiser_
->Part(out
);
290 test_
->Output(out
, CYNoFlags
);
292 if (increment_
!= NULL
)
293 increment_
->Output(out
, CYNoFlags
);
295 code_
->Output(out
, false);
298 void CYForIn::Output(std::ostream
&out
) const {
300 initialiser_
->Part(out
);
301 // XXX: deal with this space character!
304 set_
->Output(out
, CYNoLeader
);
306 code_
->Output(out
, false);
309 void CYFunction::Output(std::ostream
&out
) const {
310 CYLambda::Output(out
, CYNoFlags
);
313 void CYFunctionParameter::Output(std::ostream
&out
) const {
321 void CYIf::Output(std::ostream
&out
) const {
323 test_
->Output(out
, CYNoFlags
);
325 true_
->Output(out
, true);
326 if (false_
!= NULL
) {
328 false_
->Output(out
, false);
332 void CYIndirect::Output(std::ostream
&out
, CYFlags flags
) const {
333 rhs_
->Output(out
, 1, CYLeft(flags
));
337 void CYIndirectMember::Output(std::ostream
&out
, CYFlags flags
) const {
338 object_
->Output(out
, Precedence(), CYLeft(flags
));
340 if (const char *word
= property_
->Word())
344 property_
->Output(out
, CYNoFlags
);
349 void CYInfix::Output(std::ostream
&out
, CYFlags flags
) const {
350 const char *name(Operator());
351 bool protect((flags
& CYNoIn
) != 0 && strcmp(name
, "in"));
354 bool alphabetic(Alphabetic());
355 CYFlags
left(protect
? CYNoFlags
: CYLeft(flags
));
358 lhs_
->Output(out
, Precedence(), left
);
360 CYFlags
right(protect
? CYNoFlags
: CYRight(flags
));
363 if (strcmp(name
, "-") == 0)
365 rhs_
->Output(out
, Precedence() - 1, right
);
370 void CYLambda::Output(std::ostream
&out
, CYFlags flags
) const {
371 bool protect((flags
& CYNoFunction
) != 0);
376 out
<< ' ' << *name_
;
378 if (parameters_
!= NULL
)
388 void CYMessage::Output(std::ostream
&out
, bool replace
) const {
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
)
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(){";
408 out
<< "}.call(self);},$cyt),$cyt);";
411 void CYNew::Output(std::ostream
&out
, CYFlags flags
) const {
412 if ((flags
& CYNoLeader
) != 0)
415 constructor_
->Output(out
, Precedence(), CYCenter(flags
) | CYNoLeader
);
417 if (arguments_
!= NULL
)
418 arguments_
->Output(out
);
422 void CYNull::Output(std::ostream
&out
, CYFlags flags
) const {
423 if ((flags
& CYNoLeader
) != 0)
426 if ((flags
& CYNoTrailer
) != 0)
430 void CYNumber::Output(std::ostream
&out
, CYFlags flags
) const {
431 double value(Value());
432 if ((flags
& CYNoLeader
) != 0 || value
< 0 && (flags
& CYNoHyphen
) != 0)
434 // XXX: decide on correct precision
435 out
<< std::setprecision(9) << value
;
436 if ((flags
& CYNoTrailer
) != 0)
440 void CYNumber::PropertyName(std::ostream
&out
) const {
444 void CYObject::Output(std::ostream
&out
, CYFlags flags
) const {
445 bool protect((flags
& CYNoBrace
) != 0);
449 if (property_
!= NULL
)
450 property_
->Output(out
);
456 void CYPostfix::Output(std::ostream
&out
, CYFlags flags
) const {
457 lhs_
->Output(out
, Precedence(), CYLeft(flags
));
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)
467 CYFlags
right(CYRight(flags
));
470 rhs_
->Output(out
, Precedence(), right
);
473 void CYProperty::Output(std::ostream
&out
) const {
474 name_
->PropertyName(out
);
476 value_
->Output(out
, CYPA
, CYNoFlags
);
483 void CYReturn::Output(std::ostream
&out
) const {
486 value_
->Output(out
, CYNoLeader
);
490 void CYSelector::Output(std::ostream
&out
, CYFlags flags
) const {
491 if ((flags
& CYNoLeader
) != 0)
493 out
<< "new Selector(\"";
499 void CYSelectorPart::Output(std::ostream
&out
) const {
508 void CYSend::Output(std::ostream
&out
, CYFlags flags
) const {
509 if ((flags
& CYNoLeader
) != 0)
511 out
<< "objc_msgSend(";
512 self_
->Output(out
, CYPA
, CYNoFlags
);
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
)
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
) {
525 argument
->value_
->Output(out
, CYPA
, CYNoFlags
);
530 void CYSource::Show(std::ostream
&out
) const {
531 for (const CYSource
*next(this); next
!= NULL
; next
= next
->next_
)
535 void CYSource::Output(std::ostream
&out
, bool block
) const {
536 if (!block
&& next_
== NULL
)
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
)
550 else if (*value
== '\'')
553 bool single(quot
> apos
);
555 out
<< (single
? '\'' : '"');
556 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++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;
579 if (*value
< 0x20 || *value
>= 0x7f)
580 out
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value
);
584 out
<< (single
? '\'' : '"');
587 void CYString::PropertyName(std::ostream
&out
) const {
588 if (const char *word
= Word())
594 void CYSwitch::Output(std::ostream
&out
) const {
596 value_
->Output(out
, CYNoFlags
);
598 if (clauses_
!= NULL
)
603 void CYThis::Output(std::ostream
&out
, CYFlags flags
) const {
604 if ((flags
& CYNoLeader
) != 0)
607 if ((flags
& CYNoTrailer
) != 0)
611 void CYThrow::Output(std::ostream
&out
) const {
614 value_
->Output(out
, CYNoLeader
);
618 void CYTry::Output(std::ostream
&out
) const {
620 try_
->Output(out
, true);
623 if (finally_
!= NULL
) {
625 finally_
->Output(out
, true);
629 void CYVariable::Output(std::ostream
&out
, CYFlags flags
) const {
630 if ((flags
& CYNoLeader
) != 0)
633 if ((flags
& CYNoTrailer
) != 0)
637 void CYWhile::Output(std::ostream
&out
) const {
639 test_
->Output(out
, CYNoFlags
);
641 code_
->Output(out
, false);
644 void CYWith::Output(std::ostream
&out
) const {
646 scope_
->Output(out
, CYNoFlags
);
648 code_
->Output(out
, false);
651 void CYWord::ClassName(std::ostream
&out
) const {
652 out
<< "objc_getClass(\"" << Value() << "\")";
655 void CYWord::Output(std::ostream
&out
) const {
659 void CYWord::PropertyName(std::ostream
&out
) const {