2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 |
4066
|
23 #if defined (__GNUG__) && ! defined (NO_PRAGMA_INTERFACE_IMPLEMENTATION) |
2376
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
2376
|
32 |
3933
|
33 #include "Cell.h" |
2376
|
34 #include "error.h" |
2979
|
35 #include "oct-lvalue.h" |
3932
|
36 #include "ov-list.h" |
2376
|
37 #include "ov-struct.h" |
|
38 #include "unwind-prot.h" |
2948
|
39 #include "variables.h" |
2376
|
40 |
3219
|
41 DEFINE_OCTAVE_ALLOCATOR(octave_struct); |
2376
|
42 |
3219
|
43 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct"); |
2376
|
44 |
3933
|
45 octave_value_list |
|
46 octave_struct::dotref (const octave_value_list& idx) |
2962
|
47 { |
3933
|
48 octave_value_list retval; |
|
49 |
|
50 assert (idx.length () == 1); |
2962
|
51 |
3933
|
52 std::string nm = idx(0).string_value (); |
|
53 |
|
54 Pix p = map.seek (nm); |
2376
|
55 |
3933
|
56 if (p) |
|
57 retval = map.contents (p); |
|
58 else |
2376
|
59 error ("structure has no member `%s'", nm.c_str ()); |
|
60 |
|
61 return retval; |
|
62 } |
|
63 |
3933
|
64 static void |
|
65 gripe_invalid_index (void) |
|
66 { |
|
67 error ("invalid index for structure array"); |
|
68 } |
|
69 |
|
70 static void |
|
71 gripe_invalid_index_for_assignment (void) |
|
72 { |
|
73 error ("invalid index for structure array assignment"); |
|
74 } |
|
75 |
|
76 static void |
|
77 gripe_invalid_index_type (const std::string& nm, char t) |
|
78 { |
|
79 error ("%s cannot be indexed with %c", nm.c_str (), t); |
|
80 } |
|
81 |
|
82 static void |
|
83 gripe_failed_assignment (void) |
|
84 { |
|
85 error ("assignment to structure element failed"); |
|
86 } |
|
87 |
|
88 octave_value |
|
89 octave_struct::subsref (const std::string type, |
|
90 const SLList<octave_value_list>& idx) |
|
91 { |
|
92 octave_value retval; |
|
93 |
|
94 int skip = 1; |
|
95 |
|
96 switch (type[0]) |
|
97 { |
|
98 case '(': |
|
99 { |
|
100 if (type.length () > 1 && type[1] == '.') |
|
101 { |
|
102 Pix p = idx.first (); |
|
103 idx.next (p); |
|
104 octave_value_list key_idx = idx(p); |
|
105 |
|
106 octave_value_list tmp = dotref (key_idx); |
|
107 |
|
108 if (! error_state) |
|
109 { |
|
110 octave_value_list t_idx = idx.front (); |
|
111 |
|
112 if (t_idx.length () == 1) |
|
113 { |
|
114 idx_vector i = t_idx(0).index_vector (); |
|
115 octave_value_list t = tmp.index (i); |
|
116 |
|
117 retval = (t.length () == 1) ? t(0) : octave_value (t); |
|
118 |
|
119 // We handled two index elements, so tell |
|
120 // next_subsref to skip both of them. |
|
121 |
|
122 skip++; |
|
123 } |
|
124 else |
|
125 gripe_invalid_index (); |
|
126 } |
|
127 } |
|
128 else |
|
129 { |
|
130 octave_value_list t_idx = idx.front (); |
|
131 |
|
132 if (t_idx.length () == 1) |
|
133 { |
|
134 idx_vector i = t_idx(0).index_vector (); |
|
135 retval = map.index (i); |
|
136 } |
|
137 else |
|
138 gripe_invalid_index (); |
|
139 } |
|
140 } |
|
141 break; |
|
142 |
|
143 case '.': |
|
144 { |
|
145 octave_value_list t = dotref (idx.front ()); |
|
146 |
|
147 retval = (t.length () == 1) ? t(0) : octave_value (t); |
|
148 } |
|
149 break; |
|
150 |
|
151 case '{': |
|
152 gripe_invalid_index_type (type_name (), type[0]); |
|
153 break; |
|
154 |
|
155 default: |
|
156 panic_impossible (); |
|
157 } |
|
158 |
|
159 if (! error_state) |
|
160 retval = retval.next_subsref (type, idx, skip); |
|
161 |
|
162 return retval; |
|
163 } |
|
164 |
|
165 octave_value |
|
166 octave_struct::numeric_conv (const octave_value_list& val, |
|
167 const std::string& type) |
|
168 { |
|
169 octave_value retval; |
|
170 |
|
171 if (val.length () == 1) |
|
172 { |
|
173 retval = val(0); |
|
174 |
|
175 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) |
|
176 retval = Octave_map (); |
|
177 } |
|
178 else |
|
179 gripe_invalid_index_for_assignment (); |
|
180 |
|
181 return retval; |
|
182 } |
|
183 |
|
184 octave_value |
|
185 octave_struct::subsasgn (const std::string type, |
|
186 const SLList<octave_value_list>& idx, |
|
187 const octave_value& rhs) |
2376
|
188 { |
3933
|
189 octave_value retval; |
|
190 |
|
191 int n = type.length (); |
|
192 |
|
193 octave_value t_rhs = rhs; |
|
194 |
|
195 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) |
|
196 { |
|
197 switch (type[0]) |
|
198 { |
|
199 case '(': |
|
200 { |
|
201 if (type.length () > 1 && type[1] == '.') |
|
202 { |
|
203 Pix p = idx.first (); |
|
204 octave_value_list t_idx = idx(p); |
|
205 |
|
206 if (t_idx.length () == 1) |
|
207 { |
|
208 idx.next (p); |
|
209 octave_value_list key_idx = idx(p); |
|
210 |
|
211 assert (key_idx.length () == 1); |
|
212 |
|
213 std::string key = key_idx(0).string_value (); |
|
214 |
|
215 octave_value u; |
|
216 |
|
217 if (! map.contains (key)) |
|
218 u = octave_value::empty_conv (type.substr (2), rhs); |
|
219 else |
|
220 { |
|
221 octave_value_list map_val = map[key]; |
|
222 |
|
223 octave_value_list t_idx = idx.front (); |
|
224 |
|
225 idx_vector i = t_idx(0).index_vector (); |
|
226 |
|
227 octave_value_list map_elt = map_val.index (i, true); |
|
228 |
|
229 u = numeric_conv (map_elt, type.substr (2)); |
|
230 } |
|
231 |
|
232 if (! error_state) |
|
233 { |
|
234 SLList<octave_value_list> next_idx (idx); |
|
235 |
|
236 // We handled two index elements, so subsasgn to |
|
237 // needs to skip both of them. |
|
238 |
|
239 next_idx.remove_front (); |
|
240 next_idx.remove_front (); |
|
241 |
4059
|
242 u.make_unique (); |
|
243 |
3933
|
244 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); |
|
245 } |
|
246 } |
|
247 else |
|
248 gripe_invalid_index_for_assignment (); |
|
249 } |
|
250 else |
|
251 gripe_invalid_index_for_assignment (); |
|
252 } |
|
253 break; |
|
254 |
|
255 case '.': |
|
256 { |
|
257 octave_value_list key_idx = idx.front (); |
|
258 |
|
259 assert (key_idx.length () == 1); |
|
260 |
|
261 std::string key = key_idx(0).string_value (); |
|
262 |
|
263 octave_value u; |
|
264 |
|
265 if (! map.contains (key)) |
|
266 u = octave_value::empty_conv (type.substr (1), rhs); |
|
267 else |
|
268 { |
|
269 octave_value_list map_val = map[key]; |
|
270 |
|
271 u = numeric_conv (map_val, type.substr (1)); |
|
272 } |
|
273 |
|
274 if (! error_state) |
|
275 { |
|
276 SLList<octave_value_list> next_idx (idx); |
|
277 |
|
278 next_idx.remove_front (); |
|
279 |
4059
|
280 u.make_unique (); |
|
281 |
3933
|
282 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); |
|
283 } |
|
284 } |
|
285 break; |
|
286 |
|
287 case '{': |
|
288 gripe_invalid_index_type (type_name (), type[0]); |
|
289 break; |
|
290 |
|
291 default: |
|
292 panic_impossible (); |
|
293 } |
|
294 } |
|
295 |
|
296 if (! error_state) |
|
297 { |
|
298 switch (type[0]) |
|
299 { |
|
300 case '(': |
|
301 { |
|
302 if (n > 1 && type[1] == '.') |
|
303 { |
|
304 Pix p = idx.first (); |
|
305 idx.next (p); |
|
306 octave_value_list key_idx = idx(p); |
|
307 |
|
308 assert (key_idx.length () == 1); |
|
309 |
|
310 std::string key = key_idx(0).string_value (); |
|
311 |
|
312 if (! error_state) |
|
313 { |
|
314 octave_value_list t_idx = idx.front (); |
|
315 |
|
316 if (t_idx.length () == 1) |
|
317 { |
|
318 idx_vector i = t_idx(0).index_vector (); |
|
319 |
|
320 map.assign (i, key, t_rhs); |
|
321 |
|
322 if (! error_state) |
|
323 retval = octave_value (this, count + 1); |
|
324 else |
|
325 gripe_failed_assignment (); |
|
326 } |
|
327 else |
|
328 gripe_invalid_index_for_assignment (); |
|
329 } |
|
330 else |
|
331 gripe_failed_assignment (); |
|
332 } |
|
333 else |
|
334 gripe_invalid_index_for_assignment (); |
|
335 } |
|
336 break; |
|
337 |
|
338 case '.': |
|
339 { |
|
340 octave_value_list key_idx = idx.front (); |
|
341 |
|
342 assert (key_idx.length () == 1); |
|
343 |
|
344 std::string key = key_idx(0).string_value (); |
|
345 |
|
346 map.assign (key, t_rhs); |
|
347 |
|
348 if (! error_state) |
|
349 retval = octave_value (this, count + 1); |
|
350 else |
|
351 gripe_failed_assignment (); |
|
352 } |
|
353 break; |
|
354 |
|
355 case '{': |
|
356 gripe_invalid_index_type (type_name (), type[0]); |
|
357 break; |
|
358 |
|
359 default: |
|
360 panic_impossible (); |
|
361 } |
|
362 } |
|
363 else |
|
364 gripe_failed_assignment (); |
|
365 |
|
366 return retval; |
2376
|
367 } |
|
368 |
|
369 void |
3523
|
370 octave_struct::print (std::ostream& os, bool) const |
2901
|
371 { |
|
372 print_raw (os); |
|
373 } |
|
374 |
|
375 void |
3523
|
376 octave_struct::print_raw (std::ostream& os, bool) const |
2376
|
377 { |
|
378 // XXX FIXME XXX -- would be nice to print the output in some |
|
379 // standard order. Maybe all substructures first, maybe |
|
380 // alphabetize entries, etc. |
|
381 |
2985
|
382 unwind_protect::begin_frame ("octave_struct_print"); |
2376
|
383 |
|
384 unwind_protect_int (Vstruct_levels_to_print); |
|
385 |
3961
|
386 if (Vstruct_levels_to_print >= 0) |
2376
|
387 { |
3961
|
388 bool print_keys_only = (Vstruct_levels_to_print == 0); |
|
389 |
|
390 Vstruct_levels_to_print--; |
|
391 |
2901
|
392 indent (os); |
|
393 os << "{"; |
|
394 newline (os); |
2376
|
395 |
2901
|
396 increment_indent_level (); |
2376
|
397 |
3932
|
398 int n = map.array_length (); |
|
399 |
2901
|
400 for (Pix p = map.first (); p; map.next (p)) |
2376
|
401 { |
3523
|
402 std::string key = map.key (p); |
3932
|
403 octave_value_list val = map.contents (p); |
2376
|
404 |
3961
|
405 octave_value tmp = (n == 1) ? val(0) : octave_list (val); |
|
406 |
|
407 if (print_keys_only) |
3932
|
408 { |
3961
|
409 indent (os); |
|
410 os << key << ": " << tmp.type_name (); |
|
411 newline (os); |
3932
|
412 } |
3961
|
413 else |
|
414 val(0).print_with_name (os, key); |
2376
|
415 } |
|
416 |
2901
|
417 decrement_indent_level (); |
2376
|
418 |
2901
|
419 indent (os); |
|
420 os << "}"; |
|
421 newline (os); |
2376
|
422 } |
|
423 else |
2901
|
424 { |
3961
|
425 indent (os); |
|
426 os << "<structure>"; |
2901
|
427 newline (os); |
|
428 } |
2376
|
429 |
2985
|
430 unwind_protect::run_frame ("octave_struct_print"); |
2376
|
431 } |
|
432 |
2901
|
433 bool |
3523
|
434 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
435 { |
3961
|
436 bool retval = false; |
|
437 |
2901
|
438 indent (os); |
3961
|
439 |
|
440 if (Vstruct_levels_to_print < 0) |
|
441 os << name << " = "; |
|
442 else |
|
443 { |
|
444 os << name << " ="; |
|
445 newline (os); |
|
446 retval = true; |
|
447 } |
|
448 |
|
449 return retval; |
2901
|
450 } |
|
451 |
2376
|
452 /* |
|
453 ;;; Local Variables: *** |
|
454 ;;; mode: C++ *** |
|
455 ;;; End: *** |
|
456 */ |