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