]>
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 CYBlock::Output(std::ostream
&out
) const {
83 for (CYSource
*statement(statements_
); statement
!= NULL
; statement
= statement
->next_
)
84 statement
->Output(out
);
87 void CYBoolean::Output(std::ostream
&out
, CYFlags flags
) const {
88 if ((flags
& CYNoLeader
) != 0)
90 out
<< (Value() ? "true" : "false");
91 if ((flags
& CYNoTrailer
) != 0)
95 void CYBreak::Output(std::ostream
&out
) const {
98 out
<< ' ' << *label_
;
102 void CYCall::Output(std::ostream
&out
, CYFlags flags
) const {
103 function_
->Output(out
, Precedence(), CYLeft(flags
));
105 if (arguments_
!= NULL
)
106 arguments_
->Output(out
);
110 void CYCatch::Output(std::ostream
&out
) const {
111 out
<< "catch(" << *name_
<< ')';
112 code_
->Output(out
, true);
115 void CYCategory::Output(std::ostream
&out
) const {
116 out
<< "(function($cys,$cyp,$cyc,$cyn,$cyt){";
117 out
<< "$cyp=object_getClass($cys);";
119 if (messages_
!= NULL
)
120 messages_
->Output(out
, true);
122 name_
->ClassName(out
, true);
126 void CYClass::Output(std::ostream
&out
, CYFlags flags
) const {
127 // XXX: I don't necc. need the ()s
128 out
<< "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
129 out
<< "$cyp=object_getClass($cys);";
130 out
<< "$cyc=objc_allocateClassPair($cys,";
132 name_
->ClassName(out
, false);
134 out
<< "$cyq(\"CY$\")";
136 out
<< "$cym=object_getClass($cyc);";
138 fields_
->Output(out
);
139 if (messages_
!= NULL
)
140 messages_
->Output(out
, false);
141 out
<< "objc_registerClassPair($cyc);";
142 out
<< "return $cyc;";
145 super_
->Output(out
, CYPA
, CYNoFlags
);
151 void CYCompound::Output(std::ostream
&out
, CYFlags flags
) const {
152 if (CYExpression
*expression
= expressions_
)
153 if (CYExpression
*next
= expression
->next_
) {
154 expression
->Output(out
, CYLeft(flags
));
155 CYFlags
center(CYCenter(flags
));
156 while (next
!= NULL
) {
159 next
= expression
->next_
;
160 CYFlags
right(next
!= NULL
? center
: CYRight(flags
));
161 expression
->Output(out
, right
);
164 expression
->Output(out
, flags
);
167 void CYCondition::Output(std::ostream
&out
, CYFlags flags
) const {
168 test_
->Output(out
, Precedence() - 1, CYLeft(flags
));
171 true_
->Output(out
, CYPA
, CYNoFlags
);
173 false_
->Output(out
, CYPA
, CYRight(flags
));
176 void CYContinue::Output(std::ostream
&out
) const {
179 out
<< ' ' << *label_
;
183 void CYClause::Output(std::ostream
&out
) const {
186 case_
->Output(out
, CYNoFlags
);
191 code_
->Output(out
, false);
195 // XXX: deal with NoIn
196 void CYDeclaration::Part(std::ostream
&out
) const {
201 void CYDeclaration::Output(std::ostream
&out
) const {
203 if (initialiser_
!= NULL
) {
205 initialiser_
->Output(out
, CYPA
, CYNoFlags
);
209 // XXX: deal with NoIn
210 void CYDeclarations::Part(std::ostream
&out
) const {
213 const CYDeclarations
*declaration(this);
215 out
<< *declaration
->declaration_
;
216 declaration
= declaration
->next_
;
218 if (declaration
!= NULL
) {
224 void CYDeclarations::Output(std::ostream
&out
) const {
229 void CYDirectMember::Output(std::ostream
&out
, CYFlags flags
) const {
230 object_
->Output(out
, Precedence(), CYLeft(flags
));
231 if (const char *word
= property_
->Word())
235 property_
->Output(out
, CYNoFlags
);
240 void CYDoWhile::Output(std::ostream
&out
) const {
241 // XXX: extra space character!
243 code_
->Output(out
, false);
245 test_
->Output(out
, CYNoFlags
);
249 void CYElement::Output(std::ostream
&out
) const {
251 value_
->Output(out
, CYPA
, CYNoFlags
);
252 if (next_
!= NULL
|| value_
== NULL
)
258 void CYEmpty::Output(std::ostream
&out
) const {
262 void CYEmpty::Output(std::ostream
&out
, bool block
) const {
264 CYSource::Output(out
, block
);
269 void CYExpress::Output(std::ostream
&out
) const {
270 expression_
->Output(out
, CYNoFunction
| CYNoBrace
);
274 void CYExpression::ClassName(std::ostream
&out
, bool object
) const {
275 Output(out
, CYPA
, CYNoFlags
);
278 void CYExpression::Part(std::ostream
&out
) const {
279 // XXX: this should handle LeftHandSideExpression
283 void CYExpression::Output(std::ostream
&out
, unsigned precedence
, CYFlags flags
) const {
284 if (precedence
< Precedence()) {
286 Output(out
, CYNoFlags
);
292 void CYField::Output(std::ostream
&out
) const {
296 void CYFor::Output(std::ostream
&out
) const {
298 if (initialiser_
!= NULL
)
299 initialiser_
->Part(out
);
302 test_
->Output(out
, CYNoFlags
);
304 if (increment_
!= NULL
)
305 increment_
->Output(out
, CYNoFlags
);
307 code_
->Output(out
, false);
310 void CYForIn::Output(std::ostream
&out
) const {
312 initialiser_
->Part(out
);
313 // XXX: deal with this space character!
316 set_
->Output(out
, CYNoLeader
);
318 code_
->Output(out
, false);
321 void CYFunction::Output(std::ostream
&out
) const {
322 CYLambda::Output(out
, CYNoFlags
);
325 void CYFunctionParameter::Output(std::ostream
&out
) const {
333 void CYIf::Output(std::ostream
&out
) const {
335 test_
->Output(out
, CYNoFlags
);
337 true_
->Output(out
, true);
338 if (false_
!= NULL
) {
340 false_
->Output(out
, false);
344 void CYIndirect::Output(std::ostream
&out
, CYFlags flags
) const {
345 rhs_
->Output(out
, 1, CYLeft(flags
));
349 void CYIndirectMember::Output(std::ostream
&out
, CYFlags flags
) const {
350 object_
->Output(out
, Precedence(), CYLeft(flags
));
352 if (const char *word
= property_
->Word())
356 property_
->Output(out
, CYNoFlags
);
361 void CYInfix::Output(std::ostream
&out
, CYFlags flags
) const {
362 const char *name(Operator());
363 bool protect((flags
& CYNoIn
) != 0 && strcmp(name
, "in"));
366 bool alphabetic(Alphabetic());
367 CYFlags
left(protect
? CYNoFlags
: CYLeft(flags
));
370 lhs_
->Output(out
, Precedence(), left
);
372 CYFlags
right(protect
? CYNoFlags
: CYRight(flags
));
375 if (strcmp(name
, "-") == 0)
377 rhs_
->Output(out
, Precedence() - 1, right
);
382 void CYLambda::Output(std::ostream
&out
, CYFlags flags
) const {
383 bool protect((flags
& CYNoFunction
) != 0);
388 out
<< ' ' << *name_
;
390 if (parameters_
!= NULL
)
400 void CYMessage::Output(std::ostream
&out
, bool replace
) const {
402 next_
->Output(out
, replace
);
403 out
<< "$cyn=new Selector(\"";
404 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
405 if (parameter
->tag_
!= NULL
) {
406 out
<< *parameter
->tag_
;
407 if (parameter
->name_
!= NULL
)
411 out
<< "$cyt=$cyn.type($cy" << (instance_
? 's' : 'p') << ");";
412 out
<< "class_" << (replace
? "replace" : "add") << "Method($cy" << (instance_
? 'c' : 'm') << ",$cyn,";
413 out
<< "new Functor(function(self,_cmd";
414 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
415 if (parameter
->name_
!= NULL
)
416 out
<< ',' << *parameter
->name_
;
417 out
<< "){return function(){";
420 out
<< "}.call(self);},$cyt),$cyt);";
423 void CYNew::Output(std::ostream
&out
, CYFlags flags
) const {
424 if ((flags
& CYNoLeader
) != 0)
427 constructor_
->Output(out
, Precedence(), CYCenter(flags
) | CYNoLeader
);
429 if (arguments_
!= NULL
)
430 arguments_
->Output(out
);
434 void CYNull::Output(std::ostream
&out
, CYFlags flags
) const {
435 if ((flags
& CYNoLeader
) != 0)
438 if ((flags
& CYNoTrailer
) != 0)
442 void CYNumber::Output(std::ostream
&out
, CYFlags flags
) const {
443 double value(Value());
444 if ((flags
& CYNoLeader
) != 0 || value
< 0 && (flags
& CYNoHyphen
) != 0)
446 // XXX: decide on correct precision
447 out
<< std::setprecision(9) << value
;
448 if ((flags
& CYNoTrailer
) != 0)
452 void CYNumber::PropertyName(std::ostream
&out
) const {
456 void CYObject::Output(std::ostream
&out
, CYFlags flags
) const {
457 bool protect((flags
& CYNoBrace
) != 0);
461 if (property_
!= NULL
)
462 property_
->Output(out
);
468 void CYPostfix::Output(std::ostream
&out
, CYFlags flags
) const {
469 lhs_
->Output(out
, Precedence(), CYLeft(flags
));
473 void CYPrefix::Output(std::ostream
&out
, CYFlags flags
) const {
474 const char *name(Operator());
475 bool alphabetic(Alphabetic());
476 if (alphabetic
&& (flags
& CYNoLeader
) != 0 || name
[0] == '-' && (flags
& CYNoHyphen
) != 0)
479 CYFlags
right(CYRight(flags
));
482 rhs_
->Output(out
, Precedence(), right
);
485 void CYProperty::Output(std::ostream
&out
) const {
486 name_
->PropertyName(out
);
488 value_
->Output(out
, CYPA
, CYNoFlags
);
495 void CYReturn::Output(std::ostream
&out
) const {
498 value_
->Output(out
, CYNoLeader
);
502 void CYSelector::Output(std::ostream
&out
, CYFlags flags
) const {
503 if ((flags
& CYNoLeader
) != 0)
505 out
<< "new Selector(\"";
511 void CYSelectorPart::Output(std::ostream
&out
) const {
520 void CYSend::Output(std::ostream
&out
, CYFlags flags
) const {
521 if ((flags
& CYNoLeader
) != 0)
523 out
<< "objc_msgSend(";
524 self_
->Output(out
, CYPA
, CYNoFlags
);
526 std::ostringstream name
;
527 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
528 if (argument
->name_
!= NULL
) {
529 name
<< *argument
->name_
;
530 if (argument
->value_
!= NULL
)
533 out
<< reinterpret_cast<void *>(sel_registerName(name
.str().c_str()));
534 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
535 if (argument
->value_
!= NULL
) {
537 argument
->value_
->Output(out
, CYPA
, CYNoFlags
);
542 void CYSource::Show(std::ostream
&out
) const {
543 for (const CYSource
*next(this); next
!= NULL
; next
= next
->next_
)
547 void CYSource::Output(std::ostream
&out
, bool block
) const {
548 if (!block
&& !IsBlock())
557 void CYSource::Output_(std::ostream
&out
) const {
561 void CYStatement::Output_(std::ostream
&out
) const {
562 for (CYLabel
*label(labels_
); label
!= NULL
; label
= label
->next_
)
563 out
<< *label
->name_
<< ':';
567 void CYString::Output(std::ostream
&out
, CYFlags flags
) const {
568 unsigned quot(0), apos(0);
569 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
572 else if (*value
== '\'')
575 bool single(quot
> apos
);
577 out
<< (single
? '\'' : '"');
578 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
580 case '\\': out
<< "\\\\"; break;
581 case '\b': out
<< "\\b"; break;
582 case '\f': out
<< "\\f"; break;
583 case '\n': out
<< "\\n"; break;
584 case '\r': out
<< "\\r"; break;
585 case '\t': out
<< "\\t"; break;
586 case '\v': out
<< "\\v"; break;
601 if (*value
< 0x20 || *value
>= 0x7f)
602 out
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value
);
606 out
<< (single
? '\'' : '"');
609 void CYString::PropertyName(std::ostream
&out
) const {
610 if (const char *word
= Word())
616 void CYSwitch::Output(std::ostream
&out
) const {
618 value_
->Output(out
, CYNoFlags
);
620 if (clauses_
!= NULL
)
625 void CYThis::Output(std::ostream
&out
, CYFlags flags
) const {
626 if ((flags
& CYNoLeader
) != 0)
629 if ((flags
& CYNoTrailer
) != 0)
633 void CYThrow::Output(std::ostream
&out
) const {
636 value_
->Output(out
, CYNoLeader
);
640 void CYTry::Output(std::ostream
&out
) const {
642 try_
->Output(out
, true);
645 if (finally_
!= NULL
) {
647 finally_
->Output(out
, true);
651 void CYVariable::Output(std::ostream
&out
, CYFlags flags
) const {
652 if ((flags
& CYNoLeader
) != 0)
655 if ((flags
& CYNoTrailer
) != 0)
659 void CYWhile::Output(std::ostream
&out
) const {
661 test_
->Output(out
, CYNoFlags
);
663 code_
->Output(out
, false);
666 void CYWith::Output(std::ostream
&out
) const {
668 scope_
->Output(out
, CYNoFlags
);
670 code_
->Output(out
, false);
673 void CYWord::ClassName(std::ostream
&out
, bool object
) const {
675 out
<< "objc_getClass(";
676 out
<< '"' << Value() << '"';
681 void CYWord::Output(std::ostream
&out
) const {
685 void CYWord::PropertyName(std::ostream
&out
) const {