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 |
2620
|
556 tree_print_code::visit_no_op_command (tree_no_op_command& cmd) |
|
557 { |
|
558 indent (); |
|
559 |
|
560 os << cmd.original_command (); |
|
561 } |
|
562 |
|
563 void |
2123
|
564 tree_print_code::visit_oct_obj (tree_oct_obj&) |
|
565 { |
|
566 ::error ("visit_oct_obj: internal error"); |
|
567 } |
|
568 |
|
569 void |
2372
|
570 tree_print_code::visit_constant (tree_constant& val) |
2123
|
571 { |
|
572 indent (); |
|
573 |
|
574 bool in_parens = val.is_in_parens (); |
|
575 |
|
576 if (in_parens) |
|
577 os << "("; |
|
578 |
2530
|
579 val.print (os, true, print_original_text); |
2123
|
580 |
|
581 if (in_parens) |
|
582 os << ")"; |
|
583 } |
|
584 |
|
585 void |
|
586 tree_print_code::visit_parameter_list (tree_parameter_list& lst) |
|
587 { |
|
588 Pix p = lst.first (); |
|
589 |
|
590 while (p) |
|
591 { |
|
592 tree_identifier *elt = lst (p); |
|
593 |
|
594 lst.next (p); |
|
595 |
|
596 if (elt) |
|
597 { |
|
598 elt->accept (*this); |
|
599 |
|
600 if (p) |
|
601 os << ", "; |
|
602 } |
|
603 } |
|
604 } |
|
605 |
|
606 void |
|
607 tree_print_code::visit_plot_command (tree_plot_command& cmd) |
|
608 { |
|
609 indent (); |
|
610 |
|
611 int ndim = cmd.num_dimensions (); |
|
612 |
|
613 switch (ndim) |
|
614 { |
|
615 case 1: |
|
616 os << "replot"; |
|
617 break; |
|
618 |
|
619 case 2: |
|
620 os << "gplot"; |
|
621 break; |
|
622 |
|
623 case 3: |
|
624 os << "gsplot"; |
|
625 break; |
|
626 |
|
627 default: |
|
628 os << "<unkown plot command>"; |
|
629 break; |
|
630 } |
|
631 |
|
632 plot_limits *range = cmd.limits (); |
|
633 |
|
634 if (range) |
|
635 range->accept (*this); |
|
636 |
|
637 subplot_list *plot_list = cmd.subplots (); |
|
638 |
|
639 if (plot_list) |
|
640 plot_list->accept (*this); |
|
641 } |
|
642 |
|
643 void |
|
644 tree_print_code::visit_plot_limits (plot_limits& cmd) |
|
645 { |
|
646 plot_range *x_range = cmd.x_limits (); |
|
647 |
|
648 if (x_range) |
|
649 x_range->accept (*this); |
|
650 |
|
651 plot_range *y_range = cmd.y_limits (); |
|
652 |
|
653 if (y_range) |
|
654 y_range->accept (*this); |
|
655 |
|
656 plot_range *z_range = cmd.z_limits (); |
|
657 |
|
658 if (z_range) |
|
659 z_range->accept (*this); |
|
660 } |
|
661 |
|
662 void |
|
663 tree_print_code::visit_plot_range (plot_range& cmd) |
|
664 { |
|
665 os << " ["; |
|
666 |
|
667 tree_expression *lower = cmd.lower_bound (); |
|
668 |
|
669 if (lower) |
|
670 lower->accept (*this); |
|
671 |
|
672 os << ":"; |
|
673 |
|
674 tree_expression *upper = cmd.upper_bound (); |
|
675 |
|
676 if (upper) |
|
677 upper->accept (*this); |
|
678 |
|
679 os << "]"; |
|
680 } |
|
681 |
|
682 void |
|
683 tree_print_code::visit_postfix_expression (tree_postfix_expression& expr) |
|
684 { |
|
685 indent (); |
|
686 |
|
687 bool in_parens = expr.is_in_parens (); |
|
688 |
|
689 if (in_parens) |
|
690 os << "("; |
|
691 |
|
692 tree_identifier *id = expr.ident (); |
|
693 |
|
694 if (id) |
|
695 id->accept (*this); |
|
696 |
|
697 os << expr.oper (); |
|
698 |
|
699 if (in_parens) |
|
700 os << ")"; |
|
701 } |
|
702 |
|
703 void |
|
704 tree_print_code::visit_prefix_expression (tree_prefix_expression& expr) |
|
705 { |
|
706 indent (); |
|
707 |
|
708 bool in_parens = expr.is_in_parens (); |
|
709 |
|
710 if (in_parens) |
|
711 os << "("; |
|
712 |
|
713 os << expr.oper (); |
|
714 |
|
715 tree_identifier *id = expr.ident (); |
|
716 |
|
717 if (id) |
|
718 id->accept (*this); |
|
719 |
|
720 if (in_parens) |
|
721 os << ")"; |
|
722 } |
|
723 |
|
724 void |
|
725 tree_print_code::visit_return_command (tree_return_command&) |
|
726 { |
|
727 indent (); |
|
728 |
|
729 os << "return"; |
|
730 } |
|
731 |
|
732 void |
|
733 tree_print_code::visit_return_list (tree_return_list& lst) |
|
734 { |
|
735 Pix p = lst.first (); |
|
736 |
|
737 while (p) |
|
738 { |
|
739 tree_index_expression *elt = lst (p); |
|
740 |
|
741 lst.next (p); |
|
742 |
|
743 if (elt) |
|
744 { |
|
745 elt->accept (*this); |
|
746 |
|
747 if (p) |
|
748 os << ", "; |
|
749 } |
|
750 } |
|
751 } |
|
752 |
|
753 void |
|
754 tree_print_code::visit_simple_assignment_expression |
|
755 (tree_simple_assignment_expression& expr) |
|
756 { |
|
757 indent (); |
|
758 |
|
759 bool in_parens = expr.is_in_parens (); |
|
760 |
|
761 if (in_parens) |
|
762 os << "("; |
|
763 |
|
764 if (! expr.is_ans_assign ()) |
|
765 { |
|
766 tree_indirect_ref *lhs = expr.left_hand_side (); |
|
767 |
|
768 if (lhs) |
|
769 lhs->accept (*this); |
|
770 |
|
771 tree_argument_list *index = expr.lhs_index (); |
|
772 |
|
773 if (index) |
|
774 { |
|
775 os << " ("; |
|
776 index->accept (*this); |
|
777 os << ")"; |
|
778 } |
|
779 |
|
780 os << " = "; |
|
781 } |
|
782 |
|
783 tree_expression *rhs = expr.right_hand_side (); |
|
784 |
|
785 if (rhs) |
|
786 rhs->accept (*this); |
|
787 |
|
788 if (in_parens) |
|
789 os << ")"; |
|
790 } |
|
791 |
|
792 void |
|
793 tree_print_code::visit_statement (tree_statement& stmt) |
|
794 { |
|
795 tree_command *cmd = stmt.command (); |
|
796 |
|
797 if (cmd) |
|
798 { |
|
799 cmd->accept (*this); |
|
800 |
|
801 if (! stmt.print_result ()) |
|
802 os << ";"; |
|
803 |
|
804 newline (); |
|
805 } |
|
806 else |
|
807 { |
|
808 tree_expression *expr = stmt.expression (); |
|
809 |
|
810 if (expr) |
|
811 { |
|
812 expr->accept (*this); |
|
813 |
|
814 if (! stmt.print_result ()) |
|
815 os << ";"; |
|
816 |
|
817 newline (); |
|
818 } |
|
819 } |
|
820 } |
|
821 |
|
822 void |
|
823 tree_print_code::visit_statement_list (tree_statement_list& lst) |
|
824 { |
|
825 for (Pix p = lst.first (); p != 0; lst.next (p)) |
|
826 { |
|
827 tree_statement *elt = lst (p); |
|
828 |
|
829 if (elt) |
|
830 elt->accept (*this); |
|
831 } |
|
832 } |
|
833 |
|
834 void |
|
835 tree_print_code::visit_subplot (subplot& cmd) |
|
836 { |
|
837 tree_expression *sp_plot_data = cmd.plot_data (); |
|
838 |
|
839 if (sp_plot_data) |
|
840 { |
|
841 os << " "; |
|
842 |
|
843 sp_plot_data->accept (*this); |
|
844 } |
|
845 |
|
846 subplot_using *sp_using_clause = cmd.using_clause (); |
|
847 |
|
848 if (sp_using_clause) |
|
849 sp_using_clause->accept (*this); |
|
850 |
|
851 tree_expression *sp_title_clause = cmd.title_clause (); |
|
852 |
|
853 if (sp_title_clause) |
|
854 sp_title_clause->accept (*this); |
|
855 |
|
856 subplot_style *sp_style_clause = cmd.style_clause (); |
|
857 |
|
858 if (sp_style_clause) |
|
859 sp_style_clause->accept (*this); |
|
860 } |
|
861 |
|
862 void |
|
863 tree_print_code::visit_subplot_list (subplot_list& lst) |
|
864 { |
|
865 Pix p = lst.first (); |
|
866 |
|
867 while (p) |
|
868 { |
|
869 subplot *elt = lst (p); |
|
870 |
|
871 lst.next (p); |
|
872 |
|
873 if (elt) |
|
874 { |
|
875 elt->accept (*this); |
|
876 |
|
877 if (p) |
|
878 os << ","; |
|
879 } |
|
880 } |
|
881 } |
|
882 |
|
883 void |
|
884 tree_print_code::visit_subplot_style (subplot_style& cmd) |
|
885 { |
|
886 os << " with " << cmd.style (); |
|
887 |
|
888 tree_expression *sp_linetype = cmd.linetype (); |
|
889 |
|
890 if (sp_linetype) |
|
891 { |
|
892 os << " "; |
|
893 |
|
894 sp_linetype->accept (*this); |
|
895 } |
|
896 |
|
897 tree_expression *sp_pointtype = cmd.pointtype (); |
|
898 |
|
899 if (sp_pointtype) |
|
900 { |
|
901 os << " "; |
|
902 |
|
903 sp_pointtype->accept (*this); |
|
904 } |
|
905 } |
|
906 |
|
907 void |
|
908 tree_print_code::visit_subplot_using (subplot_using& cmd) |
|
909 { |
|
910 os << " using "; |
|
911 |
|
912 int qual_count = cmd.qualifier_count (); |
|
913 |
|
914 if (qual_count > 0) |
|
915 { |
|
916 tree_expression **x = cmd.qualifiers (); |
|
917 |
|
918 for (int i = 0; i < qual_count; i++) |
|
919 { |
|
920 if (i > 0) |
|
921 os << ":"; |
|
922 |
|
923 if (x[i]) |
|
924 x[i]->accept (*this); |
|
925 } |
|
926 } |
|
927 else |
|
928 { |
|
929 tree_expression *scanf_fmt = cmd.scanf_format (); |
|
930 |
|
931 if (scanf_fmt) |
|
932 scanf_fmt->accept (*this); |
|
933 } |
|
934 } |
|
935 |
|
936 void |
|
937 tree_print_code::visit_try_catch_command (tree_try_catch_command& cmd) |
|
938 { |
|
939 indent (); |
|
940 |
|
941 os << "try_catch"; |
|
942 |
|
943 newline (); |
|
944 |
|
945 tree_statement_list *try_code = cmd.body (); |
|
946 |
|
947 if (try_code) |
|
948 { |
|
949 increment_indent_level (); |
|
950 try_code->accept (*this); |
|
951 decrement_indent_level (); |
|
952 } |
|
953 |
|
954 indent (); |
|
955 |
|
956 os << "catch"; |
|
957 |
|
958 newline (); |
|
959 |
|
960 tree_statement_list *catch_code = cmd.cleanup (); |
|
961 |
|
962 if (catch_code) |
|
963 { |
|
964 increment_indent_level (); |
|
965 catch_code->accept (*this); |
|
966 decrement_indent_level (); |
|
967 } |
|
968 |
|
969 indent (); |
|
970 |
|
971 os << "end_try_catch"; |
|
972 } |
|
973 |
|
974 void |
|
975 tree_print_code::visit_unary_expression (tree_unary_expression& expr) |
|
976 { |
|
977 indent (); |
|
978 |
|
979 bool in_parens = expr.is_in_parens (); |
|
980 |
|
981 if (in_parens) |
|
982 os << "("; |
|
983 |
|
984 tree_expression *op = expr.operand (); |
|
985 |
2372
|
986 if (expr.is_prefix_op ()) |
|
987 { |
|
988 os << expr.oper (); |
2123
|
989 |
|
990 if (op) |
|
991 op->accept (*this); |
2372
|
992 } |
|
993 else |
|
994 { |
2123
|
995 if (op) |
|
996 op->accept (*this); |
2372
|
997 |
|
998 os << expr.oper (); |
2123
|
999 } |
|
1000 |
|
1001 if (in_parens) |
|
1002 os << ")"; |
|
1003 } |
|
1004 |
|
1005 void |
|
1006 tree_print_code::visit_unwind_protect_command |
|
1007 (tree_unwind_protect_command& cmd) |
|
1008 { |
|
1009 indent (); |
|
1010 |
|
1011 os << "unwind_protect"; |
|
1012 |
|
1013 newline (); |
|
1014 |
|
1015 tree_statement_list *unwind_protect_code = cmd.body (); |
|
1016 |
|
1017 if (unwind_protect_code) |
|
1018 { |
|
1019 increment_indent_level (); |
|
1020 unwind_protect_code->accept (*this); |
|
1021 decrement_indent_level (); |
|
1022 } |
|
1023 |
|
1024 indent (); |
|
1025 |
|
1026 os << "cleanup_code"; |
|
1027 |
|
1028 newline (); |
|
1029 |
|
1030 tree_statement_list *cleanup_code = cmd.cleanup (); |
|
1031 |
|
1032 if (cleanup_code) |
|
1033 { |
|
1034 increment_indent_level (); |
|
1035 cleanup_code->accept (*this); |
|
1036 decrement_indent_level (); |
|
1037 } |
|
1038 |
|
1039 indent (); |
|
1040 |
|
1041 os << "end_unwind_protect"; |
|
1042 } |
|
1043 |
|
1044 void |
|
1045 tree_print_code::visit_while_command (tree_while_command& cmd) |
|
1046 { |
|
1047 indent (); |
|
1048 |
|
1049 os << "while "; |
|
1050 |
|
1051 tree_expression *expr = cmd.condition (); |
|
1052 |
|
1053 if (expr) |
|
1054 expr->accept (*this); |
|
1055 |
|
1056 newline (); |
|
1057 |
|
1058 tree_statement_list *list = cmd.body (); |
|
1059 |
|
1060 if (list) |
|
1061 { |
|
1062 increment_indent_level (); |
|
1063 list->accept (*this); |
|
1064 decrement_indent_level (); |
|
1065 } |
|
1066 |
|
1067 indent (); |
|
1068 |
|
1069 os << "endwhile"; |
|
1070 } |
|
1071 |
|
1072 // Current indentation. |
|
1073 int tree_print_code::curr_print_indent_level = 0; |
|
1074 |
|
1075 // Nonzero means we are at the beginning of a line. |
|
1076 bool tree_print_code::beginning_of_line = true; |
|
1077 |
|
1078 // Each print_code() function should call this before printing |
|
1079 // anything. |
|
1080 // |
|
1081 // This doesn't need to be fast, but isn't there a better way? |
|
1082 |
|
1083 void |
|
1084 tree_print_code::indent (void) |
|
1085 { |
|
1086 assert (curr_print_indent_level >= 0); |
|
1087 |
|
1088 if (beginning_of_line) |
|
1089 { |
2530
|
1090 os.form ("%s%*s", prefix.c_str (), curr_print_indent_level, ""); |
2123
|
1091 beginning_of_line = false; |
|
1092 } |
|
1093 } |
|
1094 |
|
1095 // All print_code() functions should use this to print new lines. |
|
1096 |
|
1097 void |
|
1098 tree_print_code::newline (void) |
|
1099 { |
|
1100 os << "\n"; |
|
1101 |
|
1102 beginning_of_line = true; |
|
1103 } |
|
1104 |
|
1105 // For ressetting print_code state. |
|
1106 |
|
1107 void |
|
1108 tree_print_code::reset (void) |
|
1109 { |
|
1110 beginning_of_line = true; |
|
1111 curr_print_indent_level = 0; |
|
1112 } |
|
1113 |
|
1114 /* |
|
1115 ;;; Local Variables: *** |
|
1116 ;;; mode: C++ *** |
|
1117 ;;; End: *** |
|
1118 */ |