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