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