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" |
|
41 #include "pt-assign.h" |
|
42 #include "pt-misc.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 |
|
80 octave_value |
|
81 tree_simple_assignment::rvalue (void) |
|
82 { |
|
83 octave_value rhs_val; |
|
84 |
|
85 if (error_state) |
|
86 return rhs_val; |
|
87 |
|
88 if (rhs) |
|
89 { |
|
90 octave_value_list tmp = rhs->rvalue (); |
|
91 |
|
92 if (! (error_state || tmp.empty ())) |
|
93 { |
|
94 rhs_val = tmp(0); |
|
95 |
|
96 if (rhs_val.is_undefined ()) |
|
97 { |
|
98 error ("value on right hand side of assignment is undefined"); |
|
99 eval_error (); |
|
100 } |
|
101 else |
|
102 { |
|
103 octave_lvalue ult = lhs->lvalue (); |
|
104 |
|
105 if (error_state) |
|
106 eval_error (); |
|
107 else |
|
108 { |
|
109 ult.assign (etype, rhs_val); |
|
110 |
|
111 if (error_state) |
|
112 eval_error (); |
|
113 else if (! Vprint_rhs_assign_val) |
|
114 { |
|
115 octave_value lhs_val = ult.value (); |
|
116 |
|
117 if (! error_state && print_result ()) |
|
118 { |
|
119 if (Vprint_rhs_assign_val) |
|
120 { |
|
121 ostrstream buf; |
|
122 |
|
123 tree_print_code tpc (buf); |
|
124 |
|
125 lhs->accept (tpc); |
|
126 |
|
127 buf << ends; |
|
128 |
|
129 const char *tag = buf.str (); |
|
130 |
|
131 rhs_val.print_with_name (octave_stdout, tag); |
|
132 |
|
133 delete [] tag; |
|
134 } |
|
135 else |
|
136 lhs_val.print_with_name (octave_stdout, |
|
137 lhs->name ()); |
|
138 } |
|
139 } |
|
140 } |
|
141 } |
|
142 } |
|
143 else |
|
144 eval_error (); |
|
145 } |
|
146 |
|
147 return rhs_val; |
|
148 } |
|
149 |
|
150 void |
|
151 tree_simple_assignment::eval_error (void) |
|
152 { |
|
153 if (error_state > 0) |
|
154 { |
|
155 int l = line (); |
|
156 int c = column (); |
|
157 |
|
158 if (l != -1 && c != -1) |
|
159 ::error ("evaluating assignment expression near line %d, column %d", |
|
160 l, c); |
|
161 } |
|
162 } |
|
163 |
|
164 string |
|
165 tree_simple_assignment::oper (void) const |
|
166 { |
|
167 return octave_value::assign_op_as_string (etype); |
|
168 } |
|
169 |
|
170 void |
|
171 tree_simple_assignment::accept (tree_walker& tw) |
|
172 { |
|
173 tw.visit_simple_assignment (*this); |
|
174 } |
|
175 |
|
176 // Multi-valued assignment expressions. |
|
177 |
|
178 tree_multi_assignment::~tree_multi_assignment (void) |
|
179 { |
|
180 if (! preserve) |
|
181 delete lhs; |
|
182 |
|
183 delete rhs; |
|
184 } |
|
185 |
|
186 octave_value |
|
187 tree_multi_assignment::rvalue (void) |
|
188 { |
|
189 octave_value retval; |
|
190 |
|
191 octave_value_list tmp = rvalue (1); |
|
192 |
|
193 if (! tmp.empty ()) |
|
194 retval = tmp(0); |
|
195 |
|
196 return retval; |
|
197 } |
|
198 |
|
199 octave_value_list |
|
200 tree_multi_assignment::rvalue (int) |
|
201 { |
|
202 octave_value_list rhs_val; |
|
203 |
|
204 if (error_state) |
|
205 return rhs_val; |
|
206 |
|
207 if (rhs) |
|
208 { |
|
209 int n_out = lhs->length (); |
|
210 |
|
211 rhs_val = rhs->rvalue (n_out); |
|
212 |
|
213 if (! (error_state || rhs_val.empty ())) |
|
214 { |
|
215 if (rhs_val.empty ()) |
|
216 { |
|
217 error ("value on right hand side of assignment is undefined"); |
|
218 eval_error (); |
|
219 } |
|
220 else |
|
221 { |
|
222 int k = 0; |
|
223 |
|
224 int n = rhs_val.length (); |
|
225 |
|
226 for (Pix p = lhs->first (); p != 0; lhs->next (p)) |
|
227 { |
|
228 tree_expression *lhs_elt = lhs->operator () (p); |
|
229 |
|
230 if (lhs_elt) |
|
231 { |
|
232 octave_lvalue ult = lhs_elt->lvalue (); |
|
233 |
|
234 if (error_state) |
|
235 eval_error (); |
|
236 else |
|
237 { |
|
238 octave_value tmp = k < n |
|
239 ? rhs_val(k++) : octave_value (); |
|
240 |
|
241 if (tmp.is_defined ()) |
|
242 { |
|
243 // XXX FIXME XXX -- handle other assignment ops. |
|
244 ult.assign (octave_value::asn_eq, tmp); |
|
245 } |
|
246 else |
|
247 error ("element number %d undefined in return list", k); |
|
248 |
|
249 if (error_state) |
|
250 eval_error (); |
|
251 else if (! Vprint_rhs_assign_val) |
|
252 { |
|
253 octave_value lhs_val = ult.value (); |
|
254 |
|
255 if (! error_state && print_result ()) |
|
256 { |
|
257 if (Vprint_rhs_assign_val) |
|
258 { |
|
259 ostrstream buf; |
|
260 |
|
261 tree_print_code tpc (buf); |
|
262 |
|
263 lhs_elt->accept (tpc); |
|
264 |
|
265 buf << ends; |
|
266 |
|
267 const char *tag = buf.str (); |
|
268 |
|
269 tmp.print_with_name |
|
270 (octave_stdout, tag); |
|
271 |
|
272 delete [] tag; |
|
273 } |
|
274 else |
|
275 lhs_val.print_with_name (octave_stdout, |
|
276 lhs_elt->name ()); |
|
277 } |
|
278 } |
|
279 } |
|
280 } |
|
281 |
|
282 if (error_state) |
|
283 break; |
|
284 } |
|
285 } |
|
286 } |
|
287 else |
|
288 eval_error (); |
|
289 } |
|
290 |
|
291 return rhs_val; |
|
292 } |
|
293 |
|
294 void |
|
295 tree_multi_assignment::eval_error (void) |
|
296 { |
|
297 if (error_state > 0) |
|
298 { |
|
299 int l = line (); |
|
300 int c = column (); |
|
301 |
|
302 if (l != -1 && c != -1) |
|
303 ::error ("evaluating assignment expression near line %d, column %d", |
|
304 l, c); |
|
305 } |
|
306 } |
|
307 |
|
308 void |
|
309 tree_multi_assignment::accept (tree_walker& tw) |
|
310 { |
|
311 tw.visit_multi_assignment (*this); |
|
312 } |
|
313 |
|
314 static int |
|
315 print_rhs_assign_val (void) |
|
316 { |
|
317 Vprint_rhs_assign_val = check_preference ("print_rhs_assign_val"); |
|
318 |
|
319 return 0; |
|
320 } |
|
321 |
|
322 void |
|
323 symbols_of_pt_assign (void) |
|
324 { |
|
325 DEFVAR (print_rhs_assign_val, 0.0, 0, print_rhs_assign_val, |
|
326 "if TRUE, print the right hand side of assignments instead of the left"); |
|
327 } |
|
328 |
|
329 /* |
|
330 ;;; Local Variables: *** |
|
331 ;;; mode: C++ *** |
|
332 ;;; End: *** |
|
333 */ |