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