]>
Commit | Line | Data |
---|---|---|
5999c315 JF |
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 | ||
5befe15e JF |
31 | void CYArray::Output(std::ostream &out) const { |
32 | out << '['; | |
33 | if (elements_ != NULL) | |
34 | elements_->Output(out); | |
35 | out << ']'; | |
36 | } | |
37 | ||
5999c315 JF |
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 | ||
5befe15e | 117 | void CYElement::Output(std::ostream &out) const { |
5999c315 JF |
118 | if (value_ != NULL) |
119 | value_->Output(out, true); | |
5befe15e | 120 | if (next_ != NULL || value_ == NULL) |
5999c315 | 121 | out << ','; |
5befe15e JF |
122 | if (next_ != NULL) |
123 | next_->Output(out); | |
5999c315 JF |
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 | ||
693d501b JF |
251 | void CYObject::Output(std::ostream &out) const { |
252 | out << '{'; | |
253 | if (property_ != NULL) | |
254 | property_->Output(out); | |
255 | out << '}'; | |
256 | } | |
257 | ||
5999c315 JF |
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 | ||
693d501b | 274 | void CYProperty::Output(std::ostream &out) const { |
5999c315 JF |
275 | out << *name_ << ':' << *value_; |
276 | if (next_ != NULL) { | |
277 | out << ','; | |
693d501b | 278 | next_->Output(out); |
5999c315 | 279 | } |
5999c315 JF |
280 | } |
281 | ||
282 | void CYReturn::Output(std::ostream &out) const { | |
283 | out << "return"; | |
284 | if (value_ != NULL) | |
285 | out << ' ' << *value_; | |
286 | out << ';'; | |
287 | } | |
288 | ||
e7ed5354 JF |
289 | void CYSelector::Output(std::ostream &out) const { |
290 | out << '"'; | |
291 | out << "<unimplemented>"; | |
292 | out << '"'; | |
293 | } | |
294 | ||
b1ff2d78 | 295 | void CYSource::Show(std::ostream &out) const { |
5999c315 | 296 | for (const CYSource *next(this); next != NULL; next = next->next_) |
931b816a | 297 | next->Output(out, false); |
5999c315 JF |
298 | } |
299 | ||
300 | void CYSource::Output(std::ostream &out, bool block) const { | |
301 | if (!block && next_ == NULL) | |
302 | Output(out); | |
303 | else { | |
304 | out << '{'; | |
b1ff2d78 | 305 | Show(out); |
5999c315 JF |
306 | out << '}'; |
307 | } | |
308 | } | |
309 | ||
310 | void CYString::Output(std::ostream &out) const { | |
311 | out << '\"'; | |
312 | for (const char *value(value_), *end(value_ + size_); value != end; ++value) | |
313 | switch (*value) { | |
314 | case '"': out << "\\\""; break; | |
315 | case '\\': out << "\\\\"; break; | |
316 | case '\b': out << "\\b"; break; | |
317 | case '\f': out << "\\f"; break; | |
318 | case '\n': out << "\\n"; break; | |
319 | case '\r': out << "\\r"; break; | |
320 | case '\t': out << "\\t"; break; | |
321 | case '\v': out << "\\v"; break; | |
322 | ||
323 | default: | |
324 | if (*value < 0x20 || *value >= 0x7f) | |
325 | out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value); | |
326 | else | |
327 | out << *value; | |
328 | } | |
329 | out << '\"'; | |
330 | } | |
331 | ||
332 | void CYSwitch::Output(std::ostream &out) const { | |
333 | out << "switch" << *value_ << '{'; | |
334 | if (clauses_ != NULL) | |
335 | out << *clauses_; | |
336 | out << '}'; | |
337 | } | |
338 | ||
339 | void CYThis::Output(std::ostream &out) const { | |
340 | CYWord::Output(out); | |
341 | } | |
342 | ||
343 | void CYThrow::Output(std::ostream &out) const { | |
344 | out << "return"; | |
345 | if (value_ != NULL) | |
346 | out << ' ' << *value_; | |
347 | out << ';'; | |
348 | } | |
349 | ||
350 | void CYTry::Output(std::ostream &out) const { | |
351 | out << "try"; | |
352 | try_->Output(out, true); | |
353 | if (catch_ != NULL) | |
354 | out << catch_; | |
355 | if (finally_ != NULL) { | |
356 | out << "finally"; | |
357 | finally_->Output(out, true); | |
358 | } | |
359 | } | |
360 | ||
361 | void CYVariable::Output(std::ostream &out) const { | |
362 | out << *name_; | |
363 | } | |
364 | ||
365 | void CYWhile::Output(std::ostream &out) const { | |
366 | out << "while" << *test_; | |
367 | code_->Output(out, false); | |
368 | } | |
369 | ||
370 | void CYWith::Output(std::ostream &out) const { | |
371 | out << "with" << *scope_; | |
372 | code_->Output(out, false); | |
373 | } | |
374 | ||
375 | void CYWord::Output(std::ostream &out) const { | |
376 | out << Value(); | |
377 | } |