2123
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2123
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2123
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3665
|
28 #include <cctype> |
|
29 |
3503
|
30 #include <iostream> |
2123
|
31 |
3665
|
32 #include "comment-list.h" |
2123
|
33 #include "error.h" |
2969
|
34 #include "ov-usr-fcn.h" |
2123
|
35 #include "pr-output.h" |
2988
|
36 #include "pt-all.h" |
2123
|
37 |
|
38 void |
|
39 tree_print_code::visit_argument_list (tree_argument_list& lst) |
|
40 { |
4219
|
41 tree_argument_list::iterator p = lst.begin (); |
2123
|
42 |
4219
|
43 while (p != lst.end ()) |
2123
|
44 { |
4219
|
45 tree_expression *elt = *p++; |
2123
|
46 |
|
47 if (elt) |
|
48 { |
|
49 elt->accept (*this); |
|
50 |
4219
|
51 if (p != lst.end ()) |
2123
|
52 os << ", "; |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 void |
|
58 tree_print_code::visit_binary_expression (tree_binary_expression& expr) |
|
59 { |
|
60 indent (); |
|
61 |
2961
|
62 print_parens (expr, "("); |
2123
|
63 |
|
64 tree_expression *op1 = expr.lhs (); |
|
65 |
|
66 if (op1) |
|
67 op1->accept (*this); |
|
68 |
|
69 os << " " << expr.oper () << " "; |
|
70 |
|
71 tree_expression *op2 = expr.rhs (); |
|
72 |
|
73 if (op2) |
|
74 op2->accept (*this); |
|
75 |
2961
|
76 print_parens (expr, ")"); |
2123
|
77 } |
|
78 |
|
79 void |
4207
|
80 tree_print_code::visit_break_command (tree_break_command&) |
2123
|
81 { |
|
82 indent (); |
|
83 |
|
84 os << "break"; |
|
85 } |
|
86 |
|
87 void |
|
88 tree_print_code::visit_colon_expression (tree_colon_expression& expr) |
|
89 { |
|
90 indent (); |
|
91 |
2961
|
92 print_parens (expr, "("); |
2123
|
93 |
|
94 tree_expression *op1 = expr.base (); |
|
95 |
|
96 if (op1) |
|
97 op1->accept (*this); |
|
98 |
|
99 // Stupid syntax. |
|
100 |
|
101 tree_expression *op3 = expr.increment (); |
|
102 |
|
103 if (op3) |
|
104 { |
|
105 os << ":"; |
|
106 op3->accept (*this); |
|
107 } |
|
108 |
|
109 tree_expression *op2 = expr.limit (); |
|
110 |
|
111 if (op2) |
|
112 { |
|
113 os << ":"; |
|
114 op2->accept (*this); |
|
115 } |
|
116 |
2961
|
117 print_parens (expr, ")"); |
2123
|
118 } |
|
119 |
|
120 void |
4207
|
121 tree_print_code::visit_continue_command (tree_continue_command&) |
2123
|
122 { |
|
123 indent (); |
|
124 |
|
125 os << "continue"; |
|
126 } |
|
127 |
|
128 void |
2846
|
129 tree_print_code::visit_decl_command (tree_decl_command& cmd) |
|
130 { |
|
131 indent (); |
|
132 |
|
133 os << cmd.name () << " "; |
|
134 |
|
135 tree_decl_init_list *init_list = cmd.initializer_list (); |
|
136 |
|
137 if (init_list) |
|
138 init_list->accept (*this); |
|
139 } |
|
140 |
|
141 void |
|
142 tree_print_code::visit_decl_elt (tree_decl_elt& cmd) |
|
143 { |
|
144 tree_identifier *id = cmd.ident (); |
|
145 |
|
146 if (id) |
|
147 id->accept (*this); |
|
148 |
2969
|
149 tree_expression *expr = cmd.expression (); |
2846
|
150 |
2969
|
151 if (expr) |
|
152 { |
|
153 os << " = "; |
|
154 |
|
155 expr->accept (*this); |
|
156 } |
2846
|
157 } |
|
158 |
|
159 void |
|
160 tree_print_code::visit_decl_init_list (tree_decl_init_list& lst) |
|
161 { |
4219
|
162 tree_decl_init_list::iterator p = lst.begin (); |
2846
|
163 |
4219
|
164 while (p != lst.end ()) |
2846
|
165 { |
4219
|
166 tree_decl_elt *elt = *p++; |
2846
|
167 |
|
168 if (elt) |
|
169 { |
|
170 elt->accept (*this); |
|
171 |
4219
|
172 if (p != lst.end ()) |
2846
|
173 os << ", "; |
|
174 } |
|
175 } |
|
176 } |
|
177 |
|
178 void |
2969
|
179 tree_print_code::visit_simple_for_command (tree_simple_for_command& cmd) |
2123
|
180 { |
3665
|
181 print_comment_list (cmd.leading_comment ()); |
|
182 |
2123
|
183 indent (); |
|
184 |
|
185 os << "for "; |
|
186 |
2969
|
187 tree_expression *lhs = cmd.left_hand_side (); |
2123
|
188 |
2969
|
189 if (lhs) |
|
190 lhs->accept (*this); |
2123
|
191 |
|
192 os << " = "; |
|
193 |
|
194 tree_expression *expr = cmd.control_expr (); |
|
195 |
|
196 if (expr) |
|
197 expr->accept (*this); |
|
198 |
|
199 newline (); |
|
200 |
|
201 tree_statement_list *list = cmd.body (); |
|
202 |
|
203 if (list) |
|
204 { |
|
205 increment_indent_level (); |
3665
|
206 |
2123
|
207 list->accept (*this); |
3665
|
208 |
2123
|
209 decrement_indent_level (); |
|
210 } |
|
211 |
3665
|
212 print_indented_comment (cmd.trailing_comment ()); |
|
213 |
2123
|
214 indent (); |
|
215 |
|
216 os << "endfor"; |
|
217 } |
|
218 |
|
219 void |
2969
|
220 tree_print_code::visit_complex_for_command (tree_complex_for_command& cmd) |
|
221 { |
3665
|
222 print_comment_list (cmd.leading_comment ()); |
|
223 |
2969
|
224 indent (); |
|
225 |
|
226 os << "for ["; |
4676
|
227 nesting.push ('['); |
2969
|
228 |
|
229 tree_argument_list *lhs = cmd.left_hand_side (); |
|
230 |
|
231 if (lhs) |
|
232 lhs->accept (*this); |
|
233 |
4676
|
234 nesting.pop (); |
2969
|
235 os << "] = "; |
|
236 |
|
237 tree_expression *expr = cmd.control_expr (); |
|
238 |
|
239 if (expr) |
|
240 expr->accept (*this); |
|
241 |
|
242 newline (); |
|
243 |
|
244 tree_statement_list *list = cmd.body (); |
|
245 |
|
246 if (list) |
|
247 { |
|
248 increment_indent_level (); |
3665
|
249 |
2969
|
250 list->accept (*this); |
3665
|
251 |
2969
|
252 decrement_indent_level (); |
|
253 } |
|
254 |
3665
|
255 print_indented_comment (cmd.trailing_comment ()); |
|
256 |
2969
|
257 indent (); |
|
258 |
|
259 os << "endfor"; |
|
260 } |
|
261 |
|
262 void |
2891
|
263 tree_print_code::visit_octave_user_function (octave_user_function& fcn) |
2123
|
264 { |
|
265 reset (); |
|
266 |
2891
|
267 visit_octave_user_function_header (fcn); |
2123
|
268 |
|
269 tree_statement_list *cmd_list = fcn.body (); |
|
270 |
|
271 if (cmd_list) |
|
272 { |
|
273 increment_indent_level (); |
3665
|
274 |
2123
|
275 cmd_list->accept (*this); |
3665
|
276 |
2123
|
277 decrement_indent_level (); |
|
278 } |
|
279 |
2891
|
280 visit_octave_user_function_trailer (fcn); |
2123
|
281 } |
|
282 |
|
283 void |
2891
|
284 tree_print_code::visit_octave_user_function_header (octave_user_function& fcn) |
2123
|
285 { |
3665
|
286 octave_comment_list *leading_comment = fcn.leading_comment (); |
|
287 |
|
288 if (leading_comment) |
|
289 { |
|
290 print_comment_list (leading_comment); |
|
291 newline (); |
|
292 } |
|
293 |
2123
|
294 indent (); |
|
295 |
|
296 os << "function "; |
|
297 |
|
298 tree_parameter_list *ret_list = fcn.return_list (); |
|
299 |
|
300 if (ret_list) |
|
301 { |
|
302 bool takes_var_return = fcn.takes_var_return (); |
|
303 |
|
304 int len = ret_list->length (); |
|
305 |
|
306 if (len > 1 || takes_var_return) |
4676
|
307 { |
|
308 os << "["; |
|
309 nesting.push ('['); |
|
310 } |
2123
|
311 |
|
312 ret_list->accept (*this); |
|
313 |
|
314 if (takes_var_return) |
|
315 { |
|
316 if (len > 0) |
|
317 os << ", "; |
|
318 |
|
319 os << "..."; |
|
320 } |
|
321 |
|
322 if (len > 1 || takes_var_return) |
4676
|
323 { |
|
324 nesting.pop (); |
|
325 os << "]"; |
|
326 } |
2123
|
327 |
|
328 os << " = "; |
|
329 } |
|
330 |
4748
|
331 std::string fcn_name = fcn.name (); |
2123
|
332 |
3523
|
333 os << (fcn_name.empty () ? std::string ("(empty)") : fcn_name) << " "; |
2123
|
334 |
|
335 tree_parameter_list *param_list = fcn.parameter_list (); |
|
336 |
|
337 if (param_list) |
|
338 { |
|
339 bool takes_varargs = fcn.takes_varargs (); |
|
340 |
|
341 int len = param_list->length (); |
|
342 |
|
343 if (len > 0 || takes_varargs) |
4676
|
344 { |
|
345 os << "("; |
|
346 nesting.push ('('); |
|
347 } |
2123
|
348 |
|
349 param_list->accept (*this); |
|
350 |
|
351 if (takes_varargs) |
|
352 { |
|
353 if (len > 0) |
|
354 os << ", "; |
|
355 |
|
356 os << "..."; |
|
357 } |
|
358 |
|
359 if (len > 0 || takes_varargs) |
|
360 { |
4676
|
361 nesting.pop (); |
2123
|
362 os << ")"; |
|
363 newline (); |
|
364 } |
|
365 } |
|
366 else |
|
367 { |
|
368 os << "()"; |
|
369 newline (); |
|
370 } |
|
371 } |
|
372 |
|
373 void |
3665
|
374 tree_print_code::visit_octave_user_function_trailer (octave_user_function& fcn) |
2123
|
375 { |
3665
|
376 print_indented_comment (fcn.trailing_comment ()); |
|
377 |
2123
|
378 indent (); |
|
379 |
|
380 os << "endfunction"; |
|
381 |
|
382 newline (); |
|
383 } |
|
384 |
|
385 void |
|
386 tree_print_code::visit_identifier (tree_identifier& id) |
|
387 { |
|
388 indent (); |
|
389 |
2961
|
390 print_parens (id, "("); |
2123
|
391 |
3523
|
392 std::string nm = id.name (); |
|
393 os << (nm.empty () ? std::string ("(empty)") : nm); |
2123
|
394 |
2961
|
395 print_parens (id, ")"); |
2123
|
396 } |
|
397 |
|
398 void |
|
399 tree_print_code::visit_if_clause (tree_if_clause& cmd) |
|
400 { |
|
401 tree_expression *expr = cmd.condition (); |
|
402 |
|
403 if (expr) |
|
404 expr->accept (*this); |
|
405 |
|
406 newline (); |
|
407 |
|
408 tree_statement_list *list = cmd.commands (); |
|
409 |
|
410 if (list) |
|
411 { |
3665
|
412 increment_indent_level (); |
|
413 |
2123
|
414 list->accept (*this); |
|
415 |
|
416 decrement_indent_level (); |
|
417 } |
|
418 } |
|
419 |
|
420 void |
|
421 tree_print_code::visit_if_command (tree_if_command& cmd) |
|
422 { |
3665
|
423 print_comment_list (cmd.leading_comment ()); |
|
424 |
2123
|
425 indent (); |
|
426 |
|
427 os << "if "; |
|
428 |
|
429 tree_if_command_list *list = cmd.cmd_list (); |
|
430 |
|
431 if (list) |
|
432 list->accept (*this); |
|
433 |
3665
|
434 print_indented_comment (cmd.trailing_comment ()); |
|
435 |
2123
|
436 indent (); |
|
437 |
|
438 os << "endif"; |
|
439 } |
|
440 |
|
441 void |
|
442 tree_print_code::visit_if_command_list (tree_if_command_list& lst) |
|
443 { |
4219
|
444 tree_if_command_list::iterator p = lst.begin (); |
2123
|
445 |
|
446 bool first_elt = true; |
|
447 |
4219
|
448 while (p != lst.end ()) |
2123
|
449 { |
4219
|
450 tree_if_clause *elt = *p++; |
2123
|
451 |
|
452 if (elt) |
|
453 { |
|
454 if (! first_elt) |
|
455 { |
3665
|
456 print_indented_comment (elt->leading_comment ()); |
|
457 |
2123
|
458 indent (); |
|
459 |
|
460 if (elt->is_else_clause ()) |
|
461 os << "else"; |
|
462 else |
|
463 os << "elseif "; |
|
464 } |
|
465 |
|
466 elt->accept (*this); |
|
467 } |
|
468 |
|
469 first_elt = false; |
|
470 } |
|
471 } |
|
472 |
|
473 void |
|
474 tree_print_code::visit_index_expression (tree_index_expression& expr) |
|
475 { |
|
476 indent (); |
|
477 |
2961
|
478 print_parens (expr, "("); |
2123
|
479 |
3665
|
480 bool expr_has_parens = false; |
|
481 |
3010
|
482 tree_expression *e = expr.expression (); |
2123
|
483 |
2969
|
484 if (e) |
3665
|
485 { |
|
486 e->accept (*this); |
|
487 |
|
488 expr_has_parens = e->is_postfix_indexed (); |
|
489 } |
2123
|
490 |
4219
|
491 std::list<tree_argument_list *> arg_lists = expr.arg_lists (); |
3933
|
492 std::string type_tags = expr.type_tags (); |
4219
|
493 std::list<string_vector> arg_names = expr.arg_names (); |
2123
|
494 |
3933
|
495 int n = type_tags.length (); |
|
496 |
4219
|
497 std::list<tree_argument_list *>::iterator p_arg_lists = arg_lists.begin (); |
|
498 std::list<string_vector>::iterator p_arg_names = arg_names.begin (); |
3933
|
499 |
|
500 for (int i = 0; i < n; i++) |
2123
|
501 { |
3933
|
502 switch (type_tags[i]) |
|
503 { |
|
504 case '(': |
|
505 { |
4676
|
506 char nc = nesting.top (); |
|
507 if ((nc == '[' || nc == '{') && expr.paren_count () == 0) |
|
508 os << "("; |
|
509 else |
|
510 os << " ("; |
|
511 nesting.push ('('); |
|
512 |
4219
|
513 tree_argument_list *l = *p_arg_lists; |
3933
|
514 if (l) |
|
515 l->accept (*this); |
4676
|
516 |
|
517 nesting.pop (); |
3933
|
518 os << ")"; |
|
519 } |
|
520 break; |
|
521 |
|
522 case '{': |
|
523 { |
4676
|
524 char nc = nesting.top (); |
|
525 if ((nc == '[' || nc == '{') && expr.paren_count () == 0) |
|
526 os << "{"; |
|
527 else |
|
528 os << " {"; |
|
529 // We only care about whitespace inside [] and {} when we |
|
530 // are defining matrix and cell objects, not when indexing. |
|
531 nesting.push ('('); |
|
532 |
4219
|
533 tree_argument_list *l = *p_arg_lists; |
3933
|
534 if (l) |
|
535 l->accept (*this); |
4676
|
536 |
|
537 nesting.pop (); |
3933
|
538 os << "}"; |
|
539 } |
|
540 break; |
|
541 |
|
542 case '.': |
|
543 { |
4219
|
544 string_vector nm = *p_arg_names; |
3933
|
545 assert (nm.length () == 1); |
|
546 os << "." << nm(0); |
|
547 } |
|
548 break; |
|
549 |
|
550 default: |
|
551 panic_impossible (); |
|
552 } |
|
553 |
4219
|
554 p_arg_lists++; |
|
555 p_arg_names++; |
3930
|
556 } |
2123
|
557 |
2961
|
558 print_parens (expr, ")"); |
2123
|
559 } |
|
560 |
|
561 void |
|
562 tree_print_code::visit_matrix (tree_matrix& lst) |
|
563 { |
|
564 indent (); |
|
565 |
2961
|
566 print_parens (lst, "("); |
2123
|
567 |
|
568 os << "["; |
4676
|
569 nesting.push ('['); |
2123
|
570 |
4219
|
571 tree_matrix::iterator p = lst.begin (); |
2123
|
572 |
4219
|
573 while (p != lst.end ()) |
2123
|
574 { |
4219
|
575 tree_argument_list *elt = *p++; |
2123
|
576 |
|
577 if (elt) |
|
578 { |
|
579 elt->accept (*this); |
|
580 |
4219
|
581 if (p != lst.end ()) |
2123
|
582 os << "; "; |
|
583 } |
|
584 } |
|
585 |
4676
|
586 nesting.pop (); |
2123
|
587 os << "]"; |
|
588 |
2961
|
589 print_parens (lst, ")"); |
2123
|
590 } |
|
591 |
|
592 void |
3351
|
593 tree_print_code::visit_cell (tree_cell& lst) |
|
594 { |
|
595 indent (); |
|
596 |
|
597 print_parens (lst, "("); |
|
598 |
|
599 os << "{"; |
4676
|
600 nesting.push ('{'); |
3351
|
601 |
4219
|
602 tree_cell::iterator p = lst.begin (); |
3351
|
603 |
4219
|
604 while (p != lst.end ()) |
3351
|
605 { |
4219
|
606 tree_argument_list *elt = *p++; |
3351
|
607 |
|
608 if (elt) |
|
609 { |
|
610 elt->accept (*this); |
|
611 |
4219
|
612 if (p != lst.end ()) |
3351
|
613 os << "; "; |
|
614 } |
|
615 } |
|
616 |
4676
|
617 nesting.pop (); |
3351
|
618 os << "}"; |
|
619 |
|
620 print_parens (lst, ")"); |
|
621 } |
|
622 |
|
623 void |
2969
|
624 tree_print_code::visit_multi_assignment (tree_multi_assignment& expr) |
2123
|
625 { |
|
626 indent (); |
|
627 |
2961
|
628 print_parens (expr, "("); |
2123
|
629 |
2969
|
630 tree_argument_list *lhs = expr.left_hand_side (); |
2123
|
631 |
|
632 if (lhs) |
|
633 { |
|
634 int len = lhs->length (); |
|
635 |
|
636 if (len > 1) |
4676
|
637 { |
|
638 os << "["; |
|
639 nesting.push ('['); |
|
640 } |
2123
|
641 |
|
642 lhs->accept (*this); |
|
643 |
|
644 if (len > 1) |
4676
|
645 { |
|
646 nesting.pop (); |
|
647 os << "]"; |
|
648 } |
2123
|
649 } |
|
650 |
3208
|
651 os << " " << expr.oper () << " "; |
2123
|
652 |
2969
|
653 tree_expression *rhs = expr.right_hand_side (); |
2123
|
654 |
|
655 if (rhs) |
|
656 rhs->accept (*this); |
|
657 |
2961
|
658 print_parens (expr, ")"); |
2123
|
659 } |
|
660 |
|
661 void |
2620
|
662 tree_print_code::visit_no_op_command (tree_no_op_command& cmd) |
|
663 { |
|
664 indent (); |
|
665 |
|
666 os << cmd.original_command (); |
|
667 } |
|
668 |
|
669 void |
2372
|
670 tree_print_code::visit_constant (tree_constant& val) |
2123
|
671 { |
|
672 indent (); |
|
673 |
2961
|
674 print_parens (val, "("); |
2123
|
675 |
2942
|
676 val.print_raw (os, true, print_original_text); |
2123
|
677 |
2961
|
678 print_parens (val, ")"); |
2123
|
679 } |
|
680 |
|
681 void |
4342
|
682 tree_print_code::visit_fcn_handle (tree_fcn_handle& fh) |
|
683 { |
|
684 indent (); |
|
685 |
|
686 print_parens (fh, "("); |
|
687 |
|
688 fh.print_raw (os, true, print_original_text); |
|
689 |
|
690 print_parens (fh, ")"); |
|
691 } |
|
692 |
|
693 void |
2123
|
694 tree_print_code::visit_parameter_list (tree_parameter_list& lst) |
|
695 { |
4219
|
696 tree_parameter_list::iterator p = lst.begin (); |
2123
|
697 |
4219
|
698 while (p != lst.end ()) |
2123
|
699 { |
4219
|
700 tree_identifier *elt = *p++; |
2123
|
701 |
|
702 if (elt) |
|
703 { |
|
704 elt->accept (*this); |
|
705 |
4219
|
706 if (p != lst.end ()) |
2123
|
707 os << ", "; |
|
708 } |
|
709 } |
|
710 } |
|
711 |
|
712 void |
|
713 tree_print_code::visit_postfix_expression (tree_postfix_expression& expr) |
|
714 { |
|
715 indent (); |
|
716 |
2961
|
717 print_parens (expr, "("); |
2123
|
718 |
2960
|
719 tree_expression *e = expr.operand (); |
2123
|
720 |
2960
|
721 if (e) |
|
722 e->accept (*this); |
2123
|
723 |
|
724 os << expr.oper (); |
|
725 |
2961
|
726 print_parens (expr, ")"); |
2123
|
727 } |
|
728 |
|
729 void |
|
730 tree_print_code::visit_prefix_expression (tree_prefix_expression& expr) |
|
731 { |
|
732 indent (); |
|
733 |
2961
|
734 print_parens (expr, "("); |
2123
|
735 |
|
736 os << expr.oper (); |
|
737 |
2960
|
738 tree_expression *e = expr.operand (); |
2123
|
739 |
2960
|
740 if (e) |
|
741 e->accept (*this); |
2123
|
742 |
2961
|
743 print_parens (expr, ")"); |
2123
|
744 } |
|
745 |
|
746 void |
4207
|
747 tree_print_code::visit_return_command (tree_return_command&) |
2123
|
748 { |
|
749 indent (); |
|
750 |
|
751 os << "return"; |
|
752 } |
|
753 |
|
754 void |
|
755 tree_print_code::visit_return_list (tree_return_list& lst) |
|
756 { |
4219
|
757 tree_return_list::iterator p = lst.begin (); |
2123
|
758 |
4219
|
759 while (p != lst.end ()) |
2123
|
760 { |
4219
|
761 tree_index_expression *elt = *p++; |
2123
|
762 |
|
763 if (elt) |
|
764 { |
|
765 elt->accept (*this); |
|
766 |
4219
|
767 if (p != lst.end ()) |
2123
|
768 os << ", "; |
|
769 } |
|
770 } |
|
771 } |
|
772 |
|
773 void |
2969
|
774 tree_print_code::visit_simple_assignment (tree_simple_assignment& expr) |
2123
|
775 { |
|
776 indent (); |
|
777 |
2961
|
778 print_parens (expr, "("); |
2123
|
779 |
2969
|
780 tree_expression *lhs = expr.left_hand_side (); |
2123
|
781 |
2969
|
782 if (lhs) |
|
783 lhs->accept (*this); |
2123
|
784 |
2969
|
785 os << " " << expr.oper () << " "; |
2123
|
786 |
|
787 tree_expression *rhs = expr.right_hand_side (); |
|
788 |
|
789 if (rhs) |
|
790 rhs->accept (*this); |
|
791 |
2961
|
792 print_parens (expr, ")"); |
2123
|
793 } |
|
794 |
|
795 void |
|
796 tree_print_code::visit_statement (tree_statement& stmt) |
|
797 { |
3665
|
798 print_comment_list (stmt.comment_text ()); |
|
799 |
2123
|
800 tree_command *cmd = stmt.command (); |
|
801 |
|
802 if (cmd) |
|
803 { |
|
804 cmd->accept (*this); |
|
805 |
|
806 if (! stmt.print_result ()) |
4980
|
807 { |
|
808 os << ";"; |
|
809 newline (" "); |
|
810 } |
|
811 else |
|
812 newline (); |
2123
|
813 } |
|
814 else |
|
815 { |
|
816 tree_expression *expr = stmt.expression (); |
|
817 |
|
818 if (expr) |
|
819 { |
|
820 expr->accept (*this); |
|
821 |
|
822 if (! stmt.print_result ()) |
4980
|
823 { |
|
824 os << ";"; |
|
825 newline (" "); |
|
826 } |
|
827 else |
|
828 newline (); |
2123
|
829 } |
|
830 } |
|
831 } |
|
832 |
|
833 void |
|
834 tree_print_code::visit_statement_list (tree_statement_list& lst) |
|
835 { |
4219
|
836 for (tree_statement_list::iterator p = lst.begin (); p != lst.end (); p++) |
2123
|
837 { |
4219
|
838 tree_statement *elt = *p; |
2123
|
839 |
|
840 if (elt) |
|
841 elt->accept (*this); |
|
842 } |
|
843 } |
|
844 |
|
845 void |
2846
|
846 tree_print_code::visit_switch_case (tree_switch_case& cs) |
|
847 { |
3665
|
848 print_comment_list (cs.leading_comment ()); |
|
849 |
2846
|
850 indent (); |
|
851 |
|
852 if (cs.is_default_case ()) |
|
853 os << "otherwise"; |
|
854 else |
|
855 os << "case "; |
|
856 |
|
857 tree_expression *label = cs.case_label (); |
|
858 |
|
859 if (label) |
|
860 label->accept (*this); |
|
861 |
|
862 newline (); |
|
863 |
|
864 tree_statement_list *list = cs.commands (); |
|
865 |
|
866 if (list) |
|
867 { |
3665
|
868 increment_indent_level (); |
|
869 |
2846
|
870 list->accept (*this); |
|
871 |
3665
|
872 newline (); |
|
873 |
2846
|
874 decrement_indent_level (); |
|
875 } |
|
876 } |
|
877 |
|
878 void |
|
879 tree_print_code::visit_switch_case_list (tree_switch_case_list& lst) |
|
880 { |
4219
|
881 tree_switch_case_list::iterator p = lst.begin (); |
2846
|
882 |
4219
|
883 while (p != lst.end ()) |
2846
|
884 { |
4219
|
885 tree_switch_case *elt = *p++; |
2846
|
886 |
|
887 if (elt) |
|
888 elt->accept (*this); |
|
889 } |
|
890 } |
|
891 |
|
892 void |
|
893 tree_print_code::visit_switch_command (tree_switch_command& cmd) |
|
894 { |
3665
|
895 print_comment_list (cmd.leading_comment ()); |
|
896 |
2846
|
897 indent (); |
|
898 |
|
899 os << "switch "; |
|
900 |
|
901 tree_expression *expr = cmd.switch_value (); |
|
902 |
|
903 if (expr) |
|
904 expr->accept (*this); |
|
905 |
|
906 newline (); |
|
907 |
|
908 tree_switch_case_list *list = cmd.case_list (); |
|
909 |
|
910 if (list) |
3665
|
911 { |
|
912 increment_indent_level (); |
|
913 |
|
914 list->accept (*this); |
|
915 |
|
916 decrement_indent_level (); |
|
917 } |
|
918 |
|
919 print_indented_comment (cmd.leading_comment ()); |
2846
|
920 |
|
921 indent (); |
|
922 |
|
923 os << "endswitch"; |
|
924 } |
|
925 |
|
926 void |
2123
|
927 tree_print_code::visit_try_catch_command (tree_try_catch_command& cmd) |
|
928 { |
3665
|
929 print_comment_list (cmd.leading_comment ()); |
|
930 |
2123
|
931 indent (); |
|
932 |
3665
|
933 os << "try"; |
2123
|
934 |
|
935 newline (); |
|
936 |
|
937 tree_statement_list *try_code = cmd.body (); |
|
938 |
|
939 if (try_code) |
|
940 { |
|
941 increment_indent_level (); |
3665
|
942 |
2123
|
943 try_code->accept (*this); |
3665
|
944 |
2123
|
945 decrement_indent_level (); |
|
946 } |
|
947 |
3665
|
948 print_indented_comment (cmd.middle_comment ()); |
|
949 |
2123
|
950 indent (); |
|
951 |
|
952 os << "catch"; |
|
953 |
|
954 newline (); |
|
955 |
|
956 tree_statement_list *catch_code = cmd.cleanup (); |
|
957 |
|
958 if (catch_code) |
|
959 { |
|
960 increment_indent_level (); |
3665
|
961 |
2123
|
962 catch_code->accept (*this); |
3665
|
963 |
2123
|
964 decrement_indent_level (); |
|
965 } |
|
966 |
3665
|
967 print_indented_comment (cmd.trailing_comment ()); |
|
968 |
2123
|
969 indent (); |
|
970 |
|
971 os << "end_try_catch"; |
|
972 } |
|
973 |
|
974 void |
|
975 tree_print_code::visit_unwind_protect_command |
|
976 (tree_unwind_protect_command& cmd) |
|
977 { |
3665
|
978 print_comment_list (cmd.leading_comment ()); |
|
979 |
2123
|
980 indent (); |
|
981 |
|
982 os << "unwind_protect"; |
|
983 |
|
984 newline (); |
|
985 |
|
986 tree_statement_list *unwind_protect_code = cmd.body (); |
|
987 |
|
988 if (unwind_protect_code) |
|
989 { |
|
990 increment_indent_level (); |
3665
|
991 |
2123
|
992 unwind_protect_code->accept (*this); |
3665
|
993 |
2123
|
994 decrement_indent_level (); |
|
995 } |
|
996 |
3665
|
997 print_indented_comment (cmd.middle_comment ()); |
|
998 |
2123
|
999 indent (); |
|
1000 |
3478
|
1001 os << "unwind_protect_cleanup"; |
2123
|
1002 |
|
1003 newline (); |
|
1004 |
|
1005 tree_statement_list *cleanup_code = cmd.cleanup (); |
|
1006 |
|
1007 if (cleanup_code) |
|
1008 { |
|
1009 increment_indent_level (); |
3665
|
1010 |
2123
|
1011 cleanup_code->accept (*this); |
3665
|
1012 |
2123
|
1013 decrement_indent_level (); |
|
1014 } |
|
1015 |
3665
|
1016 print_indented_comment (cmd.trailing_comment ()); |
|
1017 |
2123
|
1018 indent (); |
|
1019 |
|
1020 os << "end_unwind_protect"; |
|
1021 } |
|
1022 |
|
1023 void |
|
1024 tree_print_code::visit_while_command (tree_while_command& cmd) |
|
1025 { |
3665
|
1026 print_comment_list (cmd.leading_comment ()); |
|
1027 |
2123
|
1028 indent (); |
|
1029 |
|
1030 os << "while "; |
|
1031 |
|
1032 tree_expression *expr = cmd.condition (); |
|
1033 |
|
1034 if (expr) |
|
1035 expr->accept (*this); |
|
1036 |
|
1037 newline (); |
|
1038 |
|
1039 tree_statement_list *list = cmd.body (); |
|
1040 |
|
1041 if (list) |
|
1042 { |
|
1043 increment_indent_level (); |
3665
|
1044 |
2123
|
1045 list->accept (*this); |
3665
|
1046 |
2123
|
1047 decrement_indent_level (); |
|
1048 } |
|
1049 |
3665
|
1050 print_indented_comment (cmd.trailing_comment ()); |
|
1051 |
2123
|
1052 indent (); |
|
1053 |
|
1054 os << "endwhile"; |
|
1055 } |
|
1056 |
3484
|
1057 void |
|
1058 tree_print_code::visit_do_until_command (tree_do_until_command& cmd) |
|
1059 { |
3665
|
1060 print_comment_list (cmd.leading_comment ()); |
|
1061 |
3484
|
1062 indent (); |
|
1063 |
|
1064 os << "do"; |
|
1065 |
|
1066 newline (); |
|
1067 |
|
1068 tree_statement_list *list = cmd.body (); |
|
1069 |
|
1070 if (list) |
|
1071 { |
|
1072 increment_indent_level (); |
3665
|
1073 |
3484
|
1074 list->accept (*this); |
3665
|
1075 |
3484
|
1076 decrement_indent_level (); |
|
1077 } |
|
1078 |
3665
|
1079 print_indented_comment (cmd.trailing_comment ()); |
|
1080 |
3484
|
1081 indent (); |
|
1082 |
|
1083 os << "until"; |
|
1084 |
|
1085 tree_expression *expr = cmd.condition (); |
|
1086 |
|
1087 if (expr) |
|
1088 expr->accept (*this); |
|
1089 |
|
1090 newline (); |
|
1091 } |
|
1092 |
2123
|
1093 // Each print_code() function should call this before printing |
|
1094 // anything. |
|
1095 // |
|
1096 // This doesn't need to be fast, but isn't there a better way? |
|
1097 |
|
1098 void |
|
1099 tree_print_code::indent (void) |
|
1100 { |
|
1101 assert (curr_print_indent_level >= 0); |
2900
|
1102 |
4980
|
1103 if (printing_newlines) |
|
1104 { |
|
1105 if (beginning_of_line) |
|
1106 { |
|
1107 os << prefix; |
2900
|
1108 |
4980
|
1109 for (int i = 0; i < curr_print_indent_level; i++) |
|
1110 os << " "; |
|
1111 |
|
1112 beginning_of_line = false; |
|
1113 } |
2123
|
1114 } |
|
1115 } |
|
1116 |
|
1117 // All print_code() functions should use this to print new lines. |
|
1118 |
|
1119 void |
4980
|
1120 tree_print_code::newline (const char *alt_txt) |
2123
|
1121 { |
4980
|
1122 os << (printing_newlines ? "\n" : alt_txt); |
2123
|
1123 |
|
1124 beginning_of_line = true; |
|
1125 } |
|
1126 |
|
1127 // For ressetting print_code state. |
|
1128 |
|
1129 void |
|
1130 tree_print_code::reset (void) |
|
1131 { |
|
1132 beginning_of_line = true; |
|
1133 curr_print_indent_level = 0; |
4676
|
1134 while (nesting.top () != 'n') |
|
1135 nesting.pop (); |
2123
|
1136 } |
|
1137 |
2961
|
1138 void |
|
1139 tree_print_code::print_parens (const tree_expression& expr, const char *txt) |
|
1140 { |
|
1141 int n = expr.paren_count (); |
|
1142 |
|
1143 for (int i = 0; i < n; i++) |
|
1144 os << txt; |
|
1145 } |
|
1146 |
3665
|
1147 void |
|
1148 tree_print_code::print_comment_elt (const octave_comment_elt& elt) |
|
1149 { |
|
1150 bool printed_something = false; |
|
1151 |
|
1152 bool prev_char_was_newline = false; |
|
1153 |
3769
|
1154 std::string comment = elt.text (); |
3665
|
1155 |
|
1156 size_t len = comment.length (); |
|
1157 |
|
1158 size_t i = 0; |
|
1159 |
|
1160 while (i < len && comment[i++] == '\n') |
|
1161 ; /* Skip leading new lines. */ |
|
1162 i--; |
|
1163 |
|
1164 while (i < len) |
|
1165 { |
|
1166 char c = comment[i++]; |
|
1167 |
|
1168 if (c == '\n') |
|
1169 { |
|
1170 if (prev_char_was_newline) |
|
1171 os << "##"; |
|
1172 |
|
1173 newline (); |
|
1174 |
|
1175 prev_char_was_newline = true; |
|
1176 } |
|
1177 else |
|
1178 { |
|
1179 if (beginning_of_line) |
|
1180 { |
|
1181 printed_something = true; |
|
1182 |
|
1183 indent (); |
|
1184 |
|
1185 os << "##"; |
|
1186 |
|
1187 if (! (isspace (c) || c == '!')) |
|
1188 os << " "; |
|
1189 } |
|
1190 |
|
1191 os << (char) c; |
|
1192 |
|
1193 prev_char_was_newline = false; |
|
1194 } |
|
1195 } |
|
1196 |
|
1197 if (printed_something && ! beginning_of_line) |
|
1198 newline (); |
|
1199 } |
|
1200 |
|
1201 void |
|
1202 tree_print_code::print_comment_list (octave_comment_list *comment_list) |
|
1203 { |
|
1204 if (comment_list) |
|
1205 { |
4219
|
1206 octave_comment_list::iterator p = comment_list->begin (); |
3665
|
1207 |
4219
|
1208 while (p != comment_list->end ()) |
3665
|
1209 { |
4219
|
1210 octave_comment_elt elt = *p++; |
3665
|
1211 |
|
1212 print_comment_elt (elt); |
|
1213 |
4219
|
1214 if (p != comment_list->end ()) |
3665
|
1215 newline (); |
|
1216 } |
|
1217 } |
|
1218 } |
|
1219 |
|
1220 void |
|
1221 tree_print_code::print_indented_comment (octave_comment_list *comment_list) |
|
1222 { |
|
1223 increment_indent_level (); |
|
1224 |
|
1225 print_comment_list (comment_list); |
|
1226 |
|
1227 decrement_indent_level (); |
|
1228 } |
|
1229 |
2123
|
1230 /* |
|
1231 ;;; Local Variables: *** |
|
1232 ;;; mode: C++ *** |
|
1233 ;;; End: *** |
|
1234 */ |