]> git.saurik.com Git - cycript.git/blame - Output.cpp
Finished implementing automatic semi-colon rules, but failed hard at fixing multi...
[cycript.git] / Output.cpp
CommitLineData
5999c315
JF
1#include "Parser.hpp"
2
3#include <iostream>
4#include <iomanip>
5
6void CYAddressOf::Output(std::ostream &out) const {
7 out << *rhs_ << ".$()";
8}
9
10void 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
31void CYBoolean::Output(std::ostream &out) const {
32 out << (Value() ? "true" : "false");
33}
34
35void CYBreak::Output(std::ostream &out) const {
36 out << "break";
37 if (label_ != NULL)
38 out << ' ' << *label_;
39 out << ';';
40}
41
42void CYCall::Output(std::ostream &out) const {
43 out << *function_ << '(';
44 if (arguments_ != NULL)
45 arguments_->Output(out, false);
46 out << ')';
47}
48
49void CYCatch::Output(std::ostream &out) const {
50 out << "catch(" << *name_ << ')';
51 code_->Output(out, true);
52}
53
54void CYCondition::Output(std::ostream &out) const {
55 out << *test_ << '?';
56 if (true_ != NULL)
57 out << *true_;
58 out << ':' << *false_;
59}
60
61void CYContinue::Output(std::ostream &out) const {
62 out << "continue";
63 if (label_ != NULL)
64 out << ' ' << *label_;
65 out << ';';
66}
67
68void 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
79void CYDeclaration::Part(std::ostream &out) const {
80 out << "var ";
81 Output(out);
82}
83
84void CYDeclaration::Output(std::ostream &out) const {
85 out << *identifier_;
86 if (initialiser_ != NULL)
87 out << '=' << *initialiser_;
88}
89
90void 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
99void CYDeclarations::Output(std::ostream &out) const {
100 Part(out);
101 out << ';';
102}
103
104void CYDoWhile::Output(std::ostream &out) const {
105 out << "do ";
106 code_->Output(out, false);
107 out << "while" << *test_ << ';';
108}
109
110void 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
123void CYElement::Output(std::ostream &out) const {
124 Output(out, false);
125}
126
127void CYEmpty::Output(std::ostream &out) const {
128 out << ';';
129}
130
131void CYEmpty::Output(std::ostream &out, bool block) const {
132 if (next_ != NULL)
133 CYSource::Output(out, block);
134 else
135 out << "{}";
136}
137
138void CYExpress::Output(std::ostream &out) const {
139 expression_->Output(out, true);
140 out << ';';
141}
142
143void CYExpression::Part(std::ostream &out) const {
144 Output(out, true);
145}
146
147void 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
159void 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
173void 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
182void CYFunction::Output(std::ostream &out) const {
183 CYLambda::Output(out);
184}
185
186void 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
195void CYIndirect::Output(std::ostream &out) const {
196 out << *rhs_ << "[0]";
197}
198
199void CYInfix::Output(std::ostream &out) const {
200 out << *lhs_ << Operator() << *rhs_;
201}
202
203void 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
214void CYMember::Output(std::ostream &out) const {
215 out << *object_ << '[';
216 property_->Output(out, true);
217 out << ']';
218}
219
220void 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
236void CYNew::Output(std::ostream &out) const {
237 out << "new " << *constructor_ << '(';
238 if (arguments_ != NULL)
239 arguments_->Output(out, false);
240 out << ')';
241}
242
243void CYNull::Output(std::ostream &out) const {
244 CYWord::Output(out);
245}
246
247void CYNumber::Output(std::ostream &out) const {
248 // XXX: this is not a useful formatting
249 out << Value();
250}
251
252void CYParameter::Output(std::ostream &out) const {
253 out << *name_;
254 if (next_ != NULL) {
255 out << ',';
256 out << *next_;
257 }
258}
259
260void CYPostfix::Output(std::ostream &out) const {
261 out << *lhs_ << Operator();
262}
263
264void CYPrefix::Output(std::ostream &out) const {
265 out << Operator() << *rhs_;
266}
267
268void CYProperty::Output(std::ostream &out, bool raw) const {
269 if (!raw)
270 out << '{';
271 out << *name_ << ':' << *value_;
272 if (next_ != NULL) {
273 out << ',';
274 next_->Output(out, true);
275 }
276 if (!raw)
277 out << '}';
278}
279
280void CYProperty::Output(std::ostream &out) const {
281 Output(out, false);
282}
283
284void CYReturn::Output(std::ostream &out) const {
285 out << "return";
286 if (value_ != NULL)
287 out << ' ' << *value_;
288 out << ';';
289}
290
b1ff2d78 291void CYSource::Show(std::ostream &out) const {
5999c315
JF
292 for (const CYSource *next(this); next != NULL; next = next->next_)
293 next->Output(out);
294}
295
296void CYSource::Output(std::ostream &out, bool block) const {
297 if (!block && next_ == NULL)
298 Output(out);
299 else {
300 out << '{';
b1ff2d78 301 Show(out);
5999c315
JF
302 out << '}';
303 }
304}
305
306void CYString::Output(std::ostream &out) const {
307 out << '\"';
308 for (const char *value(value_), *end(value_ + size_); value != end; ++value)
309 switch (*value) {
310 case '"': out << "\\\""; break;
311 case '\\': out << "\\\\"; break;
312 case '\b': out << "\\b"; break;
313 case '\f': out << "\\f"; break;
314 case '\n': out << "\\n"; break;
315 case '\r': out << "\\r"; break;
316 case '\t': out << "\\t"; break;
317 case '\v': out << "\\v"; break;
318
319 default:
320 if (*value < 0x20 || *value >= 0x7f)
321 out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
322 else
323 out << *value;
324 }
325 out << '\"';
326}
327
328void CYSwitch::Output(std::ostream &out) const {
329 out << "switch" << *value_ << '{';
330 if (clauses_ != NULL)
331 out << *clauses_;
332 out << '}';
333}
334
335void CYThis::Output(std::ostream &out) const {
336 CYWord::Output(out);
337}
338
339void CYThrow::Output(std::ostream &out) const {
340 out << "return";
341 if (value_ != NULL)
342 out << ' ' << *value_;
343 out << ';';
344}
345
346void CYTry::Output(std::ostream &out) const {
347 out << "try";
348 try_->Output(out, true);
349 if (catch_ != NULL)
350 out << catch_;
351 if (finally_ != NULL) {
352 out << "finally";
353 finally_->Output(out, true);
354 }
355}
356
357void CYVariable::Output(std::ostream &out) const {
358 out << *name_;
359}
360
361void CYWhile::Output(std::ostream &out) const {
362 out << "while" << *test_;
363 code_->Output(out, false);
364}
365
366void CYWith::Output(std::ostream &out) const {
367 out << "with" << *scope_;
368 code_->Output(out, false);
369}
370
371void CYWord::Output(std::ostream &out) const {
372 out << Value();
373}