]> git.saurik.com Git - cycript.git/blob - Output.cpp
Remove system.global temporarily, added libffi Depends:, and setup REPL with _ variab...
[cycript.git] / Output.cpp
1 #include "Parser.hpp"
2
3 #include <iostream>
4 #include <iomanip>
5
6 _finline CYFlags operator ~(CYFlags rhs) {
7 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
8 }
9
10 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
11 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
12 }
13
14 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
15 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
16 }
17
18 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
19 return lhs = lhs | rhs;
20 }
21
22 _finline CYFlags CYLeft(CYFlags flags) {
23 return flags & ~CYNoTrailer;
24 }
25
26 _finline CYFlags CYCenter(CYFlags flags) {
27 return flags & CYNoIn;
28 }
29
30 _finline CYFlags CYRight(CYFlags flags) {
31 return flags & (CYNoIn | CYNoTrailer);
32 }
33
34 bool CYFalse::Value() const {
35 return false;
36 }
37
38 bool CYTrue::Value() const {
39 return true;
40 }
41
42 #define CYPA 16
43
44 void CYAddressOf::Output(std::ostream &out, CYFlags flags) const {
45 rhs_->Output(out, 1, CYLeft(flags));
46 out << ".$()";
47 }
48
49 void CYArgument::Output(std::ostream &out) const {
50 if (name_ != NULL) {
51 out << *name_;
52 if (value_ != NULL)
53 out << ":";
54 }
55 if (value_ != NULL)
56 value_->Output(out, CYPA, CYNoFlags);
57 if (next_ != NULL) {
58 if (next_->name_ == NULL)
59 out << ',';
60 else
61 out << ' ';
62 next_->Output(out);
63 }
64 }
65
66 void CYArray::Output(std::ostream &out, CYFlags flags) const {
67 out << '[';
68 if (elements_ != NULL)
69 elements_->Output(out);
70 out << ']';
71 }
72
73 void CYAssignment::Output(std::ostream &out, CYFlags flags) const {
74 lhs_->Output(out, Precedence() - 1, CYLeft(flags));
75 out << Operator();
76 rhs_->Output(out, Precedence(), CYRight(flags));
77 }
78
79 void CYBoolean::Output(std::ostream &out, CYFlags flags) const {
80 if ((flags & CYNoLeader) != 0)
81 out << ' ';
82 out << (Value() ? "true" : "false");
83 if ((flags & CYNoTrailer) != 0)
84 out << ' ';
85 }
86
87 void CYBreak::Output(std::ostream &out) const {
88 out << "break";
89 if (label_ != NULL)
90 out << ' ' << *label_;
91 out << ';';
92 }
93
94 void CYCall::Output(std::ostream &out, CYFlags flags) const {
95 function_->Output(out, Precedence(), CYLeft(flags));
96 out << '(';
97 if (arguments_ != NULL)
98 arguments_->Output(out);
99 out << ')';
100 }
101
102 void CYCatch::Output(std::ostream &out) const {
103 out << "catch(" << *name_ << ')';
104 code_->Output(out, true);
105 }
106
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);";
111 if (fields_ != NULL)
112 fields_->Output(out);
113 if (messages_ != NULL)
114 messages_->Output(out);
115 out << "objc_registerClassPair($cyc);";
116 out << "})(";
117 if (super_ != NULL)
118 super_->Output(out, CYPA, CYNoFlags);
119 else
120 out << "null";
121 out << ");";
122 }
123
124 void CYCondition::Output(std::ostream &out, CYFlags flags) const {
125 test_->Output(out, Precedence() - 1, CYLeft(flags));
126 out << '?';
127 if (true_ != NULL)
128 true_->Output(out, CYPA, CYNoFlags);
129 out << ':';
130 false_->Output(out, CYPA, CYRight(flags));
131 }
132
133 void CYContinue::Output(std::ostream &out) const {
134 out << "continue";
135 if (label_ != NULL)
136 out << ' ' << *label_;
137 out << ';';
138 }
139
140 void CYClause::Output(std::ostream &out) const {
141 if (case_ != NULL) {
142 out << "case";
143 case_->Output(out, CYNoFlags);
144 } else
145 out << "default";
146 out << ':';
147 if (code_ != NULL)
148 code_->Output(out, false);
149 out << *next_;
150 }
151
152 // XXX: deal with NoIn
153 void CYDeclaration::Part(std::ostream &out) const {
154 out << "var ";
155 Output(out);
156 }
157
158 void CYDeclaration::Output(std::ostream &out) const {
159 out << *identifier_;
160 if (initialiser_ != NULL) {
161 out << '=';
162 initialiser_->Output(out, CYPA, CYNoFlags);
163 }
164 }
165
166 // XXX: deal with NoIn
167 void CYDeclarations::Part(std::ostream &out) const {
168 out << "var ";
169
170 const CYDeclarations *declaration(this);
171 output:
172 out << *declaration->declaration_;
173 declaration = declaration->next_;
174
175 if (declaration != NULL) {
176 out << ',';
177 goto output;
178 }
179 }
180
181 void CYDeclarations::Output(std::ostream &out) const {
182 Part(out);
183 out << ';';
184 }
185
186 void CYDoWhile::Output(std::ostream &out) const {
187 // XXX: extra space character!
188 out << "do ";
189 code_->Output(out, false);
190 out << "while(";
191 test_->Output(out, CYNoFlags);
192 out << ')';
193 }
194
195 void CYElement::Output(std::ostream &out) const {
196 if (value_ != NULL)
197 value_->Output(out, CYPA, CYNoFlags);
198 if (next_ != NULL || value_ == NULL)
199 out << ',';
200 if (next_ != NULL)
201 next_->Output(out);
202 }
203
204 void CYEmpty::Output(std::ostream &out) const {
205 out << ';';
206 }
207
208 void CYEmpty::Output(std::ostream &out, bool block) const {
209 if (next_ != NULL)
210 CYSource::Output(out, block);
211 else
212 out << "{}";
213 }
214
215 void CYExpress::Output(std::ostream &out) const {
216 expression_->Output(out, CYNoFunction | CYNoBrace);
217 out << ';';
218 }
219
220 void CYExpression::Part(std::ostream &out) const {
221 // XXX: this should handle LeftHandSideExpression
222 Output(out, CYNoIn);
223 }
224
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) {
231 expression = next;
232 out << ',';
233 next = expression->next_;
234 CYFlags right(next != NULL ? center : CYRight(flags));
235 expression->Output(out, right);
236 }
237 } else
238 expression->Output(out, flags);
239 }
240
241 void CYExpression::Output(std::ostream &out, unsigned precedence, CYFlags flags) const {
242 if (precedence < Precedence()) {
243 out << '(';
244 Output(out, CYNoFlags);
245 out << ')';
246 } else
247 Output(out, flags);
248 }
249
250 void CYField::Output(std::ostream &out) const {
251 // XXX: implement!
252 }
253
254 void CYFor::Output(std::ostream &out) const {
255 out << "for(";
256 if (initialiser_ != NULL)
257 initialiser_->Part(out);
258 out << ';';
259 if (test_ != NULL)
260 test_->Output(out, CYNoFlags);
261 out << ';';
262 if (increment_ != NULL)
263 increment_->Output(out, CYNoFlags);
264 out << ')';
265 code_->Output(out, false);
266 }
267
268 void CYForIn::Output(std::ostream &out) const {
269 out << "for(";
270 initialiser_->Part(out);
271 // XXX: deal with this space character!
272 out << ' ';
273 out << "in";
274 set_->Output(out, CYNoLeader);
275 out << ')';
276 code_->Output(out, false);
277 }
278
279 void CYFunction::Output(std::ostream &out) const {
280 CYLambda::Output(out, CYNoFlags);
281 }
282
283 void CYFunctionParameter::Output(std::ostream &out) const {
284 out << *name_;
285 if (next_ != NULL) {
286 out << ',';
287 out << *next_;
288 }
289 }
290
291 void CYIf::Output(std::ostream &out) const {
292 out << "if(";
293 test_->Output(out, CYNoFlags);
294 out << ')';
295 true_->Output(out, true);
296 if (false_ != NULL) {
297 out << "else ";
298 false_->Output(out, false);
299 }
300 }
301
302 void CYIndirect::Output(std::ostream &out, CYFlags flags) const {
303 rhs_->Output(out, 1, CYLeft(flags));
304 out << "[0]";
305 }
306
307 void CYInfix::Output(std::ostream &out, CYFlags flags) const {
308 const char *name(Operator());
309 bool protect((flags & CYNoIn) != 0 && strcmp(name, "in"));
310 if (protect)
311 out << '(';
312 bool alphabetic(Alphabetic());
313 CYFlags left(protect ? CYNoFlags : CYLeft(flags));
314 if (alphabetic)
315 left |= CYNoTrailer;
316 lhs_->Output(out, Precedence(), left);
317 out << name;
318 CYFlags right(protect ? CYNoFlags : CYRight(flags));
319 if (alphabetic)
320 right |= CYNoLeader;
321 rhs_->Output(out, Precedence() - 1, right);
322 if (protect)
323 out << ')';
324 }
325
326 void CYLambda::Output(std::ostream &out, CYFlags flags) const {
327 bool protect((flags & CYNoFunction) != 0);
328 if (protect)
329 out << '(';
330 out << "function";
331 if (name_ != NULL)
332 out << ' ' << *name_;
333 out << '(';
334 if (parameters_ != NULL)
335 out << *parameters_;
336 out << "){";
337 if (body_ != NULL)
338 body_->Show(out);
339 out << '}';
340 if (protect)
341 out << ')';
342 }
343
344 void CYMember::Output(std::ostream &out, CYFlags flags) const {
345 object_->Output(out, Precedence(), CYLeft(flags));
346 if (const char *word = property_->Word())
347 out << '.' << word;
348 else {
349 out << '[';
350 property_->Output(out, CYNoFlags);
351 out << ']';
352 }
353 }
354
355 void CYMessage::Output(std::ostream &out) const {
356 if (next_ != NULL)
357 next_->Output(out);
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)
363 out << ':';
364 }
365 out << "\");";
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(){";
373 if (body_ != NULL)
374 body_->Show(out);
375 out << "}.call(self);},$cyt),$cyt);";
376 }
377
378 void CYNew::Output(std::ostream &out, CYFlags flags) const {
379 out << "new";
380 constructor_->Output(out, Precedence(), CYCenter(flags) | CYNoLeader);
381 out << '(';
382 if (arguments_ != NULL)
383 arguments_->Output(out);
384 out << ')';
385 }
386
387 void CYNull::Output(std::ostream &out, CYFlags flags) const {
388 if ((flags & CYNoLeader) != 0)
389 out << ' ';
390 CYWord::Output(out);
391 if ((flags & CYNoTrailer) != 0)
392 out << ' ';
393 }
394
395 void CYNumber::Output(std::ostream &out, CYFlags flags) const {
396 if ((flags & CYNoLeader) != 0)
397 out << ' ';
398 // XXX: this is not a useful formatting
399 out << Value();
400 if ((flags & CYNoTrailer) != 0)
401 out << ' ';
402 }
403
404 void CYObject::Output(std::ostream &out, CYFlags flags) const {
405 bool protect((flags & CYNoBrace) != 0);
406 if (protect)
407 out << '(';
408 out << '{';
409 if (property_ != NULL)
410 property_->Output(out);
411 out << '}';
412 if (protect)
413 out << ')';
414 }
415
416 void CYPostfix::Output(std::ostream &out, CYFlags flags) const {
417 lhs_->Output(out, Precedence(), CYLeft(flags));
418 out << Operator();
419 }
420
421 void CYPrefix::Output(std::ostream &out, CYFlags flags) const {
422 bool alphabetic(Alphabetic());
423 out << Operator();
424 CYFlags right(CYRight(flags));
425 if (alphabetic)
426 right |= CYNoLeader;
427 rhs_->Output(out, Precedence(), right);
428 }
429
430 void CYProperty::Output(std::ostream &out) const {
431 out << *name_;
432 out << ':';
433 value_->Output(out, CYPA, CYNoFlags);
434 if (next_ != NULL) {
435 out << ',';
436 next_->Output(out);
437 }
438 }
439
440 void CYReturn::Output(std::ostream &out) const {
441 out << "return";
442 if (value_ != NULL)
443 value_->Output(out, CYNoLeader);
444 out << ';';
445 }
446
447 void CYSelector::Output(std::ostream &out, CYFlags flags) const {
448 out << "new Selector(\"";
449 if (name_ != NULL)
450 name_->Output(out);
451 out << "\")";
452 }
453
454 void CYSelectorPart::Output(std::ostream &out) const {
455 if (name_ != NULL)
456 out << *name_;
457 if (value_)
458 out << ':';
459 if (next_ != NULL)
460 next_->Output(out);
461 }
462
463 void CYSend::Output(std::ostream &out, CYFlags flags) const {
464 out << "objc_msgSend(";
465 self_->Output(out, CYPA, CYNoFlags);
466 out << ",\"";
467 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
468 if (argument->name_ != NULL) {
469 out << *argument->name_;
470 if (argument->value_ != NULL)
471 out << ':';
472 }
473 out << "\"";
474 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
475 if (argument->value_ != NULL) {
476 out << ",";
477 argument->value_->Output(out, CYPA, CYNoFlags);
478 }
479 out << ')';
480 }
481
482 void CYSource::Show(std::ostream &out) const {
483 for (const CYSource *next(this); next != NULL; next = next->next_)
484 next->Output(out);
485 }
486
487 void CYSource::Output(std::ostream &out, bool block) const {
488 if (!block && next_ == NULL)
489 Output(out);
490 else {
491 out << '{';
492 Show(out);
493 out << '}';
494 }
495 }
496
497 void CYString::Output(std::ostream &out, CYFlags flags) const {
498 out << '\"';
499 for (const char *value(value_), *end(value_ + size_); value != end; ++value)
500 switch (*value) {
501 case '"': out << "\\\""; break;
502 case '\\': out << "\\\\"; break;
503 case '\b': out << "\\b"; break;
504 case '\f': out << "\\f"; break;
505 case '\n': out << "\\n"; break;
506 case '\r': out << "\\r"; break;
507 case '\t': out << "\\t"; break;
508 case '\v': out << "\\v"; break;
509
510 default:
511 if (*value < 0x20 || *value >= 0x7f)
512 out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
513 else
514 out << *value;
515 }
516 out << '\"';
517 }
518
519 void CYSwitch::Output(std::ostream &out) const {
520 out << "switch(";
521 value_->Output(out, CYNoFlags);
522 out << "){";
523 if (clauses_ != NULL)
524 out << *clauses_;
525 out << '}';
526 }
527
528 void CYThis::Output(std::ostream &out, CYFlags flags) const {
529 if ((flags & CYNoLeader) != 0)
530 out << ' ';
531 CYWord::Output(out);
532 if ((flags & CYNoTrailer) != 0)
533 out << ' ';
534 }
535
536 void CYThrow::Output(std::ostream &out) const {
537 out << "throw";
538 if (value_ != NULL)
539 value_->Output(out, CYNoLeader);
540 out << ';';
541 }
542
543 void CYTry::Output(std::ostream &out) const {
544 out << "try";
545 try_->Output(out, true);
546 if (catch_ != NULL)
547 out << catch_;
548 if (finally_ != NULL) {
549 out << "finally";
550 finally_->Output(out, true);
551 }
552 }
553
554 void CYVariable::Output(std::ostream &out, CYFlags flags) const {
555 if ((flags & CYNoLeader) != 0)
556 out << ' ';
557 out << *name_;
558 if ((flags & CYNoTrailer) != 0)
559 out << ' ';
560 }
561
562 void CYWhile::Output(std::ostream &out) const {
563 out << "while(";
564 test_->Output(out, CYNoFlags);
565 out << ')';
566 code_->Output(out, false);
567 }
568
569 void CYWith::Output(std::ostream &out) const {
570 out << "with(";
571 scope_->Output(out, CYNoFlags);
572 out << ')';
573 code_->Output(out, false);
574 }
575
576 void CYWord::Output(std::ostream &out) const {
577 out << Value();
578 }