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 * |
7336
|
292 tree_simple_assignment::dup (symbol_table::scope_id scope) |
5861
|
293 { |
|
294 tree_simple_assignment *new_sa |
7336
|
295 = new tree_simple_assignment (lhs ? lhs->dup (scope) : 0, |
|
296 rhs ? rhs->dup (scope) : 0, |
5861
|
297 preserve, etype); |
|
298 |
|
299 new_sa->copy_base (*this); |
|
300 |
|
301 return new_sa; |
|
302 } |
|
303 |
2980
|
304 void |
|
305 tree_simple_assignment::accept (tree_walker& tw) |
|
306 { |
|
307 tw.visit_simple_assignment (*this); |
|
308 } |
|
309 |
|
310 // Multi-valued assignment expressions. |
|
311 |
6253
|
312 tree_multi_assignment::tree_multi_assignment |
|
313 (tree_argument_list *lst, tree_expression *r, |
|
314 bool plhs, int l, int c, octave_value::assign_op t) |
|
315 : tree_expression (l, c), lhs (lst), rhs (r), preserve (plhs), etype (t) |
|
316 { |
|
317 for (tree_argument_list::iterator p = lhs->begin (); p != lhs->end (); p++) |
|
318 { |
6483
|
319 tree_expression *lhs_expr = *p; |
6253
|
320 |
6483
|
321 if (lhs_expr) |
|
322 maybe_warn_former_built_in_variable (lhs_expr->name ()); |
6253
|
323 } |
|
324 } |
|
325 |
2980
|
326 tree_multi_assignment::~tree_multi_assignment (void) |
|
327 { |
|
328 if (! preserve) |
|
329 delete lhs; |
|
330 |
|
331 delete rhs; |
|
332 } |
|
333 |
|
334 octave_value |
|
335 tree_multi_assignment::rvalue (void) |
|
336 { |
|
337 octave_value retval; |
|
338 |
|
339 octave_value_list tmp = rvalue (1); |
|
340 |
|
341 if (! tmp.empty ()) |
|
342 retval = tmp(0); |
|
343 |
|
344 return retval; |
|
345 } |
|
346 |
5775
|
347 // FIXME -- this works, but it would look a little better if |
2984
|
348 // it were broken up into a couple of separate functions. |
|
349 |
2980
|
350 octave_value_list |
|
351 tree_multi_assignment::rvalue (int) |
|
352 { |
3208
|
353 octave_value_list retval; |
2980
|
354 |
|
355 if (error_state) |
3208
|
356 return retval; |
2980
|
357 |
|
358 if (rhs) |
|
359 { |
5846
|
360 std::list<octave_lvalue> lvalue_list = lhs->lvalue_list (); |
3977
|
361 |
|
362 if (error_state) |
|
363 return retval; |
2980
|
364 |
5846
|
365 int n_out = 0; |
|
366 |
|
367 for (std::list<octave_lvalue>::const_iterator p = lvalue_list.begin (); |
|
368 p != lvalue_list.end (); |
|
369 p++) |
|
370 n_out += p->numel (); |
|
371 |
3208
|
372 octave_value_list rhs_val = rhs->rvalue (n_out); |
2980
|
373 |
3977
|
374 if (error_state) |
|
375 return retval; |
2980
|
376 |
3977
|
377 if (rhs_val.empty ()) |
|
378 { |
5846
|
379 if (n_out > 0) |
|
380 { |
|
381 error ("value on right hand side of assignment is undefined"); |
|
382 eval_error (); |
|
383 return retval; |
|
384 } |
3977
|
385 } |
|
386 else |
|
387 { |
5846
|
388 octave_idx_type k = 0; |
2980
|
389 |
5846
|
390 octave_idx_type n = rhs_val.length (); |
2980
|
391 |
5265
|
392 if (n == 1) |
|
393 { |
|
394 octave_value tmp = rhs_val(0); |
|
395 |
|
396 if (tmp.is_cs_list ()) |
|
397 { |
7389
|
398 rhs_val = tmp.list_value (); |
|
399 n = rhs_val.length (); |
5265
|
400 } |
|
401 } |
|
402 |
3977
|
403 retval.resize (n, octave_value ()); |
3208
|
404 |
5846
|
405 tree_argument_list::iterator q = lhs->begin (); |
|
406 |
|
407 for (std::list<octave_lvalue>::iterator p = lvalue_list.begin (); |
|
408 p != lvalue_list.end (); |
4219
|
409 p++) |
3977
|
410 { |
5846
|
411 tree_expression *lhs_elt = *q++; |
|
412 |
|
413 octave_lvalue ult = *p; |
|
414 |
|
415 octave_idx_type nel = ult.numel (); |
3977
|
416 |
5846
|
417 if (nel > 1) |
3977
|
418 { |
5846
|
419 if (k + nel <= n) |
|
420 { |
|
421 if (etype == octave_value::op_asn_eq) |
|
422 { |
|
423 octave_value_list ovl (nel, octave_value ()); |
|
424 |
|
425 for (octave_idx_type j = 0; j < nel; j++) |
|
426 ovl(j) = rhs_val(k+j); |
|
427 |
|
428 ult.assign (etype, octave_value (ovl, true)); |
2980
|
429 |
5846
|
430 if (! error_state) |
|
431 { |
|
432 for (octave_idx_type j = 0; j < nel; j++) |
|
433 retval(k+j) = rhs_val(k+j); |
|
434 |
|
435 k += nel; |
|
436 } |
|
437 } |
|
438 else |
|
439 { |
|
440 std::string op = octave_value::assign_op_as_string (etype); |
|
441 error ("operator %s unsupported for comma-separated list assignment", |
|
442 op.c_str ()); |
|
443 } |
5750
|
444 } |
5846
|
445 else |
|
446 error ("some elements undefined in return list"); |
|
447 } |
|
448 else |
|
449 { |
|
450 if (k < n) |
3977
|
451 { |
|
452 ult.assign (etype, rhs_val(k)); |
|
453 |
|
454 if (! error_state) |
3208
|
455 { |
3977
|
456 if (etype == octave_value::op_asn_eq) |
|
457 retval(k) = rhs_val(k); |
3208
|
458 else |
3977
|
459 retval(k) = ult.value (); |
5846
|
460 |
|
461 k++; |
2980
|
462 } |
|
463 } |
3208
|
464 else |
3977
|
465 error ("element number %d undefined in return list", k+1); |
5846
|
466 } |
2980
|
467 |
5846
|
468 if (error_state) |
|
469 { |
|
470 eval_error (); |
|
471 break; |
|
472 } |
|
473 else if (print_result ()) |
|
474 { |
|
475 // We clear any index here so that we can get |
|
476 // the new value of the referenced object below, |
|
477 // instead of the indexed value (which should be |
|
478 // the same as the right hand side value). |
3977
|
479 |
5846
|
480 ult.clear_index (); |
3208
|
481 |
5846
|
482 octave_value lhs_val = ult.value (); |
3977
|
483 |
5846
|
484 if (! error_state) |
|
485 lhs_val.print_with_name (octave_stdout, |
|
486 lhs_elt->name ()); |
2980
|
487 } |
3977
|
488 |
|
489 if (error_state) |
|
490 break; |
|
491 |
2980
|
492 } |
|
493 } |
|
494 } |
3208
|
495 else |
|
496 eval_error (); |
2980
|
497 |
3208
|
498 return retval; |
2980
|
499 } |
|
500 |
|
501 void |
|
502 tree_multi_assignment::eval_error (void) |
|
503 { |
3965
|
504 int l = line (); |
|
505 int c = column (); |
2980
|
506 |
3965
|
507 if (l != -1 && c != -1) |
|
508 ::error ("evaluating assignment expression near line %d, column %d", |
|
509 l, c); |
2980
|
510 } |
|
511 |
3536
|
512 std::string |
3208
|
513 tree_multi_assignment::oper (void) const |
|
514 { |
|
515 return octave_value::assign_op_as_string (etype); |
|
516 } |
|
517 |
5861
|
518 tree_expression * |
7336
|
519 tree_multi_assignment::dup (symbol_table::scope_id scope) |
5861
|
520 { |
|
521 tree_multi_assignment *new_ma |
7336
|
522 = new tree_multi_assignment (lhs ? lhs->dup (scope) : 0, |
|
523 rhs ? rhs->dup (scope) : 0, |
5861
|
524 preserve, etype); |
|
525 |
|
526 new_ma->copy_base (*this); |
|
527 |
|
528 return new_ma; |
|
529 } |
|
530 |
2980
|
531 void |
|
532 tree_multi_assignment::accept (tree_walker& tw) |
|
533 { |
|
534 tw.visit_multi_assignment (*this); |
|
535 } |
|
536 |
|
537 /* |
|
538 ;;; Local Variables: *** |
|
539 ;;; mode: C++ *** |
|
540 ;;; End: *** |
|
541 */ |