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