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