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 |
4342
|
645 tree_print_code::visit_fcn_handle (tree_fcn_handle& fh) |
|
646 { |
|
647 indent (); |
|
648 |
|
649 print_parens (fh, "("); |
|
650 |
|
651 fh.print_raw (os, true, print_original_text); |
|
652 |
|
653 print_parens (fh, ")"); |
|
654 } |
|
655 |
|
656 void |
2123
|
657 tree_print_code::visit_parameter_list (tree_parameter_list& lst) |
|
658 { |
4219
|
659 tree_parameter_list::iterator p = lst.begin (); |
2123
|
660 |
4219
|
661 while (p != lst.end ()) |
2123
|
662 { |
4219
|
663 tree_identifier *elt = *p++; |
2123
|
664 |
|
665 if (elt) |
|
666 { |
|
667 elt->accept (*this); |
|
668 |
4219
|
669 if (p != lst.end ()) |
2123
|
670 os << ", "; |
|
671 } |
|
672 } |
|
673 } |
|
674 |
|
675 void |
|
676 tree_print_code::visit_plot_command (tree_plot_command& cmd) |
|
677 { |
|
678 indent (); |
|
679 |
|
680 int ndim = cmd.num_dimensions (); |
|
681 |
|
682 switch (ndim) |
|
683 { |
|
684 case 1: |
|
685 os << "replot"; |
|
686 break; |
|
687 |
|
688 case 2: |
|
689 os << "gplot"; |
|
690 break; |
|
691 |
|
692 case 3: |
|
693 os << "gsplot"; |
|
694 break; |
|
695 |
|
696 default: |
|
697 os << "<unkown plot command>"; |
|
698 break; |
|
699 } |
|
700 |
|
701 plot_limits *range = cmd.limits (); |
|
702 |
|
703 if (range) |
|
704 range->accept (*this); |
|
705 |
|
706 subplot_list *plot_list = cmd.subplots (); |
|
707 |
|
708 if (plot_list) |
|
709 plot_list->accept (*this); |
|
710 } |
|
711 |
|
712 void |
|
713 tree_print_code::visit_plot_limits (plot_limits& cmd) |
|
714 { |
|
715 plot_range *x_range = cmd.x_limits (); |
|
716 |
|
717 if (x_range) |
|
718 x_range->accept (*this); |
|
719 |
|
720 plot_range *y_range = cmd.y_limits (); |
|
721 |
|
722 if (y_range) |
|
723 y_range->accept (*this); |
|
724 |
|
725 plot_range *z_range = cmd.z_limits (); |
|
726 |
|
727 if (z_range) |
|
728 z_range->accept (*this); |
|
729 } |
|
730 |
|
731 void |
|
732 tree_print_code::visit_plot_range (plot_range& cmd) |
|
733 { |
|
734 os << " ["; |
|
735 |
|
736 tree_expression *lower = cmd.lower_bound (); |
|
737 |
|
738 if (lower) |
|
739 lower->accept (*this); |
|
740 |
|
741 os << ":"; |
|
742 |
|
743 tree_expression *upper = cmd.upper_bound (); |
|
744 |
|
745 if (upper) |
|
746 upper->accept (*this); |
|
747 |
|
748 os << "]"; |
|
749 } |
|
750 |
|
751 void |
|
752 tree_print_code::visit_postfix_expression (tree_postfix_expression& expr) |
|
753 { |
|
754 indent (); |
|
755 |
2961
|
756 print_parens (expr, "("); |
2123
|
757 |
2960
|
758 tree_expression *e = expr.operand (); |
2123
|
759 |
2960
|
760 if (e) |
|
761 e->accept (*this); |
2123
|
762 |
|
763 os << expr.oper (); |
|
764 |
2961
|
765 print_parens (expr, ")"); |
2123
|
766 } |
|
767 |
|
768 void |
|
769 tree_print_code::visit_prefix_expression (tree_prefix_expression& expr) |
|
770 { |
|
771 indent (); |
|
772 |
2961
|
773 print_parens (expr, "("); |
2123
|
774 |
|
775 os << expr.oper (); |
|
776 |
2960
|
777 tree_expression *e = expr.operand (); |
2123
|
778 |
2960
|
779 if (e) |
|
780 e->accept (*this); |
2123
|
781 |
2961
|
782 print_parens (expr, ")"); |
2123
|
783 } |
|
784 |
|
785 void |
4207
|
786 tree_print_code::visit_return_command (tree_return_command&) |
2123
|
787 { |
|
788 indent (); |
|
789 |
|
790 os << "return"; |
|
791 } |
|
792 |
|
793 void |
|
794 tree_print_code::visit_return_list (tree_return_list& lst) |
|
795 { |
4219
|
796 tree_return_list::iterator p = lst.begin (); |
2123
|
797 |
4219
|
798 while (p != lst.end ()) |
2123
|
799 { |
4219
|
800 tree_index_expression *elt = *p++; |
2123
|
801 |
|
802 if (elt) |
|
803 { |
|
804 elt->accept (*this); |
|
805 |
4219
|
806 if (p != lst.end ()) |
2123
|
807 os << ", "; |
|
808 } |
|
809 } |
|
810 } |
|
811 |
|
812 void |
2969
|
813 tree_print_code::visit_simple_assignment (tree_simple_assignment& expr) |
2123
|
814 { |
|
815 indent (); |
|
816 |
2961
|
817 print_parens (expr, "("); |
2123
|
818 |
2969
|
819 tree_expression *lhs = expr.left_hand_side (); |
2123
|
820 |
2969
|
821 if (lhs) |
|
822 lhs->accept (*this); |
2123
|
823 |
2969
|
824 os << " " << expr.oper () << " "; |
2123
|
825 |
|
826 tree_expression *rhs = expr.right_hand_side (); |
|
827 |
|
828 if (rhs) |
|
829 rhs->accept (*this); |
|
830 |
2961
|
831 print_parens (expr, ")"); |
2123
|
832 } |
|
833 |
|
834 void |
|
835 tree_print_code::visit_statement (tree_statement& stmt) |
|
836 { |
3665
|
837 print_comment_list (stmt.comment_text ()); |
|
838 |
2123
|
839 tree_command *cmd = stmt.command (); |
|
840 |
|
841 if (cmd) |
|
842 { |
|
843 cmd->accept (*this); |
|
844 |
|
845 if (! stmt.print_result ()) |
|
846 os << ";"; |
|
847 |
|
848 newline (); |
|
849 } |
|
850 else |
|
851 { |
|
852 tree_expression *expr = stmt.expression (); |
|
853 |
|
854 if (expr) |
|
855 { |
|
856 expr->accept (*this); |
|
857 |
|
858 if (! stmt.print_result ()) |
|
859 os << ";"; |
|
860 |
|
861 newline (); |
|
862 } |
|
863 } |
|
864 } |
|
865 |
|
866 void |
|
867 tree_print_code::visit_statement_list (tree_statement_list& lst) |
|
868 { |
4219
|
869 for (tree_statement_list::iterator p = lst.begin (); p != lst.end (); p++) |
2123
|
870 { |
4219
|
871 tree_statement *elt = *p; |
2123
|
872 |
|
873 if (elt) |
|
874 elt->accept (*this); |
|
875 } |
|
876 } |
|
877 |
|
878 void |
|
879 tree_print_code::visit_subplot (subplot& cmd) |
|
880 { |
|
881 tree_expression *sp_plot_data = cmd.plot_data (); |
|
882 |
|
883 if (sp_plot_data) |
|
884 { |
|
885 os << " "; |
|
886 |
|
887 sp_plot_data->accept (*this); |
|
888 } |
|
889 |
3165
|
890 subplot_axes *sp_axes_clause = cmd.axes_clause (); |
|
891 |
|
892 if (sp_axes_clause) |
|
893 sp_axes_clause->accept (*this); |
|
894 |
2123
|
895 subplot_using *sp_using_clause = cmd.using_clause (); |
|
896 |
|
897 if (sp_using_clause) |
|
898 sp_using_clause->accept (*this); |
|
899 |
|
900 tree_expression *sp_title_clause = cmd.title_clause (); |
|
901 |
|
902 if (sp_title_clause) |
|
903 sp_title_clause->accept (*this); |
|
904 |
|
905 subplot_style *sp_style_clause = cmd.style_clause (); |
|
906 |
|
907 if (sp_style_clause) |
|
908 sp_style_clause->accept (*this); |
|
909 } |
|
910 |
|
911 void |
3165
|
912 tree_print_code::visit_subplot_axes (subplot_axes& cmd) |
|
913 { |
|
914 os << " axes " << cmd.axes (); |
|
915 } |
|
916 |
|
917 void |
2123
|
918 tree_print_code::visit_subplot_list (subplot_list& lst) |
|
919 { |
4219
|
920 subplot_list::iterator p = lst.begin (); |
2123
|
921 |
4219
|
922 while (p != lst.end ()) |
2123
|
923 { |
4219
|
924 subplot *elt = *p++; |
2123
|
925 |
|
926 if (elt) |
|
927 { |
|
928 elt->accept (*this); |
|
929 |
4219
|
930 if (p != lst.end ()) |
2123
|
931 os << ","; |
|
932 } |
|
933 } |
|
934 } |
|
935 |
|
936 void |
|
937 tree_print_code::visit_subplot_style (subplot_style& cmd) |
|
938 { |
|
939 os << " with " << cmd.style (); |
|
940 |
|
941 tree_expression *sp_linetype = cmd.linetype (); |
|
942 |
|
943 if (sp_linetype) |
|
944 { |
|
945 os << " "; |
|
946 |
|
947 sp_linetype->accept (*this); |
|
948 } |
|
949 |
|
950 tree_expression *sp_pointtype = cmd.pointtype (); |
|
951 |
|
952 if (sp_pointtype) |
|
953 { |
|
954 os << " "; |
|
955 |
|
956 sp_pointtype->accept (*this); |
|
957 } |
|
958 } |
|
959 |
|
960 void |
|
961 tree_print_code::visit_subplot_using (subplot_using& cmd) |
|
962 { |
|
963 os << " using "; |
|
964 |
|
965 int qual_count = cmd.qualifier_count (); |
|
966 |
|
967 if (qual_count > 0) |
|
968 { |
|
969 tree_expression **x = cmd.qualifiers (); |
|
970 |
|
971 for (int i = 0; i < qual_count; i++) |
|
972 { |
|
973 if (i > 0) |
|
974 os << ":"; |
|
975 |
|
976 if (x[i]) |
|
977 x[i]->accept (*this); |
|
978 } |
|
979 } |
|
980 else |
|
981 { |
|
982 tree_expression *scanf_fmt = cmd.scanf_format (); |
|
983 |
|
984 if (scanf_fmt) |
|
985 scanf_fmt->accept (*this); |
|
986 } |
|
987 } |
|
988 |
|
989 void |
2846
|
990 tree_print_code::visit_switch_case (tree_switch_case& cs) |
|
991 { |
3665
|
992 print_comment_list (cs.leading_comment ()); |
|
993 |
2846
|
994 indent (); |
|
995 |
|
996 if (cs.is_default_case ()) |
|
997 os << "otherwise"; |
|
998 else |
|
999 os << "case "; |
|
1000 |
|
1001 tree_expression *label = cs.case_label (); |
|
1002 |
|
1003 if (label) |
|
1004 label->accept (*this); |
|
1005 |
|
1006 newline (); |
|
1007 |
|
1008 tree_statement_list *list = cs.commands (); |
|
1009 |
|
1010 if (list) |
|
1011 { |
3665
|
1012 increment_indent_level (); |
|
1013 |
2846
|
1014 list->accept (*this); |
|
1015 |
3665
|
1016 newline (); |
|
1017 |
2846
|
1018 decrement_indent_level (); |
|
1019 } |
|
1020 } |
|
1021 |
|
1022 void |
|
1023 tree_print_code::visit_switch_case_list (tree_switch_case_list& lst) |
|
1024 { |
4219
|
1025 tree_switch_case_list::iterator p = lst.begin (); |
2846
|
1026 |
4219
|
1027 while (p != lst.end ()) |
2846
|
1028 { |
4219
|
1029 tree_switch_case *elt = *p++; |
2846
|
1030 |
|
1031 if (elt) |
|
1032 elt->accept (*this); |
|
1033 } |
|
1034 } |
|
1035 |
|
1036 void |
|
1037 tree_print_code::visit_switch_command (tree_switch_command& cmd) |
|
1038 { |
3665
|
1039 print_comment_list (cmd.leading_comment ()); |
|
1040 |
2846
|
1041 indent (); |
|
1042 |
|
1043 os << "switch "; |
|
1044 |
|
1045 tree_expression *expr = cmd.switch_value (); |
|
1046 |
|
1047 if (expr) |
|
1048 expr->accept (*this); |
|
1049 |
|
1050 newline (); |
|
1051 |
|
1052 tree_switch_case_list *list = cmd.case_list (); |
|
1053 |
|
1054 if (list) |
3665
|
1055 { |
|
1056 increment_indent_level (); |
|
1057 |
|
1058 list->accept (*this); |
|
1059 |
|
1060 decrement_indent_level (); |
|
1061 } |
|
1062 |
|
1063 print_indented_comment (cmd.leading_comment ()); |
2846
|
1064 |
|
1065 indent (); |
|
1066 |
|
1067 os << "endswitch"; |
|
1068 } |
|
1069 |
|
1070 void |
2123
|
1071 tree_print_code::visit_try_catch_command (tree_try_catch_command& cmd) |
|
1072 { |
3665
|
1073 print_comment_list (cmd.leading_comment ()); |
|
1074 |
2123
|
1075 indent (); |
|
1076 |
3665
|
1077 os << "try"; |
2123
|
1078 |
|
1079 newline (); |
|
1080 |
|
1081 tree_statement_list *try_code = cmd.body (); |
|
1082 |
|
1083 if (try_code) |
|
1084 { |
|
1085 increment_indent_level (); |
3665
|
1086 |
2123
|
1087 try_code->accept (*this); |
3665
|
1088 |
2123
|
1089 decrement_indent_level (); |
|
1090 } |
|
1091 |
3665
|
1092 print_indented_comment (cmd.middle_comment ()); |
|
1093 |
2123
|
1094 indent (); |
|
1095 |
|
1096 os << "catch"; |
|
1097 |
|
1098 newline (); |
|
1099 |
|
1100 tree_statement_list *catch_code = cmd.cleanup (); |
|
1101 |
|
1102 if (catch_code) |
|
1103 { |
|
1104 increment_indent_level (); |
3665
|
1105 |
2123
|
1106 catch_code->accept (*this); |
3665
|
1107 |
2123
|
1108 decrement_indent_level (); |
|
1109 } |
|
1110 |
3665
|
1111 print_indented_comment (cmd.trailing_comment ()); |
|
1112 |
2123
|
1113 indent (); |
|
1114 |
|
1115 os << "end_try_catch"; |
|
1116 } |
|
1117 |
|
1118 void |
|
1119 tree_print_code::visit_unwind_protect_command |
|
1120 (tree_unwind_protect_command& cmd) |
|
1121 { |
3665
|
1122 print_comment_list (cmd.leading_comment ()); |
|
1123 |
2123
|
1124 indent (); |
|
1125 |
|
1126 os << "unwind_protect"; |
|
1127 |
|
1128 newline (); |
|
1129 |
|
1130 tree_statement_list *unwind_protect_code = cmd.body (); |
|
1131 |
|
1132 if (unwind_protect_code) |
|
1133 { |
|
1134 increment_indent_level (); |
3665
|
1135 |
2123
|
1136 unwind_protect_code->accept (*this); |
3665
|
1137 |
2123
|
1138 decrement_indent_level (); |
|
1139 } |
|
1140 |
3665
|
1141 print_indented_comment (cmd.middle_comment ()); |
|
1142 |
2123
|
1143 indent (); |
|
1144 |
3478
|
1145 os << "unwind_protect_cleanup"; |
2123
|
1146 |
|
1147 newline (); |
|
1148 |
|
1149 tree_statement_list *cleanup_code = cmd.cleanup (); |
|
1150 |
|
1151 if (cleanup_code) |
|
1152 { |
|
1153 increment_indent_level (); |
3665
|
1154 |
2123
|
1155 cleanup_code->accept (*this); |
3665
|
1156 |
2123
|
1157 decrement_indent_level (); |
|
1158 } |
|
1159 |
3665
|
1160 print_indented_comment (cmd.trailing_comment ()); |
|
1161 |
2123
|
1162 indent (); |
|
1163 |
|
1164 os << "end_unwind_protect"; |
|
1165 } |
|
1166 |
|
1167 void |
|
1168 tree_print_code::visit_while_command (tree_while_command& cmd) |
|
1169 { |
3665
|
1170 print_comment_list (cmd.leading_comment ()); |
|
1171 |
2123
|
1172 indent (); |
|
1173 |
|
1174 os << "while "; |
|
1175 |
|
1176 tree_expression *expr = cmd.condition (); |
|
1177 |
|
1178 if (expr) |
|
1179 expr->accept (*this); |
|
1180 |
|
1181 newline (); |
|
1182 |
|
1183 tree_statement_list *list = cmd.body (); |
|
1184 |
|
1185 if (list) |
|
1186 { |
|
1187 increment_indent_level (); |
3665
|
1188 |
2123
|
1189 list->accept (*this); |
3665
|
1190 |
2123
|
1191 decrement_indent_level (); |
|
1192 } |
|
1193 |
3665
|
1194 print_indented_comment (cmd.trailing_comment ()); |
|
1195 |
2123
|
1196 indent (); |
|
1197 |
|
1198 os << "endwhile"; |
|
1199 } |
|
1200 |
3484
|
1201 void |
|
1202 tree_print_code::visit_do_until_command (tree_do_until_command& cmd) |
|
1203 { |
3665
|
1204 print_comment_list (cmd.leading_comment ()); |
|
1205 |
3484
|
1206 indent (); |
|
1207 |
|
1208 os << "do"; |
|
1209 |
|
1210 newline (); |
|
1211 |
|
1212 tree_statement_list *list = cmd.body (); |
|
1213 |
|
1214 if (list) |
|
1215 { |
|
1216 increment_indent_level (); |
3665
|
1217 |
3484
|
1218 list->accept (*this); |
3665
|
1219 |
3484
|
1220 decrement_indent_level (); |
|
1221 } |
|
1222 |
3665
|
1223 print_indented_comment (cmd.trailing_comment ()); |
|
1224 |
3484
|
1225 indent (); |
|
1226 |
|
1227 os << "until"; |
|
1228 |
|
1229 tree_expression *expr = cmd.condition (); |
|
1230 |
|
1231 if (expr) |
|
1232 expr->accept (*this); |
|
1233 |
|
1234 newline (); |
|
1235 } |
|
1236 |
2123
|
1237 // Each print_code() function should call this before printing |
|
1238 // anything. |
|
1239 // |
|
1240 // This doesn't need to be fast, but isn't there a better way? |
|
1241 |
|
1242 void |
|
1243 tree_print_code::indent (void) |
|
1244 { |
|
1245 assert (curr_print_indent_level >= 0); |
|
1246 |
|
1247 if (beginning_of_line) |
|
1248 { |
2900
|
1249 os << prefix; |
|
1250 |
|
1251 for (int i = 0; i < curr_print_indent_level; i++) |
|
1252 os << " "; |
|
1253 |
2123
|
1254 beginning_of_line = false; |
|
1255 } |
|
1256 } |
|
1257 |
|
1258 // All print_code() functions should use this to print new lines. |
|
1259 |
|
1260 void |
|
1261 tree_print_code::newline (void) |
|
1262 { |
|
1263 os << "\n"; |
|
1264 |
|
1265 beginning_of_line = true; |
|
1266 } |
|
1267 |
|
1268 // For ressetting print_code state. |
|
1269 |
|
1270 void |
|
1271 tree_print_code::reset (void) |
|
1272 { |
|
1273 beginning_of_line = true; |
|
1274 curr_print_indent_level = 0; |
|
1275 } |
|
1276 |
2961
|
1277 void |
|
1278 tree_print_code::print_parens (const tree_expression& expr, const char *txt) |
|
1279 { |
|
1280 int n = expr.paren_count (); |
|
1281 |
|
1282 for (int i = 0; i < n; i++) |
|
1283 os << txt; |
|
1284 } |
|
1285 |
3665
|
1286 void |
|
1287 tree_print_code::print_comment_elt (const octave_comment_elt& elt) |
|
1288 { |
|
1289 bool printed_something = false; |
|
1290 |
|
1291 bool prev_char_was_newline = false; |
|
1292 |
3769
|
1293 std::string comment = elt.text (); |
3665
|
1294 |
|
1295 size_t len = comment.length (); |
|
1296 |
|
1297 size_t i = 0; |
|
1298 |
|
1299 while (i < len && comment[i++] == '\n') |
|
1300 ; /* Skip leading new lines. */ |
|
1301 i--; |
|
1302 |
|
1303 while (i < len) |
|
1304 { |
|
1305 char c = comment[i++]; |
|
1306 |
|
1307 if (c == '\n') |
|
1308 { |
|
1309 if (prev_char_was_newline) |
|
1310 os << "##"; |
|
1311 |
|
1312 newline (); |
|
1313 |
|
1314 prev_char_was_newline = true; |
|
1315 } |
|
1316 else |
|
1317 { |
|
1318 if (beginning_of_line) |
|
1319 { |
|
1320 printed_something = true; |
|
1321 |
|
1322 indent (); |
|
1323 |
|
1324 os << "##"; |
|
1325 |
|
1326 if (! (isspace (c) || c == '!')) |
|
1327 os << " "; |
|
1328 } |
|
1329 |
|
1330 os << (char) c; |
|
1331 |
|
1332 prev_char_was_newline = false; |
|
1333 } |
|
1334 } |
|
1335 |
|
1336 if (printed_something && ! beginning_of_line) |
|
1337 newline (); |
|
1338 } |
|
1339 |
|
1340 void |
|
1341 tree_print_code::print_comment_list (octave_comment_list *comment_list) |
|
1342 { |
|
1343 if (comment_list) |
|
1344 { |
4219
|
1345 octave_comment_list::iterator p = comment_list->begin (); |
3665
|
1346 |
4219
|
1347 while (p != comment_list->end ()) |
3665
|
1348 { |
4219
|
1349 octave_comment_elt elt = *p++; |
3665
|
1350 |
|
1351 print_comment_elt (elt); |
|
1352 |
4219
|
1353 if (p != comment_list->end ()) |
3665
|
1354 newline (); |
|
1355 } |
|
1356 } |
|
1357 } |
|
1358 |
|
1359 void |
|
1360 tree_print_code::print_indented_comment (octave_comment_list *comment_list) |
|
1361 { |
|
1362 increment_indent_level (); |
|
1363 |
|
1364 print_comment_list (comment_list); |
|
1365 |
|
1366 decrement_indent_level (); |
|
1367 } |
|
1368 |
2123
|
1369 /* |
|
1370 ;;; Local Variables: *** |
|
1371 ;;; mode: C++ *** |
|
1372 ;;; End: *** |
|
1373 */ |