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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream.h> |
|
32 #include <strstream.h> |
|
33 |
|
34 #include "defun.h" |
|
35 #include "error.h" |
|
36 #include "input.h" |
|
37 #include "oct-obj.h" |
|
38 #include "oct-lvalue.h" |
|
39 #include "pager.h" |
|
40 #include "ov.h" |
2982
|
41 #include "pt-arg-list.h" |
2980
|
42 #include "pt-assign.h" |
|
43 #include "pt-pr-code.h" |
|
44 #include "pt-walk.h" |
|
45 #include "utils.h" |
|
46 |
|
47 // Nonzero means we're returning from a function. |
|
48 extern int returning; |
|
49 |
|
50 // Nonzero means we're breaking out of a loop or function body. |
|
51 extern int breaking; |
|
52 |
|
53 // TRUE means print the right hand side of an assignment instead of |
|
54 // the left. |
|
55 static bool Vprint_rhs_assign_val; |
|
56 |
|
57 // Simple assignment expressions. |
|
58 |
|
59 tree_simple_assignment::~tree_simple_assignment (void) |
|
60 { |
|
61 if (! preserve) |
|
62 delete lhs; |
|
63 |
|
64 delete rhs; |
|
65 } |
|
66 |
|
67 octave_value_list |
|
68 tree_simple_assignment::rvalue (int nargout) |
|
69 { |
|
70 octave_value_list retval; |
|
71 |
|
72 if (nargout > 1) |
|
73 error ("invalid number of output arguments for expression X = RHS"); |
|
74 else |
|
75 retval = rvalue (); |
|
76 |
|
77 return retval; |
|
78 } |
|
79 |
2984
|
80 // XXX FIXME XXX -- this works, but it would look a little better if |
|
81 // it were broken up into a couple of separate functions. |
|
82 |
2980
|
83 octave_value |
|
84 tree_simple_assignment::rvalue (void) |
|
85 { |
|
86 octave_value rhs_val; |
|
87 |
|
88 if (error_state) |
|
89 return rhs_val; |
|
90 |
|
91 if (rhs) |
|
92 { |
|
93 octave_value_list tmp = rhs->rvalue (); |
|
94 |
|
95 if (! (error_state || tmp.empty ())) |
|
96 { |
|
97 rhs_val = tmp(0); |
|
98 |
|
99 if (rhs_val.is_undefined ()) |
|
100 { |
|
101 error ("value on right hand side of assignment is undefined"); |
|
102 eval_error (); |
|
103 } |
|
104 else |
|
105 { |
|
106 octave_lvalue ult = lhs->lvalue (); |
|
107 |
|
108 if (error_state) |
|
109 eval_error (); |
|
110 else |
|
111 { |
|
112 ult.assign (etype, rhs_val); |
|
113 |
2984
|
114 // We clear any index here so that we can get the |
|
115 // new value of the referenced object below, instead |
|
116 // of the indexed value (which should be the same as |
|
117 // the right hand side value). |
|
118 |
|
119 ult.clear_index (); |
|
120 |
2980
|
121 if (error_state) |
|
122 eval_error (); |
|
123 else if (! Vprint_rhs_assign_val) |
|
124 { |
|
125 octave_value lhs_val = ult.value (); |
|
126 |
|
127 if (! error_state && print_result ()) |
|
128 { |
|
129 if (Vprint_rhs_assign_val) |
|
130 { |
|
131 ostrstream buf; |
|
132 |
|
133 tree_print_code tpc (buf); |
|
134 |
|
135 lhs->accept (tpc); |
|
136 |
|
137 buf << ends; |
|
138 |
|
139 const char *tag = buf.str (); |
|
140 |
|
141 rhs_val.print_with_name (octave_stdout, tag); |
|
142 |
|
143 delete [] tag; |
|
144 } |
|
145 else |
|
146 lhs_val.print_with_name (octave_stdout, |
|
147 lhs->name ()); |
|
148 } |
|
149 } |
|
150 } |
|
151 } |
|
152 } |
|
153 else |
|
154 eval_error (); |
|
155 } |
|
156 |
|
157 return rhs_val; |
|
158 } |
|
159 |
|
160 void |
|
161 tree_simple_assignment::eval_error (void) |
|
162 { |
|
163 if (error_state > 0) |
|
164 { |
|
165 int l = line (); |
|
166 int c = column (); |
|
167 |
|
168 if (l != -1 && c != -1) |
|
169 ::error ("evaluating assignment expression near line %d, column %d", |
|
170 l, c); |
|
171 } |
|
172 } |
|
173 |
|
174 string |
|
175 tree_simple_assignment::oper (void) const |
|
176 { |
|
177 return octave_value::assign_op_as_string (etype); |
|
178 } |
|
179 |
|
180 void |
|
181 tree_simple_assignment::accept (tree_walker& tw) |
|
182 { |
|
183 tw.visit_simple_assignment (*this); |
|
184 } |
|
185 |
|
186 // Multi-valued assignment expressions. |
|
187 |
|
188 tree_multi_assignment::~tree_multi_assignment (void) |
|
189 { |
|
190 if (! preserve) |
|
191 delete lhs; |
|
192 |
|
193 delete rhs; |
|
194 } |
|
195 |
|
196 octave_value |
|
197 tree_multi_assignment::rvalue (void) |
|
198 { |
|
199 octave_value retval; |
|
200 |
|
201 octave_value_list tmp = rvalue (1); |
|
202 |
|
203 if (! tmp.empty ()) |
|
204 retval = tmp(0); |
|
205 |
|
206 return retval; |
|
207 } |
|
208 |
2984
|
209 // XXX FIXME XXX -- this works, but it would look a little better if |
|
210 // it were broken up into a couple of separate functions. |
|
211 |
2980
|
212 octave_value_list |
|
213 tree_multi_assignment::rvalue (int) |
|
214 { |
|
215 octave_value_list rhs_val; |
|
216 |
|
217 if (error_state) |
|
218 return rhs_val; |
|
219 |
|
220 if (rhs) |
|
221 { |
|
222 int n_out = lhs->length (); |
|
223 |
|
224 rhs_val = rhs->rvalue (n_out); |
|
225 |
|
226 if (! (error_state || rhs_val.empty ())) |
|
227 { |
|
228 if (rhs_val.empty ()) |
|
229 { |
|
230 error ("value on right hand side of assignment is undefined"); |
|
231 eval_error (); |
|
232 } |
|
233 else |
|
234 { |
|
235 int k = 0; |
|
236 |
|
237 int n = rhs_val.length (); |
|
238 |
|
239 for (Pix p = lhs->first (); p != 0; lhs->next (p)) |
|
240 { |
|
241 tree_expression *lhs_elt = lhs->operator () (p); |
|
242 |
|
243 if (lhs_elt) |
|
244 { |
|
245 octave_lvalue ult = lhs_elt->lvalue (); |
|
246 |
|
247 if (error_state) |
|
248 eval_error (); |
|
249 else |
|
250 { |
|
251 octave_value tmp = k < n |
|
252 ? rhs_val(k++) : octave_value (); |
|
253 |
|
254 if (tmp.is_defined ()) |
|
255 { |
|
256 // XXX FIXME XXX -- handle other assignment ops. |
|
257 ult.assign (octave_value::asn_eq, tmp); |
2984
|
258 |
|
259 // We clear any index here so that we |
|
260 // can get the new value of the |
|
261 // referenced object below, instead of |
|
262 // the indexed value (which should be |
|
263 // the same as the right hand side |
|
264 // value). |
|
265 |
|
266 ult.clear_index (); |
2980
|
267 } |
|
268 else |
|
269 error ("element number %d undefined in return list", k); |
|
270 |
|
271 if (error_state) |
|
272 eval_error (); |
|
273 else if (! Vprint_rhs_assign_val) |
|
274 { |
|
275 octave_value lhs_val = ult.value (); |
|
276 |
|
277 if (! error_state && print_result ()) |
|
278 { |
|
279 if (Vprint_rhs_assign_val) |
|
280 { |
|
281 ostrstream buf; |
|
282 |
|
283 tree_print_code tpc (buf); |
|
284 |
|
285 lhs_elt->accept (tpc); |
|
286 |
|
287 buf << ends; |
|
288 |
|
289 const char *tag = buf.str (); |
|
290 |
|
291 tmp.print_with_name |
|
292 (octave_stdout, tag); |
|
293 |
|
294 delete [] tag; |
|
295 } |
|
296 else |
|
297 lhs_val.print_with_name (octave_stdout, |
|
298 lhs_elt->name ()); |
|
299 } |
|
300 } |
|
301 } |
|
302 } |
|
303 |
|
304 if (error_state) |
|
305 break; |
|
306 } |
|
307 } |
|
308 } |
|
309 else |
|
310 eval_error (); |
|
311 } |
|
312 |
|
313 return rhs_val; |
|
314 } |
|
315 |
|
316 void |
|
317 tree_multi_assignment::eval_error (void) |
|
318 { |
|
319 if (error_state > 0) |
|
320 { |
|
321 int l = line (); |
|
322 int c = column (); |
|
323 |
|
324 if (l != -1 && c != -1) |
|
325 ::error ("evaluating assignment expression near line %d, column %d", |
|
326 l, c); |
|
327 } |
|
328 } |
|
329 |
|
330 void |
|
331 tree_multi_assignment::accept (tree_walker& tw) |
|
332 { |
|
333 tw.visit_multi_assignment (*this); |
|
334 } |
|
335 |
|
336 static int |
|
337 print_rhs_assign_val (void) |
|
338 { |
|
339 Vprint_rhs_assign_val = check_preference ("print_rhs_assign_val"); |
|
340 |
|
341 return 0; |
|
342 } |
|
343 |
|
344 void |
|
345 symbols_of_pt_assign (void) |
|
346 { |
|
347 DEFVAR (print_rhs_assign_val, 0.0, 0, print_rhs_assign_val, |
|
348 "if TRUE, print the right hand side of assignments instead of the left"); |
|
349 } |
|
350 |
|
351 /* |
|
352 ;;; Local Variables: *** |
|
353 ;;; mode: C++ *** |
|
354 ;;; End: *** |
|
355 */ |