]>
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(CYOutput
&out
, CYFlags flags
) const {
48 rhs_
->Output(out
, 1, CYLeft(flags
));
52 void CYArgument::Output(CYOutput
&out
) const {
59 value_
->Output(out
, CYPA
, CYNoFlags
);
61 if (next_
->name_
== NULL
)
69 void CYArray::Output(CYOutput
&out
, CYFlags flags
) const {
71 if (elements_
!= NULL
)
72 elements_
->Output(out
);
76 void CYArrayComprehension::Output(CYOutput
&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(CYOutput
&out
, CYFlags flags
) const {
95 lhs_
->Output(out
, Precedence() - 1, CYLeft(flags
));
97 rhs_
->Output(out
, Precedence(), CYRight(flags
));
100 void CYBlock::Output(CYOutput
&out
) const {
101 for (CYSource
*statement(statements_
); statement
!= NULL
; statement
= statement
->next_
)
102 statement
->Output(out
);
105 void CYBoolean::Output(CYOutput
&out
, CYFlags flags
) const {
106 if ((flags
& CYNoLeader
) != 0)
108 out
<< (Value() ? "true" : "false");
109 if ((flags
& CYNoTrailer
) != 0)
113 void CYBreak::Output(CYOutput
&out
) const {
116 out
<< ' ' << *label_
;
120 void CYCall::Output(CYOutput
&out
, CYFlags flags
) const {
121 function_
->Output(out
, Precedence(), CYLeft(flags
));
123 if (arguments_
!= NULL
)
124 arguments_
->Output(out
);
128 void CYCatch::Output(CYOutput
&out
) const {
129 out
<< "catch(" << *name_
<< ')';
130 code_
->Output(out
, true);
133 void CYCategory::Output(CYOutput
&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(CYOutput
&out
) const {
149 void CYClass::Output(CYOutput
&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(CYOutput
&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(CYOutput
&out
) const {
196 void CYCondition::Output(CYOutput
&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(CYOutput
&out
) const {
208 out
<< ' ' << *label_
;
212 void CYClause::Output(CYOutput
&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(CYOutput
&out
, CYFlags flags
) const {
230 if ((flags
& CYNoLeader
) != 0)
233 Output(out
, CYRight(flags
));
236 void CYDeclaration::ForEachIn(CYOutput
&out
) const {
240 void CYDeclaration::Output(CYOutput
&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(CYOutput
&out
) const {
254 void CYDeclarations::Output(CYOutput
&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(CYOutput
&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(CYOutput
&out
) const {
280 // XXX: extra space character!
282 code_
->Output(out
, false);
284 test_
->Output(out
, CYNoFlags
);
288 void CYElement::Output(CYOutput
&out
) const {
290 value_
->Output(out
, CYPA
, CYNoFlags
);
291 if (next_
!= NULL
|| value_
== NULL
)
297 void CYEmpty::Output(CYOutput
&out
) const {
301 void CYEmpty::Output(CYOutput
&out
, bool block
) const {
303 CYSource::Output(out
, block
);
308 void CYExpress::Output(CYOutput
&out
) const {
309 expression_
->Output(out
, CYNoBF
);
313 void CYExpression::ClassName(CYOutput
&out
, bool object
) const {
314 Output(out
, CYPA
, CYNoFlags
);
317 const char *CYExpression::ForEachIn() const {
321 void CYExpression::For(CYOutput
&out
) const {
325 void CYExpression::ForEachIn(CYOutput
&out
) const {
326 // XXX: this should handle LeftHandSideExpression
327 Output(out
, CYPA
, CYNoFlags
);
330 void CYExpression::ForIn(CYOutput
&out
, CYFlags flags
) const {
331 // XXX: this should handle LeftHandSideExpression
335 void CYExpression::Output(CYOutput
&out
, unsigned precedence
, CYFlags flags
) const {
336 if (precedence
< Precedence()) {
338 Output(out
, CYNoFlags
);
344 void CYField::Output(CYOutput
&out
) const {
348 void CYFor::Output(CYOutput
&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(CYOutput
&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_(CYOutput
&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_(CYOutput
&out
) const {
395 void CYForIn::Output(CYOutput
&out
) const {
397 initialiser_
->ForIn(out
, CYNoIn
| CYNoTrailer
);
399 set_
->Output(out
, CYNoLeader
);
401 code_
->Output(out
, false);
404 void CYForInComprehension::Begin_(CYOutput
&out
) const {
405 out
<< "for(" << *name_
<< " in";
406 set_
->Output(out
, CYNoLeader
);
410 void CYFunction::Output(CYOutput
&out
) const {
411 CYLambda::Output(out
, CYNoFlags
);
414 void CYFunctionParameter::Output(CYOutput
&out
) const {
422 void CYIf::Output(CYOutput
&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_(CYOutput
&out
) const {
435 test_
->Output(out
, CYNoFlags
);
439 void CYIndirect::Output(CYOutput
&out
, CYFlags flags
) const {
440 rhs_
->Output(out
, 1, CYLeft(flags
));
444 void CYIndirectMember::Output(CYOutput
&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(CYOutput
&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(CYOutput
&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(CYOutput
&out
) const {
499 declarations_
->Output(out
, CYNoFlags
);
501 if (statements_
!= NULL
)
502 statements_
->Show(out
);
506 void CYMessage::Output(CYOutput
&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(CYOutput
&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(CYOutput
&out
, CYFlags flags
) const {
541 if ((flags
& CYNoLeader
) != 0)
544 if ((flags
& CYNoTrailer
) != 0)
548 void CYNumber::Output(CYOutput
&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
.out_
<< std::setprecision(9) << value
;
554 if ((flags
& CYNoTrailer
) != 0)
558 void CYNumber::PropertyName(CYOutput
&out
) const {
562 void CYObject::Output(CYOutput
&out
, CYFlags flags
) const {
563 bool protect((flags
& CYNoBrace
) != 0);
567 if (property_
!= NULL
)
568 property_
->Output(out
);
574 void CYPostfix::Output(CYOutput
&out
, CYFlags flags
) const {
575 lhs_
->Output(out
, Precedence(), CYLeft(flags
));
579 void CYPrefix::Output(CYOutput
&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(CYOutput
&out
) const {
592 name_
->PropertyName(out
);
594 value_
->Output(out
, CYPA
, CYNoFlags
);
601 void CYRegEx::Output(CYOutput
&out
, CYFlags flags
) const {
603 if ((flags
& CYNoTrailer
) != 0)
607 void CYReturn::Output(CYOutput
&out
) const {
610 value_
->Output(out
, CYNoLeader
);
614 void CYSelector::Output(CYOutput
&out
, CYFlags flags
) const {
615 if ((flags
& CYNoLeader
) != 0)
617 out
<< "new Selector(\"";
623 void CYSelectorPart::Output(CYOutput
&out
) const {
632 void CYSend::Output(CYOutput
&out
, CYFlags flags
) const {
633 if ((flags
& CYNoLeader
) != 0)
635 out
<< "objc_msgSend(";
636 self_
->Output(out
, CYPA
, CYNoFlags
);
638 std::ostringstream name
;
639 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
640 if (argument
->name_
!= NULL
) {
641 name
<< *argument
->name_
;
642 if (argument
->value_
!= NULL
)
645 out
.out_
<< reinterpret_cast<void *>(sel_registerName(name
.str().c_str()));
646 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
647 if (argument
->value_
!= NULL
) {
649 argument
->value_
->Output(out
, CYPA
, CYNoFlags
);
654 void CYSource::Show(CYOutput
&out
) const {
655 for (const CYSource
*next(this); next
!= NULL
; next
= next
->next_
)
659 void CYSource::Output(CYOutput
&out
, bool block
) const {
660 if (!block
&& !IsBlock())
669 void CYSource::Output_(CYOutput
&out
) const {
673 void CYStatement::Output_(CYOutput
&out
) const {
674 for (CYLabel
*label(labels_
); label
!= NULL
; label
= label
->next_
)
675 out
<< *label
->name_
<< ':';
679 void CYString::Output(CYOutput
&out
, CYFlags flags
) const {
680 unsigned quot(0), apos(0);
681 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
684 else if (*value
== '\'')
687 bool single(quot
> apos
);
689 out
<< (single
? '\'' : '"');
690 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
692 case '\\': out
<< "\\\\"; break;
693 case '\b': out
<< "\\b"; break;
694 case '\f': out
<< "\\f"; break;
695 case '\n': out
<< "\\n"; break;
696 case '\r': out
<< "\\r"; break;
697 case '\t': out
<< "\\t"; break;
698 case '\v': out
<< "\\v"; break;
713 if (*value
< 0x20 || *value
>= 0x7f)
714 out
.out_
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value
);
718 out
<< (single
? '\'' : '"');
721 void CYString::PropertyName(CYOutput
&out
) const {
722 if (const char *word
= Word())
728 void CYSwitch::Output(CYOutput
&out
) const {
730 value_
->Output(out
, CYNoFlags
);
732 if (clauses_
!= NULL
)
737 void CYThis::Output(CYOutput
&out
, CYFlags flags
) const {
738 if ((flags
& CYNoLeader
) != 0)
741 if ((flags
& CYNoTrailer
) != 0)
745 void CYThrow::Output(CYOutput
&out
) const {
748 value_
->Output(out
, CYNoLeader
);
752 void CYTry::Output(CYOutput
&out
) const {
754 try_
->Output(out
, true);
757 if (finally_
!= NULL
) {
759 finally_
->Output(out
, true);
763 void CYVar::Output(CYOutput
&out
) const {
765 declarations_
->Output(out
, CYNoFlags
);
769 void CYVariable::Output(CYOutput
&out
, CYFlags flags
) const {
770 if ((flags
& CYNoLeader
) != 0)
773 if ((flags
& CYNoTrailer
) != 0)
777 void CYWhile::Output(CYOutput
&out
) const {
779 test_
->Output(out
, CYNoFlags
);
781 code_
->Output(out
, false);
784 void CYWith::Output(CYOutput
&out
) const {
786 scope_
->Output(out
, CYNoFlags
);
788 code_
->Output(out
, false);
791 void CYWord::ClassName(CYOutput
&out
, bool object
) const {
793 out
<< "objc_getClass(";
794 out
<< '"' << Value() << '"';
799 void CYWord::Output(CYOutput
&out
) const {
803 void CYWord::PropertyName(CYOutput
&out
) const {