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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2123
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3665
|
31 #include <cctype> |
|
32 |
3503
|
33 #include <iostream> |
2123
|
34 |
3665
|
35 #include "comment-list.h" |
2123
|
36 #include "error.h" |
2969
|
37 #include "ov-usr-fcn.h" |
2123
|
38 #include "pr-output.h" |
2988
|
39 #include "pt-all.h" |
2123
|
40 |
|
41 void |
|
42 tree_print_code::visit_argument_list (tree_argument_list& lst) |
|
43 { |
4219
|
44 tree_argument_list::iterator p = lst.begin (); |
2123
|
45 |
4219
|
46 while (p != lst.end ()) |
2123
|
47 { |
4219
|
48 tree_expression *elt = *p++; |
2123
|
49 |
|
50 if (elt) |
|
51 { |
|
52 elt->accept (*this); |
|
53 |
4219
|
54 if (p != lst.end ()) |
2123
|
55 os << ", "; |
|
56 } |
|
57 } |
|
58 } |
|
59 |
|
60 void |
|
61 tree_print_code::visit_binary_expression (tree_binary_expression& expr) |
|
62 { |
|
63 indent (); |
|
64 |
2961
|
65 print_parens (expr, "("); |
2123
|
66 |
|
67 tree_expression *op1 = expr.lhs (); |
|
68 |
|
69 if (op1) |
|
70 op1->accept (*this); |
|
71 |
|
72 os << " " << expr.oper () << " "; |
|
73 |
|
74 tree_expression *op2 = expr.rhs (); |
|
75 |
|
76 if (op2) |
|
77 op2->accept (*this); |
|
78 |
2961
|
79 print_parens (expr, ")"); |
2123
|
80 } |
|
81 |
|
82 void |
4207
|
83 tree_print_code::visit_break_command (tree_break_command&) |
2123
|
84 { |
|
85 indent (); |
|
86 |
|
87 os << "break"; |
|
88 } |
|
89 |
|
90 void |
|
91 tree_print_code::visit_colon_expression (tree_colon_expression& expr) |
|
92 { |
|
93 indent (); |
|
94 |
2961
|
95 print_parens (expr, "("); |
2123
|
96 |
|
97 tree_expression *op1 = expr.base (); |
|
98 |
|
99 if (op1) |
|
100 op1->accept (*this); |
|
101 |
|
102 // Stupid syntax. |
|
103 |
|
104 tree_expression *op3 = expr.increment (); |
|
105 |
|
106 if (op3) |
|
107 { |
|
108 os << ":"; |
|
109 op3->accept (*this); |
|
110 } |
|
111 |
|
112 tree_expression *op2 = expr.limit (); |
|
113 |
|
114 if (op2) |
|
115 { |
|
116 os << ":"; |
|
117 op2->accept (*this); |
|
118 } |
|
119 |
2961
|
120 print_parens (expr, ")"); |
2123
|
121 } |
|
122 |
|
123 void |
4207
|
124 tree_print_code::visit_continue_command (tree_continue_command&) |
2123
|
125 { |
|
126 indent (); |
|
127 |
|
128 os << "continue"; |
|
129 } |
|
130 |
|
131 void |
2846
|
132 tree_print_code::visit_decl_command (tree_decl_command& cmd) |
|
133 { |
|
134 indent (); |
|
135 |
|
136 os << cmd.name () << " "; |
|
137 |
|
138 tree_decl_init_list *init_list = cmd.initializer_list (); |
|
139 |
|
140 if (init_list) |
|
141 init_list->accept (*this); |
|
142 } |
|
143 |
|
144 void |
|
145 tree_print_code::visit_decl_elt (tree_decl_elt& cmd) |
|
146 { |
|
147 tree_identifier *id = cmd.ident (); |
|
148 |
|
149 if (id) |
|
150 id->accept (*this); |
|
151 |
2969
|
152 tree_expression *expr = cmd.expression (); |
2846
|
153 |
2969
|
154 if (expr) |
|
155 { |
|
156 os << " = "; |
|
157 |
|
158 expr->accept (*this); |
|
159 } |
2846
|
160 } |
|
161 |
|
162 void |
|
163 tree_print_code::visit_decl_init_list (tree_decl_init_list& lst) |
|
164 { |
4219
|
165 tree_decl_init_list::iterator p = lst.begin (); |
2846
|
166 |
4219
|
167 while (p != lst.end ()) |
2846
|
168 { |
4219
|
169 tree_decl_elt *elt = *p++; |
2846
|
170 |
|
171 if (elt) |
|
172 { |
|
173 elt->accept (*this); |
|
174 |
4219
|
175 if (p != lst.end ()) |
2846
|
176 os << ", "; |
|
177 } |
|
178 } |
|
179 } |
|
180 |
|
181 void |
2969
|
182 tree_print_code::visit_simple_for_command (tree_simple_for_command& cmd) |
2123
|
183 { |
3665
|
184 print_comment_list (cmd.leading_comment ()); |
|
185 |
2123
|
186 indent (); |
|
187 |
|
188 os << "for "; |
|
189 |
2969
|
190 tree_expression *lhs = cmd.left_hand_side (); |
2123
|
191 |
2969
|
192 if (lhs) |
|
193 lhs->accept (*this); |
2123
|
194 |
|
195 os << " = "; |
|
196 |
|
197 tree_expression *expr = cmd.control_expr (); |
|
198 |
|
199 if (expr) |
|
200 expr->accept (*this); |
|
201 |
|
202 newline (); |
|
203 |
|
204 tree_statement_list *list = cmd.body (); |
|
205 |
|
206 if (list) |
|
207 { |
|
208 increment_indent_level (); |
3665
|
209 |
2123
|
210 list->accept (*this); |
3665
|
211 |
2123
|
212 decrement_indent_level (); |
|
213 } |
|
214 |
3665
|
215 print_indented_comment (cmd.trailing_comment ()); |
|
216 |
2123
|
217 indent (); |
|
218 |
|
219 os << "endfor"; |
|
220 } |
|
221 |
|
222 void |
2969
|
223 tree_print_code::visit_complex_for_command (tree_complex_for_command& cmd) |
|
224 { |
3665
|
225 print_comment_list (cmd.leading_comment ()); |
|
226 |
2969
|
227 indent (); |
|
228 |
|
229 os << "for ["; |
|
230 |
|
231 tree_argument_list *lhs = cmd.left_hand_side (); |
|
232 |
|
233 if (lhs) |
|
234 lhs->accept (*this); |
|
235 |
|
236 os << "] = "; |
|
237 |
|
238 tree_expression *expr = cmd.control_expr (); |
|
239 |
|
240 if (expr) |
|
241 expr->accept (*this); |
|
242 |
|
243 newline (); |
|
244 |
|
245 tree_statement_list *list = cmd.body (); |
|
246 |
|
247 if (list) |
|
248 { |
|
249 increment_indent_level (); |
3665
|
250 |
2969
|
251 list->accept (*this); |
3665
|
252 |
2969
|
253 decrement_indent_level (); |
|
254 } |
|
255 |
3665
|
256 print_indented_comment (cmd.trailing_comment ()); |
|
257 |
2969
|
258 indent (); |
|
259 |
|
260 os << "endfor"; |
|
261 } |
|
262 |
|
263 void |
2891
|
264 tree_print_code::visit_octave_user_function (octave_user_function& fcn) |
2123
|
265 { |
|
266 reset (); |
|
267 |
2891
|
268 visit_octave_user_function_header (fcn); |
2123
|
269 |
|
270 tree_statement_list *cmd_list = fcn.body (); |
|
271 |
|
272 if (cmd_list) |
|
273 { |
|
274 increment_indent_level (); |
3665
|
275 |
2123
|
276 cmd_list->accept (*this); |
3665
|
277 |
2123
|
278 decrement_indent_level (); |
|
279 } |
|
280 |
2891
|
281 visit_octave_user_function_trailer (fcn); |
2123
|
282 } |
|
283 |
|
284 void |
2891
|
285 tree_print_code::visit_octave_user_function_header (octave_user_function& fcn) |
2123
|
286 { |
3665
|
287 octave_comment_list *leading_comment = fcn.leading_comment (); |
|
288 |
|
289 if (leading_comment) |
|
290 { |
|
291 print_comment_list (leading_comment); |
|
292 newline (); |
|
293 } |
|
294 |
2123
|
295 indent (); |
|
296 |
|
297 os << "function "; |
|
298 |
|
299 tree_parameter_list *ret_list = fcn.return_list (); |
|
300 |
|
301 if (ret_list) |
|
302 { |
|
303 bool takes_var_return = fcn.takes_var_return (); |
|
304 |
|
305 int len = ret_list->length (); |
|
306 |
|
307 if (len > 1 || takes_var_return) |
|
308 os << "["; |
|
309 |
|
310 ret_list->accept (*this); |
|
311 |
|
312 if (takes_var_return) |
|
313 { |
|
314 if (len > 0) |
|
315 os << ", "; |
|
316 |
|
317 os << "..."; |
|
318 } |
|
319 |
|
320 if (len > 1 || takes_var_return) |
|
321 os << "]"; |
|
322 |
|
323 os << " = "; |
|
324 } |
|
325 |
3523
|
326 std::string fcn_name = fcn.function_name (); |
2123
|
327 |
3523
|
328 os << (fcn_name.empty () ? std::string ("(empty)") : fcn_name) << " "; |
2123
|
329 |
|
330 tree_parameter_list *param_list = fcn.parameter_list (); |
|
331 |
|
332 if (param_list) |
|
333 { |
|
334 bool takes_varargs = fcn.takes_varargs (); |
|
335 |
|
336 int len = param_list->length (); |
|
337 |
|
338 if (len > 0 || takes_varargs) |
|
339 os << "("; |
|
340 |
|
341 param_list->accept (*this); |
|
342 |
|
343 if (takes_varargs) |
|
344 { |
|
345 if (len > 0) |
|
346 os << ", "; |
|
347 |
|
348 os << "..."; |
|
349 } |
|
350 |
|
351 if (len > 0 || takes_varargs) |
|
352 { |
|
353 os << ")"; |
|
354 newline (); |
|
355 } |
|
356 } |
|
357 else |
|
358 { |
|
359 os << "()"; |
|
360 newline (); |
|
361 } |
|
362 } |
|
363 |
|
364 void |
3665
|
365 tree_print_code::visit_octave_user_function_trailer (octave_user_function& fcn) |
2123
|
366 { |
3665
|
367 print_indented_comment (fcn.trailing_comment ()); |
|
368 |
2123
|
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 |
3523
|
383 std::string nm = id.name (); |
|
384 os << (nm.empty () ? std::string ("(empty)") : nm); |
2123
|
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 tree_statement_list *list = cmd.commands (); |
|
400 |
|
401 if (list) |
|
402 { |
3665
|
403 increment_indent_level (); |
|
404 |
2123
|
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 { |
3665
|
414 print_comment_list (cmd.leading_comment ()); |
|
415 |
2123
|
416 indent (); |
|
417 |
|
418 os << "if "; |
|
419 |
|
420 tree_if_command_list *list = cmd.cmd_list (); |
|
421 |
|
422 if (list) |
|
423 list->accept (*this); |
|
424 |
3665
|
425 print_indented_comment (cmd.trailing_comment ()); |
|
426 |
2123
|
427 indent (); |
|
428 |
|
429 os << "endif"; |
|
430 } |
|
431 |
|
432 void |
|
433 tree_print_code::visit_if_command_list (tree_if_command_list& lst) |
|
434 { |
4219
|
435 tree_if_command_list::iterator p = lst.begin (); |
2123
|
436 |
|
437 bool first_elt = true; |
|
438 |
4219
|
439 while (p != lst.end ()) |
2123
|
440 { |
4219
|
441 tree_if_clause *elt = *p++; |
2123
|
442 |
|
443 if (elt) |
|
444 { |
|
445 if (! first_elt) |
|
446 { |
3665
|
447 print_indented_comment (elt->leading_comment ()); |
|
448 |
2123
|
449 indent (); |
|
450 |
|
451 if (elt->is_else_clause ()) |
|
452 os << "else"; |
|
453 else |
|
454 os << "elseif "; |
|
455 } |
|
456 |
|
457 elt->accept (*this); |
|
458 } |
|
459 |
|
460 first_elt = false; |
|
461 } |
|
462 } |
|
463 |
|
464 void |
|
465 tree_print_code::visit_index_expression (tree_index_expression& expr) |
|
466 { |
|
467 indent (); |
|
468 |
2961
|
469 print_parens (expr, "("); |
2123
|
470 |
3665
|
471 bool expr_has_parens = false; |
|
472 |
3010
|
473 tree_expression *e = expr.expression (); |
2123
|
474 |
2969
|
475 if (e) |
3665
|
476 { |
|
477 e->accept (*this); |
|
478 |
|
479 expr_has_parens = e->is_postfix_indexed (); |
|
480 } |
2123
|
481 |
4219
|
482 std::list<tree_argument_list *> arg_lists = expr.arg_lists (); |
3933
|
483 std::string type_tags = expr.type_tags (); |
4219
|
484 std::list<string_vector> arg_names = expr.arg_names (); |
2123
|
485 |
3933
|
486 int n = type_tags.length (); |
|
487 |
4219
|
488 std::list<tree_argument_list *>::iterator p_arg_lists = arg_lists.begin (); |
|
489 std::list<string_vector>::iterator p_arg_names = arg_names.begin (); |
3933
|
490 |
|
491 for (int i = 0; i < n; i++) |
2123
|
492 { |
3933
|
493 switch (type_tags[i]) |
|
494 { |
|
495 case '(': |
|
496 { |
|
497 os << " ("; |
4219
|
498 tree_argument_list *l = *p_arg_lists; |
3933
|
499 if (l) |
|
500 l->accept (*this); |
|
501 os << ")"; |
|
502 } |
|
503 break; |
|
504 |
|
505 case '{': |
|
506 { |
|
507 os << " {"; |
4219
|
508 tree_argument_list *l = *p_arg_lists; |
3933
|
509 if (l) |
|
510 l->accept (*this); |
|
511 os << "}"; |
|
512 } |
|
513 break; |
|
514 |
|
515 case '.': |
|
516 { |
4219
|
517 string_vector nm = *p_arg_names; |
3933
|
518 assert (nm.length () == 1); |
|
519 os << "." << nm(0); |
|
520 } |
|
521 break; |
|
522 |
|
523 default: |
|
524 panic_impossible (); |
|
525 } |
|
526 |
4219
|
527 p_arg_lists++; |
|
528 p_arg_names++; |
3930
|
529 } |
2123
|
530 |
2961
|
531 print_parens (expr, ")"); |
2123
|
532 } |
|
533 |
|
534 void |
|
535 tree_print_code::visit_matrix (tree_matrix& lst) |
|
536 { |
|
537 indent (); |
|
538 |
2961
|
539 print_parens (lst, "("); |
2123
|
540 |
|
541 os << "["; |
|
542 |
4219
|
543 tree_matrix::iterator p = lst.begin (); |
2123
|
544 |
4219
|
545 while (p != lst.end ()) |
2123
|
546 { |
4219
|
547 tree_argument_list *elt = *p++; |
2123
|
548 |
|
549 if (elt) |
|
550 { |
|
551 elt->accept (*this); |
|
552 |
4219
|
553 if (p != lst.end ()) |
2123
|
554 os << "; "; |
|
555 } |
|
556 } |
|
557 |
|
558 os << "]"; |
|
559 |
2961
|
560 print_parens (lst, ")"); |
2123
|
561 } |
|
562 |
|
563 void |
3351
|
564 tree_print_code::visit_cell (tree_cell& lst) |
|
565 { |
|
566 indent (); |
|
567 |
|
568 print_parens (lst, "("); |
|
569 |
|
570 os << "{"; |
|
571 |
4219
|
572 tree_cell::iterator p = lst.begin (); |
3351
|
573 |
4219
|
574 while (p != lst.end ()) |
3351
|
575 { |
4219
|
576 tree_argument_list *elt = *p++; |
3351
|
577 |
|
578 if (elt) |
|
579 { |
|
580 elt->accept (*this); |
|
581 |
4219
|
582 if (p != lst.end ()) |
3351
|
583 os << "; "; |
|
584 } |
|
585 } |
|
586 |
|
587 os << "}"; |
|
588 |
|
589 print_parens (lst, ")"); |
|
590 } |
|
591 |
|
592 void |
2969
|
593 tree_print_code::visit_multi_assignment (tree_multi_assignment& expr) |
2123
|
594 { |
|
595 indent (); |
|
596 |
2961
|
597 print_parens (expr, "("); |
2123
|
598 |
2969
|
599 tree_argument_list *lhs = expr.left_hand_side (); |
2123
|
600 |
|
601 if (lhs) |
|
602 { |
|
603 int len = lhs->length (); |
|
604 |
|
605 if (len > 1) |
|
606 os << "["; |
|
607 |
|
608 lhs->accept (*this); |
|
609 |
|
610 if (len > 1) |
|
611 os << "]"; |
|
612 } |
|
613 |
3208
|
614 os << " " << expr.oper () << " "; |
2123
|
615 |
2969
|
616 tree_expression *rhs = expr.right_hand_side (); |
2123
|
617 |
|
618 if (rhs) |
|
619 rhs->accept (*this); |
|
620 |
2961
|
621 print_parens (expr, ")"); |
2123
|
622 } |
|
623 |
|
624 void |
2620
|
625 tree_print_code::visit_no_op_command (tree_no_op_command& cmd) |
|
626 { |
|
627 indent (); |
|
628 |
|
629 os << cmd.original_command (); |
|
630 } |
|
631 |
|
632 void |
2372
|
633 tree_print_code::visit_constant (tree_constant& val) |
2123
|
634 { |
|
635 indent (); |
|
636 |
2961
|
637 print_parens (val, "("); |
2123
|
638 |
2942
|
639 val.print_raw (os, true, print_original_text); |
2123
|
640 |
2961
|
641 print_parens (val, ")"); |
2123
|
642 } |
|
643 |
|
644 void |
|
645 tree_print_code::visit_parameter_list (tree_parameter_list& lst) |
|
646 { |
4219
|
647 tree_parameter_list::iterator p = lst.begin (); |
2123
|
648 |
4219
|
649 while (p != lst.end ()) |
2123
|
650 { |
4219
|
651 tree_identifier *elt = *p++; |
2123
|
652 |
|
653 if (elt) |
|
654 { |
|
655 elt->accept (*this); |
|
656 |
4219
|
657 if (p != lst.end ()) |
2123
|
658 os << ", "; |
|
659 } |
|
660 } |
|
661 } |
|
662 |
|
663 void |
|
664 tree_print_code::visit_plot_command (tree_plot_command& cmd) |
|
665 { |
|
666 indent (); |
|
667 |
|
668 int ndim = cmd.num_dimensions (); |
|
669 |
|
670 switch (ndim) |
|
671 { |
|
672 case 1: |
|
673 os << "replot"; |
|
674 break; |
|
675 |
|
676 case 2: |
|
677 os << "gplot"; |
|
678 break; |
|
679 |
|
680 case 3: |
|
681 os << "gsplot"; |
|
682 break; |
|
683 |
|
684 default: |
|
685 os << "<unkown plot command>"; |
|
686 break; |
|
687 } |
|
688 |
|
689 plot_limits *range = cmd.limits (); |
|
690 |
|
691 if (range) |
|
692 range->accept (*this); |
|
693 |
|
694 subplot_list *plot_list = cmd.subplots (); |
|
695 |
|
696 if (plot_list) |
|
697 plot_list->accept (*this); |
|
698 } |
|
699 |
|
700 void |
|
701 tree_print_code::visit_plot_limits (plot_limits& cmd) |
|
702 { |
|
703 plot_range *x_range = cmd.x_limits (); |
|
704 |
|
705 if (x_range) |
|
706 x_range->accept (*this); |
|
707 |
|
708 plot_range *y_range = cmd.y_limits (); |
|
709 |
|
710 if (y_range) |
|
711 y_range->accept (*this); |
|
712 |
|
713 plot_range *z_range = cmd.z_limits (); |
|
714 |
|
715 if (z_range) |
|
716 z_range->accept (*this); |
|
717 } |
|
718 |
|
719 void |
|
720 tree_print_code::visit_plot_range (plot_range& cmd) |
|
721 { |
|
722 os << " ["; |
|
723 |
|
724 tree_expression *lower = cmd.lower_bound (); |
|
725 |
|
726 if (lower) |
|
727 lower->accept (*this); |
|
728 |
|
729 os << ":"; |
|
730 |
|
731 tree_expression *upper = cmd.upper_bound (); |
|
732 |
|
733 if (upper) |
|
734 upper->accept (*this); |
|
735 |
|
736 os << "]"; |
|
737 } |
|
738 |
|
739 void |
|
740 tree_print_code::visit_postfix_expression (tree_postfix_expression& expr) |
|
741 { |
|
742 indent (); |
|
743 |
2961
|
744 print_parens (expr, "("); |
2123
|
745 |
2960
|
746 tree_expression *e = expr.operand (); |
2123
|
747 |
2960
|
748 if (e) |
|
749 e->accept (*this); |
2123
|
750 |
|
751 os << expr.oper (); |
|
752 |
2961
|
753 print_parens (expr, ")"); |
2123
|
754 } |
|
755 |
|
756 void |
|
757 tree_print_code::visit_prefix_expression (tree_prefix_expression& expr) |
|
758 { |
|
759 indent (); |
|
760 |
2961
|
761 print_parens (expr, "("); |
2123
|
762 |
|
763 os << expr.oper (); |
|
764 |
2960
|
765 tree_expression *e = expr.operand (); |
2123
|
766 |
2960
|
767 if (e) |
|
768 e->accept (*this); |
2123
|
769 |
2961
|
770 print_parens (expr, ")"); |
2123
|
771 } |
|
772 |
|
773 void |
4207
|
774 tree_print_code::visit_return_command (tree_return_command&) |
2123
|
775 { |
|
776 indent (); |
|
777 |
|
778 os << "return"; |
|
779 } |
|
780 |
|
781 void |
|
782 tree_print_code::visit_return_list (tree_return_list& lst) |
|
783 { |
4219
|
784 tree_return_list::iterator p = lst.begin (); |
2123
|
785 |
4219
|
786 while (p != lst.end ()) |
2123
|
787 { |
4219
|
788 tree_index_expression *elt = *p++; |
2123
|
789 |
|
790 if (elt) |
|
791 { |
|
792 elt->accept (*this); |
|
793 |
4219
|
794 if (p != lst.end ()) |
2123
|
795 os << ", "; |
|
796 } |
|
797 } |
|
798 } |
|
799 |
|
800 void |
2969
|
801 tree_print_code::visit_simple_assignment (tree_simple_assignment& expr) |
2123
|
802 { |
|
803 indent (); |
|
804 |
2961
|
805 print_parens (expr, "("); |
2123
|
806 |
2969
|
807 tree_expression *lhs = expr.left_hand_side (); |
2123
|
808 |
2969
|
809 if (lhs) |
|
810 lhs->accept (*this); |
2123
|
811 |
2969
|
812 os << " " << expr.oper () << " "; |
2123
|
813 |
|
814 tree_expression *rhs = expr.right_hand_side (); |
|
815 |
|
816 if (rhs) |
|
817 rhs->accept (*this); |
|
818 |
2961
|
819 print_parens (expr, ")"); |
2123
|
820 } |
|
821 |
|
822 void |
|
823 tree_print_code::visit_statement (tree_statement& stmt) |
|
824 { |
3665
|
825 print_comment_list (stmt.comment_text ()); |
|
826 |
2123
|
827 tree_command *cmd = stmt.command (); |
|
828 |
|
829 if (cmd) |
|
830 { |
|
831 cmd->accept (*this); |
|
832 |
|
833 if (! stmt.print_result ()) |
|
834 os << ";"; |
|
835 |
|
836 newline (); |
|
837 } |
|
838 else |
|
839 { |
|
840 tree_expression *expr = stmt.expression (); |
|
841 |
|
842 if (expr) |
|
843 { |
|
844 expr->accept (*this); |
|
845 |
|
846 if (! stmt.print_result ()) |
|
847 os << ";"; |
|
848 |
|
849 newline (); |
|
850 } |
|
851 } |
|
852 } |
|
853 |
|
854 void |
|
855 tree_print_code::visit_statement_list (tree_statement_list& lst) |
|
856 { |
4219
|
857 for (tree_statement_list::iterator p = lst.begin (); p != lst.end (); p++) |
2123
|
858 { |
4219
|
859 tree_statement *elt = *p; |
2123
|
860 |
|
861 if (elt) |
|
862 elt->accept (*this); |
|
863 } |
|
864 } |
|
865 |
|
866 void |
|
867 tree_print_code::visit_subplot (subplot& cmd) |
|
868 { |
|
869 tree_expression *sp_plot_data = cmd.plot_data (); |
|
870 |
|
871 if (sp_plot_data) |
|
872 { |
|
873 os << " "; |
|
874 |
|
875 sp_plot_data->accept (*this); |
|
876 } |
|
877 |
3165
|
878 subplot_axes *sp_axes_clause = cmd.axes_clause (); |
|
879 |
|
880 if (sp_axes_clause) |
|
881 sp_axes_clause->accept (*this); |
|
882 |
2123
|
883 subplot_using *sp_using_clause = cmd.using_clause (); |
|
884 |
|
885 if (sp_using_clause) |
|
886 sp_using_clause->accept (*this); |
|
887 |
|
888 tree_expression *sp_title_clause = cmd.title_clause (); |
|
889 |
|
890 if (sp_title_clause) |
|
891 sp_title_clause->accept (*this); |
|
892 |
|
893 subplot_style *sp_style_clause = cmd.style_clause (); |
|
894 |
|
895 if (sp_style_clause) |
|
896 sp_style_clause->accept (*this); |
|
897 } |
|
898 |
|
899 void |
3165
|
900 tree_print_code::visit_subplot_axes (subplot_axes& cmd) |
|
901 { |
|
902 os << " axes " << cmd.axes (); |
|
903 } |
|
904 |
|
905 void |
2123
|
906 tree_print_code::visit_subplot_list (subplot_list& lst) |
|
907 { |
4219
|
908 subplot_list::iterator p = lst.begin (); |
2123
|
909 |
4219
|
910 while (p != lst.end ()) |
2123
|
911 { |
4219
|
912 subplot *elt = *p++; |
2123
|
913 |
|
914 if (elt) |
|
915 { |
|
916 elt->accept (*this); |
|
917 |
4219
|
918 if (p != lst.end ()) |
2123
|
919 os << ","; |
|
920 } |
|
921 } |
|
922 } |
|
923 |
|
924 void |
|
925 tree_print_code::visit_subplot_style (subplot_style& cmd) |
|
926 { |
|
927 os << " with " << cmd.style (); |
|
928 |
|
929 tree_expression *sp_linetype = cmd.linetype (); |
|
930 |
|
931 if (sp_linetype) |
|
932 { |
|
933 os << " "; |
|
934 |
|
935 sp_linetype->accept (*this); |
|
936 } |
|
937 |
|
938 tree_expression *sp_pointtype = cmd.pointtype (); |
|
939 |
|
940 if (sp_pointtype) |
|
941 { |
|
942 os << " "; |
|
943 |
|
944 sp_pointtype->accept (*this); |
|
945 } |
|
946 } |
|
947 |
|
948 void |
|
949 tree_print_code::visit_subplot_using (subplot_using& cmd) |
|
950 { |
|
951 os << " using "; |
|
952 |
|
953 int qual_count = cmd.qualifier_count (); |
|
954 |
|
955 if (qual_count > 0) |
|
956 { |
|
957 tree_expression **x = cmd.qualifiers (); |
|
958 |
|
959 for (int i = 0; i < qual_count; i++) |
|
960 { |
|
961 if (i > 0) |
|
962 os << ":"; |
|
963 |
|
964 if (x[i]) |
|
965 x[i]->accept (*this); |
|
966 } |
|
967 } |
|
968 else |
|
969 { |
|
970 tree_expression *scanf_fmt = cmd.scanf_format (); |
|
971 |
|
972 if (scanf_fmt) |
|
973 scanf_fmt->accept (*this); |
|
974 } |
|
975 } |
|
976 |
|
977 void |
2846
|
978 tree_print_code::visit_switch_case (tree_switch_case& cs) |
|
979 { |
3665
|
980 print_comment_list (cs.leading_comment ()); |
|
981 |
2846
|
982 indent (); |
|
983 |
|
984 if (cs.is_default_case ()) |
|
985 os << "otherwise"; |
|
986 else |
|
987 os << "case "; |
|
988 |
|
989 tree_expression *label = cs.case_label (); |
|
990 |
|
991 if (label) |
|
992 label->accept (*this); |
|
993 |
|
994 newline (); |
|
995 |
|
996 tree_statement_list *list = cs.commands (); |
|
997 |
|
998 if (list) |
|
999 { |
3665
|
1000 increment_indent_level (); |
|
1001 |
2846
|
1002 list->accept (*this); |
|
1003 |
3665
|
1004 newline (); |
|
1005 |
2846
|
1006 decrement_indent_level (); |
|
1007 } |
|
1008 } |
|
1009 |
|
1010 void |
|
1011 tree_print_code::visit_switch_case_list (tree_switch_case_list& lst) |
|
1012 { |
4219
|
1013 tree_switch_case_list::iterator p = lst.begin (); |
2846
|
1014 |
4219
|
1015 while (p != lst.end ()) |
2846
|
1016 { |
4219
|
1017 tree_switch_case *elt = *p++; |
2846
|
1018 |
|
1019 if (elt) |
|
1020 elt->accept (*this); |
|
1021 } |
|
1022 } |
|
1023 |
|
1024 void |
|
1025 tree_print_code::visit_switch_command (tree_switch_command& cmd) |
|
1026 { |
3665
|
1027 print_comment_list (cmd.leading_comment ()); |
|
1028 |
2846
|
1029 indent (); |
|
1030 |
|
1031 os << "switch "; |
|
1032 |
|
1033 tree_expression *expr = cmd.switch_value (); |
|
1034 |
|
1035 if (expr) |
|
1036 expr->accept (*this); |
|
1037 |
|
1038 newline (); |
|
1039 |
|
1040 tree_switch_case_list *list = cmd.case_list (); |
|
1041 |
|
1042 if (list) |
3665
|
1043 { |
|
1044 increment_indent_level (); |
|
1045 |
|
1046 list->accept (*this); |
|
1047 |
|
1048 decrement_indent_level (); |
|
1049 } |
|
1050 |
|
1051 print_indented_comment (cmd.leading_comment ()); |
2846
|
1052 |
|
1053 indent (); |
|
1054 |
|
1055 os << "endswitch"; |
|
1056 } |
|
1057 |
|
1058 void |
2123
|
1059 tree_print_code::visit_try_catch_command (tree_try_catch_command& cmd) |
|
1060 { |
3665
|
1061 print_comment_list (cmd.leading_comment ()); |
|
1062 |
2123
|
1063 indent (); |
|
1064 |
3665
|
1065 os << "try"; |
2123
|
1066 |
|
1067 newline (); |
|
1068 |
|
1069 tree_statement_list *try_code = cmd.body (); |
|
1070 |
|
1071 if (try_code) |
|
1072 { |
|
1073 increment_indent_level (); |
3665
|
1074 |
2123
|
1075 try_code->accept (*this); |
3665
|
1076 |
2123
|
1077 decrement_indent_level (); |
|
1078 } |
|
1079 |
3665
|
1080 print_indented_comment (cmd.middle_comment ()); |
|
1081 |
2123
|
1082 indent (); |
|
1083 |
|
1084 os << "catch"; |
|
1085 |
|
1086 newline (); |
|
1087 |
|
1088 tree_statement_list *catch_code = cmd.cleanup (); |
|
1089 |
|
1090 if (catch_code) |
|
1091 { |
|
1092 increment_indent_level (); |
3665
|
1093 |
2123
|
1094 catch_code->accept (*this); |
3665
|
1095 |
2123
|
1096 decrement_indent_level (); |
|
1097 } |
|
1098 |
3665
|
1099 print_indented_comment (cmd.trailing_comment ()); |
|
1100 |
2123
|
1101 indent (); |
|
1102 |
|
1103 os << "end_try_catch"; |
|
1104 } |
|
1105 |
|
1106 void |
|
1107 tree_print_code::visit_unwind_protect_command |
|
1108 (tree_unwind_protect_command& cmd) |
|
1109 { |
3665
|
1110 print_comment_list (cmd.leading_comment ()); |
|
1111 |
2123
|
1112 indent (); |
|
1113 |
|
1114 os << "unwind_protect"; |
|
1115 |
|
1116 newline (); |
|
1117 |
|
1118 tree_statement_list *unwind_protect_code = cmd.body (); |
|
1119 |
|
1120 if (unwind_protect_code) |
|
1121 { |
|
1122 increment_indent_level (); |
3665
|
1123 |
2123
|
1124 unwind_protect_code->accept (*this); |
3665
|
1125 |
2123
|
1126 decrement_indent_level (); |
|
1127 } |
|
1128 |
3665
|
1129 print_indented_comment (cmd.middle_comment ()); |
|
1130 |
2123
|
1131 indent (); |
|
1132 |
3478
|
1133 os << "unwind_protect_cleanup"; |
2123
|
1134 |
|
1135 newline (); |
|
1136 |
|
1137 tree_statement_list *cleanup_code = cmd.cleanup (); |
|
1138 |
|
1139 if (cleanup_code) |
|
1140 { |
|
1141 increment_indent_level (); |
3665
|
1142 |
2123
|
1143 cleanup_code->accept (*this); |
3665
|
1144 |
2123
|
1145 decrement_indent_level (); |
|
1146 } |
|
1147 |
3665
|
1148 print_indented_comment (cmd.trailing_comment ()); |
|
1149 |
2123
|
1150 indent (); |
|
1151 |
|
1152 os << "end_unwind_protect"; |
|
1153 } |
|
1154 |
|
1155 void |
|
1156 tree_print_code::visit_while_command (tree_while_command& cmd) |
|
1157 { |
3665
|
1158 print_comment_list (cmd.leading_comment ()); |
|
1159 |
2123
|
1160 indent (); |
|
1161 |
|
1162 os << "while "; |
|
1163 |
|
1164 tree_expression *expr = cmd.condition (); |
|
1165 |
|
1166 if (expr) |
|
1167 expr->accept (*this); |
|
1168 |
|
1169 newline (); |
|
1170 |
|
1171 tree_statement_list *list = cmd.body (); |
|
1172 |
|
1173 if (list) |
|
1174 { |
|
1175 increment_indent_level (); |
3665
|
1176 |
2123
|
1177 list->accept (*this); |
3665
|
1178 |
2123
|
1179 decrement_indent_level (); |
|
1180 } |
|
1181 |
3665
|
1182 print_indented_comment (cmd.trailing_comment ()); |
|
1183 |
2123
|
1184 indent (); |
|
1185 |
|
1186 os << "endwhile"; |
|
1187 } |
|
1188 |
3484
|
1189 void |
|
1190 tree_print_code::visit_do_until_command (tree_do_until_command& cmd) |
|
1191 { |
3665
|
1192 print_comment_list (cmd.leading_comment ()); |
|
1193 |
3484
|
1194 indent (); |
|
1195 |
|
1196 os << "do"; |
|
1197 |
|
1198 newline (); |
|
1199 |
|
1200 tree_statement_list *list = cmd.body (); |
|
1201 |
|
1202 if (list) |
|
1203 { |
|
1204 increment_indent_level (); |
3665
|
1205 |
3484
|
1206 list->accept (*this); |
3665
|
1207 |
3484
|
1208 decrement_indent_level (); |
|
1209 } |
|
1210 |
3665
|
1211 print_indented_comment (cmd.trailing_comment ()); |
|
1212 |
3484
|
1213 indent (); |
|
1214 |
|
1215 os << "until"; |
|
1216 |
|
1217 tree_expression *expr = cmd.condition (); |
|
1218 |
|
1219 if (expr) |
|
1220 expr->accept (*this); |
|
1221 |
|
1222 newline (); |
|
1223 } |
|
1224 |
2123
|
1225 // Each print_code() function should call this before printing |
|
1226 // anything. |
|
1227 // |
|
1228 // This doesn't need to be fast, but isn't there a better way? |
|
1229 |
|
1230 void |
|
1231 tree_print_code::indent (void) |
|
1232 { |
|
1233 assert (curr_print_indent_level >= 0); |
|
1234 |
|
1235 if (beginning_of_line) |
|
1236 { |
2900
|
1237 os << prefix; |
|
1238 |
|
1239 for (int i = 0; i < curr_print_indent_level; i++) |
|
1240 os << " "; |
|
1241 |
2123
|
1242 beginning_of_line = false; |
|
1243 } |
|
1244 } |
|
1245 |
|
1246 // All print_code() functions should use this to print new lines. |
|
1247 |
|
1248 void |
|
1249 tree_print_code::newline (void) |
|
1250 { |
|
1251 os << "\n"; |
|
1252 |
|
1253 beginning_of_line = true; |
|
1254 } |
|
1255 |
|
1256 // For ressetting print_code state. |
|
1257 |
|
1258 void |
|
1259 tree_print_code::reset (void) |
|
1260 { |
|
1261 beginning_of_line = true; |
|
1262 curr_print_indent_level = 0; |
|
1263 } |
|
1264 |
2961
|
1265 void |
|
1266 tree_print_code::print_parens (const tree_expression& expr, const char *txt) |
|
1267 { |
|
1268 int n = expr.paren_count (); |
|
1269 |
|
1270 for (int i = 0; i < n; i++) |
|
1271 os << txt; |
|
1272 } |
|
1273 |
3665
|
1274 void |
|
1275 tree_print_code::print_comment_elt (const octave_comment_elt& elt) |
|
1276 { |
|
1277 bool printed_something = false; |
|
1278 |
|
1279 bool prev_char_was_newline = false; |
|
1280 |
3769
|
1281 std::string comment = elt.text (); |
3665
|
1282 |
|
1283 size_t len = comment.length (); |
|
1284 |
|
1285 size_t i = 0; |
|
1286 |
|
1287 while (i < len && comment[i++] == '\n') |
|
1288 ; /* Skip leading new lines. */ |
|
1289 i--; |
|
1290 |
|
1291 while (i < len) |
|
1292 { |
|
1293 char c = comment[i++]; |
|
1294 |
|
1295 if (c == '\n') |
|
1296 { |
|
1297 if (prev_char_was_newline) |
|
1298 os << "##"; |
|
1299 |
|
1300 newline (); |
|
1301 |
|
1302 prev_char_was_newline = true; |
|
1303 } |
|
1304 else |
|
1305 { |
|
1306 if (beginning_of_line) |
|
1307 { |
|
1308 printed_something = true; |
|
1309 |
|
1310 indent (); |
|
1311 |
|
1312 os << "##"; |
|
1313 |
|
1314 if (! (isspace (c) || c == '!')) |
|
1315 os << " "; |
|
1316 } |
|
1317 |
|
1318 os << (char) c; |
|
1319 |
|
1320 prev_char_was_newline = false; |
|
1321 } |
|
1322 } |
|
1323 |
|
1324 if (printed_something && ! beginning_of_line) |
|
1325 newline (); |
|
1326 } |
|
1327 |
|
1328 void |
|
1329 tree_print_code::print_comment_list (octave_comment_list *comment_list) |
|
1330 { |
|
1331 if (comment_list) |
|
1332 { |
4219
|
1333 octave_comment_list::iterator p = comment_list->begin (); |
3665
|
1334 |
4219
|
1335 while (p != comment_list->end ()) |
3665
|
1336 { |
4219
|
1337 octave_comment_elt elt = *p++; |
3665
|
1338 |
|
1339 print_comment_elt (elt); |
|
1340 |
4219
|
1341 if (p != comment_list->end ()) |
3665
|
1342 newline (); |
|
1343 } |
|
1344 } |
|
1345 } |
|
1346 |
|
1347 void |
|
1348 tree_print_code::print_indented_comment (octave_comment_list *comment_list) |
|
1349 { |
|
1350 increment_indent_level (); |
|
1351 |
|
1352 print_comment_list (comment_list); |
|
1353 |
|
1354 decrement_indent_level (); |
|
1355 } |
|
1356 |
2123
|
1357 /* |
|
1358 ;;; Local Variables: *** |
|
1359 ;;; mode: C++ *** |
|
1360 ;;; End: *** |
|
1361 */ |