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