]>
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 {
356 out
<< "$cyn=new Selector(\"";
357 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
358 if (parameter
->tag_
!= NULL
) {
359 out
<< *parameter
->tag_
;
360 if (parameter
->name_
!= NULL
)
364 out
<< "$cyt=$cyn.type($cys," << (instance_
? "true" : "false") << ");";
365 out
<< "class_addMethod($cy" << (instance_
? 'c' : 'm') << ",$cyn,";
366 out
<< "new Functor(function(self,_cmd";
367 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
368 if (parameter
->name_
!= NULL
)
369 out
<< ',' << *parameter
->name_
;
370 out
<< "){return function(){";
373 out
<< "}.call(self);},$cyt),$cyt);";
376 void CYNew::Output(std::ostream
&out
, CYFlags flags
) const {
378 constructor_
->Output(out
, Precedence(), CYCenter(flags
) | CYNoLeader
);
380 if (arguments_
!= NULL
)
381 arguments_
->Output(out
);
385 void CYNull::Output(std::ostream
&out
, CYFlags flags
) const {
386 if ((flags
& CYNoLeader
) != 0)
389 if ((flags
& CYNoTrailer
) != 0)
393 void CYNumber::Output(std::ostream
&out
, CYFlags flags
) const {
394 if ((flags
& CYNoLeader
) != 0)
396 // XXX: this is not a useful formatting
398 if ((flags
& CYNoTrailer
) != 0)
402 void CYObject::Output(std::ostream
&out
, CYFlags flags
) const {
403 bool protect((flags
& CYNoBrace
) != 0);
407 if (property_
!= NULL
)
408 property_
->Output(out
);
414 void CYPostfix::Output(std::ostream
&out
, CYFlags flags
) const {
415 lhs_
->Output(out
, Precedence(), CYLeft(flags
));
419 void CYPrefix::Output(std::ostream
&out
, CYFlags flags
) const {
420 bool alphabetic(Alphabetic());
422 CYFlags
right(CYRight(flags
));
425 rhs_
->Output(out
, Precedence(), right
);
428 void CYProperty::Output(std::ostream
&out
) const {
429 out
<< *name_
<< ':';
430 value_
->Output(out
, CYPA
, CYNoFlags
);
437 void CYReturn::Output(std::ostream
&out
) const {
440 value_
->Output(out
, CYNoLeader
);
444 void CYSelector::Output(std::ostream
&out
, CYFlags flags
) const {
445 out
<< "new Selector(\"";
451 void CYSelectorPart::Output(std::ostream
&out
) const {
460 void CYSend::Output(std::ostream
&out
, CYFlags flags
) const {
461 out
<< "objc_msgSend(";
462 self_
->Output(out
, CYPA
, CYNoFlags
);
464 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
465 if (argument
->name_
!= NULL
) {
466 out
<< *argument
->name_
;
467 if (argument
->value_
!= NULL
)
471 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
472 if (argument
->value_
!= NULL
) {
474 argument
->value_
->Output(out
, CYPA
, CYNoFlags
);
479 void CYSource::Show(std::ostream
&out
) const {
480 for (const CYSource
*next(this); next
!= NULL
; next
= next
->next_
)
484 void CYSource::Output(std::ostream
&out
, bool block
) const {
485 if (!block
&& next_
== NULL
)
494 void CYString::Output(std::ostream
&out
, CYFlags flags
) const {
496 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
498 case '"': out
<< "\\\""; break;
499 case '\\': out
<< "\\\\"; break;
500 case '\b': out
<< "\\b"; break;
501 case '\f': out
<< "\\f"; break;
502 case '\n': out
<< "\\n"; break;
503 case '\r': out
<< "\\r"; break;
504 case '\t': out
<< "\\t"; break;
505 case '\v': out
<< "\\v"; break;
508 if (*value
< 0x20 || *value
>= 0x7f)
509 out
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value
);
516 void CYSwitch::Output(std::ostream
&out
) const {
518 value_
->Output(out
, CYNoFlags
);
520 if (clauses_
!= NULL
)
525 void CYThis::Output(std::ostream
&out
, CYFlags flags
) const {
526 if ((flags
& CYNoLeader
) != 0)
529 if ((flags
& CYNoTrailer
) != 0)
533 void CYThrow::Output(std::ostream
&out
) const {
536 value_
->Output(out
, CYNoLeader
);
540 void CYTry::Output(std::ostream
&out
) const {
542 try_
->Output(out
, true);
545 if (finally_
!= NULL
) {
547 finally_
->Output(out
, true);
551 void CYVariable::Output(std::ostream
&out
, CYFlags flags
) const {
552 if ((flags
& CYNoLeader
) != 0)
555 if ((flags
& CYNoTrailer
) != 0)
559 void CYWhile::Output(std::ostream
&out
) const {
561 test_
->Output(out
, CYNoFlags
);
563 code_
->Output(out
, false);
566 void CYWith::Output(std::ostream
&out
) const {
568 scope_
->Output(out
, CYNoFlags
);
570 code_
->Output(out
, false);
573 void CYWord::Output(std::ostream
&out
) const {