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