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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_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 |
4219
|
54 Octave_map::const_iterator p = map.seek (nm); |
2376
|
55 |
4219
|
56 if (p != map.end ()) |
3933
|
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 |
4247
|
89 octave_struct::subsref (const std::string& type, |
4219
|
90 const std::list<octave_value_list>& idx) |
3933
|
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 { |
4219
|
102 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
103 octave_value_list key_idx = *++p; |
3933
|
104 |
|
105 octave_value_list tmp = dotref (key_idx); |
|
106 |
|
107 if (! error_state) |
|
108 { |
|
109 octave_value_list t_idx = idx.front (); |
|
110 |
|
111 if (t_idx.length () == 1) |
|
112 { |
|
113 idx_vector i = t_idx(0).index_vector (); |
|
114 octave_value_list t = tmp.index (i); |
|
115 |
|
116 retval = (t.length () == 1) ? t(0) : octave_value (t); |
|
117 |
|
118 // We handled two index elements, so tell |
|
119 // next_subsref to skip both of them. |
|
120 |
|
121 skip++; |
|
122 } |
|
123 else |
|
124 gripe_invalid_index (); |
|
125 } |
|
126 } |
|
127 else |
|
128 { |
|
129 octave_value_list t_idx = idx.front (); |
|
130 |
|
131 if (t_idx.length () == 1) |
|
132 { |
|
133 idx_vector i = t_idx(0).index_vector (); |
|
134 retval = map.index (i); |
|
135 } |
|
136 else |
|
137 gripe_invalid_index (); |
|
138 } |
|
139 } |
|
140 break; |
|
141 |
|
142 case '.': |
|
143 { |
|
144 octave_value_list t = dotref (idx.front ()); |
|
145 |
|
146 retval = (t.length () == 1) ? t(0) : octave_value (t); |
|
147 } |
|
148 break; |
|
149 |
|
150 case '{': |
|
151 gripe_invalid_index_type (type_name (), type[0]); |
|
152 break; |
|
153 |
|
154 default: |
|
155 panic_impossible (); |
|
156 } |
|
157 |
|
158 if (! error_state) |
|
159 retval = retval.next_subsref (type, idx, skip); |
|
160 |
|
161 return retval; |
|
162 } |
|
163 |
|
164 octave_value |
|
165 octave_struct::numeric_conv (const octave_value_list& val, |
|
166 const std::string& type) |
|
167 { |
|
168 octave_value retval; |
|
169 |
|
170 if (val.length () == 1) |
|
171 { |
|
172 retval = val(0); |
|
173 |
|
174 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) |
|
175 retval = Octave_map (); |
|
176 } |
|
177 else |
|
178 gripe_invalid_index_for_assignment (); |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 octave_value |
4247
|
184 octave_struct::subsasgn (const std::string& type, |
4219
|
185 const std::list<octave_value_list>& idx, |
3933
|
186 const octave_value& rhs) |
2376
|
187 { |
3933
|
188 octave_value retval; |
|
189 |
|
190 int n = type.length (); |
|
191 |
|
192 octave_value t_rhs = rhs; |
|
193 |
|
194 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) |
|
195 { |
|
196 switch (type[0]) |
|
197 { |
|
198 case '(': |
|
199 { |
|
200 if (type.length () > 1 && type[1] == '.') |
|
201 { |
4219
|
202 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
203 octave_value_list t_idx = *p; |
3933
|
204 |
|
205 if (t_idx.length () == 1) |
|
206 { |
4219
|
207 octave_value_list key_idx = *++p; |
3933
|
208 |
|
209 assert (key_idx.length () == 1); |
|
210 |
|
211 std::string key = key_idx(0).string_value (); |
|
212 |
|
213 octave_value u; |
|
214 |
|
215 if (! map.contains (key)) |
|
216 u = octave_value::empty_conv (type.substr (2), rhs); |
|
217 else |
|
218 { |
|
219 octave_value_list map_val = map[key]; |
|
220 |
|
221 octave_value_list t_idx = idx.front (); |
|
222 |
|
223 idx_vector i = t_idx(0).index_vector (); |
|
224 |
|
225 octave_value_list map_elt = map_val.index (i, true); |
|
226 |
|
227 u = numeric_conv (map_elt, type.substr (2)); |
|
228 } |
|
229 |
|
230 if (! error_state) |
|
231 { |
4219
|
232 std::list<octave_value_list> next_idx (idx); |
3933
|
233 |
|
234 // We handled two index elements, so subsasgn to |
|
235 // needs to skip both of them. |
|
236 |
4219
|
237 next_idx.erase (next_idx.begin ()); |
|
238 next_idx.erase (next_idx.begin ()); |
3933
|
239 |
4059
|
240 u.make_unique (); |
|
241 |
3933
|
242 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); |
|
243 } |
|
244 } |
|
245 else |
|
246 gripe_invalid_index_for_assignment (); |
|
247 } |
|
248 else |
|
249 gripe_invalid_index_for_assignment (); |
|
250 } |
|
251 break; |
|
252 |
|
253 case '.': |
|
254 { |
|
255 octave_value_list key_idx = idx.front (); |
|
256 |
|
257 assert (key_idx.length () == 1); |
|
258 |
|
259 std::string key = key_idx(0).string_value (); |
|
260 |
|
261 octave_value u; |
|
262 |
|
263 if (! map.contains (key)) |
|
264 u = octave_value::empty_conv (type.substr (1), rhs); |
|
265 else |
|
266 { |
|
267 octave_value_list map_val = map[key]; |
|
268 |
|
269 u = numeric_conv (map_val, type.substr (1)); |
|
270 } |
|
271 |
|
272 if (! error_state) |
|
273 { |
4219
|
274 std::list<octave_value_list> next_idx (idx); |
3933
|
275 |
4219
|
276 next_idx.erase (next_idx.begin ()); |
3933
|
277 |
4059
|
278 u.make_unique (); |
|
279 |
3933
|
280 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); |
|
281 } |
|
282 } |
|
283 break; |
|
284 |
|
285 case '{': |
|
286 gripe_invalid_index_type (type_name (), type[0]); |
|
287 break; |
|
288 |
|
289 default: |
|
290 panic_impossible (); |
|
291 } |
|
292 } |
|
293 |
|
294 if (! error_state) |
|
295 { |
|
296 switch (type[0]) |
|
297 { |
|
298 case '(': |
|
299 { |
|
300 if (n > 1 && type[1] == '.') |
|
301 { |
4219
|
302 std::list<octave_value_list>::const_iterator p = idx.begin (); |
|
303 octave_value_list key_idx = *++p; |
3933
|
304 |
|
305 assert (key_idx.length () == 1); |
|
306 |
|
307 std::string key = key_idx(0).string_value (); |
|
308 |
|
309 if (! error_state) |
|
310 { |
|
311 octave_value_list t_idx = idx.front (); |
|
312 |
|
313 if (t_idx.length () == 1) |
|
314 { |
|
315 idx_vector i = t_idx(0).index_vector (); |
|
316 |
|
317 map.assign (i, key, t_rhs); |
|
318 |
|
319 if (! error_state) |
|
320 retval = octave_value (this, count + 1); |
|
321 else |
|
322 gripe_failed_assignment (); |
|
323 } |
|
324 else |
|
325 gripe_invalid_index_for_assignment (); |
|
326 } |
|
327 else |
|
328 gripe_failed_assignment (); |
|
329 } |
|
330 else |
4197
|
331 { |
|
332 octave_value_list t_idx = idx.front (); |
|
333 |
|
334 if (t_idx.length () == 1) |
|
335 { |
|
336 idx_vector i = t_idx(0).index_vector (); |
|
337 |
|
338 Octave_map rhs_map = t_rhs.map_value (); |
|
339 |
|
340 if (! error_state) |
|
341 { |
|
342 map.assign (i, rhs_map); |
|
343 |
|
344 if (! error_state) |
|
345 retval = octave_value (this, count + 1); |
|
346 else |
|
347 gripe_failed_assignment (); |
|
348 } |
|
349 else |
|
350 error ("invalid structure assignment"); |
|
351 } |
|
352 } |
3933
|
353 } |
|
354 break; |
|
355 |
|
356 case '.': |
|
357 { |
|
358 octave_value_list key_idx = idx.front (); |
|
359 |
|
360 assert (key_idx.length () == 1); |
|
361 |
|
362 std::string key = key_idx(0).string_value (); |
|
363 |
|
364 map.assign (key, t_rhs); |
|
365 |
|
366 if (! error_state) |
|
367 retval = octave_value (this, count + 1); |
|
368 else |
|
369 gripe_failed_assignment (); |
|
370 } |
|
371 break; |
|
372 |
|
373 case '{': |
|
374 gripe_invalid_index_type (type_name (), type[0]); |
|
375 break; |
|
376 |
|
377 default: |
|
378 panic_impossible (); |
|
379 } |
|
380 } |
|
381 else |
|
382 gripe_failed_assignment (); |
|
383 |
|
384 return retval; |
2376
|
385 } |
|
386 |
|
387 void |
3523
|
388 octave_struct::print (std::ostream& os, bool) const |
2901
|
389 { |
|
390 print_raw (os); |
|
391 } |
|
392 |
|
393 void |
3523
|
394 octave_struct::print_raw (std::ostream& os, bool) const |
2376
|
395 { |
|
396 // XXX FIXME XXX -- would be nice to print the output in some |
|
397 // standard order. Maybe all substructures first, maybe |
|
398 // alphabetize entries, etc. |
|
399 |
2985
|
400 unwind_protect::begin_frame ("octave_struct_print"); |
2376
|
401 |
|
402 unwind_protect_int (Vstruct_levels_to_print); |
|
403 |
3961
|
404 if (Vstruct_levels_to_print >= 0) |
2376
|
405 { |
3961
|
406 bool print_keys_only = (Vstruct_levels_to_print == 0); |
|
407 |
|
408 Vstruct_levels_to_print--; |
|
409 |
2901
|
410 indent (os); |
|
411 os << "{"; |
|
412 newline (os); |
2376
|
413 |
2901
|
414 increment_indent_level (); |
2376
|
415 |
3932
|
416 int n = map.array_length (); |
|
417 |
4219
|
418 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) |
2376
|
419 { |
3523
|
420 std::string key = map.key (p); |
3932
|
421 octave_value_list val = map.contents (p); |
2376
|
422 |
4121
|
423 octave_value tmp = (n == 1) ? val(0) : octave_value (val); |
3961
|
424 |
|
425 if (print_keys_only) |
3932
|
426 { |
3961
|
427 indent (os); |
|
428 os << key << ": " << tmp.type_name (); |
|
429 newline (os); |
3932
|
430 } |
3961
|
431 else |
4121
|
432 tmp.print_with_name (os, key); |
2376
|
433 } |
|
434 |
2901
|
435 decrement_indent_level (); |
2376
|
436 |
2901
|
437 indent (os); |
|
438 os << "}"; |
|
439 newline (os); |
2376
|
440 } |
|
441 else |
2901
|
442 { |
3961
|
443 indent (os); |
|
444 os << "<structure>"; |
2901
|
445 newline (os); |
|
446 } |
2376
|
447 |
2985
|
448 unwind_protect::run_frame ("octave_struct_print"); |
2376
|
449 } |
|
450 |
2901
|
451 bool |
3523
|
452 octave_struct::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
453 { |
3961
|
454 bool retval = false; |
|
455 |
2901
|
456 indent (os); |
3961
|
457 |
|
458 if (Vstruct_levels_to_print < 0) |
|
459 os << name << " = "; |
|
460 else |
|
461 { |
|
462 os << name << " ="; |
|
463 newline (os); |
|
464 retval = true; |
|
465 } |
|
466 |
|
467 return retval; |
2901
|
468 } |
|
469 |
2376
|
470 /* |
|
471 ;;; Local Variables: *** |
|
472 ;;; mode: C++ *** |
|
473 ;;; End: *** |
|
474 */ |