]> git.saurik.com Git - cycript.git/blame - Output.cpp
Fleshed out support for &, reworked nil internally, setup new Instance(), and impleme...
[cycript.git] / Output.cpp
CommitLineData
5999c315
JF
1#include "Parser.hpp"
2
3#include <iostream>
4#include <iomanip>
5
4afefdd9
JF
6#include <objc/runtime.h>
7#include <sstream>
8
b09da87b
JF
9_finline CYFlags operator ~(CYFlags rhs) {
10 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
11}
12
13_finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
14 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
15}
16
17_finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
18 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
19}
20
21_finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
22 return lhs = lhs | rhs;
23}
24
25_finline CYFlags CYLeft(CYFlags flags) {
26 return flags & ~CYNoTrailer;
27}
28
29_finline CYFlags CYCenter(CYFlags flags) {
30 return flags & CYNoIn;
31}
32
33_finline CYFlags CYRight(CYFlags flags) {
34 return flags & (CYNoIn | CYNoTrailer);
35}
36
37bool CYFalse::Value() const {
38 return false;
39}
40
41bool CYTrue::Value() const {
42 return true;
43}
44
d35a3b07
JF
45#define CYPA 16
46
b09da87b
JF
47void CYAddressOf::Output(std::ostream &out, CYFlags flags) const {
48 rhs_->Output(out, 1, CYLeft(flags));
9b5527f0 49 out << ".$cya()";
5999c315
JF
50}
51
d35a3b07
JF
52void CYArgument::Output(std::ostream &out) const {
53 if (name_ != NULL) {
5999c315
JF
54 out << *name_;
55 if (value_ != NULL)
56 out << ":";
57 }
d35a3b07 58 if (value_ != NULL)
b09da87b 59 value_->Output(out, CYPA, CYNoFlags);
5999c315 60 if (next_ != NULL) {
d35a3b07
JF
61 if (next_->name_ == NULL)
62 out << ',';
63 else
64 out << ' ';
65 next_->Output(out);
5999c315
JF
66 }
67}
68
b09da87b 69void CYArray::Output(std::ostream &out, CYFlags flags) const {
5befe15e
JF
70 out << '[';
71 if (elements_ != NULL)
72 elements_->Output(out);
73 out << ']';
74}
75
b09da87b
JF
76void CYAssignment::Output(std::ostream &out, CYFlags flags) const {
77 lhs_->Output(out, Precedence() - 1, CYLeft(flags));
d35a3b07 78 out << Operator();
b09da87b 79 rhs_->Output(out, Precedence(), CYRight(flags));
d35a3b07
JF
80}
81
b09da87b
JF
82void CYBoolean::Output(std::ostream &out, CYFlags flags) const {
83 if ((flags & CYNoLeader) != 0)
84 out << ' ';
5999c315 85 out << (Value() ? "true" : "false");
b09da87b
JF
86 if ((flags & CYNoTrailer) != 0)
87 out << ' ';
5999c315
JF
88}
89
90void CYBreak::Output(std::ostream &out) const {
91 out << "break";
92 if (label_ != NULL)
93 out << ' ' << *label_;
94 out << ';';
95}
96
b09da87b
JF
97void CYCall::Output(std::ostream &out, CYFlags flags) const {
98 function_->Output(out, Precedence(), CYLeft(flags));
d35a3b07 99 out << '(';
5999c315 100 if (arguments_ != NULL)
d35a3b07 101 arguments_->Output(out);
5999c315
JF
102 out << ')';
103}
104
105void CYCatch::Output(std::ostream &out) const {
106 out << "catch(" << *name_ << ')';
107 code_->Output(out, true);
108}
109
e5bc40db
JF
110void CYCategory::Output(std::ostream &out) const {
111 out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
112 out << "$cyp=object_getClass($cys);";
113 out << "$cyc=$cys;";
114 if (messages_ != NULL)
115 messages_->Output(out, true);
116 out << "})(";
117 name_->ClassName(out);
118 out << ");";
119}
120
b09da87b 121void CYClass::Output(std::ostream &out) const {
e5bc40db
JF
122 out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
123 out << "$cyp=object_getClass($cys);";
b09da87b
JF
124 out << "$cyc=objc_allocateClassPair($cys,\"" << *name_ << "\",0);";
125 out << "$cym=object_getClass($cyc);";
126 if (fields_ != NULL)
127 fields_->Output(out);
128 if (messages_ != NULL)
e5bc40db 129 messages_->Output(out, false);
b09da87b
JF
130 out << "objc_registerClassPair($cyc);";
131 out << "})(";
132 if (super_ != NULL)
133 super_->Output(out, CYPA, CYNoFlags);
134 else
135 out << "null";
136 out << ");";
137}
138
e5bc40db
JF
139void 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) {
145 expression = next;
146 out << ',';
147 next = expression->next_;
148 CYFlags right(next != NULL ? center : CYRight(flags));
149 expression->Output(out, right);
150 }
151 } else
152 expression->Output(out, flags);
153}
154
b09da87b
JF
155void CYCondition::Output(std::ostream &out, CYFlags flags) const {
156 test_->Output(out, Precedence() - 1, CYLeft(flags));
d35a3b07 157 out << '?';
5999c315 158 if (true_ != NULL)
b09da87b 159 true_->Output(out, CYPA, CYNoFlags);
d35a3b07 160 out << ':';
b09da87b 161 false_->Output(out, CYPA, CYRight(flags));
5999c315
JF
162}
163
164void CYContinue::Output(std::ostream &out) const {
165 out << "continue";
166 if (label_ != NULL)
167 out << ' ' << *label_;
168 out << ';';
169}
170
171void CYClause::Output(std::ostream &out) const {
d35a3b07
JF
172 if (case_ != NULL) {
173 out << "case";
b09da87b 174 case_->Output(out, CYNoFlags);
d35a3b07 175 } else
5999c315
JF
176 out << "default";
177 out << ':';
178 if (code_ != NULL)
179 code_->Output(out, false);
180 out << *next_;
181}
182
b09da87b 183// XXX: deal with NoIn
5999c315
JF
184void CYDeclaration::Part(std::ostream &out) const {
185 out << "var ";
186 Output(out);
187}
188
189void CYDeclaration::Output(std::ostream &out) const {
190 out << *identifier_;
d35a3b07
JF
191 if (initialiser_ != NULL) {
192 out << '=';
b09da87b 193 initialiser_->Output(out, CYPA, CYNoFlags);
d35a3b07 194 }
5999c315
JF
195}
196
b09da87b 197// XXX: deal with NoIn
5999c315
JF
198void CYDeclarations::Part(std::ostream &out) const {
199 out << "var ";
d35a3b07 200
5999c315 201 const CYDeclarations *declaration(this);
d35a3b07
JF
202 output:
203 out << *declaration->declaration_;
204 declaration = declaration->next_;
205
206 if (declaration != NULL) {
207 out << ',';
208 goto output;
209 }
5999c315
JF
210}
211
212void CYDeclarations::Output(std::ostream &out) const {
213 Part(out);
214 out << ';';
215}
216
9b5527f0
JF
217void CYDirectMember::Output(std::ostream &out, CYFlags flags) const {
218 object_->Output(out, Precedence(), CYLeft(flags));
219 if (const char *word = property_->Word())
220 out << '.' << word;
221 else {
222 out << '[';
223 property_->Output(out, CYNoFlags);
224 out << ']';
225 }
226}
227
5999c315 228void CYDoWhile::Output(std::ostream &out) const {
b09da87b 229 // XXX: extra space character!
5999c315
JF
230 out << "do ";
231 code_->Output(out, false);
d35a3b07 232 out << "while(";
b09da87b
JF
233 test_->Output(out, CYNoFlags);
234 out << ')';
5999c315
JF
235}
236
5befe15e 237void CYElement::Output(std::ostream &out) const {
5999c315 238 if (value_ != NULL)
b09da87b 239 value_->Output(out, CYPA, CYNoFlags);
5befe15e 240 if (next_ != NULL || value_ == NULL)
5999c315 241 out << ',';
5befe15e
JF
242 if (next_ != NULL)
243 next_->Output(out);
5999c315
JF
244}
245
246void CYEmpty::Output(std::ostream &out) const {
247 out << ';';
248}
249
250void CYEmpty::Output(std::ostream &out, bool block) const {
251 if (next_ != NULL)
252 CYSource::Output(out, block);
253 else
254 out << "{}";
255}
256
257void CYExpress::Output(std::ostream &out) const {
b09da87b 258 expression_->Output(out, CYNoFunction | CYNoBrace);
5999c315
JF
259 out << ';';
260}
261
e5bc40db
JF
262void CYExpression::ClassName(std::ostream &out) const {
263 Output(out, CYPA, CYNoFlags);
264}
265
5999c315 266void CYExpression::Part(std::ostream &out) const {
d35a3b07 267 // XXX: this should handle LeftHandSideExpression
b09da87b 268 Output(out, CYNoIn);
d35a3b07
JF
269}
270
b09da87b
JF
271void CYExpression::Output(std::ostream &out, unsigned precedence, CYFlags flags) const {
272 if (precedence < Precedence()) {
5999c315 273 out << '(';
b09da87b 274 Output(out, CYNoFlags);
5999c315 275 out << ')';
b09da87b
JF
276 } else
277 Output(out, flags);
278}
279
280void CYField::Output(std::ostream &out) const {
281 // XXX: implement!
5999c315
JF
282}
283
284void CYFor::Output(std::ostream &out) const {
285 out << "for(";
286 if (initialiser_ != NULL)
287 initialiser_->Part(out);
288 out << ';';
289 if (test_ != NULL)
b09da87b 290 test_->Output(out, CYNoFlags);
5999c315
JF
291 out << ';';
292 if (increment_ != NULL)
b09da87b 293 increment_->Output(out, CYNoFlags);
5999c315
JF
294 out << ')';
295 code_->Output(out, false);
296}
297
298void CYForIn::Output(std::ostream &out) const {
299 out << "for(";
300 initialiser_->Part(out);
b09da87b
JF
301 // XXX: deal with this space character!
302 out << ' ';
303 out << "in";
304 set_->Output(out, CYNoLeader);
5999c315
JF
305 out << ')';
306 code_->Output(out, false);
307}
308
309void CYFunction::Output(std::ostream &out) const {
b09da87b
JF
310 CYLambda::Output(out, CYNoFlags);
311}
312
313void CYFunctionParameter::Output(std::ostream &out) const {
314 out << *name_;
315 if (next_ != NULL) {
316 out << ',';
317 out << *next_;
318 }
5999c315
JF
319}
320
321void CYIf::Output(std::ostream &out) const {
d35a3b07 322 out << "if(";
b09da87b 323 test_->Output(out, CYNoFlags);
d35a3b07 324 out << ')';
5999c315
JF
325 true_->Output(out, true);
326 if (false_ != NULL) {
327 out << "else ";
328 false_->Output(out, false);
329 }
330}
331
b09da87b
JF
332void CYIndirect::Output(std::ostream &out, CYFlags flags) const {
333 rhs_->Output(out, 1, CYLeft(flags));
9b5527f0
JF
334 out << ".$cyi";
335}
336
337void CYIndirectMember::Output(std::ostream &out, CYFlags flags) const {
338 object_->Output(out, Precedence(), CYLeft(flags));
339 out << ".$cyi";
340 if (const char *word = property_->Word())
341 out << '.' << word;
342 else {
343 out << '[';
344 property_->Output(out, CYNoFlags);
345 out << ']';
346 }
5999c315
JF
347}
348
b09da87b
JF
349void CYInfix::Output(std::ostream &out, CYFlags flags) const {
350 const char *name(Operator());
351 bool protect((flags & CYNoIn) != 0 && strcmp(name, "in"));
352 if (protect)
353 out << '(';
354 bool alphabetic(Alphabetic());
355 CYFlags left(protect ? CYNoFlags : CYLeft(flags));
356 if (alphabetic)
357 left |= CYNoTrailer;
358 lhs_->Output(out, Precedence(), left);
359 out << name;
360 CYFlags right(protect ? CYNoFlags : CYRight(flags));
361 if (alphabetic)
362 right |= CYNoLeader;
363 rhs_->Output(out, Precedence() - 1, right);
364 if (protect)
365 out << ')';
5999c315
JF
366}
367
b09da87b
JF
368void CYLambda::Output(std::ostream &out, CYFlags flags) const {
369 bool protect((flags & CYNoFunction) != 0);
370 if (protect)
371 out << '(';
5999c315
JF
372 out << "function";
373 if (name_ != NULL)
374 out << ' ' << *name_;
375 out << '(';
376 if (parameters_ != NULL)
377 out << *parameters_;
b09da87b
JF
378 out << "){";
379 if (body_ != NULL)
380 body_->Show(out);
381 out << '}';
382 if (protect)
383 out << ')';
5999c315
JF
384}
385
e5bc40db 386void CYMessage::Output(std::ostream &out, bool replace) const {
4e8c99fb 387 if (next_ != NULL)
e5bc40db 388 next_->Output(out, replace);
b09da87b
JF
389 out << "$cyn=new Selector(\"";
390 for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
391 if (parameter->tag_ != NULL) {
392 out << *parameter->tag_;
393 if (parameter->name_ != NULL)
5999c315
JF
394 out << ':';
395 }
b09da87b 396 out << "\");";
e5bc40db
JF
397 out << "$cyt=$cyn.type($cy" << (instance_ ? 's' : 'p') << ");";
398 out << "class_" << (replace ? "replace" : "add") << "Method($cy" << (instance_ ? 'c' : 'm') << ",$cyn,";
478d4ed0 399 out << "new Functor(function(self,_cmd";
b09da87b 400 for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
478d4ed0
JF
401 if (parameter->name_ != NULL)
402 out << ',' << *parameter->name_;
403 out << "){return function(){";
b09da87b
JF
404 if (body_ != NULL)
405 body_->Show(out);
478d4ed0 406 out << "}.call(self);},$cyt),$cyt);";
5999c315
JF
407}
408
b09da87b 409void CYNew::Output(std::ostream &out, CYFlags flags) const {
bce8339b
JF
410 if ((flags & CYNoLeader) != 0)
411 out << ' ';
d35a3b07 412 out << "new";
b09da87b 413 constructor_->Output(out, Precedence(), CYCenter(flags) | CYNoLeader);
d35a3b07 414 out << '(';
5999c315 415 if (arguments_ != NULL)
d35a3b07 416 arguments_->Output(out);
5999c315
JF
417 out << ')';
418}
419
b09da87b
JF
420void CYNull::Output(std::ostream &out, CYFlags flags) const {
421 if ((flags & CYNoLeader) != 0)
422 out << ' ';
5999c315 423 CYWord::Output(out);
b09da87b
JF
424 if ((flags & CYNoTrailer) != 0)
425 out << ' ';
5999c315
JF
426}
427
b09da87b
JF
428void CYNumber::Output(std::ostream &out, CYFlags flags) const {
429 if ((flags & CYNoLeader) != 0)
430 out << ' ';
18401062
JF
431 // XXX: decide on correct precision
432 out << std::setprecision(9) << Value();
b09da87b
JF
433 if ((flags & CYNoTrailer) != 0)
434 out << ' ';
5999c315
JF
435}
436
e5bc40db
JF
437void CYNumber::PropertyName(std::ostream &out) const {
438 Output(out);
439}
440
b09da87b
JF
441void CYObject::Output(std::ostream &out, CYFlags flags) const {
442 bool protect((flags & CYNoBrace) != 0);
443 if (protect)
444 out << '(';
693d501b
JF
445 out << '{';
446 if (property_ != NULL)
447 property_->Output(out);
448 out << '}';
b09da87b
JF
449 if (protect)
450 out << ')';
693d501b
JF
451}
452
b09da87b
JF
453void CYPostfix::Output(std::ostream &out, CYFlags flags) const {
454 lhs_->Output(out, Precedence(), CYLeft(flags));
d35a3b07 455 out << Operator();
5999c315
JF
456}
457
b09da87b
JF
458void CYPrefix::Output(std::ostream &out, CYFlags flags) const {
459 bool alphabetic(Alphabetic());
d35a3b07 460 out << Operator();
b09da87b
JF
461 CYFlags right(CYRight(flags));
462 if (alphabetic)
463 right |= CYNoLeader;
464 rhs_->Output(out, Precedence(), right);
5999c315
JF
465}
466
693d501b 467void CYProperty::Output(std::ostream &out) const {
e5bc40db 468 name_->PropertyName(out);
579ed526 469 out << ':';
b09da87b 470 value_->Output(out, CYPA, CYNoFlags);
5999c315
JF
471 if (next_ != NULL) {
472 out << ',';
693d501b 473 next_->Output(out);
5999c315 474 }
5999c315
JF
475}
476
477void CYReturn::Output(std::ostream &out) const {
478 out << "return";
b09da87b
JF
479 if (value_ != NULL)
480 value_->Output(out, CYNoLeader);
5999c315
JF
481 out << ';';
482}
483
b09da87b 484void CYSelector::Output(std::ostream &out, CYFlags flags) const {
bce8339b
JF
485 if ((flags & CYNoLeader) != 0)
486 out << ' ';
b09da87b 487 out << "new Selector(\"";
62014ea9
JF
488 if (name_ != NULL)
489 name_->Output(out);
dea834b0 490 out << "\")";
e7ed5354
JF
491}
492
62014ea9
JF
493void CYSelectorPart::Output(std::ostream &out) const {
494 if (name_ != NULL)
495 out << *name_;
496 if (value_)
497 out << ':';
498 if (next_ != NULL)
499 next_->Output(out);
500}
501
b09da87b 502void CYSend::Output(std::ostream &out, CYFlags flags) const {
c239b9f8
JF
503 if ((flags & CYNoLeader) != 0)
504 out << ' ';
b09da87b
JF
505 out << "objc_msgSend(";
506 self_->Output(out, CYPA, CYNoFlags);
4afefdd9
JF
507 out << ",";
508 std::ostringstream name;
b09da87b
JF
509 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
510 if (argument->name_ != NULL) {
4afefdd9 511 name << *argument->name_;
b09da87b 512 if (argument->value_ != NULL)
4afefdd9 513 name << ':';
b09da87b 514 }
4afefdd9 515 out << reinterpret_cast<void *>(sel_registerName(name.str().c_str()));
b09da87b
JF
516 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
517 if (argument->value_ != NULL) {
518 out << ",";
519 argument->value_->Output(out, CYPA, CYNoFlags);
520 }
521 out << ')';
522}
523
b1ff2d78 524void CYSource::Show(std::ostream &out) const {
5999c315 525 for (const CYSource *next(this); next != NULL; next = next->next_)
b09da87b 526 next->Output(out);
5999c315
JF
527}
528
529void CYSource::Output(std::ostream &out, bool block) const {
530 if (!block && next_ == NULL)
531 Output(out);
532 else {
533 out << '{';
b1ff2d78 534 Show(out);
5999c315
JF
535 out << '}';
536 }
537}
538
b09da87b 539void CYString::Output(std::ostream &out, CYFlags flags) const {
b4aa79af
JF
540 unsigned quot(0), apos(0);
541 for (const char *value(value_), *end(value_ + size_); value != end; ++value)
542 if (*value == '"')
543 ++quot;
544 else if (*value == '\'')
545 ++apos;
546
547 bool single(quot > apos);
548
549 out << (single ? '\'' : '"');
5999c315
JF
550 for (const char *value(value_), *end(value_ + size_); value != end; ++value)
551 switch (*value) {
5999c315
JF
552 case '\\': out << "\\\\"; break;
553 case '\b': out << "\\b"; break;
554 case '\f': out << "\\f"; break;
555 case '\n': out << "\\n"; break;
556 case '\r': out << "\\r"; break;
557 case '\t': out << "\\t"; break;
558 case '\v': out << "\\v"; break;
559
b4aa79af
JF
560 case '"':
561 if (!single)
562 out << "\\\"";
563 else goto simple;
564 break;
565
566 case '\'':
567 if (single)
568 out << "\\'";
569 else goto simple;
570 break;
571
5999c315
JF
572 default:
573 if (*value < 0x20 || *value >= 0x7f)
574 out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
b4aa79af 575 else simple:
5999c315
JF
576 out << *value;
577 }
b4aa79af 578 out << (single ? '\'' : '"');
5999c315
JF
579}
580
e5bc40db
JF
581void CYString::PropertyName(std::ostream &out) const {
582 if (const char *word = Word())
583 out << word;
584 else
585 Output(out);
586}
587
5999c315 588void CYSwitch::Output(std::ostream &out) const {
d35a3b07 589 out << "switch(";
b09da87b 590 value_->Output(out, CYNoFlags);
d35a3b07 591 out << "){";
5999c315
JF
592 if (clauses_ != NULL)
593 out << *clauses_;
594 out << '}';
595}
596
b09da87b
JF
597void CYThis::Output(std::ostream &out, CYFlags flags) const {
598 if ((flags & CYNoLeader) != 0)
599 out << ' ';
5999c315 600 CYWord::Output(out);
b09da87b
JF
601 if ((flags & CYNoTrailer) != 0)
602 out << ' ';
5999c315
JF
603}
604
605void CYThrow::Output(std::ostream &out) const {
d35a3b07 606 out << "throw";
b09da87b
JF
607 if (value_ != NULL)
608 value_->Output(out, CYNoLeader);
5999c315
JF
609 out << ';';
610}
611
612void CYTry::Output(std::ostream &out) const {
613 out << "try";
614 try_->Output(out, true);
615 if (catch_ != NULL)
9b5527f0 616 catch_->Output(out);
5999c315
JF
617 if (finally_ != NULL) {
618 out << "finally";
619 finally_->Output(out, true);
620 }
621}
622
b09da87b 623void CYVariable::Output(std::ostream &out, CYFlags flags) const {
478d4ed0
JF
624 if ((flags & CYNoLeader) != 0)
625 out << ' ';
5999c315 626 out << *name_;
478d4ed0
JF
627 if ((flags & CYNoTrailer) != 0)
628 out << ' ';
5999c315
JF
629}
630
631void CYWhile::Output(std::ostream &out) const {
d35a3b07 632 out << "while(";
b09da87b 633 test_->Output(out, CYNoFlags);
d35a3b07 634 out << ')';
5999c315
JF
635 code_->Output(out, false);
636}
637
638void CYWith::Output(std::ostream &out) const {
d35a3b07 639 out << "with(";
b09da87b 640 scope_->Output(out, CYNoFlags);
d35a3b07 641 out << ')';
5999c315
JF
642 code_->Output(out, false);
643}
644
e5bc40db
JF
645void CYWord::ClassName(std::ostream &out) const {
646 out << "objc_getClass(\"" << Value() << "\")";
647}
648
5999c315
JF
649void CYWord::Output(std::ostream &out) const {
650 out << Value();
651}
e5bc40db
JF
652
653void CYWord::PropertyName(std::ostream &out) const {
654 Output(out);
655}