2980
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 (); |
|
212 } |
5750
|
213 else if (rhs_val.is_cs_list ()) |
|
214 { |
|
215 error ("invalid assignment of comma-separated list"); |
|
216 eval_error (); |
|
217 } |
2980
|
218 else |
|
219 { |
|
220 octave_lvalue ult = lhs->lvalue (); |
|
221 |
|
222 if (error_state) |
|
223 eval_error (); |
|
224 else |
|
225 { |
|
226 ult.assign (etype, rhs_val); |
|
227 |
3215
|
228 if (! error_state) |
|
229 { |
3527
|
230 if (etype == octave_value::op_asn_eq) |
3215
|
231 retval = rhs_val; |
|
232 else |
|
233 retval = ult.value (); |
2984
|
234 |
3215
|
235 if (print_result ()) |
3208
|
236 { |
5841
|
237 // We clear any index here so that we can |
|
238 // get the new value of the referenced |
|
239 // object below, instead of the indexed |
|
240 // value (which should be the same as the |
|
241 // right hand side value). |
2980
|
242 |
5841
|
243 ult.clear_index (); |
3208
|
244 |
5841
|
245 octave_value lhs_val = ult.value (); |
3208
|
246 |
5841
|
247 if (! error_state) |
|
248 lhs_val.print_with_name (octave_stdout, |
|
249 lhs->name ()); |
2980
|
250 } |
|
251 } |
3215
|
252 else |
|
253 eval_error (); |
2980
|
254 } |
|
255 } |
|
256 } |
|
257 else |
|
258 eval_error (); |
|
259 } |
|
260 |
3208
|
261 return retval; |
2980
|
262 } |
|
263 |
|
264 void |
|
265 tree_simple_assignment::eval_error (void) |
|
266 { |
3965
|
267 int l = line (); |
|
268 int c = column (); |
2980
|
269 |
3965
|
270 if (l != -1 && c != -1) |
|
271 ::error ("evaluating assignment expression near line %d, column %d", |
|
272 l, c); |
2980
|
273 } |
|
274 |
3536
|
275 std::string |
2980
|
276 tree_simple_assignment::oper (void) const |
|
277 { |
|
278 return octave_value::assign_op_as_string (etype); |
|
279 } |
|
280 |
5861
|
281 tree_expression * |
|
282 tree_simple_assignment::dup (symbol_table *sym_tab) |
|
283 { |
|
284 tree_simple_assignment *new_sa |
|
285 = new tree_simple_assignment (lhs ? lhs->dup (sym_tab) : 0, |
|
286 rhs ? rhs->dup (sym_tab) : 0, |
|
287 preserve, etype); |
|
288 |
|
289 new_sa->copy_base (*this); |
|
290 |
|
291 return new_sa; |
|
292 } |
|
293 |
2980
|
294 void |
|
295 tree_simple_assignment::accept (tree_walker& tw) |
|
296 { |
|
297 tw.visit_simple_assignment (*this); |
|
298 } |
|
299 |
|
300 // Multi-valued assignment expressions. |
|
301 |
6253
|
302 tree_multi_assignment::tree_multi_assignment |
|
303 (tree_argument_list *lst, tree_expression *r, |
|
304 bool plhs, int l, int c, octave_value::assign_op t) |
|
305 : tree_expression (l, c), lhs (lst), rhs (r), preserve (plhs), etype (t) |
|
306 { |
|
307 for (tree_argument_list::iterator p = lhs->begin (); p != lhs->end (); p++) |
|
308 { |
6483
|
309 tree_expression *lhs_expr = *p; |
6253
|
310 |
6483
|
311 if (lhs_expr) |
|
312 maybe_warn_former_built_in_variable (lhs_expr->name ()); |
6253
|
313 } |
|
314 } |
|
315 |
2980
|
316 tree_multi_assignment::~tree_multi_assignment (void) |
|
317 { |
|
318 if (! preserve) |
|
319 delete lhs; |
|
320 |
|
321 delete rhs; |
|
322 } |
|
323 |
|
324 octave_value |
|
325 tree_multi_assignment::rvalue (void) |
|
326 { |
|
327 octave_value retval; |
|
328 |
|
329 octave_value_list tmp = rvalue (1); |
|
330 |
|
331 if (! tmp.empty ()) |
|
332 retval = tmp(0); |
|
333 |
|
334 return retval; |
|
335 } |
|
336 |
5775
|
337 // FIXME -- this works, but it would look a little better if |
2984
|
338 // it were broken up into a couple of separate functions. |
|
339 |
2980
|
340 octave_value_list |
|
341 tree_multi_assignment::rvalue (int) |
|
342 { |
3208
|
343 octave_value_list retval; |
2980
|
344 |
|
345 if (error_state) |
3208
|
346 return retval; |
2980
|
347 |
|
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 eval_error (); |
|
373 return retval; |
|
374 } |
3977
|
375 } |
|
376 else |
|
377 { |
5846
|
378 octave_idx_type k = 0; |
2980
|
379 |
5846
|
380 octave_idx_type n = rhs_val.length (); |
2980
|
381 |
5265
|
382 if (n == 1) |
|
383 { |
|
384 octave_value tmp = rhs_val(0); |
|
385 |
|
386 if (tmp.is_cs_list ()) |
|
387 { |
5750
|
388 error ("invalid assignment of comma-separated list"); |
|
389 eval_error (); |
|
390 return retval; |
5265
|
391 } |
|
392 } |
|
393 |
3977
|
394 retval.resize (n, octave_value ()); |
3208
|
395 |
5846
|
396 tree_argument_list::iterator q = lhs->begin (); |
|
397 |
|
398 for (std::list<octave_lvalue>::iterator p = lvalue_list.begin (); |
|
399 p != lvalue_list.end (); |
4219
|
400 p++) |
3977
|
401 { |
5846
|
402 tree_expression *lhs_elt = *q++; |
|
403 |
|
404 octave_lvalue ult = *p; |
|
405 |
|
406 octave_idx_type nel = ult.numel (); |
3977
|
407 |
5846
|
408 if (nel > 1) |
3977
|
409 { |
5846
|
410 if (k + nel <= n) |
|
411 { |
|
412 if (etype == octave_value::op_asn_eq) |
|
413 { |
|
414 octave_value_list ovl (nel, octave_value ()); |
|
415 |
|
416 for (octave_idx_type j = 0; j < nel; j++) |
|
417 ovl(j) = rhs_val(k+j); |
|
418 |
|
419 ult.assign (etype, octave_value (ovl, true)); |
2980
|
420 |
5846
|
421 if (! error_state) |
|
422 { |
|
423 for (octave_idx_type j = 0; j < nel; j++) |
|
424 retval(k+j) = rhs_val(k+j); |
|
425 |
|
426 k += nel; |
|
427 } |
|
428 } |
|
429 else |
|
430 { |
|
431 std::string op = octave_value::assign_op_as_string (etype); |
|
432 error ("operator %s unsupported for comma-separated list assignment", |
|
433 op.c_str ()); |
|
434 } |
5750
|
435 } |
5846
|
436 else |
|
437 error ("some elements undefined in return list"); |
|
438 } |
|
439 else |
|
440 { |
|
441 if (k < n) |
3977
|
442 { |
|
443 ult.assign (etype, rhs_val(k)); |
|
444 |
|
445 if (! error_state) |
3208
|
446 { |
3977
|
447 if (etype == octave_value::op_asn_eq) |
|
448 retval(k) = rhs_val(k); |
3208
|
449 else |
3977
|
450 retval(k) = ult.value (); |
5846
|
451 |
|
452 k++; |
2980
|
453 } |
|
454 } |
3208
|
455 else |
3977
|
456 error ("element number %d undefined in return list", k+1); |
5846
|
457 } |
2980
|
458 |
5846
|
459 if (error_state) |
|
460 { |
|
461 eval_error (); |
|
462 break; |
|
463 } |
|
464 else if (print_result ()) |
|
465 { |
|
466 // We clear any index here so that we can get |
|
467 // the new value of the referenced object below, |
|
468 // instead of the indexed value (which should be |
|
469 // the same as the right hand side value). |
3977
|
470 |
5846
|
471 ult.clear_index (); |
3208
|
472 |
5846
|
473 octave_value lhs_val = ult.value (); |
3977
|
474 |
5846
|
475 if (! error_state) |
|
476 lhs_val.print_with_name (octave_stdout, |
|
477 lhs_elt->name ()); |
2980
|
478 } |
3977
|
479 |
|
480 if (error_state) |
|
481 break; |
|
482 |
2980
|
483 } |
|
484 } |
|
485 } |
3208
|
486 else |
|
487 eval_error (); |
2980
|
488 |
3208
|
489 return retval; |
2980
|
490 } |
|
491 |
|
492 void |
|
493 tree_multi_assignment::eval_error (void) |
|
494 { |
3965
|
495 int l = line (); |
|
496 int c = column (); |
2980
|
497 |
3965
|
498 if (l != -1 && c != -1) |
|
499 ::error ("evaluating assignment expression near line %d, column %d", |
|
500 l, c); |
2980
|
501 } |
|
502 |
3536
|
503 std::string |
3208
|
504 tree_multi_assignment::oper (void) const |
|
505 { |
|
506 return octave_value::assign_op_as_string (etype); |
|
507 } |
|
508 |
5861
|
509 tree_expression * |
|
510 tree_multi_assignment::dup (symbol_table *sym_tab) |
|
511 { |
|
512 tree_multi_assignment *new_ma |
|
513 = new tree_multi_assignment (lhs ? lhs->dup (sym_tab) : 0, |
|
514 rhs ? rhs->dup (sym_tab) : 0, |
|
515 preserve, etype); |
|
516 |
|
517 new_ma->copy_base (*this); |
|
518 |
|
519 return new_ma; |
|
520 } |
|
521 |
2980
|
522 void |
|
523 tree_multi_assignment::accept (tree_walker& tw) |
|
524 { |
|
525 tw.visit_multi_assignment (*this); |
|
526 } |
|
527 |
|
528 /* |
|
529 ;;; Local Variables: *** |
|
530 ;;; mode: C++ *** |
|
531 ;;; End: *** |
|
532 */ |