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