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 #if defined (__GNUG__) |
|
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 |
|
487 tree_argument_list *list = expr.arg_list (); |
|
488 |
|
489 if (list) |
|
490 { |
|
491 os << " ("; |
|
492 list->accept (*this); |
|
493 os << ")"; |
|
494 } |
3665
|
495 else if (expr_has_parens) |
|
496 os << " ()"; |
2123
|
497 |
2961
|
498 print_parens (expr, ")"); |
2123
|
499 } |
|
500 |
|
501 void |
|
502 tree_print_code::visit_indirect_ref (tree_indirect_ref& expr) |
|
503 { |
|
504 indent (); |
|
505 |
2961
|
506 print_parens (expr, "("); |
2123
|
507 |
2969
|
508 tree_expression *e = expr.expression (); |
2123
|
509 |
2969
|
510 if (e) |
|
511 e->accept (*this); |
|
512 |
|
513 os << "." << expr.elt_name (); |
2123
|
514 |
2961
|
515 print_parens (expr, ")"); |
2123
|
516 } |
|
517 |
|
518 void |
|
519 tree_print_code::visit_matrix (tree_matrix& lst) |
|
520 { |
|
521 indent (); |
|
522 |
2961
|
523 print_parens (lst, "("); |
2123
|
524 |
|
525 os << "["; |
|
526 |
|
527 Pix p = lst.first (); |
|
528 |
|
529 while (p) |
|
530 { |
2969
|
531 tree_argument_list *elt = lst (p); |
2123
|
532 |
|
533 lst.next (p); |
|
534 |
|
535 if (elt) |
|
536 { |
|
537 elt->accept (*this); |
|
538 |
|
539 if (p) |
|
540 os << "; "; |
|
541 } |
|
542 } |
|
543 |
|
544 os << "]"; |
|
545 |
2961
|
546 print_parens (lst, ")"); |
2123
|
547 } |
|
548 |
|
549 void |
3351
|
550 tree_print_code::visit_cell (tree_cell& lst) |
|
551 { |
|
552 indent (); |
|
553 |
|
554 print_parens (lst, "("); |
|
555 |
|
556 os << "{"; |
|
557 |
|
558 Pix p = lst.first (); |
|
559 |
|
560 while (p) |
|
561 { |
|
562 tree_argument_list *elt = lst (p); |
|
563 |
|
564 lst.next (p); |
|
565 |
|
566 if (elt) |
|
567 { |
|
568 elt->accept (*this); |
|
569 |
|
570 if (p) |
|
571 os << "; "; |
|
572 } |
|
573 } |
|
574 |
|
575 os << "}"; |
|
576 |
|
577 print_parens (lst, ")"); |
|
578 } |
|
579 |
|
580 void |
2969
|
581 tree_print_code::visit_multi_assignment (tree_multi_assignment& expr) |
2123
|
582 { |
|
583 indent (); |
|
584 |
2961
|
585 print_parens (expr, "("); |
2123
|
586 |
2969
|
587 tree_argument_list *lhs = expr.left_hand_side (); |
2123
|
588 |
|
589 if (lhs) |
|
590 { |
|
591 int len = lhs->length (); |
|
592 |
|
593 if (len > 1) |
|
594 os << "["; |
|
595 |
|
596 lhs->accept (*this); |
|
597 |
|
598 if (len > 1) |
|
599 os << "]"; |
|
600 } |
|
601 |
3208
|
602 os << " " << expr.oper () << " "; |
2123
|
603 |
2969
|
604 tree_expression *rhs = expr.right_hand_side (); |
2123
|
605 |
|
606 if (rhs) |
|
607 rhs->accept (*this); |
|
608 |
2961
|
609 print_parens (expr, ")"); |
2123
|
610 } |
|
611 |
|
612 void |
2620
|
613 tree_print_code::visit_no_op_command (tree_no_op_command& cmd) |
|
614 { |
|
615 indent (); |
|
616 |
|
617 os << cmd.original_command (); |
|
618 } |
|
619 |
|
620 void |
2372
|
621 tree_print_code::visit_constant (tree_constant& val) |
2123
|
622 { |
|
623 indent (); |
|
624 |
2961
|
625 print_parens (val, "("); |
2123
|
626 |
2942
|
627 val.print_raw (os, true, print_original_text); |
2123
|
628 |
2961
|
629 print_parens (val, ")"); |
2123
|
630 } |
|
631 |
|
632 void |
|
633 tree_print_code::visit_parameter_list (tree_parameter_list& lst) |
|
634 { |
|
635 Pix p = lst.first (); |
|
636 |
|
637 while (p) |
|
638 { |
|
639 tree_identifier *elt = lst (p); |
|
640 |
|
641 lst.next (p); |
|
642 |
|
643 if (elt) |
|
644 { |
|
645 elt->accept (*this); |
|
646 |
|
647 if (p) |
|
648 os << ", "; |
|
649 } |
|
650 } |
|
651 } |
|
652 |
|
653 void |
|
654 tree_print_code::visit_plot_command (tree_plot_command& cmd) |
|
655 { |
|
656 indent (); |
|
657 |
|
658 int ndim = cmd.num_dimensions (); |
|
659 |
|
660 switch (ndim) |
|
661 { |
|
662 case 1: |
|
663 os << "replot"; |
|
664 break; |
|
665 |
|
666 case 2: |
|
667 os << "gplot"; |
|
668 break; |
|
669 |
|
670 case 3: |
|
671 os << "gsplot"; |
|
672 break; |
|
673 |
|
674 default: |
|
675 os << "<unkown plot command>"; |
|
676 break; |
|
677 } |
|
678 |
|
679 plot_limits *range = cmd.limits (); |
|
680 |
|
681 if (range) |
|
682 range->accept (*this); |
|
683 |
|
684 subplot_list *plot_list = cmd.subplots (); |
|
685 |
|
686 if (plot_list) |
|
687 plot_list->accept (*this); |
|
688 } |
|
689 |
|
690 void |
|
691 tree_print_code::visit_plot_limits (plot_limits& cmd) |
|
692 { |
|
693 plot_range *x_range = cmd.x_limits (); |
|
694 |
|
695 if (x_range) |
|
696 x_range->accept (*this); |
|
697 |
|
698 plot_range *y_range = cmd.y_limits (); |
|
699 |
|
700 if (y_range) |
|
701 y_range->accept (*this); |
|
702 |
|
703 plot_range *z_range = cmd.z_limits (); |
|
704 |
|
705 if (z_range) |
|
706 z_range->accept (*this); |
|
707 } |
|
708 |
|
709 void |
|
710 tree_print_code::visit_plot_range (plot_range& cmd) |
|
711 { |
|
712 os << " ["; |
|
713 |
|
714 tree_expression *lower = cmd.lower_bound (); |
|
715 |
|
716 if (lower) |
|
717 lower->accept (*this); |
|
718 |
|
719 os << ":"; |
|
720 |
|
721 tree_expression *upper = cmd.upper_bound (); |
|
722 |
|
723 if (upper) |
|
724 upper->accept (*this); |
|
725 |
|
726 os << "]"; |
|
727 } |
|
728 |
|
729 void |
|
730 tree_print_code::visit_postfix_expression (tree_postfix_expression& expr) |
|
731 { |
|
732 indent (); |
|
733 |
2961
|
734 print_parens (expr, "("); |
2123
|
735 |
2960
|
736 tree_expression *e = expr.operand (); |
2123
|
737 |
2960
|
738 if (e) |
|
739 e->accept (*this); |
2123
|
740 |
|
741 os << expr.oper (); |
|
742 |
2961
|
743 print_parens (expr, ")"); |
2123
|
744 } |
|
745 |
|
746 void |
|
747 tree_print_code::visit_prefix_expression (tree_prefix_expression& expr) |
|
748 { |
|
749 indent (); |
|
750 |
2961
|
751 print_parens (expr, "("); |
2123
|
752 |
|
753 os << expr.oper (); |
|
754 |
2960
|
755 tree_expression *e = expr.operand (); |
2123
|
756 |
2960
|
757 if (e) |
|
758 e->accept (*this); |
2123
|
759 |
2961
|
760 print_parens (expr, ")"); |
2123
|
761 } |
|
762 |
|
763 void |
|
764 tree_print_code::visit_return_command (tree_return_command&) |
|
765 { |
|
766 indent (); |
|
767 |
|
768 os << "return"; |
|
769 } |
|
770 |
|
771 void |
|
772 tree_print_code::visit_return_list (tree_return_list& lst) |
|
773 { |
|
774 Pix p = lst.first (); |
|
775 |
|
776 while (p) |
|
777 { |
|
778 tree_index_expression *elt = lst (p); |
|
779 |
|
780 lst.next (p); |
|
781 |
|
782 if (elt) |
|
783 { |
|
784 elt->accept (*this); |
|
785 |
|
786 if (p) |
|
787 os << ", "; |
|
788 } |
|
789 } |
|
790 } |
|
791 |
|
792 void |
2969
|
793 tree_print_code::visit_simple_assignment (tree_simple_assignment& expr) |
2123
|
794 { |
|
795 indent (); |
|
796 |
2961
|
797 print_parens (expr, "("); |
2123
|
798 |
2969
|
799 tree_expression *lhs = expr.left_hand_side (); |
2123
|
800 |
2969
|
801 if (lhs) |
|
802 lhs->accept (*this); |
2123
|
803 |
2969
|
804 os << " " << expr.oper () << " "; |
2123
|
805 |
|
806 tree_expression *rhs = expr.right_hand_side (); |
|
807 |
|
808 if (rhs) |
|
809 rhs->accept (*this); |
|
810 |
2961
|
811 print_parens (expr, ")"); |
2123
|
812 } |
|
813 |
|
814 void |
|
815 tree_print_code::visit_statement (tree_statement& stmt) |
|
816 { |
3665
|
817 print_comment_list (stmt.comment_text ()); |
|
818 |
2123
|
819 tree_command *cmd = stmt.command (); |
|
820 |
|
821 if (cmd) |
|
822 { |
|
823 cmd->accept (*this); |
|
824 |
|
825 if (! stmt.print_result ()) |
|
826 os << ";"; |
|
827 |
|
828 newline (); |
|
829 } |
|
830 else |
|
831 { |
|
832 tree_expression *expr = stmt.expression (); |
|
833 |
|
834 if (expr) |
|
835 { |
|
836 expr->accept (*this); |
|
837 |
|
838 if (! stmt.print_result ()) |
|
839 os << ";"; |
|
840 |
|
841 newline (); |
|
842 } |
|
843 } |
|
844 } |
|
845 |
|
846 void |
|
847 tree_print_code::visit_statement_list (tree_statement_list& lst) |
|
848 { |
|
849 for (Pix p = lst.first (); p != 0; lst.next (p)) |
|
850 { |
|
851 tree_statement *elt = lst (p); |
|
852 |
|
853 if (elt) |
|
854 elt->accept (*this); |
|
855 } |
|
856 } |
|
857 |
|
858 void |
|
859 tree_print_code::visit_subplot (subplot& cmd) |
|
860 { |
|
861 tree_expression *sp_plot_data = cmd.plot_data (); |
|
862 |
|
863 if (sp_plot_data) |
|
864 { |
|
865 os << " "; |
|
866 |
|
867 sp_plot_data->accept (*this); |
|
868 } |
|
869 |
3165
|
870 subplot_axes *sp_axes_clause = cmd.axes_clause (); |
|
871 |
|
872 if (sp_axes_clause) |
|
873 sp_axes_clause->accept (*this); |
|
874 |
2123
|
875 subplot_using *sp_using_clause = cmd.using_clause (); |
|
876 |
|
877 if (sp_using_clause) |
|
878 sp_using_clause->accept (*this); |
|
879 |
|
880 tree_expression *sp_title_clause = cmd.title_clause (); |
|
881 |
|
882 if (sp_title_clause) |
|
883 sp_title_clause->accept (*this); |
|
884 |
|
885 subplot_style *sp_style_clause = cmd.style_clause (); |
|
886 |
|
887 if (sp_style_clause) |
|
888 sp_style_clause->accept (*this); |
|
889 } |
|
890 |
|
891 void |
3165
|
892 tree_print_code::visit_subplot_axes (subplot_axes& cmd) |
|
893 { |
|
894 os << " axes " << cmd.axes (); |
|
895 } |
|
896 |
|
897 void |
2123
|
898 tree_print_code::visit_subplot_list (subplot_list& lst) |
|
899 { |
|
900 Pix p = lst.first (); |
|
901 |
|
902 while (p) |
|
903 { |
|
904 subplot *elt = lst (p); |
|
905 |
|
906 lst.next (p); |
|
907 |
|
908 if (elt) |
|
909 { |
|
910 elt->accept (*this); |
|
911 |
|
912 if (p) |
|
913 os << ","; |
|
914 } |
|
915 } |
|
916 } |
|
917 |
|
918 void |
|
919 tree_print_code::visit_subplot_style (subplot_style& cmd) |
|
920 { |
|
921 os << " with " << cmd.style (); |
|
922 |
|
923 tree_expression *sp_linetype = cmd.linetype (); |
|
924 |
|
925 if (sp_linetype) |
|
926 { |
|
927 os << " "; |
|
928 |
|
929 sp_linetype->accept (*this); |
|
930 } |
|
931 |
|
932 tree_expression *sp_pointtype = cmd.pointtype (); |
|
933 |
|
934 if (sp_pointtype) |
|
935 { |
|
936 os << " "; |
|
937 |
|
938 sp_pointtype->accept (*this); |
|
939 } |
|
940 } |
|
941 |
|
942 void |
|
943 tree_print_code::visit_subplot_using (subplot_using& cmd) |
|
944 { |
|
945 os << " using "; |
|
946 |
|
947 int qual_count = cmd.qualifier_count (); |
|
948 |
|
949 if (qual_count > 0) |
|
950 { |
|
951 tree_expression **x = cmd.qualifiers (); |
|
952 |
|
953 for (int i = 0; i < qual_count; i++) |
|
954 { |
|
955 if (i > 0) |
|
956 os << ":"; |
|
957 |
|
958 if (x[i]) |
|
959 x[i]->accept (*this); |
|
960 } |
|
961 } |
|
962 else |
|
963 { |
|
964 tree_expression *scanf_fmt = cmd.scanf_format (); |
|
965 |
|
966 if (scanf_fmt) |
|
967 scanf_fmt->accept (*this); |
|
968 } |
|
969 } |
|
970 |
|
971 void |
2846
|
972 tree_print_code::visit_switch_case (tree_switch_case& cs) |
|
973 { |
3665
|
974 print_comment_list (cs.leading_comment ()); |
|
975 |
2846
|
976 indent (); |
|
977 |
|
978 if (cs.is_default_case ()) |
|
979 os << "otherwise"; |
|
980 else |
|
981 os << "case "; |
|
982 |
|
983 tree_expression *label = cs.case_label (); |
|
984 |
|
985 if (label) |
|
986 label->accept (*this); |
|
987 |
|
988 newline (); |
|
989 |
|
990 tree_statement_list *list = cs.commands (); |
|
991 |
|
992 if (list) |
|
993 { |
3665
|
994 increment_indent_level (); |
|
995 |
2846
|
996 list->accept (*this); |
|
997 |
3665
|
998 newline (); |
|
999 |
2846
|
1000 decrement_indent_level (); |
|
1001 } |
|
1002 } |
|
1003 |
|
1004 void |
|
1005 tree_print_code::visit_switch_case_list (tree_switch_case_list& lst) |
|
1006 { |
|
1007 Pix p = lst.first (); |
|
1008 |
|
1009 while (p) |
|
1010 { |
|
1011 tree_switch_case *elt = lst (p); |
|
1012 |
|
1013 if (elt) |
|
1014 elt->accept (*this); |
|
1015 |
|
1016 lst.next (p); |
|
1017 } |
|
1018 } |
|
1019 |
|
1020 void |
|
1021 tree_print_code::visit_switch_command (tree_switch_command& cmd) |
|
1022 { |
3665
|
1023 print_comment_list (cmd.leading_comment ()); |
|
1024 |
2846
|
1025 indent (); |
|
1026 |
|
1027 os << "switch "; |
|
1028 |
|
1029 tree_expression *expr = cmd.switch_value (); |
|
1030 |
|
1031 if (expr) |
|
1032 expr->accept (*this); |
|
1033 |
|
1034 newline (); |
|
1035 |
|
1036 tree_switch_case_list *list = cmd.case_list (); |
|
1037 |
|
1038 if (list) |
3665
|
1039 { |
|
1040 increment_indent_level (); |
|
1041 |
|
1042 list->accept (*this); |
|
1043 |
|
1044 decrement_indent_level (); |
|
1045 } |
|
1046 |
|
1047 print_indented_comment (cmd.leading_comment ()); |
2846
|
1048 |
|
1049 indent (); |
|
1050 |
|
1051 os << "endswitch"; |
|
1052 } |
|
1053 |
|
1054 void |
2123
|
1055 tree_print_code::visit_try_catch_command (tree_try_catch_command& cmd) |
|
1056 { |
3665
|
1057 print_comment_list (cmd.leading_comment ()); |
|
1058 |
2123
|
1059 indent (); |
|
1060 |
3665
|
1061 os << "try"; |
2123
|
1062 |
|
1063 newline (); |
|
1064 |
|
1065 tree_statement_list *try_code = cmd.body (); |
|
1066 |
|
1067 if (try_code) |
|
1068 { |
|
1069 increment_indent_level (); |
3665
|
1070 |
2123
|
1071 try_code->accept (*this); |
3665
|
1072 |
2123
|
1073 decrement_indent_level (); |
|
1074 } |
|
1075 |
3665
|
1076 print_indented_comment (cmd.middle_comment ()); |
|
1077 |
2123
|
1078 indent (); |
|
1079 |
|
1080 os << "catch"; |
|
1081 |
|
1082 newline (); |
|
1083 |
|
1084 tree_statement_list *catch_code = cmd.cleanup (); |
|
1085 |
|
1086 if (catch_code) |
|
1087 { |
|
1088 increment_indent_level (); |
3665
|
1089 |
2123
|
1090 catch_code->accept (*this); |
3665
|
1091 |
2123
|
1092 decrement_indent_level (); |
|
1093 } |
|
1094 |
3665
|
1095 print_indented_comment (cmd.trailing_comment ()); |
|
1096 |
2123
|
1097 indent (); |
|
1098 |
|
1099 os << "end_try_catch"; |
|
1100 } |
|
1101 |
|
1102 void |
|
1103 tree_print_code::visit_unwind_protect_command |
|
1104 (tree_unwind_protect_command& cmd) |
|
1105 { |
3665
|
1106 print_comment_list (cmd.leading_comment ()); |
|
1107 |
2123
|
1108 indent (); |
|
1109 |
|
1110 os << "unwind_protect"; |
|
1111 |
|
1112 newline (); |
|
1113 |
|
1114 tree_statement_list *unwind_protect_code = cmd.body (); |
|
1115 |
|
1116 if (unwind_protect_code) |
|
1117 { |
|
1118 increment_indent_level (); |
3665
|
1119 |
2123
|
1120 unwind_protect_code->accept (*this); |
3665
|
1121 |
2123
|
1122 decrement_indent_level (); |
|
1123 } |
|
1124 |
3665
|
1125 print_indented_comment (cmd.middle_comment ()); |
|
1126 |
2123
|
1127 indent (); |
|
1128 |
3478
|
1129 os << "unwind_protect_cleanup"; |
2123
|
1130 |
|
1131 newline (); |
|
1132 |
|
1133 tree_statement_list *cleanup_code = cmd.cleanup (); |
|
1134 |
|
1135 if (cleanup_code) |
|
1136 { |
|
1137 increment_indent_level (); |
3665
|
1138 |
2123
|
1139 cleanup_code->accept (*this); |
3665
|
1140 |
2123
|
1141 decrement_indent_level (); |
|
1142 } |
|
1143 |
3665
|
1144 print_indented_comment (cmd.trailing_comment ()); |
|
1145 |
2123
|
1146 indent (); |
|
1147 |
|
1148 os << "end_unwind_protect"; |
|
1149 } |
|
1150 |
|
1151 void |
|
1152 tree_print_code::visit_while_command (tree_while_command& cmd) |
|
1153 { |
3665
|
1154 print_comment_list (cmd.leading_comment ()); |
|
1155 |
2123
|
1156 indent (); |
|
1157 |
|
1158 os << "while "; |
|
1159 |
|
1160 tree_expression *expr = cmd.condition (); |
|
1161 |
|
1162 if (expr) |
|
1163 expr->accept (*this); |
|
1164 |
|
1165 newline (); |
|
1166 |
|
1167 tree_statement_list *list = cmd.body (); |
|
1168 |
|
1169 if (list) |
|
1170 { |
|
1171 increment_indent_level (); |
3665
|
1172 |
2123
|
1173 list->accept (*this); |
3665
|
1174 |
2123
|
1175 decrement_indent_level (); |
|
1176 } |
|
1177 |
3665
|
1178 print_indented_comment (cmd.trailing_comment ()); |
|
1179 |
2123
|
1180 indent (); |
|
1181 |
|
1182 os << "endwhile"; |
|
1183 } |
|
1184 |
3484
|
1185 void |
|
1186 tree_print_code::visit_do_until_command (tree_do_until_command& cmd) |
|
1187 { |
3665
|
1188 print_comment_list (cmd.leading_comment ()); |
|
1189 |
3484
|
1190 indent (); |
|
1191 |
|
1192 os << "do"; |
|
1193 |
|
1194 newline (); |
|
1195 |
|
1196 tree_statement_list *list = cmd.body (); |
|
1197 |
|
1198 if (list) |
|
1199 { |
|
1200 increment_indent_level (); |
3665
|
1201 |
3484
|
1202 list->accept (*this); |
3665
|
1203 |
3484
|
1204 decrement_indent_level (); |
|
1205 } |
|
1206 |
3665
|
1207 print_indented_comment (cmd.trailing_comment ()); |
|
1208 |
3484
|
1209 indent (); |
|
1210 |
|
1211 os << "until"; |
|
1212 |
|
1213 tree_expression *expr = cmd.condition (); |
|
1214 |
|
1215 if (expr) |
|
1216 expr->accept (*this); |
|
1217 |
|
1218 newline (); |
|
1219 } |
|
1220 |
2123
|
1221 // Each print_code() function should call this before printing |
|
1222 // anything. |
|
1223 // |
|
1224 // This doesn't need to be fast, but isn't there a better way? |
|
1225 |
|
1226 void |
|
1227 tree_print_code::indent (void) |
|
1228 { |
|
1229 assert (curr_print_indent_level >= 0); |
|
1230 |
|
1231 if (beginning_of_line) |
|
1232 { |
2900
|
1233 os << prefix; |
|
1234 |
|
1235 for (int i = 0; i < curr_print_indent_level; i++) |
|
1236 os << " "; |
|
1237 |
2123
|
1238 beginning_of_line = false; |
|
1239 } |
|
1240 } |
|
1241 |
|
1242 // All print_code() functions should use this to print new lines. |
|
1243 |
|
1244 void |
|
1245 tree_print_code::newline (void) |
|
1246 { |
|
1247 os << "\n"; |
|
1248 |
|
1249 beginning_of_line = true; |
|
1250 } |
|
1251 |
|
1252 // For ressetting print_code state. |
|
1253 |
|
1254 void |
|
1255 tree_print_code::reset (void) |
|
1256 { |
|
1257 beginning_of_line = true; |
|
1258 curr_print_indent_level = 0; |
|
1259 } |
|
1260 |
2961
|
1261 void |
|
1262 tree_print_code::print_parens (const tree_expression& expr, const char *txt) |
|
1263 { |
|
1264 int n = expr.paren_count (); |
|
1265 |
|
1266 for (int i = 0; i < n; i++) |
|
1267 os << txt; |
|
1268 } |
|
1269 |
3665
|
1270 void |
|
1271 tree_print_code::print_comment_elt (const octave_comment_elt& elt) |
|
1272 { |
|
1273 bool printed_something = false; |
|
1274 |
|
1275 bool prev_char_was_newline = false; |
|
1276 |
3769
|
1277 std::string comment = elt.text (); |
3665
|
1278 |
|
1279 size_t len = comment.length (); |
|
1280 |
|
1281 size_t i = 0; |
|
1282 |
|
1283 while (i < len && comment[i++] == '\n') |
|
1284 ; /* Skip leading new lines. */ |
|
1285 i--; |
|
1286 |
|
1287 while (i < len) |
|
1288 { |
|
1289 char c = comment[i++]; |
|
1290 |
|
1291 if (c == '\n') |
|
1292 { |
|
1293 if (prev_char_was_newline) |
|
1294 os << "##"; |
|
1295 |
|
1296 newline (); |
|
1297 |
|
1298 prev_char_was_newline = true; |
|
1299 } |
|
1300 else |
|
1301 { |
|
1302 if (beginning_of_line) |
|
1303 { |
|
1304 printed_something = true; |
|
1305 |
|
1306 indent (); |
|
1307 |
|
1308 os << "##"; |
|
1309 |
|
1310 if (! (isspace (c) || c == '!')) |
|
1311 os << " "; |
|
1312 } |
|
1313 |
|
1314 os << (char) c; |
|
1315 |
|
1316 prev_char_was_newline = false; |
|
1317 } |
|
1318 } |
|
1319 |
|
1320 if (printed_something && ! beginning_of_line) |
|
1321 newline (); |
|
1322 } |
|
1323 |
|
1324 void |
|
1325 tree_print_code::print_comment_list (octave_comment_list *comment_list) |
|
1326 { |
|
1327 if (comment_list) |
|
1328 { |
|
1329 Pix p = comment_list->first (); |
|
1330 |
|
1331 while (p) |
|
1332 { |
|
1333 octave_comment_elt elt = comment_list->operator () (p); |
|
1334 |
|
1335 print_comment_elt (elt); |
|
1336 |
|
1337 comment_list->next (p); |
|
1338 |
|
1339 if (p) |
|
1340 newline (); |
|
1341 } |
|
1342 } |
|
1343 } |
|
1344 |
|
1345 void |
|
1346 tree_print_code::print_indented_comment (octave_comment_list *comment_list) |
|
1347 { |
|
1348 increment_indent_level (); |
|
1349 |
|
1350 print_comment_list (comment_list); |
|
1351 |
|
1352 decrement_indent_level (); |
|
1353 } |
|
1354 |
2123
|
1355 /* |
|
1356 ;;; Local Variables: *** |
|
1357 ;;; mode: C++ *** |
|
1358 ;;; End: *** |
|
1359 */ |