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