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 ["; |
4676
|
230 nesting.push ('['); |
2969
|
231 |
|
232 tree_argument_list *lhs = cmd.left_hand_side (); |
|
233 |
|
234 if (lhs) |
|
235 lhs->accept (*this); |
|
236 |
4676
|
237 nesting.pop (); |
2969
|
238 os << "] = "; |
|
239 |
|
240 tree_expression *expr = cmd.control_expr (); |
|
241 |
|
242 if (expr) |
|
243 expr->accept (*this); |
|
244 |
|
245 newline (); |
|
246 |
|
247 tree_statement_list *list = cmd.body (); |
|
248 |
|
249 if (list) |
|
250 { |
|
251 increment_indent_level (); |
3665
|
252 |
2969
|
253 list->accept (*this); |
3665
|
254 |
2969
|
255 decrement_indent_level (); |
|
256 } |
|
257 |
3665
|
258 print_indented_comment (cmd.trailing_comment ()); |
|
259 |
2969
|
260 indent (); |
|
261 |
|
262 os << "endfor"; |
|
263 } |
|
264 |
|
265 void |
2891
|
266 tree_print_code::visit_octave_user_function (octave_user_function& fcn) |
2123
|
267 { |
|
268 reset (); |
|
269 |
2891
|
270 visit_octave_user_function_header (fcn); |
2123
|
271 |
|
272 tree_statement_list *cmd_list = fcn.body (); |
|
273 |
|
274 if (cmd_list) |
|
275 { |
|
276 increment_indent_level (); |
3665
|
277 |
2123
|
278 cmd_list->accept (*this); |
3665
|
279 |
2123
|
280 decrement_indent_level (); |
|
281 } |
|
282 |
2891
|
283 visit_octave_user_function_trailer (fcn); |
2123
|
284 } |
|
285 |
|
286 void |
2891
|
287 tree_print_code::visit_octave_user_function_header (octave_user_function& fcn) |
2123
|
288 { |
3665
|
289 octave_comment_list *leading_comment = fcn.leading_comment (); |
|
290 |
|
291 if (leading_comment) |
|
292 { |
|
293 print_comment_list (leading_comment); |
|
294 newline (); |
|
295 } |
|
296 |
2123
|
297 indent (); |
|
298 |
|
299 os << "function "; |
|
300 |
|
301 tree_parameter_list *ret_list = fcn.return_list (); |
|
302 |
|
303 if (ret_list) |
|
304 { |
|
305 bool takes_var_return = fcn.takes_var_return (); |
|
306 |
|
307 int len = ret_list->length (); |
|
308 |
|
309 if (len > 1 || takes_var_return) |
4676
|
310 { |
|
311 os << "["; |
|
312 nesting.push ('['); |
|
313 } |
2123
|
314 |
|
315 ret_list->accept (*this); |
|
316 |
|
317 if (takes_var_return) |
|
318 { |
|
319 if (len > 0) |
|
320 os << ", "; |
|
321 |
|
322 os << "..."; |
|
323 } |
|
324 |
|
325 if (len > 1 || takes_var_return) |
4676
|
326 { |
|
327 nesting.pop (); |
|
328 os << "]"; |
|
329 } |
2123
|
330 |
|
331 os << " = "; |
|
332 } |
|
333 |
3523
|
334 std::string fcn_name = fcn.function_name (); |
2123
|
335 |
3523
|
336 os << (fcn_name.empty () ? std::string ("(empty)") : fcn_name) << " "; |
2123
|
337 |
|
338 tree_parameter_list *param_list = fcn.parameter_list (); |
|
339 |
|
340 if (param_list) |
|
341 { |
|
342 bool takes_varargs = fcn.takes_varargs (); |
|
343 |
|
344 int len = param_list->length (); |
|
345 |
|
346 if (len > 0 || takes_varargs) |
4676
|
347 { |
|
348 os << "("; |
|
349 nesting.push ('('); |
|
350 } |
2123
|
351 |
|
352 param_list->accept (*this); |
|
353 |
|
354 if (takes_varargs) |
|
355 { |
|
356 if (len > 0) |
|
357 os << ", "; |
|
358 |
|
359 os << "..."; |
|
360 } |
|
361 |
|
362 if (len > 0 || takes_varargs) |
|
363 { |
4676
|
364 nesting.pop (); |
2123
|
365 os << ")"; |
|
366 newline (); |
|
367 } |
|
368 } |
|
369 else |
|
370 { |
|
371 os << "()"; |
|
372 newline (); |
|
373 } |
|
374 } |
|
375 |
|
376 void |
3665
|
377 tree_print_code::visit_octave_user_function_trailer (octave_user_function& fcn) |
2123
|
378 { |
3665
|
379 print_indented_comment (fcn.trailing_comment ()); |
|
380 |
2123
|
381 indent (); |
|
382 |
|
383 os << "endfunction"; |
|
384 |
|
385 newline (); |
|
386 } |
|
387 |
|
388 void |
|
389 tree_print_code::visit_identifier (tree_identifier& id) |
|
390 { |
|
391 indent (); |
|
392 |
2961
|
393 print_parens (id, "("); |
2123
|
394 |
3523
|
395 std::string nm = id.name (); |
|
396 os << (nm.empty () ? std::string ("(empty)") : nm); |
2123
|
397 |
2961
|
398 print_parens (id, ")"); |
2123
|
399 } |
|
400 |
|
401 void |
|
402 tree_print_code::visit_if_clause (tree_if_clause& cmd) |
|
403 { |
|
404 tree_expression *expr = cmd.condition (); |
|
405 |
|
406 if (expr) |
|
407 expr->accept (*this); |
|
408 |
|
409 newline (); |
|
410 |
|
411 tree_statement_list *list = cmd.commands (); |
|
412 |
|
413 if (list) |
|
414 { |
3665
|
415 increment_indent_level (); |
|
416 |
2123
|
417 list->accept (*this); |
|
418 |
|
419 decrement_indent_level (); |
|
420 } |
|
421 } |
|
422 |
|
423 void |
|
424 tree_print_code::visit_if_command (tree_if_command& cmd) |
|
425 { |
3665
|
426 print_comment_list (cmd.leading_comment ()); |
|
427 |
2123
|
428 indent (); |
|
429 |
|
430 os << "if "; |
|
431 |
|
432 tree_if_command_list *list = cmd.cmd_list (); |
|
433 |
|
434 if (list) |
|
435 list->accept (*this); |
|
436 |
3665
|
437 print_indented_comment (cmd.trailing_comment ()); |
|
438 |
2123
|
439 indent (); |
|
440 |
|
441 os << "endif"; |
|
442 } |
|
443 |
|
444 void |
|
445 tree_print_code::visit_if_command_list (tree_if_command_list& lst) |
|
446 { |
4219
|
447 tree_if_command_list::iterator p = lst.begin (); |
2123
|
448 |
|
449 bool first_elt = true; |
|
450 |
4219
|
451 while (p != lst.end ()) |
2123
|
452 { |
4219
|
453 tree_if_clause *elt = *p++; |
2123
|
454 |
|
455 if (elt) |
|
456 { |
|
457 if (! first_elt) |
|
458 { |
3665
|
459 print_indented_comment (elt->leading_comment ()); |
|
460 |
2123
|
461 indent (); |
|
462 |
|
463 if (elt->is_else_clause ()) |
|
464 os << "else"; |
|
465 else |
|
466 os << "elseif "; |
|
467 } |
|
468 |
|
469 elt->accept (*this); |
|
470 } |
|
471 |
|
472 first_elt = false; |
|
473 } |
|
474 } |
|
475 |
|
476 void |
|
477 tree_print_code::visit_index_expression (tree_index_expression& expr) |
|
478 { |
|
479 indent (); |
|
480 |
2961
|
481 print_parens (expr, "("); |
2123
|
482 |
3665
|
483 bool expr_has_parens = false; |
|
484 |
3010
|
485 tree_expression *e = expr.expression (); |
2123
|
486 |
2969
|
487 if (e) |
3665
|
488 { |
|
489 e->accept (*this); |
|
490 |
|
491 expr_has_parens = e->is_postfix_indexed (); |
|
492 } |
2123
|
493 |
4219
|
494 std::list<tree_argument_list *> arg_lists = expr.arg_lists (); |
3933
|
495 std::string type_tags = expr.type_tags (); |
4219
|
496 std::list<string_vector> arg_names = expr.arg_names (); |
2123
|
497 |
3933
|
498 int n = type_tags.length (); |
|
499 |
4219
|
500 std::list<tree_argument_list *>::iterator p_arg_lists = arg_lists.begin (); |
|
501 std::list<string_vector>::iterator p_arg_names = arg_names.begin (); |
3933
|
502 |
|
503 for (int i = 0; i < n; i++) |
2123
|
504 { |
3933
|
505 switch (type_tags[i]) |
|
506 { |
|
507 case '(': |
|
508 { |
4676
|
509 char nc = nesting.top (); |
|
510 if ((nc == '[' || nc == '{') && expr.paren_count () == 0) |
|
511 os << "("; |
|
512 else |
|
513 os << " ("; |
|
514 nesting.push ('('); |
|
515 |
4219
|
516 tree_argument_list *l = *p_arg_lists; |
3933
|
517 if (l) |
|
518 l->accept (*this); |
4676
|
519 |
|
520 nesting.pop (); |
3933
|
521 os << ")"; |
|
522 } |
|
523 break; |
|
524 |
|
525 case '{': |
|
526 { |
4676
|
527 char nc = nesting.top (); |
|
528 if ((nc == '[' || nc == '{') && expr.paren_count () == 0) |
|
529 os << "{"; |
|
530 else |
|
531 os << " {"; |
|
532 // We only care about whitespace inside [] and {} when we |
|
533 // are defining matrix and cell objects, not when indexing. |
|
534 nesting.push ('('); |
|
535 |
4219
|
536 tree_argument_list *l = *p_arg_lists; |
3933
|
537 if (l) |
|
538 l->accept (*this); |
4676
|
539 |
|
540 nesting.pop (); |
3933
|
541 os << "}"; |
|
542 } |
|
543 break; |
|
544 |
|
545 case '.': |
|
546 { |
4219
|
547 string_vector nm = *p_arg_names; |
3933
|
548 assert (nm.length () == 1); |
|
549 os << "." << nm(0); |
|
550 } |
|
551 break; |
|
552 |
|
553 default: |
|
554 panic_impossible (); |
|
555 } |
|
556 |
4219
|
557 p_arg_lists++; |
|
558 p_arg_names++; |
3930
|
559 } |
2123
|
560 |
2961
|
561 print_parens (expr, ")"); |
2123
|
562 } |
|
563 |
|
564 void |
|
565 tree_print_code::visit_matrix (tree_matrix& lst) |
|
566 { |
|
567 indent (); |
|
568 |
2961
|
569 print_parens (lst, "("); |
2123
|
570 |
|
571 os << "["; |
4676
|
572 nesting.push ('['); |
2123
|
573 |
4219
|
574 tree_matrix::iterator p = lst.begin (); |
2123
|
575 |
4219
|
576 while (p != lst.end ()) |
2123
|
577 { |
4219
|
578 tree_argument_list *elt = *p++; |
2123
|
579 |
|
580 if (elt) |
|
581 { |
|
582 elt->accept (*this); |
|
583 |
4219
|
584 if (p != lst.end ()) |
2123
|
585 os << "; "; |
|
586 } |
|
587 } |
|
588 |
4676
|
589 nesting.pop (); |
2123
|
590 os << "]"; |
|
591 |
2961
|
592 print_parens (lst, ")"); |
2123
|
593 } |
|
594 |
|
595 void |
3351
|
596 tree_print_code::visit_cell (tree_cell& lst) |
|
597 { |
|
598 indent (); |
|
599 |
|
600 print_parens (lst, "("); |
|
601 |
|
602 os << "{"; |
4676
|
603 nesting.push ('{'); |
3351
|
604 |
4219
|
605 tree_cell::iterator p = lst.begin (); |
3351
|
606 |
4219
|
607 while (p != lst.end ()) |
3351
|
608 { |
4219
|
609 tree_argument_list *elt = *p++; |
3351
|
610 |
|
611 if (elt) |
|
612 { |
|
613 elt->accept (*this); |
|
614 |
4219
|
615 if (p != lst.end ()) |
3351
|
616 os << "; "; |
|
617 } |
|
618 } |
|
619 |
4676
|
620 nesting.pop (); |
3351
|
621 os << "}"; |
|
622 |
|
623 print_parens (lst, ")"); |
|
624 } |
|
625 |
|
626 void |
2969
|
627 tree_print_code::visit_multi_assignment (tree_multi_assignment& expr) |
2123
|
628 { |
|
629 indent (); |
|
630 |
2961
|
631 print_parens (expr, "("); |
2123
|
632 |
2969
|
633 tree_argument_list *lhs = expr.left_hand_side (); |
2123
|
634 |
|
635 if (lhs) |
|
636 { |
|
637 int len = lhs->length (); |
|
638 |
|
639 if (len > 1) |
4676
|
640 { |
|
641 os << "["; |
|
642 nesting.push ('['); |
|
643 } |
2123
|
644 |
|
645 lhs->accept (*this); |
|
646 |
|
647 if (len > 1) |
4676
|
648 { |
|
649 nesting.pop (); |
|
650 os << "]"; |
|
651 } |
2123
|
652 } |
|
653 |
3208
|
654 os << " " << expr.oper () << " "; |
2123
|
655 |
2969
|
656 tree_expression *rhs = expr.right_hand_side (); |
2123
|
657 |
|
658 if (rhs) |
|
659 rhs->accept (*this); |
|
660 |
2961
|
661 print_parens (expr, ")"); |
2123
|
662 } |
|
663 |
|
664 void |
2620
|
665 tree_print_code::visit_no_op_command (tree_no_op_command& cmd) |
|
666 { |
|
667 indent (); |
|
668 |
|
669 os << cmd.original_command (); |
|
670 } |
|
671 |
|
672 void |
2372
|
673 tree_print_code::visit_constant (tree_constant& val) |
2123
|
674 { |
|
675 indent (); |
|
676 |
2961
|
677 print_parens (val, "("); |
2123
|
678 |
2942
|
679 val.print_raw (os, true, print_original_text); |
2123
|
680 |
2961
|
681 print_parens (val, ")"); |
2123
|
682 } |
|
683 |
|
684 void |
4342
|
685 tree_print_code::visit_fcn_handle (tree_fcn_handle& fh) |
|
686 { |
|
687 indent (); |
|
688 |
|
689 print_parens (fh, "("); |
|
690 |
|
691 fh.print_raw (os, true, print_original_text); |
|
692 |
|
693 print_parens (fh, ")"); |
|
694 } |
|
695 |
|
696 void |
2123
|
697 tree_print_code::visit_parameter_list (tree_parameter_list& lst) |
|
698 { |
4219
|
699 tree_parameter_list::iterator p = lst.begin (); |
2123
|
700 |
4219
|
701 while (p != lst.end ()) |
2123
|
702 { |
4219
|
703 tree_identifier *elt = *p++; |
2123
|
704 |
|
705 if (elt) |
|
706 { |
|
707 elt->accept (*this); |
|
708 |
4219
|
709 if (p != lst.end ()) |
2123
|
710 os << ", "; |
|
711 } |
|
712 } |
|
713 } |
|
714 |
|
715 void |
|
716 tree_print_code::visit_plot_command (tree_plot_command& cmd) |
|
717 { |
|
718 indent (); |
|
719 |
|
720 int ndim = cmd.num_dimensions (); |
|
721 |
|
722 switch (ndim) |
|
723 { |
|
724 case 1: |
|
725 os << "replot"; |
|
726 break; |
|
727 |
|
728 case 2: |
|
729 os << "gplot"; |
|
730 break; |
|
731 |
|
732 case 3: |
|
733 os << "gsplot"; |
|
734 break; |
|
735 |
|
736 default: |
|
737 os << "<unkown plot command>"; |
|
738 break; |
|
739 } |
|
740 |
|
741 plot_limits *range = cmd.limits (); |
|
742 |
|
743 if (range) |
|
744 range->accept (*this); |
|
745 |
|
746 subplot_list *plot_list = cmd.subplots (); |
|
747 |
|
748 if (plot_list) |
|
749 plot_list->accept (*this); |
|
750 } |
|
751 |
|
752 void |
|
753 tree_print_code::visit_plot_limits (plot_limits& cmd) |
|
754 { |
|
755 plot_range *x_range = cmd.x_limits (); |
|
756 |
|
757 if (x_range) |
|
758 x_range->accept (*this); |
|
759 |
|
760 plot_range *y_range = cmd.y_limits (); |
|
761 |
|
762 if (y_range) |
|
763 y_range->accept (*this); |
|
764 |
|
765 plot_range *z_range = cmd.z_limits (); |
|
766 |
|
767 if (z_range) |
|
768 z_range->accept (*this); |
|
769 } |
|
770 |
|
771 void |
|
772 tree_print_code::visit_plot_range (plot_range& cmd) |
|
773 { |
|
774 os << " ["; |
|
775 |
|
776 tree_expression *lower = cmd.lower_bound (); |
|
777 |
|
778 if (lower) |
|
779 lower->accept (*this); |
|
780 |
|
781 os << ":"; |
|
782 |
|
783 tree_expression *upper = cmd.upper_bound (); |
|
784 |
|
785 if (upper) |
|
786 upper->accept (*this); |
|
787 |
|
788 os << "]"; |
|
789 } |
|
790 |
|
791 void |
|
792 tree_print_code::visit_postfix_expression (tree_postfix_expression& expr) |
|
793 { |
|
794 indent (); |
|
795 |
2961
|
796 print_parens (expr, "("); |
2123
|
797 |
2960
|
798 tree_expression *e = expr.operand (); |
2123
|
799 |
2960
|
800 if (e) |
|
801 e->accept (*this); |
2123
|
802 |
|
803 os << expr.oper (); |
|
804 |
2961
|
805 print_parens (expr, ")"); |
2123
|
806 } |
|
807 |
|
808 void |
|
809 tree_print_code::visit_prefix_expression (tree_prefix_expression& expr) |
|
810 { |
|
811 indent (); |
|
812 |
2961
|
813 print_parens (expr, "("); |
2123
|
814 |
|
815 os << expr.oper (); |
|
816 |
2960
|
817 tree_expression *e = expr.operand (); |
2123
|
818 |
2960
|
819 if (e) |
|
820 e->accept (*this); |
2123
|
821 |
2961
|
822 print_parens (expr, ")"); |
2123
|
823 } |
|
824 |
|
825 void |
4207
|
826 tree_print_code::visit_return_command (tree_return_command&) |
2123
|
827 { |
|
828 indent (); |
|
829 |
|
830 os << "return"; |
|
831 } |
|
832 |
|
833 void |
|
834 tree_print_code::visit_return_list (tree_return_list& lst) |
|
835 { |
4219
|
836 tree_return_list::iterator p = lst.begin (); |
2123
|
837 |
4219
|
838 while (p != lst.end ()) |
2123
|
839 { |
4219
|
840 tree_index_expression *elt = *p++; |
2123
|
841 |
|
842 if (elt) |
|
843 { |
|
844 elt->accept (*this); |
|
845 |
4219
|
846 if (p != lst.end ()) |
2123
|
847 os << ", "; |
|
848 } |
|
849 } |
|
850 } |
|
851 |
|
852 void |
2969
|
853 tree_print_code::visit_simple_assignment (tree_simple_assignment& expr) |
2123
|
854 { |
|
855 indent (); |
|
856 |
2961
|
857 print_parens (expr, "("); |
2123
|
858 |
2969
|
859 tree_expression *lhs = expr.left_hand_side (); |
2123
|
860 |
2969
|
861 if (lhs) |
|
862 lhs->accept (*this); |
2123
|
863 |
2969
|
864 os << " " << expr.oper () << " "; |
2123
|
865 |
|
866 tree_expression *rhs = expr.right_hand_side (); |
|
867 |
|
868 if (rhs) |
|
869 rhs->accept (*this); |
|
870 |
2961
|
871 print_parens (expr, ")"); |
2123
|
872 } |
|
873 |
|
874 void |
|
875 tree_print_code::visit_statement (tree_statement& stmt) |
|
876 { |
3665
|
877 print_comment_list (stmt.comment_text ()); |
|
878 |
2123
|
879 tree_command *cmd = stmt.command (); |
|
880 |
|
881 if (cmd) |
|
882 { |
|
883 cmd->accept (*this); |
|
884 |
|
885 if (! stmt.print_result ()) |
|
886 os << ";"; |
|
887 |
|
888 newline (); |
|
889 } |
|
890 else |
|
891 { |
|
892 tree_expression *expr = stmt.expression (); |
|
893 |
|
894 if (expr) |
|
895 { |
|
896 expr->accept (*this); |
|
897 |
|
898 if (! stmt.print_result ()) |
|
899 os << ";"; |
|
900 |
|
901 newline (); |
|
902 } |
|
903 } |
|
904 } |
|
905 |
|
906 void |
|
907 tree_print_code::visit_statement_list (tree_statement_list& lst) |
|
908 { |
4219
|
909 for (tree_statement_list::iterator p = lst.begin (); p != lst.end (); p++) |
2123
|
910 { |
4219
|
911 tree_statement *elt = *p; |
2123
|
912 |
|
913 if (elt) |
|
914 elt->accept (*this); |
|
915 } |
|
916 } |
|
917 |
|
918 void |
|
919 tree_print_code::visit_subplot (subplot& cmd) |
|
920 { |
|
921 tree_expression *sp_plot_data = cmd.plot_data (); |
|
922 |
|
923 if (sp_plot_data) |
|
924 { |
|
925 os << " "; |
|
926 |
|
927 sp_plot_data->accept (*this); |
|
928 } |
|
929 |
3165
|
930 subplot_axes *sp_axes_clause = cmd.axes_clause (); |
|
931 |
|
932 if (sp_axes_clause) |
|
933 sp_axes_clause->accept (*this); |
|
934 |
2123
|
935 subplot_using *sp_using_clause = cmd.using_clause (); |
|
936 |
|
937 if (sp_using_clause) |
|
938 sp_using_clause->accept (*this); |
|
939 |
|
940 tree_expression *sp_title_clause = cmd.title_clause (); |
|
941 |
|
942 if (sp_title_clause) |
|
943 sp_title_clause->accept (*this); |
|
944 |
|
945 subplot_style *sp_style_clause = cmd.style_clause (); |
|
946 |
|
947 if (sp_style_clause) |
|
948 sp_style_clause->accept (*this); |
|
949 } |
|
950 |
|
951 void |
3165
|
952 tree_print_code::visit_subplot_axes (subplot_axes& cmd) |
|
953 { |
|
954 os << " axes " << cmd.axes (); |
|
955 } |
|
956 |
|
957 void |
2123
|
958 tree_print_code::visit_subplot_list (subplot_list& lst) |
|
959 { |
4219
|
960 subplot_list::iterator p = lst.begin (); |
2123
|
961 |
4219
|
962 while (p != lst.end ()) |
2123
|
963 { |
4219
|
964 subplot *elt = *p++; |
2123
|
965 |
|
966 if (elt) |
|
967 { |
|
968 elt->accept (*this); |
|
969 |
4219
|
970 if (p != lst.end ()) |
2123
|
971 os << ","; |
|
972 } |
|
973 } |
|
974 } |
|
975 |
|
976 void |
|
977 tree_print_code::visit_subplot_style (subplot_style& cmd) |
|
978 { |
|
979 os << " with " << cmd.style (); |
|
980 |
|
981 tree_expression *sp_linetype = cmd.linetype (); |
|
982 |
|
983 if (sp_linetype) |
|
984 { |
|
985 os << " "; |
|
986 |
|
987 sp_linetype->accept (*this); |
|
988 } |
|
989 |
|
990 tree_expression *sp_pointtype = cmd.pointtype (); |
|
991 |
|
992 if (sp_pointtype) |
|
993 { |
|
994 os << " "; |
|
995 |
|
996 sp_pointtype->accept (*this); |
|
997 } |
|
998 } |
|
999 |
|
1000 void |
|
1001 tree_print_code::visit_subplot_using (subplot_using& cmd) |
|
1002 { |
|
1003 os << " using "; |
|
1004 |
|
1005 int qual_count = cmd.qualifier_count (); |
|
1006 |
|
1007 if (qual_count > 0) |
|
1008 { |
|
1009 tree_expression **x = cmd.qualifiers (); |
|
1010 |
|
1011 for (int i = 0; i < qual_count; i++) |
|
1012 { |
|
1013 if (i > 0) |
|
1014 os << ":"; |
|
1015 |
|
1016 if (x[i]) |
|
1017 x[i]->accept (*this); |
|
1018 } |
|
1019 } |
|
1020 else |
|
1021 { |
|
1022 tree_expression *scanf_fmt = cmd.scanf_format (); |
|
1023 |
|
1024 if (scanf_fmt) |
|
1025 scanf_fmt->accept (*this); |
|
1026 } |
|
1027 } |
|
1028 |
|
1029 void |
2846
|
1030 tree_print_code::visit_switch_case (tree_switch_case& cs) |
|
1031 { |
3665
|
1032 print_comment_list (cs.leading_comment ()); |
|
1033 |
2846
|
1034 indent (); |
|
1035 |
|
1036 if (cs.is_default_case ()) |
|
1037 os << "otherwise"; |
|
1038 else |
|
1039 os << "case "; |
|
1040 |
|
1041 tree_expression *label = cs.case_label (); |
|
1042 |
|
1043 if (label) |
|
1044 label->accept (*this); |
|
1045 |
|
1046 newline (); |
|
1047 |
|
1048 tree_statement_list *list = cs.commands (); |
|
1049 |
|
1050 if (list) |
|
1051 { |
3665
|
1052 increment_indent_level (); |
|
1053 |
2846
|
1054 list->accept (*this); |
|
1055 |
3665
|
1056 newline (); |
|
1057 |
2846
|
1058 decrement_indent_level (); |
|
1059 } |
|
1060 } |
|
1061 |
|
1062 void |
|
1063 tree_print_code::visit_switch_case_list (tree_switch_case_list& lst) |
|
1064 { |
4219
|
1065 tree_switch_case_list::iterator p = lst.begin (); |
2846
|
1066 |
4219
|
1067 while (p != lst.end ()) |
2846
|
1068 { |
4219
|
1069 tree_switch_case *elt = *p++; |
2846
|
1070 |
|
1071 if (elt) |
|
1072 elt->accept (*this); |
|
1073 } |
|
1074 } |
|
1075 |
|
1076 void |
|
1077 tree_print_code::visit_switch_command (tree_switch_command& cmd) |
|
1078 { |
3665
|
1079 print_comment_list (cmd.leading_comment ()); |
|
1080 |
2846
|
1081 indent (); |
|
1082 |
|
1083 os << "switch "; |
|
1084 |
|
1085 tree_expression *expr = cmd.switch_value (); |
|
1086 |
|
1087 if (expr) |
|
1088 expr->accept (*this); |
|
1089 |
|
1090 newline (); |
|
1091 |
|
1092 tree_switch_case_list *list = cmd.case_list (); |
|
1093 |
|
1094 if (list) |
3665
|
1095 { |
|
1096 increment_indent_level (); |
|
1097 |
|
1098 list->accept (*this); |
|
1099 |
|
1100 decrement_indent_level (); |
|
1101 } |
|
1102 |
|
1103 print_indented_comment (cmd.leading_comment ()); |
2846
|
1104 |
|
1105 indent (); |
|
1106 |
|
1107 os << "endswitch"; |
|
1108 } |
|
1109 |
|
1110 void |
2123
|
1111 tree_print_code::visit_try_catch_command (tree_try_catch_command& cmd) |
|
1112 { |
3665
|
1113 print_comment_list (cmd.leading_comment ()); |
|
1114 |
2123
|
1115 indent (); |
|
1116 |
3665
|
1117 os << "try"; |
2123
|
1118 |
|
1119 newline (); |
|
1120 |
|
1121 tree_statement_list *try_code = cmd.body (); |
|
1122 |
|
1123 if (try_code) |
|
1124 { |
|
1125 increment_indent_level (); |
3665
|
1126 |
2123
|
1127 try_code->accept (*this); |
3665
|
1128 |
2123
|
1129 decrement_indent_level (); |
|
1130 } |
|
1131 |
3665
|
1132 print_indented_comment (cmd.middle_comment ()); |
|
1133 |
2123
|
1134 indent (); |
|
1135 |
|
1136 os << "catch"; |
|
1137 |
|
1138 newline (); |
|
1139 |
|
1140 tree_statement_list *catch_code = cmd.cleanup (); |
|
1141 |
|
1142 if (catch_code) |
|
1143 { |
|
1144 increment_indent_level (); |
3665
|
1145 |
2123
|
1146 catch_code->accept (*this); |
3665
|
1147 |
2123
|
1148 decrement_indent_level (); |
|
1149 } |
|
1150 |
3665
|
1151 print_indented_comment (cmd.trailing_comment ()); |
|
1152 |
2123
|
1153 indent (); |
|
1154 |
|
1155 os << "end_try_catch"; |
|
1156 } |
|
1157 |
|
1158 void |
|
1159 tree_print_code::visit_unwind_protect_command |
|
1160 (tree_unwind_protect_command& cmd) |
|
1161 { |
3665
|
1162 print_comment_list (cmd.leading_comment ()); |
|
1163 |
2123
|
1164 indent (); |
|
1165 |
|
1166 os << "unwind_protect"; |
|
1167 |
|
1168 newline (); |
|
1169 |
|
1170 tree_statement_list *unwind_protect_code = cmd.body (); |
|
1171 |
|
1172 if (unwind_protect_code) |
|
1173 { |
|
1174 increment_indent_level (); |
3665
|
1175 |
2123
|
1176 unwind_protect_code->accept (*this); |
3665
|
1177 |
2123
|
1178 decrement_indent_level (); |
|
1179 } |
|
1180 |
3665
|
1181 print_indented_comment (cmd.middle_comment ()); |
|
1182 |
2123
|
1183 indent (); |
|
1184 |
3478
|
1185 os << "unwind_protect_cleanup"; |
2123
|
1186 |
|
1187 newline (); |
|
1188 |
|
1189 tree_statement_list *cleanup_code = cmd.cleanup (); |
|
1190 |
|
1191 if (cleanup_code) |
|
1192 { |
|
1193 increment_indent_level (); |
3665
|
1194 |
2123
|
1195 cleanup_code->accept (*this); |
3665
|
1196 |
2123
|
1197 decrement_indent_level (); |
|
1198 } |
|
1199 |
3665
|
1200 print_indented_comment (cmd.trailing_comment ()); |
|
1201 |
2123
|
1202 indent (); |
|
1203 |
|
1204 os << "end_unwind_protect"; |
|
1205 } |
|
1206 |
|
1207 void |
|
1208 tree_print_code::visit_while_command (tree_while_command& cmd) |
|
1209 { |
3665
|
1210 print_comment_list (cmd.leading_comment ()); |
|
1211 |
2123
|
1212 indent (); |
|
1213 |
|
1214 os << "while "; |
|
1215 |
|
1216 tree_expression *expr = cmd.condition (); |
|
1217 |
|
1218 if (expr) |
|
1219 expr->accept (*this); |
|
1220 |
|
1221 newline (); |
|
1222 |
|
1223 tree_statement_list *list = cmd.body (); |
|
1224 |
|
1225 if (list) |
|
1226 { |
|
1227 increment_indent_level (); |
3665
|
1228 |
2123
|
1229 list->accept (*this); |
3665
|
1230 |
2123
|
1231 decrement_indent_level (); |
|
1232 } |
|
1233 |
3665
|
1234 print_indented_comment (cmd.trailing_comment ()); |
|
1235 |
2123
|
1236 indent (); |
|
1237 |
|
1238 os << "endwhile"; |
|
1239 } |
|
1240 |
3484
|
1241 void |
|
1242 tree_print_code::visit_do_until_command (tree_do_until_command& cmd) |
|
1243 { |
3665
|
1244 print_comment_list (cmd.leading_comment ()); |
|
1245 |
3484
|
1246 indent (); |
|
1247 |
|
1248 os << "do"; |
|
1249 |
|
1250 newline (); |
|
1251 |
|
1252 tree_statement_list *list = cmd.body (); |
|
1253 |
|
1254 if (list) |
|
1255 { |
|
1256 increment_indent_level (); |
3665
|
1257 |
3484
|
1258 list->accept (*this); |
3665
|
1259 |
3484
|
1260 decrement_indent_level (); |
|
1261 } |
|
1262 |
3665
|
1263 print_indented_comment (cmd.trailing_comment ()); |
|
1264 |
3484
|
1265 indent (); |
|
1266 |
|
1267 os << "until"; |
|
1268 |
|
1269 tree_expression *expr = cmd.condition (); |
|
1270 |
|
1271 if (expr) |
|
1272 expr->accept (*this); |
|
1273 |
|
1274 newline (); |
|
1275 } |
|
1276 |
2123
|
1277 // Each print_code() function should call this before printing |
|
1278 // anything. |
|
1279 // |
|
1280 // This doesn't need to be fast, but isn't there a better way? |
|
1281 |
|
1282 void |
|
1283 tree_print_code::indent (void) |
|
1284 { |
|
1285 assert (curr_print_indent_level >= 0); |
|
1286 |
|
1287 if (beginning_of_line) |
|
1288 { |
2900
|
1289 os << prefix; |
|
1290 |
|
1291 for (int i = 0; i < curr_print_indent_level; i++) |
|
1292 os << " "; |
|
1293 |
2123
|
1294 beginning_of_line = false; |
|
1295 } |
|
1296 } |
|
1297 |
|
1298 // All print_code() functions should use this to print new lines. |
|
1299 |
|
1300 void |
|
1301 tree_print_code::newline (void) |
|
1302 { |
|
1303 os << "\n"; |
|
1304 |
|
1305 beginning_of_line = true; |
|
1306 } |
|
1307 |
|
1308 // For ressetting print_code state. |
|
1309 |
|
1310 void |
|
1311 tree_print_code::reset (void) |
|
1312 { |
|
1313 beginning_of_line = true; |
|
1314 curr_print_indent_level = 0; |
4676
|
1315 while (nesting.top () != 'n') |
|
1316 nesting.pop (); |
2123
|
1317 } |
|
1318 |
2961
|
1319 void |
|
1320 tree_print_code::print_parens (const tree_expression& expr, const char *txt) |
|
1321 { |
|
1322 int n = expr.paren_count (); |
|
1323 |
|
1324 for (int i = 0; i < n; i++) |
|
1325 os << txt; |
|
1326 } |
|
1327 |
3665
|
1328 void |
|
1329 tree_print_code::print_comment_elt (const octave_comment_elt& elt) |
|
1330 { |
|
1331 bool printed_something = false; |
|
1332 |
|
1333 bool prev_char_was_newline = false; |
|
1334 |
3769
|
1335 std::string comment = elt.text (); |
3665
|
1336 |
|
1337 size_t len = comment.length (); |
|
1338 |
|
1339 size_t i = 0; |
|
1340 |
|
1341 while (i < len && comment[i++] == '\n') |
|
1342 ; /* Skip leading new lines. */ |
|
1343 i--; |
|
1344 |
|
1345 while (i < len) |
|
1346 { |
|
1347 char c = comment[i++]; |
|
1348 |
|
1349 if (c == '\n') |
|
1350 { |
|
1351 if (prev_char_was_newline) |
|
1352 os << "##"; |
|
1353 |
|
1354 newline (); |
|
1355 |
|
1356 prev_char_was_newline = true; |
|
1357 } |
|
1358 else |
|
1359 { |
|
1360 if (beginning_of_line) |
|
1361 { |
|
1362 printed_something = true; |
|
1363 |
|
1364 indent (); |
|
1365 |
|
1366 os << "##"; |
|
1367 |
|
1368 if (! (isspace (c) || c == '!')) |
|
1369 os << " "; |
|
1370 } |
|
1371 |
|
1372 os << (char) c; |
|
1373 |
|
1374 prev_char_was_newline = false; |
|
1375 } |
|
1376 } |
|
1377 |
|
1378 if (printed_something && ! beginning_of_line) |
|
1379 newline (); |
|
1380 } |
|
1381 |
|
1382 void |
|
1383 tree_print_code::print_comment_list (octave_comment_list *comment_list) |
|
1384 { |
|
1385 if (comment_list) |
|
1386 { |
4219
|
1387 octave_comment_list::iterator p = comment_list->begin (); |
3665
|
1388 |
4219
|
1389 while (p != comment_list->end ()) |
3665
|
1390 { |
4219
|
1391 octave_comment_elt elt = *p++; |
3665
|
1392 |
|
1393 print_comment_elt (elt); |
|
1394 |
4219
|
1395 if (p != comment_list->end ()) |
3665
|
1396 newline (); |
|
1397 } |
|
1398 } |
|
1399 } |
|
1400 |
|
1401 void |
|
1402 tree_print_code::print_indented_comment (octave_comment_list *comment_list) |
|
1403 { |
|
1404 increment_indent_level (); |
|
1405 |
|
1406 print_comment_list (comment_list); |
|
1407 |
|
1408 decrement_indent_level (); |
|
1409 } |
|
1410 |
2123
|
1411 /* |
|
1412 ;;; Local Variables: *** |
|
1413 ;;; mode: C++ *** |
|
1414 ;;; End: *** |
|
1415 */ |