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