]>
git.saurik.com Git - cycript.git/blob - Output.cpp
6 _finline CYFlags
operator ~(CYFlags rhs
) {
7 return static_cast<CYFlags
>(~static_cast<unsigned>(rhs
));
10 _finline CYFlags
operator &(CYFlags lhs
, CYFlags rhs
) {
11 return static_cast<CYFlags
>(static_cast<unsigned>(lhs
) & static_cast<unsigned>(rhs
));
14 _finline CYFlags
operator |(CYFlags lhs
, CYFlags rhs
) {
15 return static_cast<CYFlags
>(static_cast<unsigned>(lhs
) | static_cast<unsigned>(rhs
));
18 _finline CYFlags
&operator |=(CYFlags
&lhs
, CYFlags rhs
) {
19 return lhs
= lhs
| rhs
;
22 _finline CYFlags
CYLeft(CYFlags flags
) {
23 return flags
& ~CYNoTrailer
;
26 _finline CYFlags
CYCenter(CYFlags flags
) {
27 return flags
& CYNoIn
;
30 _finline CYFlags
CYRight(CYFlags flags
) {
31 return flags
& (CYNoIn
| CYNoTrailer
);
34 bool CYFalse::Value() const {
38 bool CYTrue::Value() const {
44 void CYAddressOf::Output(std::ostream
&out
, CYFlags flags
) const {
45 rhs_
->Output(out
, 1, CYLeft(flags
));
49 void CYArgument::Output(std::ostream
&out
) const {
56 value_
->Output(out
, CYPA
, CYNoFlags
);
58 if (next_
->name_
== NULL
)
66 void CYArray::Output(std::ostream
&out
, CYFlags flags
) const {
68 if (elements_
!= NULL
)
69 elements_
->Output(out
);
73 void CYAssignment::Output(std::ostream
&out
, CYFlags flags
) const {
74 lhs_
->Output(out
, Precedence() - 1, CYLeft(flags
));
76 rhs_
->Output(out
, Precedence(), CYRight(flags
));
79 void CYBoolean::Output(std::ostream
&out
, CYFlags flags
) const {
80 if ((flags
& CYNoLeader
) != 0)
82 out
<< (Value() ? "true" : "false");
83 if ((flags
& CYNoTrailer
) != 0)
87 void CYBreak::Output(std::ostream
&out
) const {
90 out
<< ' ' << *label_
;
94 void CYCall::Output(std::ostream
&out
, CYFlags flags
) const {
95 function_
->Output(out
, Precedence(), CYLeft(flags
));
97 if (arguments_
!= NULL
)
98 arguments_
->Output(out
);
102 void CYCatch::Output(std::ostream
&out
) const {
103 out
<< "catch(" << *name_
<< ')';
104 code_
->Output(out
, true);
107 void CYClass::Output(std::ostream
&out
) const {
108 out
<< "(function($cys,$cyc,$cym,$cyn,$cyt){";
109 out
<< "$cyc=objc_allocateClassPair($cys,\"" << *name_
<< "\",0);";
110 out
<< "$cym=object_getClass($cyc);";
112 fields_
->Output(out
);
113 if (messages_
!= NULL
)
114 messages_
->Output(out
);
115 out
<< "objc_registerClassPair($cyc);";
118 super_
->Output(out
, CYPA
, CYNoFlags
);
124 void CYCondition::Output(std::ostream
&out
, CYFlags flags
) const {
125 test_
->Output(out
, Precedence() - 1, CYLeft(flags
));
128 true_
->Output(out
, CYPA
, CYNoFlags
);
130 false_
->Output(out
, CYPA
, CYRight(flags
));
133 void CYContinue::Output(std::ostream
&out
) const {
136 out
<< ' ' << *label_
;
140 void CYClause::Output(std::ostream
&out
) const {
143 case_
->Output(out
, CYNoFlags
);
148 code_
->Output(out
, false);
152 // XXX: deal with NoIn
153 void CYDeclaration::Part(std::ostream
&out
) const {
158 void CYDeclaration::Output(std::ostream
&out
) const {
160 if (initialiser_
!= NULL
) {
162 initialiser_
->Output(out
, CYPA
, CYNoFlags
);
166 // XXX: deal with NoIn
167 void CYDeclarations::Part(std::ostream
&out
) const {
170 const CYDeclarations
*declaration(this);
172 out
<< *declaration
->declaration_
;
173 declaration
= declaration
->next_
;
175 if (declaration
!= NULL
) {
181 void CYDeclarations::Output(std::ostream
&out
) const {
186 void CYDoWhile::Output(std::ostream
&out
) const {
187 // XXX: extra space character!
189 code_
->Output(out
, false);
191 test_
->Output(out
, CYNoFlags
);
195 void CYElement::Output(std::ostream
&out
) const {
197 value_
->Output(out
, CYPA
, CYNoFlags
);
198 if (next_
!= NULL
|| value_
== NULL
)
204 void CYEmpty::Output(std::ostream
&out
) const {
208 void CYEmpty::Output(std::ostream
&out
, bool block
) const {
210 CYSource::Output(out
, block
);
215 void CYExpress::Output(std::ostream
&out
) const {
216 expression_
->Output(out
, CYNoFunction
| CYNoBrace
);
220 void CYExpression::Part(std::ostream
&out
) const {
221 // XXX: this should handle LeftHandSideExpression
225 void CYCompound::Output(std::ostream
&out
, CYFlags flags
) const {
226 if (CYExpression
*expression
= expressions_
)
227 if (CYExpression
*next
= expression
->next_
) {
228 expression
->Output(out
, CYLeft(flags
));
229 CYFlags
center(CYCenter(flags
));
230 while (next
!= NULL
) {
233 next
= expression
->next_
;
234 CYFlags
right(next
!= NULL
? center
: CYRight(flags
));
235 expression
->Output(out
, right
);
238 expression
->Output(out
, flags
);
241 void CYExpression::Output(std::ostream
&out
, unsigned precedence
, CYFlags flags
) const {
242 if (precedence
< Precedence()) {
244 Output(out
, CYNoFlags
);
250 void CYField::Output(std::ostream
&out
) const {
254 void CYFor::Output(std::ostream
&out
) const {
256 if (initialiser_
!= NULL
)
257 initialiser_
->Part(out
);
260 test_
->Output(out
, CYNoFlags
);
262 if (increment_
!= NULL
)
263 increment_
->Output(out
, CYNoFlags
);
265 code_
->Output(out
, false);
268 void CYForIn::Output(std::ostream
&out
) const {
270 initialiser_
->Part(out
);
271 // XXX: deal with this space character!
274 set_
->Output(out
, CYNoLeader
);
276 code_
->Output(out
, false);
279 void CYFunction::Output(std::ostream
&out
) const {
280 CYLambda::Output(out
, CYNoFlags
);
283 void CYFunctionParameter::Output(std::ostream
&out
) const {
291 void CYIf::Output(std::ostream
&out
) const {
293 test_
->Output(out
, CYNoFlags
);
295 true_
->Output(out
, true);
296 if (false_
!= NULL
) {
298 false_
->Output(out
, false);
302 void CYIndirect::Output(std::ostream
&out
, CYFlags flags
) const {
303 rhs_
->Output(out
, 1, CYLeft(flags
));
307 void CYInfix::Output(std::ostream
&out
, CYFlags flags
) const {
308 const char *name(Operator());
309 bool protect((flags
& CYNoIn
) != 0 && strcmp(name
, "in"));
312 bool alphabetic(Alphabetic());
313 CYFlags
left(protect
? CYNoFlags
: CYLeft(flags
));
316 lhs_
->Output(out
, Precedence(), left
);
318 CYFlags
right(protect
? CYNoFlags
: CYRight(flags
));
321 rhs_
->Output(out
, Precedence() - 1, right
);
326 void CYLambda::Output(std::ostream
&out
, CYFlags flags
) const {
327 bool protect((flags
& CYNoFunction
) != 0);
332 out
<< ' ' << *name_
;
334 if (parameters_
!= NULL
)
344 void CYMember::Output(std::ostream
&out
, CYFlags flags
) const {
345 object_
->Output(out
, Precedence(), CYLeft(flags
));
346 if (const char *word
= property_
->Word())
350 property_
->Output(out
, CYNoFlags
);
355 void CYMessage::Output(std::ostream
&out
) const {
358 out
<< "$cyn=new Selector(\"";
359 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
360 if (parameter
->tag_
!= NULL
) {
361 out
<< *parameter
->tag_
;
362 if (parameter
->name_
!= NULL
)
366 out
<< "$cyt=$cyn.type($cys," << (instance_
? "true" : "false") << ");";
367 out
<< "class_addMethod($cy" << (instance_
? 'c' : 'm') << ",$cyn,";
368 out
<< "new Functor(function(self,_cmd";
369 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
370 if (parameter
->name_
!= NULL
)
371 out
<< ',' << *parameter
->name_
;
372 out
<< "){return function(){";
375 out
<< "}.call(self);},$cyt),$cyt);";
378 void CYNew::Output(std::ostream
&out
, CYFlags flags
) const {
380 constructor_
->Output(out
, Precedence(), CYCenter(flags
) | CYNoLeader
);
382 if (arguments_
!= NULL
)
383 arguments_
->Output(out
);
387 void CYNull::Output(std::ostream
&out
, CYFlags flags
) const {
388 if ((flags
& CYNoLeader
) != 0)
391 if ((flags
& CYNoTrailer
) != 0)
395 void CYNumber::Output(std::ostream
&out
, CYFlags flags
) const {
396 if ((flags
& CYNoLeader
) != 0)
398 // XXX: this is not a useful formatting
400 if ((flags
& CYNoTrailer
) != 0)
404 void CYObject::Output(std::ostream
&out
, CYFlags flags
) const {
405 bool protect((flags
& CYNoBrace
) != 0);
409 if (property_
!= NULL
)
410 property_
->Output(out
);
416 void CYPostfix::Output(std::ostream
&out
, CYFlags flags
) const {
417 lhs_
->Output(out
, Precedence(), CYLeft(flags
));
421 void CYPrefix::Output(std::ostream
&out
, CYFlags flags
) const {
422 bool alphabetic(Alphabetic());
424 CYFlags
right(CYRight(flags
));
427 rhs_
->Output(out
, Precedence(), right
);
430 void CYProperty::Output(std::ostream
&out
) const {
431 out
<< *name_
<< ':';
432 value_
->Output(out
, CYPA
, CYNoFlags
);
439 void CYReturn::Output(std::ostream
&out
) const {
442 value_
->Output(out
, CYNoLeader
);
446 void CYSelector::Output(std::ostream
&out
, CYFlags flags
) const {
447 out
<< "new Selector(\"";
453 void CYSelectorPart::Output(std::ostream
&out
) const {
462 void CYSend::Output(std::ostream
&out
, CYFlags flags
) const {
463 out
<< "objc_msgSend(";
464 self_
->Output(out
, CYPA
, CYNoFlags
);
466 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
467 if (argument
->name_
!= NULL
) {
468 out
<< *argument
->name_
;
469 if (argument
->value_
!= NULL
)
473 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
474 if (argument
->value_
!= NULL
) {
476 argument
->value_
->Output(out
, CYPA
, CYNoFlags
);
481 void CYSource::Show(std::ostream
&out
) const {
482 for (const CYSource
*next(this); next
!= NULL
; next
= next
->next_
)
486 void CYSource::Output(std::ostream
&out
, bool block
) const {
487 if (!block
&& next_
== NULL
)
496 void CYString::Output(std::ostream
&out
, CYFlags flags
) const {
498 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
500 case '"': out
<< "\\\""; break;
501 case '\\': out
<< "\\\\"; break;
502 case '\b': out
<< "\\b"; break;
503 case '\f': out
<< "\\f"; break;
504 case '\n': out
<< "\\n"; break;
505 case '\r': out
<< "\\r"; break;
506 case '\t': out
<< "\\t"; break;
507 case '\v': out
<< "\\v"; break;
510 if (*value
< 0x20 || *value
>= 0x7f)
511 out
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value
);
518 void CYSwitch::Output(std::ostream
&out
) const {
520 value_
->Output(out
, CYNoFlags
);
522 if (clauses_
!= NULL
)
527 void CYThis::Output(std::ostream
&out
, CYFlags flags
) const {
528 if ((flags
& CYNoLeader
) != 0)
531 if ((flags
& CYNoTrailer
) != 0)
535 void CYThrow::Output(std::ostream
&out
) const {
538 value_
->Output(out
, CYNoLeader
);
542 void CYTry::Output(std::ostream
&out
) const {
544 try_
->Output(out
, true);
547 if (finally_
!= NULL
) {
549 finally_
->Output(out
, true);
553 void CYVariable::Output(std::ostream
&out
, CYFlags flags
) const {
554 if ((flags
& CYNoLeader
) != 0)
557 if ((flags
& CYNoTrailer
) != 0)
561 void CYWhile::Output(std::ostream
&out
) const {
563 test_
->Output(out
, CYNoFlags
);
565 code_
->Output(out
, false);
568 void CYWith::Output(std::ostream
&out
) const {
570 scope_
->Output(out
, CYNoFlags
);
572 code_
->Output(out
, false);
575 void CYWord::Output(std::ostream
&out
) const {