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