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