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