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