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 |
3933
|
31 #include "Cell.h" |
2980
|
32 #include "error.h" |
3930
|
33 #include "oct-map.h" |
2980
|
34 #include "oct-obj.h" |
|
35 #include "oct-lvalue.h" |
|
36 #include "ov.h" |
3930
|
37 #include "pager.h" |
2982
|
38 #include "pt-arg-list.h" |
3930
|
39 #include "pt-bp.h" |
2980
|
40 #include "pt-idx.h" |
|
41 #include "pt-walk.h" |
3930
|
42 #include "utils.h" |
|
43 #include "variables.h" |
2980
|
44 |
3933
|
45 #include "SLList.cc" |
|
46 |
|
47 template class SLNode<string_vector>; |
|
48 template class SLList<string_vector>; |
|
49 |
2980
|
50 // Index expressions. |
|
51 |
3546
|
52 tree_index_expression::tree_index_expression (tree_expression *e, |
|
53 tree_argument_list *lst, |
3933
|
54 int l, int c, char t) |
|
55 : tree_expression (l, c), expr (e), args (), type (), arg_nm () |
|
56 { |
|
57 append (lst, t); |
|
58 } |
3215
|
59 |
3930
|
60 tree_index_expression::tree_index_expression (tree_expression *e, |
|
61 const std::string& n, |
|
62 int l = -1, int c = -1) |
3933
|
63 : tree_expression (l, c), expr (e), args (), type (), arg_nm () |
|
64 { |
|
65 append (n); |
|
66 } |
|
67 |
|
68 void |
|
69 tree_index_expression::append (tree_argument_list *lst, char t) |
|
70 { |
|
71 args.append (lst); |
|
72 type.append (1, t); |
|
73 arg_nm.append (lst ? lst->get_arg_names () : string_vector ()); |
|
74 } |
|
75 |
|
76 void |
|
77 tree_index_expression::append (const std::string& n) |
|
78 { |
|
79 args.append (static_cast<tree_argument_list *> (0)); |
|
80 type.append ("."); |
|
81 arg_nm.append (n); |
|
82 } |
3930
|
83 |
2980
|
84 tree_index_expression::~tree_index_expression (void) |
|
85 { |
|
86 delete expr; |
3933
|
87 |
|
88 while (! args.empty ()) |
|
89 { |
|
90 tree_argument_list *t = args.remove_front (); |
|
91 delete t; |
|
92 } |
2980
|
93 } |
|
94 |
2991
|
95 // This is useful for printing the name of the variable in an indexed |
|
96 // assignment. |
|
97 |
3536
|
98 std::string |
2991
|
99 tree_index_expression::name (void) const |
|
100 { |
3930
|
101 // ??? FIXME ??? |
|
102 std::string xname = expr->name (); |
|
103 |
3933
|
104 return (type[0] != '.' || xname == "<unknown>") |
|
105 ? xname : xname + "." + arg_nm.front ()(0); |
|
106 } |
|
107 |
|
108 static Cell |
|
109 make_subs_cell (tree_argument_list *args, const string_vector& arg_nm) |
|
110 { |
|
111 Cell retval; |
|
112 |
|
113 octave_value_list arg_values; |
|
114 |
|
115 if (args) |
|
116 arg_values = args->convert_to_const_vector (); |
|
117 |
|
118 if (! error_state) |
|
119 { |
|
120 int n = arg_values.length (); |
|
121 |
|
122 if (n > 0) |
|
123 { |
|
124 arg_values.stash_name_tags (arg_nm); |
|
125 |
|
126 retval.resize (1, n); |
|
127 |
|
128 for (int i = 0; i < n; i++) |
|
129 retval(0,i) = arg_values(i); |
|
130 } |
|
131 } |
|
132 |
|
133 return retval; |
|
134 } |
|
135 |
|
136 static inline octave_value_list |
|
137 make_value_list (tree_argument_list *args, const string_vector& arg_nm) |
|
138 { |
|
139 octave_value_list retval; |
|
140 |
|
141 if (args) |
|
142 retval = args->convert_to_const_vector (); |
|
143 |
|
144 if (! error_state) |
|
145 { |
|
146 int n = retval.length (); |
|
147 |
|
148 if (n > 0) |
|
149 retval.stash_name_tags (arg_nm); |
|
150 } |
|
151 |
|
152 return retval; |
|
153 } |
|
154 |
|
155 Octave_map |
|
156 tree_index_expression::make_arg_struct (void) const |
|
157 { |
|
158 int n = args.length (); |
|
159 |
|
160 octave_value_list subs_list (n, octave_value ()); |
|
161 octave_value_list type_list (n, octave_value ()); |
|
162 |
|
163 Pix p_args = args.first (); |
|
164 Pix p_arg_nm = arg_nm.first (); |
|
165 |
|
166 Octave_map m; |
|
167 |
|
168 for (int i = 0; i < n; i++) |
|
169 { |
|
170 switch (type[i]) |
|
171 { |
|
172 case '(': |
|
173 subs_list(i) = make_subs_cell (args(p_args), arg_nm(p_arg_nm)); |
|
174 break; |
|
175 |
|
176 case '{': |
|
177 subs_list(i) = make_subs_cell (args(p_args), arg_nm(p_arg_nm)); |
|
178 break; |
|
179 |
|
180 case '.': |
|
181 subs_list(i) = arg_nm(p_arg_nm)(0); |
|
182 break; |
|
183 |
|
184 default: |
|
185 panic_impossible (); |
|
186 } |
|
187 |
|
188 if (error_state) |
|
189 return m; |
|
190 |
|
191 args.next (p_args); |
|
192 arg_nm.next (p_arg_nm); |
|
193 } |
|
194 |
|
195 m ["subs"] = subs_list; |
|
196 m ["type"] = type_list; |
|
197 |
|
198 return m; |
2991
|
199 } |
|
200 |
2980
|
201 octave_value_list |
|
202 tree_index_expression::rvalue (int nargout) |
|
203 { |
|
204 octave_value_list retval; |
|
205 |
|
206 if (error_state) |
|
207 return retval; |
|
208 |
|
209 octave_value tmp = expr->rvalue (); |
|
210 |
|
211 if (! error_state) |
|
212 { |
3933
|
213 SLList<octave_value_list> idx; |
3930
|
214 |
3933
|
215 int n = args.length (); |
|
216 |
|
217 Pix p_args = args.first (); |
|
218 Pix p_arg_nm = arg_nm.first (); |
3930
|
219 |
3933
|
220 for (int i = 0; i < n; i++) |
|
221 { |
|
222 switch (type[i]) |
|
223 { |
|
224 case '(': |
|
225 idx.append (make_value_list (args(p_args), arg_nm(p_arg_nm))); |
|
226 break; |
3930
|
227 |
3933
|
228 case '{': |
|
229 idx.append (make_value_list (args(p_args), arg_nm(p_arg_nm))); |
|
230 break; |
2980
|
231 |
3933
|
232 case '.': |
|
233 idx.append (arg_nm(p_arg_nm)(0)); |
|
234 break; |
3930
|
235 |
3933
|
236 default: |
|
237 panic_impossible (); |
|
238 } |
3930
|
239 |
3933
|
240 if (error_state) |
|
241 break; |
|
242 |
|
243 args.next (p_args); |
|
244 arg_nm.next (p_arg_nm); |
2980
|
245 } |
3933
|
246 |
|
247 if (! error_state) |
|
248 retval = tmp.subsref (type, idx, nargout); |
2980
|
249 } |
|
250 |
|
251 return retval; |
|
252 } |
|
253 |
|
254 octave_value |
|
255 tree_index_expression::rvalue (void) |
|
256 { |
|
257 octave_value retval; |
|
258 |
|
259 octave_value_list tmp = rvalue (1); |
|
260 |
|
261 if (! tmp.empty ()) |
|
262 retval = tmp(0); |
|
263 |
|
264 return retval; |
|
265 } |
|
266 |
|
267 octave_lvalue |
|
268 tree_index_expression::lvalue (void) |
|
269 { |
|
270 octave_lvalue retval; |
|
271 |
3933
|
272 SLList<octave_value_list> idx; |
|
273 |
|
274 int n = args.length (); |
|
275 |
|
276 Pix p_args = args.first (); |
|
277 Pix p_arg_nm = arg_nm.first (); |
|
278 |
|
279 for (int i = 0; i < n; i++) |
|
280 { |
|
281 switch (type[i]) |
|
282 { |
|
283 case '(': |
|
284 idx.append (make_value_list (args(p_args), arg_nm(p_arg_nm))); |
|
285 break; |
|
286 |
|
287 case '{': |
|
288 idx.append (make_value_list (args(p_args), arg_nm(p_arg_nm))); |
|
289 break; |
|
290 |
|
291 case '.': |
|
292 idx.append (arg_nm(p_arg_nm)(0)); |
|
293 break; |
|
294 |
|
295 default: |
|
296 panic_impossible (); |
|
297 } |
|
298 |
|
299 if (error_state) |
|
300 break; |
|
301 |
|
302 args.next (p_args); |
|
303 arg_nm.next (p_arg_nm); |
|
304 } |
|
305 |
2980
|
306 if (! error_state) |
|
307 { |
3933
|
308 retval = expr->lvalue (); |
3930
|
309 |
3933
|
310 if (! error_state) |
|
311 retval.set_index (type, idx); |
2980
|
312 } |
|
313 |
|
314 return retval; |
|
315 } |
|
316 |
|
317 void |
|
318 tree_index_expression::eval_error (void) |
|
319 { |
|
320 if (error_state > 0) |
|
321 { |
|
322 int l = line (); |
|
323 int c = column (); |
|
324 |
3930
|
325 const char *type_str; |
|
326 |
3933
|
327 if (type[0] == '.') |
3930
|
328 type_str = "structure reference operator"; |
3933
|
329 else if (args.front ()) |
3930
|
330 type_str = "index expression"; |
|
331 else |
|
332 type_str = "expression"; |
|
333 |
2980
|
334 if (l != -1 && c != -1) |
3930
|
335 ::error ("evaluating %s near line %d, column %d", type_str, l, c); |
2980
|
336 else |
3930
|
337 ::error ("evaluating %s", type_str); |
2980
|
338 } |
|
339 } |
|
340 |
|
341 void |
|
342 tree_index_expression::accept (tree_walker& tw) |
|
343 { |
|
344 tw.visit_index_expression (*this); |
|
345 } |
|
346 |
|
347 /* |
|
348 ;;; Local Variables: *** |
|
349 ;;; mode: C++ *** |
|
350 ;;; End: *** |
|
351 */ |