]> git.saurik.com Git - cycript.git/blame - Output.cpp
Added console commands.
[cycript.git] / Output.cpp
CommitLineData
5999c315
JF
1#include "Parser.hpp"
2
3#include <iostream>
4#include <iomanip>
5
d35a3b07
JF
6#define CYPA 16
7
5999c315 8void CYAddressOf::Output(std::ostream &out) const {
d35a3b07
JF
9 rhs_->Output(out, 1);
10 out << ".$()";
5999c315
JF
11}
12
d35a3b07
JF
13void CYArgument::Output(std::ostream &out) const {
14 if (name_ != NULL) {
5999c315
JF
15 out << *name_;
16 if (value_ != NULL)
17 out << ":";
18 }
d35a3b07
JF
19 if (value_ != NULL)
20 value_->Output(out, false);
5999c315 21 if (next_ != NULL) {
d35a3b07
JF
22 if (next_->name_ == NULL)
23 out << ',';
24 else
25 out << ' ';
26 next_->Output(out);
5999c315
JF
27 }
28}
29
5befe15e
JF
30void CYArray::Output(std::ostream &out) const {
31 out << '[';
32 if (elements_ != NULL)
33 elements_->Output(out);
34 out << ']';
35}
36
d35a3b07
JF
37void CYAssignment::Output(std::ostream &out) const {
38 lhs_->Output(out, Precedence() - 1);
39 out << Operator();
40 rhs_->Output(out, Precedence());
41}
42
5999c315
JF
43void CYBoolean::Output(std::ostream &out) const {
44 out << (Value() ? "true" : "false");
45}
46
47void CYBreak::Output(std::ostream &out) const {
48 out << "break";
49 if (label_ != NULL)
50 out << ' ' << *label_;
51 out << ';';
52}
53
54void CYCall::Output(std::ostream &out) const {
dea834b0 55 function_->Output(out, Precedence());
d35a3b07 56 out << '(';
5999c315 57 if (arguments_ != NULL)
d35a3b07 58 arguments_->Output(out);
5999c315
JF
59 out << ')';
60}
61
62void CYCatch::Output(std::ostream &out) const {
63 out << "catch(" << *name_ << ')';
64 code_->Output(out, true);
65}
66
67void CYCondition::Output(std::ostream &out) const {
d35a3b07
JF
68 test_->Output(out, Precedence() - 1);
69 out << '?';
5999c315 70 if (true_ != NULL)
d35a3b07
JF
71 true_->Output(out, CYPA);
72 out << ':';
73 false_->Output(out, CYPA);
5999c315
JF
74}
75
76void CYContinue::Output(std::ostream &out) const {
77 out << "continue";
78 if (label_ != NULL)
79 out << ' ' << *label_;
80 out << ';';
81}
82
83void CYClause::Output(std::ostream &out) const {
d35a3b07
JF
84 if (case_ != NULL) {
85 out << "case";
86 case_->Output(out);
87 } else
5999c315
JF
88 out << "default";
89 out << ':';
90 if (code_ != NULL)
91 code_->Output(out, false);
92 out << *next_;
93}
94
95void CYDeclaration::Part(std::ostream &out) const {
96 out << "var ";
97 Output(out);
98}
99
100void CYDeclaration::Output(std::ostream &out) const {
101 out << *identifier_;
d35a3b07
JF
102 if (initialiser_ != NULL) {
103 out << '=';
104 initialiser_->Output(out, CYPA);
105 }
5999c315
JF
106}
107
108void CYDeclarations::Part(std::ostream &out) const {
109 out << "var ";
d35a3b07 110
5999c315 111 const CYDeclarations *declaration(this);
d35a3b07
JF
112 output:
113 out << *declaration->declaration_;
114 declaration = declaration->next_;
115
116 if (declaration != NULL) {
117 out << ',';
118 goto output;
119 }
5999c315
JF
120}
121
122void CYDeclarations::Output(std::ostream &out) const {
123 Part(out);
124 out << ';';
125}
126
127void CYDoWhile::Output(std::ostream &out) const {
128 out << "do ";
129 code_->Output(out, false);
d35a3b07
JF
130 out << "while(";
131 test_->Output(out);
132 out << ';';
5999c315
JF
133}
134
5befe15e 135void CYElement::Output(std::ostream &out) const {
5999c315 136 if (value_ != NULL)
d35a3b07 137 value_->Output(out, CYPA);
5befe15e 138 if (next_ != NULL || value_ == NULL)
5999c315 139 out << ',';
5befe15e
JF
140 if (next_ != NULL)
141 next_->Output(out);
5999c315
JF
142}
143
144void CYEmpty::Output(std::ostream &out) const {
145 out << ';';
146}
147
148void CYEmpty::Output(std::ostream &out, bool block) const {
149 if (next_ != NULL)
150 CYSource::Output(out, block);
151 else
152 out << "{}";
153}
154
155void CYExpress::Output(std::ostream &out) const {
d35a3b07 156 expression_->Output(out);
5999c315
JF
157 out << ';';
158}
159
160void CYExpression::Part(std::ostream &out) const {
d35a3b07
JF
161 // XXX: this should notice "in" expressions
162 // XXX: this should handle LeftHandSideExpression
163 Output(out);
164}
165
166void CYCompound::Output(std::ostream &out) const {
167 if (CYExpression *expression = expressions_)
168 for (;;) {
169 expression->Output(out);
170 expression = expression->next_;
171 if (expression == NULL)
172 break;
173 out << ',';
174 }
5999c315
JF
175}
176
d35a3b07
JF
177void CYExpression::Output(std::ostream &out, unsigned precedence) const {
178 bool protect(precedence < Precedence());
179 if (protect)
5999c315
JF
180 out << '(';
181 Output(out);
d35a3b07 182 if (protect)
5999c315
JF
183 out << ')';
184}
185
186void CYFor::Output(std::ostream &out) const {
187 out << "for(";
188 if (initialiser_ != NULL)
189 initialiser_->Part(out);
190 out << ';';
191 if (test_ != NULL)
d35a3b07 192 test_->Output(out);
5999c315
JF
193 out << ';';
194 if (increment_ != NULL)
d35a3b07 195 increment_->Output(out);
5999c315
JF
196 out << ')';
197 code_->Output(out, false);
198}
199
200void CYForIn::Output(std::ostream &out) const {
201 out << "for(";
202 initialiser_->Part(out);
203 out << " in ";
d35a3b07 204 set_->Output(out);
5999c315
JF
205 out << ')';
206 code_->Output(out, false);
207}
208
209void CYFunction::Output(std::ostream &out) const {
210 CYLambda::Output(out);
211}
212
213void CYIf::Output(std::ostream &out) const {
d35a3b07
JF
214 out << "if(";
215 test_->Output(out);
216 out << ')';
5999c315
JF
217 true_->Output(out, true);
218 if (false_ != NULL) {
219 out << "else ";
220 false_->Output(out, false);
221 }
222}
223
224void CYIndirect::Output(std::ostream &out) const {
d35a3b07
JF
225 rhs_->Output(out, 1);
226 out << "[0]";
5999c315
JF
227}
228
229void CYInfix::Output(std::ostream &out) const {
d35a3b07
JF
230 lhs_->Output(out, Precedence());
231 out << Operator();
232 rhs_->Output(out, Precedence() - 1);
5999c315
JF
233}
234
235void CYLambda::Output(std::ostream &out) const {
236 out << "function";
237 if (name_ != NULL)
238 out << ' ' << *name_;
239 out << '(';
240 if (parameters_ != NULL)
241 out << *parameters_;
242 out << ')';
243 body_->Output(out, true);
244}
245
246void CYMember::Output(std::ostream &out) const {
d35a3b07 247 object_->Output(out, Precedence());
dea834b0
JF
248 if (const char *word = property_->Word())
249 out << '.' << word;
250 else {
251 out << '[';
252 property_->Output(out);
253 out << ']';
254 }
5999c315
JF
255}
256
257void CYMessage::Output(std::ostream &out) const {
258 out << "objc_msgSend(";
d35a3b07 259 self_->Output(out, CYPA);
5999c315
JF
260 out << ",\"";
261 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
262 if (argument->name_ != NULL) {
263 out << *argument->name_;
264 if (argument->value_ != NULL)
265 out << ':';
266 }
267 out << "\"";
d35a3b07
JF
268 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
269 if (argument->value_ != NULL) {
270 out << ",";
271 argument->value_->Output(out, CYPA);
272 }
5999c315
JF
273 out << ')';
274}
275
276void CYNew::Output(std::ostream &out) const {
d35a3b07
JF
277 out << "new";
278 // XXX: I don't /always/ need this character
279 out << ' ';
280 constructor_->Output(out, Precedence());
281 out << '(';
5999c315 282 if (arguments_ != NULL)
d35a3b07 283 arguments_->Output(out);
5999c315
JF
284 out << ')';
285}
286
287void CYNull::Output(std::ostream &out) const {
288 CYWord::Output(out);
289}
290
291void CYNumber::Output(std::ostream &out) const {
292 // XXX: this is not a useful formatting
293 out << Value();
294}
295
693d501b
JF
296void CYObject::Output(std::ostream &out) const {
297 out << '{';
298 if (property_ != NULL)
299 property_->Output(out);
300 out << '}';
301}
302
5999c315
JF
303void CYParameter::Output(std::ostream &out) const {
304 out << *name_;
305 if (next_ != NULL) {
306 out << ',';
307 out << *next_;
308 }
309}
310
311void CYPostfix::Output(std::ostream &out) const {
d35a3b07
JF
312 lhs_->Output(out, Precedence());
313 out << Operator();
5999c315
JF
314}
315
316void CYPrefix::Output(std::ostream &out) const {
d35a3b07
JF
317 out << Operator();
318 rhs_->Output(out, Precedence());
5999c315
JF
319}
320
693d501b 321void CYProperty::Output(std::ostream &out) const {
d35a3b07
JF
322 out << *name_ << ':';
323 value_->Output(out, CYPA);
5999c315
JF
324 if (next_ != NULL) {
325 out << ',';
693d501b 326 next_->Output(out);
5999c315 327 }
5999c315
JF
328}
329
330void CYReturn::Output(std::ostream &out) const {
331 out << "return";
d35a3b07
JF
332 if (value_ != NULL) {
333 out << ' ';
334 value_->Output(out);
335 }
5999c315
JF
336 out << ';';
337}
338
e7ed5354 339void CYSelector::Output(std::ostream &out) const {
dea834b0 340 out << "new SEL(\"";
62014ea9
JF
341 if (name_ != NULL)
342 name_->Output(out);
dea834b0 343 out << "\")";
e7ed5354
JF
344}
345
62014ea9
JF
346void CYSelectorPart::Output(std::ostream &out) const {
347 if (name_ != NULL)
348 out << *name_;
349 if (value_)
350 out << ':';
351 if (next_ != NULL)
352 next_->Output(out);
353}
354
b1ff2d78 355void CYSource::Show(std::ostream &out) const {
5999c315 356 for (const CYSource *next(this); next != NULL; next = next->next_)
931b816a 357 next->Output(out, false);
5999c315
JF
358}
359
360void CYSource::Output(std::ostream &out, bool block) const {
361 if (!block && next_ == NULL)
362 Output(out);
363 else {
364 out << '{';
b1ff2d78 365 Show(out);
5999c315
JF
366 out << '}';
367 }
368}
369
370void CYString::Output(std::ostream &out) const {
371 out << '\"';
372 for (const char *value(value_), *end(value_ + size_); value != end; ++value)
373 switch (*value) {
374 case '"': out << "\\\""; break;
375 case '\\': out << "\\\\"; break;
376 case '\b': out << "\\b"; break;
377 case '\f': out << "\\f"; break;
378 case '\n': out << "\\n"; break;
379 case '\r': out << "\\r"; break;
380 case '\t': out << "\\t"; break;
381 case '\v': out << "\\v"; break;
382
383 default:
384 if (*value < 0x20 || *value >= 0x7f)
385 out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
386 else
387 out << *value;
388 }
389 out << '\"';
390}
391
392void CYSwitch::Output(std::ostream &out) const {
d35a3b07
JF
393 out << "switch(";
394 value_->Output(out);
395 out << "){";
5999c315
JF
396 if (clauses_ != NULL)
397 out << *clauses_;
398 out << '}';
399}
400
401void CYThis::Output(std::ostream &out) const {
402 CYWord::Output(out);
403}
404
405void CYThrow::Output(std::ostream &out) const {
d35a3b07
JF
406 out << "throw";
407 if (value_ != NULL) {
408 out << ' ';
409 value_->Output(out);
410 }
5999c315
JF
411 out << ';';
412}
413
414void CYTry::Output(std::ostream &out) const {
415 out << "try";
416 try_->Output(out, true);
417 if (catch_ != NULL)
418 out << catch_;
419 if (finally_ != NULL) {
420 out << "finally";
421 finally_->Output(out, true);
422 }
423}
424
425void CYVariable::Output(std::ostream &out) const {
426 out << *name_;
427}
428
429void CYWhile::Output(std::ostream &out) const {
d35a3b07
JF
430 out << "while(";
431 test_->Output(out);
432 out << ')';
5999c315
JF
433 code_->Output(out, false);
434}
435
436void CYWith::Output(std::ostream &out) const {
d35a3b07
JF
437 out << "with(";
438 scope_->Output(out);
439 out << ')';
5999c315
JF
440 code_->Output(out, false);
441}
442
443void CYWord::Output(std::ostream &out) const {
444 out << Value();
445}