Mercurial > hg > octave-lyh
annotate src/pt-assign.cc @ 8037:0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 19 Aug 2008 14:09:24 -0400 |
parents | 3100283874d7 |
children | aa6484781a5b |
rev | line source |
---|---|
2980 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, |
4 2006, 2007 John W. Eaton | |
2980 | 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. | |
2980 | 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/>. | |
2980 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3503 | 28 #include <iostream> |
6253 | 29 #include <set> |
2980 | 30 |
31 #include "defun.h" | |
32 #include "error.h" | |
33 #include "input.h" | |
34 #include "oct-obj.h" | |
35 #include "oct-lvalue.h" | |
36 #include "pager.h" | |
37 #include "ov.h" | |
2982 | 38 #include "pt-arg-list.h" |
3770 | 39 #include "pt-bp.h" |
2980 | 40 #include "pt-assign.h" |
41 #include "pt-walk.h" | |
42 #include "utils.h" | |
5794 | 43 #include "variables.h" |
2980 | 44 |
45 // Simple assignment expressions. | |
46 | |
8037
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
47 // FIXME -- the following variable and the function that uses it |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
48 // should be removed from some future version of Octave. |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
49 |
6253 | 50 static const char *former_built_in_variables[] = |
51 { | |
52 "DEFAULT_EXEC_PATH", | |
53 "DEFAULT_LOADPATH", | |
54 "EDITOR", | |
55 "EXEC_PATH", | |
56 "FFTW_WISDOM_PROGRAM", | |
57 "IMAGEPATH", | |
58 "INFO_FILE", | |
59 "INFO_PROGRAM", | |
60 "LOADPATH", | |
61 "MAKEINFO_PROGRAM", | |
62 "PAGER", | |
63 "PS1", | |
64 "PS2", | |
65 "PS4", | |
66 "__kluge_procbuf_delay__", | |
67 "ans", | |
68 "automatic_replot", | |
69 "beep_on_error", | |
70 "completion_append_char", | |
71 "crash_dumps_octave_core", | |
72 "current_script_file_name", | |
73 "debug_on_error", | |
74 "debug_on_interrupt", | |
75 "debug_on_warning", | |
76 "debug_symtab_lookups", | |
77 "default_save_format", | |
78 "echo_executing_commands", | |
79 "fixed_point_format", | |
80 "gnuplot_binary", | |
81 "gnuplot_command_axes", | |
82 "gnuplot_command_end", | |
83 "gnuplot_command_plot", | |
84 "gnuplot_command_replot", | |
85 "gnuplot_command_splot", | |
86 "gnuplot_command_title", | |
87 "gnuplot_command_using", | |
88 "gnuplot_command_with", | |
89 "gnuplot_has_frames", | |
90 "history_file", | |
91 "history_size", | |
92 "ignore_function_time_stamp", | |
93 "max_recursion_depth", | |
94 "octave_core_file_format", | |
95 "octave_core_file_limit", | |
96 "octave_core_file_name", | |
97 "output_max_field_width", | |
98 "output_precision", | |
99 "page_output_immediately", | |
100 "page_screen_output", | |
101 "print_answer_id_name", | |
102 "print_empty_dimensions", | |
103 "print_rhs_assign_val", | |
104 "save_header_format_string", | |
105 "save_precision", | |
106 "saving_history", | |
107 "sighup_dumps_octave_core", | |
108 "sigterm_dumps_octave_core", | |
109 "silent_functions", | |
110 "split_long_rows", | |
111 "string_fill_char", | |
112 "struct_levels_to_print", | |
113 "suppress_verbose_help_message", | |
114 "variables_can_hide_functions", | |
115 "warn_assign_as_truth_value", | |
116 "warn_associativity_change", | |
117 "warn_divide_by_zero", | |
118 "warn_empty_list_elements", | |
119 "warn_fortran_indexing", | |
120 "warn_function_name_clash", | |
121 "warn_future_time_stamp", | |
122 "warn_imag_to_real", | |
123 "warn_matlab_incompatible", | |
124 "warn_missing_semicolon", | |
125 "warn_neg_dim_as_zero", | |
126 "warn_num_to_str", | |
127 "warn_precedence_change", | |
128 "warn_reload_forces_clear", | |
129 "warn_resize_on_range_error", | |
130 "warn_separator_insert", | |
131 "warn_single_quote_string", | |
132 "warn_str_to_num", | |
133 "warn_undefined_return_values", | |
134 "warn_variable_switch_label", | |
135 "whos_line_format", | |
136 0, | |
137 }; | |
138 | |
139 static void | |
140 maybe_warn_former_built_in_variable (const std::string& nm) | |
141 { | |
142 static bool initialized = false; | |
143 | |
144 static std::set<std::string> vars; | |
145 | |
146 if (! initialized) | |
147 { | |
148 const char **p = former_built_in_variables; | |
149 | |
150 while (*p) | |
151 vars.insert (*p++); | |
152 } | |
153 | |
154 if (vars.find (nm) != vars.end ()) | |
155 warning_with_id ("Octave:built-in-variable-assignment", | |
156 "%s is no longer a built-in variable; please read the NEWS file or type `news' for details", | |
157 nm.c_str ()); | |
158 } | |
159 | |
160 tree_simple_assignment::tree_simple_assignment | |
161 (tree_expression *le, tree_expression *re, | |
162 bool plhs, int l, int c, octave_value::assign_op t) | |
163 : tree_expression (l, c), lhs (le), rhs (re), preserve (plhs), etype (t) | |
8037
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
164 first_execution (true) { } |
6253 | 165 |
2980 | 166 tree_simple_assignment::~tree_simple_assignment (void) |
167 { | |
168 if (! preserve) | |
169 delete lhs; | |
170 | |
171 delete rhs; | |
172 } | |
173 | |
174 octave_value_list | |
175 tree_simple_assignment::rvalue (int nargout) | |
176 { | |
177 octave_value_list retval; | |
178 | |
3770 | 179 MAYBE_DO_BREAKPOINT; |
180 | |
2980 | 181 if (nargout > 1) |
182 error ("invalid number of output arguments for expression X = RHS"); | |
183 else | |
184 retval = rvalue (); | |
185 | |
186 return retval; | |
187 } | |
188 | |
5775 | 189 // FIXME -- this works, but it would look a little better if |
2984 | 190 // it were broken up into a couple of separate functions. |
191 | |
2980 | 192 octave_value |
193 tree_simple_assignment::rvalue (void) | |
194 { | |
3208 | 195 octave_value retval; |
2980 | 196 |
8037
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
197 if (first_execution && lhs) |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
198 maybe_warn_former_built_in_variable (lhs->name ()); |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
199 |
2980 | 200 if (error_state) |
3208 | 201 return retval; |
2980 | 202 |
203 if (rhs) | |
204 { | |
205 octave_value_list tmp = rhs->rvalue (); | |
206 | |
207 if (! (error_state || tmp.empty ())) | |
208 { | |
3208 | 209 octave_value rhs_val = tmp(0); |
2980 | 210 |
211 if (rhs_val.is_undefined ()) | |
212 { | |
213 error ("value on right hand side of assignment is undefined"); | |
7389 | 214 return retval; |
5750 | 215 } |
2980 | 216 else |
217 { | |
7389 | 218 if (rhs_val.is_cs_list ()) |
219 { | |
220 octave_value_list lst = rhs_val.list_value (); | |
221 | |
222 if (! lst.empty ()) | |
223 rhs_val = lst(0); | |
224 else | |
225 { | |
226 error ("invalid number of elements on RHS of assignment"); | |
227 return retval; | |
228 } | |
229 } | |
230 | |
2980 | 231 octave_lvalue ult = lhs->lvalue (); |
232 | |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
233 if (! error_state) |
2980 | 234 { |
235 ult.assign (etype, rhs_val); | |
236 | |
3215 | 237 if (! error_state) |
238 { | |
3527 | 239 if (etype == octave_value::op_asn_eq) |
3215 | 240 retval = rhs_val; |
241 else | |
242 retval = ult.value (); | |
2984 | 243 |
3215 | 244 if (print_result ()) |
3208 | 245 { |
5841 | 246 // We clear any index here so that we can |
247 // get the new value of the referenced | |
248 // object below, instead of the indexed | |
249 // value (which should be the same as the | |
250 // right hand side value). | |
2980 | 251 |
5841 | 252 ult.clear_index (); |
3208 | 253 |
5841 | 254 octave_value lhs_val = ult.value (); |
3208 | 255 |
5841 | 256 if (! error_state) |
257 lhs_val.print_with_name (octave_stdout, | |
258 lhs->name ()); | |
2980 | 259 } |
260 } | |
261 } | |
262 } | |
263 } | |
264 } | |
265 | |
8037
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
266 first_execution = false; |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
267 |
3208 | 268 return retval; |
2980 | 269 } |
270 | |
3536 | 271 std::string |
2980 | 272 tree_simple_assignment::oper (void) const |
273 { | |
274 return octave_value::assign_op_as_string (etype); | |
275 } | |
276 | |
5861 | 277 tree_expression * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7389
diff
changeset
|
278 tree_simple_assignment::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7389
diff
changeset
|
279 symbol_table::context_id context) |
5861 | 280 { |
281 tree_simple_assignment *new_sa | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7389
diff
changeset
|
282 = new tree_simple_assignment (lhs ? lhs->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7389
diff
changeset
|
283 rhs ? rhs->dup (scope, context) : 0, |
5861 | 284 preserve, etype); |
285 | |
286 new_sa->copy_base (*this); | |
287 | |
288 return new_sa; | |
289 } | |
290 | |
2980 | 291 void |
292 tree_simple_assignment::accept (tree_walker& tw) | |
293 { | |
294 tw.visit_simple_assignment (*this); | |
295 } | |
296 | |
297 // Multi-valued assignment expressions. | |
298 | |
6253 | 299 tree_multi_assignment::tree_multi_assignment |
300 (tree_argument_list *lst, tree_expression *r, | |
301 bool plhs, int l, int c, octave_value::assign_op t) | |
302 : tree_expression (l, c), lhs (lst), rhs (r), preserve (plhs), etype (t) | |
8037
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
303 first_execution (true) { } |
6253 | 304 |
2980 | 305 tree_multi_assignment::~tree_multi_assignment (void) |
306 { | |
307 if (! preserve) | |
308 delete lhs; | |
309 | |
310 delete rhs; | |
311 } | |
312 | |
313 octave_value | |
314 tree_multi_assignment::rvalue (void) | |
315 { | |
316 octave_value retval; | |
317 | |
318 octave_value_list tmp = rvalue (1); | |
319 | |
320 if (! tmp.empty ()) | |
321 retval = tmp(0); | |
322 | |
323 return retval; | |
324 } | |
325 | |
5775 | 326 // FIXME -- this works, but it would look a little better if |
2984 | 327 // it were broken up into a couple of separate functions. |
328 | |
2980 | 329 octave_value_list |
330 tree_multi_assignment::rvalue (int) | |
331 { | |
3208 | 332 octave_value_list retval; |
2980 | 333 |
334 if (error_state) | |
3208 | 335 return retval; |
2980 | 336 |
8037
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
337 if (first_execution) |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
338 { |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
339 for (tree_argument_list::iterator p = lhs->begin (); p != lhs->end (); p++) |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
340 { |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
341 tree_expression *lhs_expr = *p; |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
342 |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
343 if (lhs_expr) |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
344 maybe_warn_former_built_in_variable (lhs_expr->name ()); |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
345 } |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
346 } |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
347 |
2980 | 348 if (rhs) |
349 { | |
5846 | 350 std::list<octave_lvalue> lvalue_list = lhs->lvalue_list (); |
3977 | 351 |
352 if (error_state) | |
353 return retval; | |
2980 | 354 |
5846 | 355 int n_out = 0; |
356 | |
357 for (std::list<octave_lvalue>::const_iterator p = lvalue_list.begin (); | |
358 p != lvalue_list.end (); | |
359 p++) | |
360 n_out += p->numel (); | |
361 | |
3208 | 362 octave_value_list rhs_val = rhs->rvalue (n_out); |
2980 | 363 |
3977 | 364 if (error_state) |
365 return retval; | |
2980 | 366 |
3977 | 367 if (rhs_val.empty ()) |
368 { | |
5846 | 369 if (n_out > 0) |
370 { | |
371 error ("value on right hand side of assignment is undefined"); | |
372 return retval; | |
373 } | |
3977 | 374 } |
375 else | |
376 { | |
5846 | 377 octave_idx_type k = 0; |
2980 | 378 |
5846 | 379 octave_idx_type n = rhs_val.length (); |
2980 | 380 |
5265 | 381 if (n == 1) |
382 { | |
383 octave_value tmp = rhs_val(0); | |
384 | |
385 if (tmp.is_cs_list ()) | |
386 { | |
7389 | 387 rhs_val = tmp.list_value (); |
388 n = rhs_val.length (); | |
5265 | 389 } |
390 } | |
391 | |
3977 | 392 retval.resize (n, octave_value ()); |
3208 | 393 |
5846 | 394 tree_argument_list::iterator q = lhs->begin (); |
395 | |
396 for (std::list<octave_lvalue>::iterator p = lvalue_list.begin (); | |
397 p != lvalue_list.end (); | |
4219 | 398 p++) |
3977 | 399 { |
5846 | 400 tree_expression *lhs_elt = *q++; |
401 | |
402 octave_lvalue ult = *p; | |
403 | |
404 octave_idx_type nel = ult.numel (); | |
3977 | 405 |
5846 | 406 if (nel > 1) |
3977 | 407 { |
5846 | 408 if (k + nel <= n) |
409 { | |
410 if (etype == octave_value::op_asn_eq) | |
411 { | |
412 octave_value_list ovl (nel, octave_value ()); | |
413 | |
414 for (octave_idx_type j = 0; j < nel; j++) | |
415 ovl(j) = rhs_val(k+j); | |
416 | |
417 ult.assign (etype, octave_value (ovl, true)); | |
2980 | 418 |
5846 | 419 if (! error_state) |
420 { | |
421 for (octave_idx_type j = 0; j < nel; j++) | |
422 retval(k+j) = rhs_val(k+j); | |
423 | |
424 k += nel; | |
425 } | |
426 } | |
427 else | |
428 { | |
429 std::string op = octave_value::assign_op_as_string (etype); | |
430 error ("operator %s unsupported for comma-separated list assignment", | |
431 op.c_str ()); | |
432 } | |
5750 | 433 } |
5846 | 434 else |
435 error ("some elements undefined in return list"); | |
436 } | |
437 else | |
438 { | |
439 if (k < n) | |
3977 | 440 { |
441 ult.assign (etype, rhs_val(k)); | |
442 | |
443 if (! error_state) | |
3208 | 444 { |
3977 | 445 if (etype == octave_value::op_asn_eq) |
446 retval(k) = rhs_val(k); | |
3208 | 447 else |
3977 | 448 retval(k) = ult.value (); |
5846 | 449 |
450 k++; | |
2980 | 451 } |
452 } | |
3208 | 453 else |
3977 | 454 error ("element number %d undefined in return list", k+1); |
5846 | 455 } |
2980 | 456 |
5846 | 457 if (error_state) |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7767
diff
changeset
|
458 break; |
5846 | 459 else if (print_result ()) |
460 { | |
461 // We clear any index here so that we can get | |
462 // the new value of the referenced object below, | |
463 // instead of the indexed value (which should be | |
464 // the same as the right hand side value). | |
3977 | 465 |
5846 | 466 ult.clear_index (); |
3208 | 467 |
5846 | 468 octave_value lhs_val = ult.value (); |
3977 | 469 |
5846 | 470 if (! error_state) |
471 lhs_val.print_with_name (octave_stdout, | |
472 lhs_elt->name ()); | |
2980 | 473 } |
3977 | 474 |
475 if (error_state) | |
476 break; | |
477 | |
2980 | 478 } |
479 } | |
480 } | |
481 | |
8037
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
482 first_execution = false; |
0be8cf23b95c
check for obsolete built-in variable assignment at first execution rather than parse time
Jaroslav Hajek <highegg@gmail.com>
parents:
8011
diff
changeset
|
483 |
3208 | 484 return retval; |
2980 | 485 } |
486 | |
3536 | 487 std::string |
3208 | 488 tree_multi_assignment::oper (void) const |
489 { | |
490 return octave_value::assign_op_as_string (etype); | |
491 } | |
492 | |
5861 | 493 tree_expression * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7389
diff
changeset
|
494 tree_multi_assignment::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7389
diff
changeset
|
495 symbol_table::context_id context) |
5861 | 496 { |
497 tree_multi_assignment *new_ma | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7389
diff
changeset
|
498 = new tree_multi_assignment (lhs ? lhs->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7389
diff
changeset
|
499 rhs ? rhs->dup (scope, context) : 0, |
5861 | 500 preserve, etype); |
501 | |
502 new_ma->copy_base (*this); | |
503 | |
504 return new_ma; | |
505 } | |
506 | |
2980 | 507 void |
508 tree_multi_assignment::accept (tree_walker& tw) | |
509 { | |
510 tw.visit_multi_assignment (*this); | |
511 } | |
512 | |
513 /* | |
514 ;;; Local Variables: *** | |
515 ;;; mode: C++ *** | |
516 ;;; End: *** | |
517 */ |