1
|
1 /* |
|
2 |
1827
|
3 Copyright (C) 1996 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1299
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1558
|
31 #include <cctype> |
1355
|
32 #include <cstring> |
|
33 |
1728
|
34 #include <string> |
|
35 |
1558
|
36 #include <fstream.h> |
581
|
37 #include <iostream.h> |
1199
|
38 #include <strstream.h> |
581
|
39 |
1742
|
40 #include <SLList.h> |
|
41 |
1558
|
42 #include "mx-base.h" |
|
43 #include "Range.h" |
1755
|
44 #include "str-vec.h" |
1558
|
45 |
|
46 #include "arith-ops.h" |
1
|
47 #include "error.h" |
|
48 #include "gripes.h" |
1558
|
49 #include "idx-vector.h" |
1742
|
50 #include "mappers.h" |
747
|
51 #include "oct-map.h" |
1742
|
52 #include "oct-obj.h" |
1355
|
53 #include "pager.h" |
1558
|
54 #include "pr-output.h" |
|
55 #include "sysdep.h" |
1742
|
56 #include "pt-const.h" |
1558
|
57 #include "unwind-prot.h" |
1355
|
58 #include "user-prefs.h" |
1430
|
59 #include "utils.h" |
1558
|
60 #include "variables.h" |
|
61 |
|
62 #ifndef TC_REP |
|
63 #define TC_REP tree_constant::tree_constant_rep |
|
64 #endif |
|
65 |
|
66 #ifndef MAX |
|
67 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
68 #endif |
|
69 |
|
70 #ifndef TC_REP |
|
71 #define TC_REP tree_constant::tree_constant_rep |
|
72 #endif |
|
73 |
|
74 #ifndef MAX |
|
75 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
76 #endif |
|
77 |
|
78 // The following three variables could be made static members of the |
|
79 // TC_REP class. |
|
80 |
|
81 // Pointer to the blocks of memory we manage. |
|
82 static TC_REP *tc_rep_newlist = 0; |
|
83 |
|
84 // Multiplier for allocating new blocks. |
|
85 static const int tc_rep_newlist_grow_size = 128; |
|
86 |
|
87 // Indentation level for structures. |
1973
|
88 static int struct_indent = 0; |
1558
|
89 |
|
90 static void |
1973
|
91 increment_struct_indent (void) |
1558
|
92 { |
1973
|
93 struct_indent += 2; |
1558
|
94 } |
|
95 |
|
96 static void |
1973
|
97 decrement_struct_indent (void) |
1558
|
98 { |
1973
|
99 struct_indent -= 2; |
1558
|
100 } |
|
101 |
1827
|
102 static bool |
1558
|
103 any_element_is_complex (const ComplexMatrix& a) |
|
104 { |
|
105 int nr = a.rows (); |
|
106 int nc = a.columns (); |
1827
|
107 |
1558
|
108 for (int j = 0; j < nc; j++) |
|
109 for (int i = 0; i < nr; i++) |
|
110 if (imag (a.elem (i, j)) != 0.0) |
1827
|
111 return true; |
|
112 |
|
113 return false; |
1558
|
114 } |
747
|
115 |
1168
|
116 // The following three variables could be made static members of the |
|
117 // tree_constant class. |
|
118 |
|
119 // Pointer to the blocks of memory we manage. |
1299
|
120 static tree_constant *tc_newlist = 0; |
1168
|
121 |
|
122 // Multiplier for allocating new blocks. |
1299
|
123 static const int tc_newlist_grow_size = 128; |
1168
|
124 |
747
|
125 Octave_map |
|
126 tree_constant::map_value (void) const |
|
127 { |
|
128 return rep->map_value (); |
|
129 } |
1
|
130 |
|
131 tree_constant::~tree_constant (void) |
|
132 { |
|
133 #if defined (MDEBUG) |
|
134 cerr << "~tree_constant: rep: " << rep |
|
135 << " rep->count: " << rep->count << "\n"; |
|
136 #endif |
|
137 |
|
138 if (--rep->count <= 0) |
|
139 { |
|
140 delete rep; |
531
|
141 rep = 0; |
1
|
142 } |
|
143 } |
|
144 |
|
145 void * |
|
146 tree_constant::operator new (size_t size) |
|
147 { |
1168
|
148 assert (size == sizeof (tree_constant)); |
|
149 |
1299
|
150 if (! tc_newlist) |
|
151 { |
|
152 int block_size = tc_newlist_grow_size * sizeof (tree_constant); |
|
153 tc_newlist = (tree_constant *) new char [block_size]; |
|
154 |
1321
|
155 int i = 0; |
|
156 |
|
157 for (i = 0; i < tc_newlist_grow_size - 1; i++) |
1299
|
158 tc_newlist[i].freeptr = &tc_newlist[i+1]; |
|
159 |
|
160 tc_newlist[i].freeptr = 0; |
|
161 } |
|
162 |
|
163 tree_constant *tmp = tc_newlist; |
|
164 tc_newlist = tc_newlist->freeptr; |
1168
|
165 return tmp; |
1
|
166 } |
|
167 |
|
168 void |
1488
|
169 tree_constant::operator delete (void *p, size_t /* size */) |
1
|
170 { |
1168
|
171 tree_constant *tmp = (tree_constant *) p; |
1299
|
172 tmp->freeptr = tc_newlist; |
|
173 tc_newlist = tmp; |
1
|
174 } |
|
175 |
747
|
176 // Simple assignment. |
|
177 |
|
178 tree_constant |
|
179 tree_constant::operator = (const tree_constant& a) |
|
180 { |
|
181 if (rep != a.rep) |
|
182 { |
|
183 if (--rep->count <= 0) |
|
184 delete rep; |
|
185 rep = a.rep; |
|
186 rep->count++; |
|
187 } |
|
188 return *this; |
|
189 } |
|
190 |
|
191 tree_constant |
1827
|
192 tree_constant::lookup_map_element (const string& ref, bool insert, |
|
193 bool silent) |
1277
|
194 { |
|
195 tree_constant retval; |
|
196 |
1755
|
197 if (! ref.empty ()) |
1277
|
198 { |
1755
|
199 SLList<string> list; |
|
200 |
|
201 size_t beg = 0; |
|
202 size_t end; |
|
203 |
1277
|
204 do |
|
205 { |
1755
|
206 end = ref.find ('.', beg); |
|
207 |
|
208 string tmp = (end == NPOS) |
|
209 ? ref.substr (beg) : ref.substr (beg, end - 1); |
|
210 |
|
211 list.append (tmp); |
1277
|
212 } |
1755
|
213 while (end != NPOS && (beg = end + 1)); |
1277
|
214 |
|
215 retval = lookup_map_element (list, insert, silent); |
|
216 } |
|
217 |
|
218 return retval; |
|
219 } |
|
220 |
|
221 tree_constant |
1827
|
222 tree_constant::lookup_map_element (SLList<string>& list, bool insert, |
|
223 bool silent) |
747
|
224 { |
|
225 tree_constant retval; |
|
226 |
|
227 tree_constant_rep *tmp_rep = rep; |
|
228 |
|
229 Pix p = list.first (); |
|
230 while (p) |
|
231 { |
1755
|
232 string elt = list (p); |
747
|
233 |
|
234 list.next (p); |
|
235 |
1277
|
236 tree_constant tmp; |
|
237 |
|
238 tmp = tmp_rep->lookup_map_element (elt, insert, silent); |
747
|
239 |
|
240 if (error_state) |
|
241 break; |
|
242 |
|
243 tmp_rep = tmp.rep; |
|
244 |
|
245 if (! p) |
|
246 retval = tmp; |
|
247 } |
|
248 |
|
249 return retval; |
|
250 } |
|
251 |
1199
|
252 void |
|
253 tree_constant::print (void) |
|
254 { |
|
255 ostrstream output_buf; |
|
256 print (output_buf); |
|
257 output_buf << ends; |
|
258 maybe_page_output (output_buf); |
|
259 } |
|
260 |
1755
|
261 void |
1827
|
262 tree_constant::print_with_name (const string& name, bool print_padding) |
1755
|
263 { |
|
264 ostrstream output_buf; |
|
265 print_with_name (output_buf, name, print_padding); |
|
266 output_buf << ends; |
|
267 maybe_page_output (output_buf); |
|
268 } |
|
269 |
|
270 void |
|
271 tree_constant::print_with_name (ostream& output_buf, const string& name, |
1827
|
272 bool print_padding) |
1755
|
273 { |
1827
|
274 bool pad_after = false; |
1755
|
275 |
|
276 if (user_pref.print_answer_id_name) |
|
277 { |
1973
|
278 if (print_as_scalar ()) |
1755
|
279 output_buf << name << " = "; |
1973
|
280 else if (print_as_structure ()) |
|
281 { |
|
282 pad_after = true; |
|
283 output_buf << name << " ="; |
|
284 } |
1755
|
285 else |
|
286 { |
1827
|
287 pad_after = true; |
1755
|
288 output_buf << name << " =\n\n"; |
|
289 } |
|
290 } |
|
291 |
|
292 print (output_buf); |
|
293 |
|
294 if (print_padding && pad_after) |
|
295 output_buf << "\n"; |
|
296 } |
|
297 |
747
|
298 // Simple structure assignment. |
|
299 |
|
300 void |
|
301 tree_constant::make_unique (void) |
|
302 { |
|
303 if (rep->count > 1) |
|
304 { |
|
305 --rep->count; |
|
306 rep = new tree_constant_rep (*rep); |
|
307 rep->count = 1; |
|
308 } |
|
309 |
|
310 if (rep->is_map ()) |
|
311 { |
|
312 for (Pix p = rep->a_map->first (); p != 0; rep->a_map->next (p)) |
|
313 { |
|
314 rep->a_map->contents (p) . make_unique (); |
|
315 } |
|
316 } |
|
317 } |
|
318 |
|
319 tree_constant::tree_constant_rep * |
|
320 tree_constant::make_unique_map (void) |
|
321 { |
|
322 if (! rep->is_map ()) |
|
323 { |
|
324 if (--rep->count <= 0) |
|
325 delete rep; |
|
326 |
|
327 Octave_map m; |
|
328 rep = new tree_constant_rep (m); |
|
329 rep->count = 1; |
|
330 } |
|
331 |
|
332 make_unique (); |
|
333 |
|
334 return rep; |
|
335 } |
|
336 |
|
337 tree_constant |
1755
|
338 tree_constant::assign_map_element (SLList<string>& list, |
782
|
339 tree_constant& rhs) |
747
|
340 { |
|
341 tree_constant_rep *tmp_rep = make_unique_map (); |
|
342 |
|
343 if (rhs.is_map ()) |
|
344 rhs.make_unique (); |
|
345 |
|
346 Pix p = list.first (); |
|
347 while (p) |
|
348 { |
1755
|
349 string elt = list (p); |
747
|
350 |
|
351 list.next (p); |
|
352 |
|
353 tree_constant& tmp = tmp_rep->lookup_map_element (elt, 1); |
|
354 |
|
355 if (! p) |
|
356 { |
|
357 tmp = rhs; |
|
358 return tmp; |
|
359 } |
|
360 |
|
361 tmp_rep = tmp.make_unique_map (); |
|
362 } |
|
363 |
|
364 return tree_constant (); |
|
365 } |
|
366 |
|
367 // Indexed structure assignment. |
|
368 |
|
369 tree_constant |
1755
|
370 tree_constant::assign_map_element (SLList<string>& list, |
782
|
371 tree_constant& rhs, |
747
|
372 const Octave_object& args) |
|
373 { |
|
374 tree_constant_rep *tmp_rep = make_unique_map (); |
|
375 |
|
376 if (rhs.is_map ()) |
|
377 rhs.make_unique (); |
|
378 |
|
379 Pix p = list.first (); |
|
380 while (p) |
|
381 { |
1755
|
382 string elt = list (p); |
747
|
383 |
|
384 list.next (p); |
|
385 |
|
386 tree_constant& tmp = tmp_rep->lookup_map_element (elt, 1); |
|
387 |
|
388 if (! p) |
|
389 { |
|
390 tmp.assign (rhs, args); |
|
391 return tmp; |
|
392 } |
|
393 |
|
394 tmp_rep = tmp.make_unique_map (); |
|
395 } |
|
396 |
|
397 return tree_constant (); |
|
398 } |
|
399 |
1742
|
400 Octave_object |
1827
|
401 tree_constant::eval (bool print, int, const Octave_object& args) |
1742
|
402 { |
|
403 Octave_object retval; |
|
404 |
|
405 if (args.length () > 0) |
|
406 retval(0) = rep->do_index (args); |
|
407 else |
|
408 retval(0) = *this; |
|
409 |
|
410 if (retval(0).is_defined ()) |
|
411 retval(0).eval (print); |
|
412 |
|
413 return retval; |
|
414 } |
|
415 |
747
|
416 void |
|
417 tree_constant::print_code (ostream& os) |
|
418 { |
|
419 print_code_indent (os); |
|
420 |
|
421 if (in_parens) |
|
422 os << "("; |
|
423 |
|
424 if (rep) |
|
425 rep->print_code (os); |
|
426 |
|
427 if (in_parens) |
|
428 os << ")"; |
|
429 } |
|
430 |
1299
|
431 // The real representation of constants. |
|
432 |
|
433 TC_REP::tree_constant_rep (void) |
|
434 { |
|
435 type_tag = unknown_constant; |
|
436 } |
|
437 |
|
438 TC_REP::tree_constant_rep (double d) |
|
439 { |
|
440 scalar = d; |
|
441 type_tag = scalar_constant; |
|
442 } |
|
443 |
|
444 TC_REP::tree_constant_rep (const Matrix& m) |
|
445 { |
|
446 if (m.rows () == 1 && m.columns () == 1) |
|
447 { |
|
448 scalar = m.elem (0, 0); |
|
449 type_tag = scalar_constant; |
|
450 } |
|
451 else |
|
452 { |
|
453 matrix = new Matrix (m); |
|
454 type_tag = matrix_constant; |
|
455 } |
|
456 } |
|
457 |
|
458 TC_REP::tree_constant_rep (const DiagMatrix& d) |
|
459 { |
|
460 if (d.rows () == 1 && d.columns () == 1) |
|
461 { |
|
462 scalar = d.elem (0, 0); |
|
463 type_tag = scalar_constant; |
|
464 } |
|
465 else |
|
466 { |
|
467 matrix = new Matrix (d); |
|
468 type_tag = matrix_constant; |
|
469 } |
|
470 } |
|
471 |
|
472 TC_REP::tree_constant_rep (const RowVector& v, int prefer_column_vector) |
|
473 { |
|
474 int len = v.capacity (); |
|
475 if (len == 1) |
|
476 { |
|
477 scalar = v.elem (0); |
|
478 type_tag = scalar_constant; |
|
479 } |
|
480 else |
|
481 { |
|
482 int pcv = (prefer_column_vector < 0) |
|
483 ? user_pref.prefer_column_vectors |
|
484 : prefer_column_vector; |
|
485 |
|
486 if (pcv) |
|
487 { |
|
488 Matrix m (len, 1); |
|
489 for (int i = 0; i < len; i++) |
|
490 m.elem (i, 0) = v.elem (i); |
|
491 matrix = new Matrix (m); |
|
492 type_tag = matrix_constant; |
|
493 } |
|
494 else |
|
495 { |
|
496 Matrix m (1, len); |
|
497 for (int i = 0; i < len; i++) |
|
498 m.elem (0, i) = v.elem (i); |
|
499 matrix = new Matrix (m); |
|
500 type_tag = matrix_constant; |
|
501 } |
|
502 } |
|
503 } |
|
504 |
|
505 TC_REP::tree_constant_rep (const ColumnVector& v, int prefer_column_vector) |
|
506 { |
|
507 int len = v.capacity (); |
|
508 if (len == 1) |
|
509 { |
|
510 scalar = v.elem (0); |
|
511 type_tag = scalar_constant; |
|
512 } |
|
513 else |
|
514 { |
|
515 int pcv = (prefer_column_vector < 0) |
|
516 ? user_pref.prefer_column_vectors |
|
517 : prefer_column_vector; |
|
518 |
|
519 if (pcv) |
|
520 { |
|
521 Matrix m (len, 1); |
|
522 for (int i = 0; i < len; i++) |
|
523 m.elem (i, 0) = v.elem (i); |
|
524 matrix = new Matrix (m); |
|
525 type_tag = matrix_constant; |
|
526 } |
|
527 else |
|
528 { |
|
529 Matrix m (1, len); |
|
530 for (int i = 0; i < len; i++) |
|
531 m.elem (0, i) = v.elem (i); |
|
532 matrix = new Matrix (m); |
|
533 type_tag = matrix_constant; |
|
534 } |
|
535 } |
|
536 } |
|
537 |
|
538 TC_REP::tree_constant_rep (const Complex& c) |
|
539 { |
1615
|
540 if (::imag (c) == 0.0) |
|
541 { |
|
542 scalar = ::real (c); |
|
543 type_tag = scalar_constant; |
|
544 } |
|
545 else |
|
546 { |
|
547 complex_scalar = new Complex (c); |
|
548 type_tag = complex_scalar_constant; |
|
549 } |
1299
|
550 } |
|
551 |
|
552 TC_REP::tree_constant_rep (const ComplexMatrix& m) |
|
553 { |
|
554 if (m.rows () == 1 && m.columns () == 1) |
|
555 { |
1615
|
556 Complex c = m.elem (0, 0); |
|
557 |
|
558 if (::imag (c) == 0.0) |
|
559 { |
|
560 scalar = ::real (c); |
|
561 type_tag = scalar_constant; |
|
562 } |
|
563 else |
|
564 { |
|
565 complex_scalar = new Complex (c); |
|
566 type_tag = complex_scalar_constant; |
|
567 } |
1299
|
568 } |
|
569 else |
|
570 { |
|
571 complex_matrix = new ComplexMatrix (m); |
|
572 type_tag = complex_matrix_constant; |
|
573 } |
|
574 } |
|
575 |
|
576 TC_REP::tree_constant_rep (const ComplexDiagMatrix& d) |
|
577 { |
|
578 if (d.rows () == 1 && d.columns () == 1) |
|
579 { |
1615
|
580 Complex c = d.elem (0, 0); |
|
581 |
|
582 if (::imag (c) == 0.0) |
|
583 { |
|
584 scalar = ::real (c); |
|
585 type_tag = scalar_constant; |
|
586 } |
|
587 else |
|
588 { |
|
589 complex_scalar = new Complex (c); |
|
590 type_tag = complex_scalar_constant; |
|
591 } |
1299
|
592 } |
|
593 else |
|
594 { |
|
595 complex_matrix = new ComplexMatrix (d); |
|
596 type_tag = complex_matrix_constant; |
|
597 } |
|
598 } |
|
599 |
|
600 TC_REP::tree_constant_rep (const ComplexRowVector& v, |
|
601 int prefer_column_vector) |
|
602 { |
|
603 int len = v.capacity (); |
|
604 if (len == 1) |
|
605 { |
1615
|
606 Complex c = v.elem (0); |
|
607 |
|
608 if (::imag (c) == 0.0) |
|
609 { |
|
610 scalar = ::real (c); |
|
611 type_tag = scalar_constant; |
|
612 } |
|
613 else |
|
614 { |
|
615 complex_scalar = new Complex (c); |
|
616 type_tag = complex_scalar_constant; |
|
617 } |
1299
|
618 } |
|
619 else |
|
620 { |
|
621 int pcv = (prefer_column_vector < 0) |
|
622 ? user_pref.prefer_column_vectors |
|
623 : prefer_column_vector; |
|
624 |
|
625 if (pcv) |
|
626 { |
|
627 ComplexMatrix m (len, 1); |
|
628 for (int i = 0; i < len; i++) |
|
629 m.elem (i, 0) = v.elem (i); |
|
630 complex_matrix = new ComplexMatrix (m); |
|
631 type_tag = complex_matrix_constant; |
|
632 } |
|
633 else |
|
634 { |
|
635 ComplexMatrix m (1, len); |
|
636 for (int i = 0; i < len; i++) |
|
637 m.elem (0, i) = v.elem (i); |
|
638 complex_matrix = new ComplexMatrix (m); |
|
639 type_tag = complex_matrix_constant; |
|
640 } |
|
641 } |
|
642 } |
|
643 |
|
644 TC_REP::tree_constant_rep (const ComplexColumnVector& v, int |
|
645 prefer_column_vector) |
|
646 { |
|
647 int len = v.capacity (); |
|
648 if (len == 1) |
|
649 { |
1615
|
650 Complex c = v.elem (0); |
|
651 |
|
652 if (::imag (c) == 0.0) |
|
653 { |
|
654 scalar = ::real (c); |
|
655 type_tag = scalar_constant; |
|
656 } |
|
657 else |
|
658 { |
|
659 complex_scalar = new Complex (c); |
|
660 type_tag = complex_scalar_constant; |
|
661 } |
1299
|
662 } |
|
663 else |
|
664 { |
|
665 int pcv = (prefer_column_vector < 0) |
|
666 ? user_pref.prefer_column_vectors |
|
667 : prefer_column_vector; |
|
668 |
|
669 if (pcv) |
|
670 { |
|
671 ComplexMatrix m (len, 1); |
|
672 for (int i = 0; i < len; i++) |
|
673 m.elem (i, 0) = v.elem (i); |
|
674 complex_matrix = new ComplexMatrix (m); |
|
675 type_tag = complex_matrix_constant; |
|
676 } |
|
677 else |
|
678 { |
|
679 ComplexMatrix m (1, len); |
|
680 for (int i = 0; i < len; i++) |
|
681 m.elem (0, i) = v.elem (i); |
|
682 complex_matrix = new ComplexMatrix (m); |
|
683 type_tag = complex_matrix_constant; |
|
684 } |
|
685 } |
|
686 } |
|
687 |
|
688 TC_REP::tree_constant_rep (const char *s) |
|
689 { |
1572
|
690 char_matrix = new charMatrix (s); |
|
691 type_tag = char_matrix_constant_str; |
1355
|
692 } |
|
693 |
1731
|
694 TC_REP::tree_constant_rep (const string& s) |
|
695 { |
|
696 char_matrix = new charMatrix (s); |
|
697 type_tag = char_matrix_constant_str; |
1755
|
698 } |
|
699 |
|
700 TC_REP::tree_constant_rep (const string_vector& s) |
|
701 { |
|
702 int nr = s.length (); |
|
703 int nc = s.max_length (); |
|
704 char_matrix = new charMatrix (nr, nc, 0); |
|
705 for (int i = 0; i < nr; i++) |
|
706 { |
|
707 nc = s[i].length (); |
|
708 for (int j = 0; j < nc; j++) |
|
709 char_matrix->elem (i, j) = s[i][j]; |
|
710 } |
|
711 type_tag = char_matrix_constant_str; |
1731
|
712 } |
|
713 |
1827
|
714 TC_REP::tree_constant_rep (const charMatrix& chm, bool is_str) |
1355
|
715 { |
1572
|
716 char_matrix = new charMatrix (chm); |
|
717 type_tag = is_str ? char_matrix_constant_str : char_matrix_constant; |
1299
|
718 } |
|
719 |
|
720 TC_REP::tree_constant_rep (double b, double l, double i) |
|
721 { |
|
722 range = new Range (b, l, i); |
|
723 int nel = range->nelem (); |
|
724 if (nel > 1) |
|
725 type_tag = range_constant; |
|
726 else |
|
727 { |
|
728 delete range; |
|
729 if (nel == 1) |
|
730 { |
|
731 scalar = b; |
|
732 type_tag = scalar_constant; |
|
733 } |
|
734 else if (nel == 0) |
|
735 { |
|
736 matrix = new Matrix (); |
|
737 type_tag = matrix_constant; |
|
738 } |
|
739 else |
|
740 { |
|
741 type_tag = unknown_constant; |
|
742 if (nel == -1) |
|
743 ::error ("number of elements in range exceeds INT_MAX"); |
|
744 else |
|
745 ::error ("invalid range"); |
|
746 } |
|
747 } |
|
748 } |
|
749 |
|
750 TC_REP::tree_constant_rep (const Range& r) |
|
751 { |
|
752 int nel = r.nelem (); |
|
753 if (nel > 1) |
|
754 { |
|
755 range = new Range (r); |
|
756 type_tag = range_constant; |
|
757 } |
|
758 else if (nel == 1) |
|
759 { |
|
760 scalar = r.base (); |
|
761 type_tag = scalar_constant; |
|
762 } |
|
763 else if (nel == 0) |
|
764 { |
|
765 matrix = new Matrix (); |
|
766 type_tag = matrix_constant; |
|
767 } |
|
768 else |
|
769 { |
|
770 type_tag = unknown_constant; |
|
771 if (nel == -1) |
|
772 ::error ("number of elements in range exceeds INT_MAX"); |
|
773 else |
|
774 ::error ("invalid range"); |
|
775 } |
|
776 } |
|
777 |
|
778 TC_REP::tree_constant_rep (const Octave_map& m) |
|
779 { |
|
780 a_map = new Octave_map (m); |
|
781 type_tag = map_constant; |
|
782 } |
|
783 |
|
784 TC_REP::tree_constant_rep (TC_REP::constant_type t) |
|
785 { |
|
786 assert (t == magic_colon || t == all_va_args); |
|
787 type_tag = t; |
|
788 } |
|
789 |
|
790 TC_REP::tree_constant_rep (const tree_constant_rep& t) |
|
791 { |
|
792 type_tag = t.type_tag; |
|
793 |
|
794 switch (t.type_tag) |
|
795 { |
|
796 case unknown_constant: |
|
797 break; |
|
798 |
|
799 case scalar_constant: |
|
800 scalar = t.scalar; |
|
801 break; |
|
802 |
|
803 case matrix_constant: |
|
804 matrix = new Matrix (*(t.matrix)); |
|
805 break; |
|
806 |
1572
|
807 case char_matrix_constant: |
|
808 char_matrix = new charMatrix (*(t.char_matrix)); |
|
809 break; |
|
810 |
|
811 case char_matrix_constant_str: |
|
812 char_matrix = new charMatrix (*(t.char_matrix)); |
1299
|
813 break; |
|
814 |
|
815 case complex_matrix_constant: |
|
816 complex_matrix = new ComplexMatrix (*(t.complex_matrix)); |
|
817 break; |
|
818 |
|
819 case complex_scalar_constant: |
|
820 complex_scalar = new Complex (*(t.complex_scalar)); |
|
821 break; |
|
822 |
|
823 case range_constant: |
|
824 range = new Range (*(t.range)); |
|
825 break; |
|
826 |
|
827 case map_constant: |
|
828 a_map = new Octave_map (*(t.a_map)); |
|
829 break; |
|
830 |
|
831 case magic_colon: |
|
832 case all_va_args: |
|
833 break; |
|
834 } |
|
835 |
1755
|
836 orig_text = t.orig_text; |
1299
|
837 } |
|
838 |
|
839 TC_REP::~tree_constant_rep (void) |
|
840 { |
|
841 switch (type_tag) |
|
842 { |
|
843 case matrix_constant: |
|
844 delete matrix; |
|
845 break; |
|
846 |
|
847 case complex_scalar_constant: |
|
848 delete complex_scalar; |
|
849 break; |
|
850 |
|
851 case complex_matrix_constant: |
|
852 delete complex_matrix; |
|
853 break; |
|
854 |
1572
|
855 case char_matrix_constant: |
|
856 case char_matrix_constant_str: |
|
857 delete char_matrix; |
1299
|
858 break; |
|
859 |
|
860 case range_constant: |
|
861 delete range; |
|
862 break; |
|
863 |
|
864 case map_constant: |
|
865 delete a_map; |
|
866 break; |
|
867 |
|
868 case unknown_constant: |
|
869 case scalar_constant: |
|
870 case magic_colon: |
|
871 case all_va_args: |
|
872 break; |
|
873 } |
|
874 } |
|
875 |
|
876 void * |
|
877 TC_REP::operator new (size_t size) |
|
878 { |
|
879 assert (size == sizeof (TC_REP)); |
|
880 |
|
881 if (! tc_rep_newlist) |
|
882 { |
|
883 int block_size = tc_rep_newlist_grow_size * sizeof (TC_REP); |
|
884 tc_rep_newlist = (TC_REP *) new char [block_size]; |
|
885 |
1321
|
886 int i = 0; |
|
887 |
|
888 for (i = 0; i < tc_rep_newlist_grow_size - 1; i++) |
1299
|
889 tc_rep_newlist[i].freeptr = &tc_rep_newlist[i+1]; |
|
890 |
|
891 tc_rep_newlist[i].freeptr = 0; |
|
892 } |
|
893 |
|
894 TC_REP *tmp = tc_rep_newlist; |
|
895 tc_rep_newlist = tc_rep_newlist->freeptr; |
|
896 return tmp; |
|
897 } |
|
898 |
|
899 void |
1488
|
900 TC_REP::operator delete (void *p, size_t /* size */) |
1299
|
901 { |
|
902 TC_REP *tmp = (TC_REP *) p; |
|
903 tmp->freeptr = tc_rep_newlist; |
|
904 tc_rep_newlist = tmp; |
|
905 } |
|
906 |
|
907 int |
|
908 TC_REP::rows (void) const |
|
909 { |
|
910 int retval = -1; |
|
911 |
|
912 switch (type_tag) |
|
913 { |
|
914 case scalar_constant: |
|
915 case complex_scalar_constant: |
|
916 retval = 1; |
|
917 break; |
|
918 |
1572
|
919 case char_matrix_constant: |
|
920 case char_matrix_constant_str: |
|
921 retval = char_matrix->rows (); |
1355
|
922 break; |
|
923 |
1299
|
924 case range_constant: |
|
925 retval = (columns () > 0); |
|
926 break; |
|
927 |
|
928 case matrix_constant: |
|
929 retval = matrix->rows (); |
|
930 break; |
|
931 |
|
932 case complex_matrix_constant: |
|
933 retval = complex_matrix->rows (); |
|
934 break; |
|
935 |
|
936 default: |
|
937 break; |
|
938 } |
|
939 |
|
940 return retval; |
|
941 } |
|
942 |
|
943 int |
|
944 TC_REP::columns (void) const |
|
945 { |
|
946 int retval = -1; |
|
947 |
|
948 switch (type_tag) |
|
949 { |
|
950 case scalar_constant: |
|
951 case complex_scalar_constant: |
|
952 retval = 1; |
|
953 break; |
|
954 |
|
955 case matrix_constant: |
|
956 retval = matrix->columns (); |
|
957 break; |
|
958 |
|
959 case complex_matrix_constant: |
|
960 retval = complex_matrix->columns (); |
|
961 break; |
|
962 |
1572
|
963 case char_matrix_constant: |
|
964 case char_matrix_constant_str: |
|
965 retval = char_matrix->columns (); |
1299
|
966 break; |
|
967 |
|
968 case range_constant: |
|
969 retval = range->nelem (); |
|
970 break; |
|
971 |
|
972 default: |
|
973 break; |
|
974 } |
|
975 |
|
976 return retval; |
|
977 } |
|
978 |
|
979 tree_constant |
|
980 TC_REP::all (void) const |
|
981 { |
|
982 tree_constant retval; |
|
983 |
|
984 if (error_state) |
|
985 return retval; |
|
986 |
|
987 if (! is_numeric_type ()) |
|
988 { |
|
989 tree_constant tmp = make_numeric (); |
|
990 |
|
991 if (error_state) |
|
992 return retval; |
|
993 |
|
994 return tmp.all (); |
|
995 } |
|
996 |
|
997 switch (type_tag) |
|
998 { |
|
999 case scalar_constant: |
1355
|
1000 retval = (double) (scalar != 0.0); |
1299
|
1001 break; |
|
1002 |
|
1003 case matrix_constant: |
1355
|
1004 retval = matrix->all (); |
1299
|
1005 break; |
|
1006 |
|
1007 case complex_scalar_constant: |
1355
|
1008 retval = (double) (*complex_scalar != 0.0); |
1299
|
1009 break; |
|
1010 |
|
1011 case complex_matrix_constant: |
1355
|
1012 retval = complex_matrix->all (); |
1299
|
1013 break; |
|
1014 |
|
1015 default: |
|
1016 gripe_wrong_type_arg ("all", *this); |
|
1017 break; |
|
1018 } |
|
1019 |
|
1020 return retval; |
|
1021 } |
|
1022 |
|
1023 tree_constant |
|
1024 TC_REP::any (void) const |
|
1025 { |
|
1026 tree_constant retval; |
|
1027 |
|
1028 if (error_state) |
|
1029 return retval; |
|
1030 |
|
1031 if (! is_numeric_type ()) |
|
1032 { |
|
1033 tree_constant tmp = make_numeric (); |
|
1034 |
|
1035 if (error_state) |
|
1036 return retval; |
|
1037 |
|
1038 return tmp.any (); |
|
1039 } |
|
1040 |
|
1041 switch (type_tag) |
|
1042 { |
|
1043 case scalar_constant: |
1355
|
1044 retval = (double) (scalar != 0.0); |
1299
|
1045 break; |
|
1046 |
|
1047 case matrix_constant: |
1355
|
1048 retval = matrix->any (); |
1299
|
1049 break; |
|
1050 |
|
1051 case complex_scalar_constant: |
1355
|
1052 retval = (double) (*complex_scalar != 0.0); |
1299
|
1053 break; |
|
1054 |
|
1055 case complex_matrix_constant: |
1355
|
1056 retval = complex_matrix->any (); |
1299
|
1057 break; |
|
1058 |
|
1059 default: |
|
1060 gripe_wrong_type_arg ("any", *this); |
|
1061 break; |
|
1062 } |
|
1063 |
|
1064 return retval; |
|
1065 } |
|
1066 |
1827
|
1067 bool |
1299
|
1068 TC_REP::valid_as_scalar_index (void) const |
|
1069 { |
|
1070 return (type_tag == magic_colon |
|
1071 || (type_tag == scalar_constant |
|
1072 && ! xisnan (scalar) |
|
1073 && NINT (scalar) == 1) |
|
1074 || (type_tag == range_constant |
|
1075 && range->nelem () == 1 |
|
1076 && ! xisnan (range->base ()) |
|
1077 && NINT (range->base ()) == 1)); |
|
1078 } |
|
1079 |
1827
|
1080 bool |
1299
|
1081 TC_REP::valid_as_zero_index (void) const |
|
1082 { |
|
1083 return ((type_tag == scalar_constant |
|
1084 && ! xisnan (scalar) |
|
1085 && NINT (scalar) == 0) |
|
1086 || (type_tag == matrix_constant |
|
1087 && matrix->rows () == 0 |
|
1088 && matrix->columns () == 0) |
|
1089 || (type_tag == range_constant |
|
1090 && range->nelem () == 1 |
|
1091 && ! xisnan (range->base ()) |
|
1092 && NINT (range->base ()) == 0)); |
|
1093 } |
|
1094 |
1827
|
1095 bool |
1299
|
1096 TC_REP::is_true (void) const |
|
1097 { |
1827
|
1098 int retval = false; |
1299
|
1099 |
|
1100 if (error_state) |
|
1101 return retval; |
|
1102 |
|
1103 if (! is_numeric_type ()) |
|
1104 { |
|
1105 tree_constant tmp = make_numeric (); |
|
1106 |
|
1107 if (error_state) |
|
1108 return retval; |
|
1109 |
|
1110 return tmp.is_true (); |
|
1111 } |
|
1112 |
|
1113 switch (type_tag) |
|
1114 { |
|
1115 case scalar_constant: |
|
1116 retval = (scalar != 0.0); |
|
1117 break; |
|
1118 |
|
1119 case matrix_constant: |
|
1120 { |
|
1121 Matrix m = (matrix->all ()) . all (); |
|
1122 retval = (m.rows () == 1 |
|
1123 && m.columns () == 1 |
|
1124 && m.elem (0, 0) != 0.0); |
|
1125 } |
|
1126 break; |
|
1127 |
|
1128 case complex_scalar_constant: |
|
1129 retval = (*complex_scalar != 0.0); |
|
1130 break; |
|
1131 |
|
1132 case complex_matrix_constant: |
|
1133 { |
|
1134 Matrix m = (complex_matrix->all ()) . all (); |
|
1135 retval = (m.rows () == 1 |
|
1136 && m.columns () == 1 |
|
1137 && m.elem (0, 0) != 0.0); |
|
1138 } |
|
1139 break; |
|
1140 |
|
1141 default: |
|
1142 gripe_wrong_type_arg (0, *this); |
|
1143 break; |
|
1144 } |
|
1145 |
|
1146 return retval; |
|
1147 } |
|
1148 |
|
1149 static void |
|
1150 warn_implicit_conversion (const char *from, const char *to) |
|
1151 { |
|
1152 warning ("implicit conversion from %s to %s", from, to); |
|
1153 } |
|
1154 |
2067
|
1155 // XXX FIXME XXX -- we need a better way of handling conversions. |
|
1156 |
1299
|
1157 double |
1827
|
1158 TC_REP::double_value (bool force_string_conv) const |
1299
|
1159 { |
|
1160 double retval = octave_NaN; |
|
1161 |
|
1162 switch (type_tag) |
|
1163 { |
|
1164 case scalar_constant: |
|
1165 retval = scalar; |
|
1166 break; |
|
1167 |
|
1168 case matrix_constant: |
|
1169 { |
|
1170 if (user_pref.do_fortran_indexing && rows () > 0 && columns () > 0) |
|
1171 retval = matrix->elem (0, 0); |
|
1172 else |
|
1173 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
1174 } |
|
1175 break; |
|
1176 |
|
1177 case complex_matrix_constant: |
|
1178 case complex_scalar_constant: |
|
1179 { |
|
1180 int flag = user_pref.ok_to_lose_imaginary_part; |
|
1181 |
|
1182 if (flag < 0) |
|
1183 warn_implicit_conversion ("complex scalar", "real scalar"); |
|
1184 |
|
1185 if (flag) |
|
1186 { |
|
1187 if (type_tag == complex_scalar_constant) |
|
1188 retval = ::real (*complex_scalar); |
|
1189 else if (type_tag == complex_matrix_constant) |
|
1190 { |
|
1191 if (user_pref.do_fortran_indexing |
|
1192 && rows () > 0 && columns () > 0) |
|
1193 retval = ::real (complex_matrix->elem (0, 0)); |
|
1194 else |
|
1195 gripe_invalid_conversion ("complex matrix", "real scalar"); |
|
1196 } |
|
1197 else |
|
1198 panic_impossible (); |
|
1199 } |
|
1200 else |
|
1201 gripe_invalid_conversion ("complex scalar", "real scalar"); |
|
1202 } |
|
1203 break; |
|
1204 |
1572
|
1205 case char_matrix_constant: |
1299
|
1206 { |
1572
|
1207 int len = char_matrix->rows (); |
|
1208 if ((char_matrix->rows () == 1 && len == 1) |
|
1209 || (len > 1 && user_pref.do_fortran_indexing)) |
|
1210 retval = toascii ((int) char_matrix->elem (0, 0)); |
|
1211 else |
|
1212 gripe_invalid_conversion ("char matrix", "real scalar"); |
|
1213 } |
|
1214 break; |
|
1215 |
|
1216 case char_matrix_constant_str: |
|
1217 { |
1827
|
1218 int flag = force_string_conv; |
1299
|
1219 if (! flag) |
|
1220 flag = user_pref.implicit_str_to_num_ok; |
|
1221 |
|
1222 if (flag < 0) |
|
1223 warn_implicit_conversion ("string", "real scalar"); |
|
1224 |
1572
|
1225 int len = char_matrix->rows (); |
1355
|
1226 if (flag |
1572
|
1227 && ((char_matrix->rows () == 1 && len == 1) |
1355
|
1228 || (len > 1 && user_pref.do_fortran_indexing))) |
1572
|
1229 retval = toascii ((int) char_matrix->elem (0, 0)); |
1299
|
1230 else |
|
1231 gripe_invalid_conversion ("string", "real scalar"); |
|
1232 } |
|
1233 break; |
|
1234 |
|
1235 case range_constant: |
|
1236 { |
|
1237 int nel = range->nelem (); |
|
1238 if (nel == 1 || (nel > 1 && user_pref.do_fortran_indexing)) |
|
1239 retval = range->base (); |
|
1240 else |
|
1241 gripe_invalid_conversion ("range", "real scalar"); |
|
1242 } |
|
1243 break; |
|
1244 |
|
1245 default: |
|
1246 gripe_invalid_conversion (type_as_string (), "real scalar"); |
|
1247 break; |
|
1248 } |
|
1249 |
|
1250 return retval; |
|
1251 } |
|
1252 |
|
1253 Matrix |
1827
|
1254 TC_REP::matrix_value (bool force_string_conv) const |
1299
|
1255 { |
|
1256 Matrix retval; |
|
1257 |
|
1258 switch (type_tag) |
|
1259 { |
|
1260 case scalar_constant: |
|
1261 retval = Matrix (1, 1, scalar); |
|
1262 break; |
|
1263 |
|
1264 case matrix_constant: |
|
1265 retval = *matrix; |
|
1266 break; |
|
1267 |
|
1268 case complex_scalar_constant: |
|
1269 case complex_matrix_constant: |
|
1270 { |
|
1271 int flag = user_pref.ok_to_lose_imaginary_part; |
|
1272 if (flag < 0) |
|
1273 warn_implicit_conversion ("complex matrix", "real matrix"); |
|
1274 |
|
1275 if (flag) |
|
1276 { |
|
1277 if (type_tag == complex_scalar_constant) |
|
1278 retval = Matrix (1, 1, ::real (*complex_scalar)); |
|
1279 else if (type_tag == complex_matrix_constant) |
|
1280 retval = ::real (*complex_matrix); |
|
1281 else |
|
1282 panic_impossible (); |
|
1283 } |
|
1284 else |
|
1285 gripe_invalid_conversion ("complex matrix", "real matrix"); |
|
1286 } |
|
1287 break; |
|
1288 |
1572
|
1289 case char_matrix_constant: |
|
1290 retval = Matrix (*char_matrix); |
|
1291 break; |
|
1292 |
|
1293 case char_matrix_constant_str: |
1299
|
1294 { |
1827
|
1295 int flag = force_string_conv; |
1299
|
1296 if (! flag) |
|
1297 flag = user_pref.implicit_str_to_num_ok; |
|
1298 |
|
1299 if (flag < 0) |
|
1300 warn_implicit_conversion ("string", "real matrix"); |
|
1301 |
|
1302 if (flag) |
1572
|
1303 retval = Matrix (*char_matrix); |
1299
|
1304 else |
|
1305 gripe_invalid_conversion ("string", "real matrix"); |
|
1306 } |
|
1307 break; |
|
1308 |
|
1309 case range_constant: |
|
1310 retval = range->matrix_value (); |
|
1311 break; |
|
1312 |
|
1313 default: |
|
1314 gripe_invalid_conversion (type_as_string (), "real matrix"); |
|
1315 break; |
|
1316 } |
|
1317 |
|
1318 return retval; |
|
1319 } |
|
1320 |
|
1321 Complex |
1827
|
1322 TC_REP::complex_value (bool force_string_conv) const |
1299
|
1323 { |
|
1324 Complex retval (octave_NaN, octave_NaN); |
|
1325 |
|
1326 switch (type_tag) |
|
1327 { |
|
1328 case complex_scalar_constant: |
|
1329 retval = *complex_scalar; |
|
1330 break; |
|
1331 |
|
1332 case scalar_constant: |
|
1333 retval = scalar; |
|
1334 break; |
|
1335 |
|
1336 case complex_matrix_constant: |
|
1337 case matrix_constant: |
|
1338 { |
|
1339 if (user_pref.do_fortran_indexing && rows () > 0 && columns () > 0) |
|
1340 { |
|
1341 if (type_tag == complex_matrix_constant) |
|
1342 retval = complex_matrix->elem (0, 0); |
|
1343 else |
|
1344 retval = matrix->elem (0, 0); |
|
1345 } |
|
1346 else |
|
1347 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
1348 } |
|
1349 break; |
|
1350 |
1572
|
1351 case char_matrix_constant: |
1299
|
1352 { |
1572
|
1353 int len = char_matrix->cols (); |
|
1354 if ((char_matrix->rows () == 1 && len == 1) |
|
1355 || (len > 1 && user_pref.do_fortran_indexing)) |
|
1356 retval = toascii ((int) char_matrix->elem (0, 0)); |
|
1357 else |
|
1358 gripe_invalid_conversion ("char matrix", "complex scalar"); |
|
1359 } |
|
1360 break; |
|
1361 |
|
1362 case char_matrix_constant_str: |
|
1363 { |
1827
|
1364 int flag = force_string_conv; |
1299
|
1365 if (! flag) |
|
1366 flag = user_pref.implicit_str_to_num_ok; |
|
1367 |
|
1368 if (flag < 0) |
|
1369 warn_implicit_conversion ("string", "complex scalar"); |
|
1370 |
1572
|
1371 int len = char_matrix->cols (); |
1355
|
1372 if (flag |
1572
|
1373 && ((char_matrix->rows () == 1 && len == 1) |
1355
|
1374 || (len > 1 && user_pref.do_fortran_indexing))) |
1572
|
1375 retval = toascii ((int) char_matrix->elem (0, 0)); |
1299
|
1376 else |
|
1377 gripe_invalid_conversion ("string", "complex scalar"); |
|
1378 } |
|
1379 break; |
|
1380 |
|
1381 case range_constant: |
|
1382 { |
|
1383 int nel = range->nelem (); |
|
1384 if (nel == 1 || (nel > 1 && user_pref.do_fortran_indexing)) |
|
1385 retval = range->base (); |
|
1386 else |
|
1387 gripe_invalid_conversion ("range", "complex scalar"); |
|
1388 } |
|
1389 break; |
|
1390 |
|
1391 default: |
|
1392 gripe_invalid_conversion (type_as_string (), "complex scalar"); |
|
1393 break; |
|
1394 } |
|
1395 |
|
1396 return retval; |
|
1397 } |
|
1398 |
|
1399 ComplexMatrix |
1827
|
1400 TC_REP::complex_matrix_value (bool force_string_conv) const |
1299
|
1401 { |
|
1402 ComplexMatrix retval; |
|
1403 |
|
1404 switch (type_tag) |
|
1405 { |
|
1406 case scalar_constant: |
|
1407 retval = ComplexMatrix (1, 1, Complex (scalar)); |
|
1408 break; |
|
1409 |
|
1410 case complex_scalar_constant: |
|
1411 retval = ComplexMatrix (1, 1, *complex_scalar); |
|
1412 break; |
|
1413 |
|
1414 case matrix_constant: |
|
1415 retval = ComplexMatrix (*matrix); |
|
1416 break; |
|
1417 |
|
1418 case complex_matrix_constant: |
|
1419 retval = *complex_matrix; |
|
1420 break; |
|
1421 |
1572
|
1422 case char_matrix_constant: |
|
1423 retval = ComplexMatrix (*char_matrix); |
|
1424 break; |
|
1425 |
|
1426 case char_matrix_constant_str: |
1299
|
1427 { |
1827
|
1428 int flag = force_string_conv; |
1299
|
1429 if (! flag) |
|
1430 flag = user_pref.implicit_str_to_num_ok; |
|
1431 |
|
1432 if (flag < 0) |
|
1433 warn_implicit_conversion ("string", "complex matrix"); |
|
1434 |
|
1435 if (flag) |
1572
|
1436 retval = ComplexMatrix (*char_matrix); |
1299
|
1437 else |
1572
|
1438 gripe_invalid_conversion ("complex", "real matrix"); |
1299
|
1439 } |
|
1440 break; |
|
1441 |
|
1442 case range_constant: |
|
1443 retval = range->matrix_value (); |
|
1444 break; |
|
1445 |
|
1446 default: |
|
1447 gripe_invalid_conversion (type_as_string (), "complex matrix"); |
|
1448 break; |
|
1449 } |
|
1450 |
|
1451 return retval; |
|
1452 } |
|
1453 |
1572
|
1454 // XXX FIXME XXX -- this needs to try to do some conversions... |
|
1455 |
|
1456 charMatrix |
1827
|
1457 TC_REP::char_matrix_value (bool force_string_conv) const |
1572
|
1458 { |
|
1459 charMatrix retval; |
|
1460 |
1827
|
1461 int flag = force_string_conv; |
1572
|
1462 if (! flag) |
|
1463 flag = user_pref.implicit_str_to_num_ok; |
|
1464 |
|
1465 switch (type_tag) |
|
1466 { |
|
1467 case char_matrix_constant: |
|
1468 case char_matrix_constant_str: |
|
1469 retval = *char_matrix; |
|
1470 break; |
|
1471 |
|
1472 default: |
1652
|
1473 if (! (rows () == 0 && columns () == 0)) |
|
1474 gripe_invalid_conversion (type_as_string (), "string"); |
1572
|
1475 break; |
|
1476 } |
|
1477 |
|
1478 return retval; |
|
1479 } |
|
1480 |
|
1481 charMatrix |
1355
|
1482 TC_REP::all_strings (void) const |
|
1483 { |
1572
|
1484 if (type_tag == char_matrix_constant_str) |
|
1485 return *char_matrix; |
1355
|
1486 else |
|
1487 { |
|
1488 gripe_invalid_conversion (type_as_string (), "string"); |
|
1489 return 0; |
|
1490 } |
|
1491 } |
|
1492 |
1728
|
1493 string |
1299
|
1494 TC_REP::string_value (void) const |
|
1495 { |
1728
|
1496 string retval; |
|
1497 |
1572
|
1498 if (type_tag == char_matrix_constant_str) |
1728
|
1499 retval = char_matrix->row_as_string (0); // XXX FIXME??? XXX |
1299
|
1500 else |
1728
|
1501 gripe_invalid_conversion (type_as_string (), "string"); |
|
1502 |
|
1503 return retval; |
1299
|
1504 } |
|
1505 |
|
1506 Range |
|
1507 TC_REP::range_value (void) const |
|
1508 { |
|
1509 assert (type_tag == range_constant); |
|
1510 return *range; |
|
1511 } |
|
1512 |
|
1513 Octave_map |
|
1514 TC_REP::map_value (void) const |
|
1515 { |
|
1516 assert (type_tag == map_constant); |
|
1517 return *a_map; |
|
1518 } |
|
1519 |
|
1520 tree_constant& |
1827
|
1521 TC_REP::lookup_map_element (const string& name, bool insert, bool silent) |
1299
|
1522 { |
|
1523 static tree_constant retval; |
|
1524 |
|
1525 if (type_tag == map_constant) |
|
1526 { |
|
1527 Pix idx = a_map->seek (name); |
|
1528 |
|
1529 if (idx) |
|
1530 return a_map->contents (idx); |
|
1531 else if (insert) |
|
1532 return (*a_map) [name]; |
|
1533 else if (! silent) |
1755
|
1534 error ("structure has no member `%s'", name.c_str ()); |
1299
|
1535 } |
|
1536 else if (! silent) |
|
1537 error ("invalid structure access attempted"); |
|
1538 |
|
1539 return retval; |
|
1540 } |
|
1541 |
|
1542 // This could be made more efficient by doing all the work here rather |
|
1543 // than relying on matrix_value() to do any possible type conversions. |
|
1544 |
|
1545 ColumnVector |
1827
|
1546 TC_REP::vector_value (bool force_string_conv, |
|
1547 bool force_vector_conversion) const |
1299
|
1548 { |
|
1549 ColumnVector retval; |
|
1550 |
1827
|
1551 Matrix m = matrix_value (force_string_conv); |
1299
|
1552 |
|
1553 if (error_state) |
|
1554 return retval; |
|
1555 |
|
1556 int nr = m.rows (); |
|
1557 int nc = m.columns (); |
|
1558 if (nr == 1) |
|
1559 { |
|
1560 retval.resize (nc); |
|
1561 for (int i = 0; i < nc; i++) |
|
1562 retval.elem (i) = m (0, i); |
|
1563 } |
|
1564 else if (nc == 1) |
|
1565 { |
|
1566 retval.resize (nr); |
|
1567 for (int i = 0; i < nr; i++) |
|
1568 retval.elem (i) = m.elem (i, 0); |
|
1569 } |
|
1570 else if (nr > 0 && nc > 0 |
|
1571 && (user_pref.do_fortran_indexing || force_vector_conversion)) |
|
1572 { |
|
1573 retval.resize (nr * nc); |
|
1574 int k = 0; |
|
1575 for (int j = 0; j < nc; j++) |
|
1576 for (int i = 0; i < nr; i++) |
|
1577 retval.elem (k++) = m.elem (i, j); |
|
1578 } |
|
1579 else |
|
1580 gripe_invalid_conversion ("real matrix", "real vector"); |
|
1581 |
|
1582 return retval; |
|
1583 } |
|
1584 |
|
1585 // This could be made more efficient by doing all the work here rather |
|
1586 // than relying on complex_matrix_value() to do any possible type |
|
1587 // conversions. |
|
1588 |
|
1589 ComplexColumnVector |
1827
|
1590 TC_REP::complex_vector_value (bool force_string_conv, |
|
1591 bool force_vector_conversion) const |
1299
|
1592 { |
|
1593 ComplexColumnVector retval; |
|
1594 |
1827
|
1595 ComplexMatrix m = complex_matrix_value (force_string_conv); |
1299
|
1596 |
|
1597 if (error_state) |
|
1598 return retval; |
|
1599 |
|
1600 int nr = m.rows (); |
|
1601 int nc = m.columns (); |
|
1602 if (nr == 1) |
|
1603 { |
|
1604 retval.resize (nc); |
|
1605 for (int i = 0; i < nc; i++) |
|
1606 retval.elem (i) = m (0, i); |
|
1607 } |
|
1608 else if (nc == 1) |
|
1609 { |
|
1610 retval.resize (nr); |
|
1611 for (int i = 0; i < nr; i++) |
|
1612 retval.elem (i) = m.elem (i, 0); |
|
1613 } |
|
1614 else if (nr > 0 && nc > 0 |
|
1615 && (user_pref.do_fortran_indexing || force_vector_conversion)) |
|
1616 { |
|
1617 retval.resize (nr * nc); |
|
1618 int k = 0; |
|
1619 for (int j = 0; j < nc; j++) |
|
1620 for (int i = 0; i < nr; i++) |
|
1621 retval.elem (k++) = m.elem (i, j); |
|
1622 } |
|
1623 else |
|
1624 gripe_invalid_conversion ("complex matrix", "complex vector"); |
|
1625 |
|
1626 return retval; |
|
1627 } |
|
1628 |
|
1629 tree_constant |
|
1630 TC_REP::convert_to_str (void) const |
|
1631 { |
|
1632 tree_constant retval; |
|
1633 |
|
1634 switch (type_tag) |
|
1635 { |
|
1636 case complex_scalar_constant: |
|
1637 case scalar_constant: |
|
1638 { |
|
1639 double d = double_value (); |
|
1640 |
|
1641 if (xisnan (d)) |
|
1642 { |
|
1643 ::error ("invalid conversion from NaN to character"); |
|
1644 return retval; |
|
1645 } |
|
1646 else |
|
1647 { |
1358
|
1648 // XXX FIXME XXX -- warn about out of range conversions? |
|
1649 |
1299
|
1650 int i = NINT (d); |
|
1651 char s[2]; |
|
1652 s[0] = (char) i; |
|
1653 s[1] = '\0'; |
1572
|
1654 retval = tree_constant (s, 1); |
1299
|
1655 } |
|
1656 } |
|
1657 break; |
|
1658 |
|
1659 case complex_matrix_constant: |
|
1660 case matrix_constant: |
|
1661 { |
|
1662 if (rows () == 0 && columns () == 0) |
|
1663 { |
|
1664 char s = '\0'; |
1572
|
1665 retval = tree_constant (&s, 1); |
1299
|
1666 } |
|
1667 else |
|
1668 { |
1355
|
1669 Matrix m = matrix_value (); |
|
1670 |
|
1671 int nr = m.rows (); |
|
1672 int nc = m.columns (); |
|
1673 |
|
1674 if (nr == 0 || nc == 0) |
1299
|
1675 { |
|
1676 char s = '\0'; |
1572
|
1677 retval = tree_constant (&s, 1); |
1299
|
1678 } |
|
1679 else |
|
1680 { |
1572
|
1681 charMatrix chm (nr, nc); |
|
1682 |
|
1683 for (int j = 0; j < nc; j++) |
1299
|
1684 { |
1572
|
1685 for (int i = 0; i < nr; i++) |
1299
|
1686 { |
1355
|
1687 double d = m.elem (i, j); |
|
1688 |
|
1689 if (xisnan (d)) |
|
1690 { |
|
1691 ::error ("invalid conversion from NaN to character"); |
|
1692 return retval; |
|
1693 } |
|
1694 else |
|
1695 { |
1358
|
1696 // XXX FIXME XXX -- warn about out of |
|
1697 // range conversions? |
1355
|
1698 |
|
1699 int ival = NINT (d); |
1572
|
1700 chm.elem (i, j) = (char) ival; |
1355
|
1701 } |
1299
|
1702 } |
|
1703 } |
1355
|
1704 |
1572
|
1705 retval = tree_constant (chm, 1); |
1299
|
1706 } |
|
1707 } |
|
1708 } |
|
1709 break; |
|
1710 |
|
1711 case range_constant: |
|
1712 { |
|
1713 Range r = range_value (); |
|
1714 double b = r.base (); |
|
1715 double incr = r.inc (); |
|
1716 int nel = r.nelem (); |
|
1717 char *s = new char [nel+1]; |
|
1718 s[nel] = '\0'; |
|
1719 for (int i = 0; i < nel; i++) |
|
1720 { |
|
1721 double d = b + i * incr; |
|
1722 |
|
1723 if (xisnan (d)) |
|
1724 { |
|
1725 ::error ("invalid conversion from NaN to character"); |
|
1726 delete [] s; |
|
1727 return retval; |
|
1728 } |
|
1729 else |
|
1730 { |
1358
|
1731 // XXX FIXME XXX -- warn about out of range |
|
1732 // conversions? |
|
1733 |
1299
|
1734 int ival = NINT (d); |
|
1735 s[i] = (char) ival; |
|
1736 } |
|
1737 } |
1572
|
1738 retval = tree_constant (s, 1); |
1299
|
1739 delete [] s; |
|
1740 } |
|
1741 break; |
|
1742 |
1572
|
1743 case char_matrix_constant: |
|
1744 retval = tree_constant (*char_matrix, 1); |
|
1745 break; |
|
1746 |
|
1747 case char_matrix_constant_str: |
|
1748 retval = tree_constant (*char_matrix, 1); |
1299
|
1749 break; |
|
1750 |
|
1751 default: |
|
1752 gripe_invalid_conversion (type_as_string (), "string"); |
|
1753 break; |
|
1754 } |
|
1755 |
|
1756 return retval; |
|
1757 } |
|
1758 |
|
1759 void |
|
1760 TC_REP::convert_to_row_or_column_vector (void) |
|
1761 { |
|
1762 assert (type_tag == matrix_constant || type_tag == complex_matrix_constant); |
|
1763 |
|
1764 int nr = rows (); |
|
1765 int nc = columns (); |
|
1766 |
|
1767 if (nr == 1 || nc == 1) |
|
1768 return; |
|
1769 |
|
1770 int len = nr * nc; |
|
1771 |
|
1772 assert (len > 0); |
|
1773 |
|
1774 int new_nr = 1; |
|
1775 int new_nc = 1; |
|
1776 |
|
1777 if (user_pref.prefer_column_vectors) |
|
1778 new_nr = len; |
|
1779 else |
|
1780 new_nc = len; |
|
1781 |
|
1782 if (type_tag == matrix_constant) |
|
1783 { |
|
1784 Matrix *m = new Matrix (new_nr, new_nc); |
|
1785 |
|
1786 double *cop_out = matrix->fortran_vec (); |
|
1787 |
|
1788 for (int i = 0; i < len; i++) |
|
1789 { |
|
1790 if (new_nr == 1) |
|
1791 m->elem (0, i) = *cop_out++; |
|
1792 else |
|
1793 m->elem (i, 0) = *cop_out++; |
|
1794 } |
|
1795 |
|
1796 delete matrix; |
|
1797 matrix = m; |
|
1798 } |
|
1799 else |
|
1800 { |
|
1801 ComplexMatrix *cm = new ComplexMatrix (new_nr, new_nc); |
|
1802 |
|
1803 Complex *cop_out = complex_matrix->fortran_vec (); |
|
1804 |
|
1805 for (int i = 0; i < len; i++) |
|
1806 { |
|
1807 if (new_nr == 1) |
|
1808 cm->elem (0, i) = *cop_out++; |
|
1809 else |
|
1810 cm->elem (i, 0) = *cop_out++; |
|
1811 } |
|
1812 |
|
1813 delete complex_matrix; |
|
1814 complex_matrix = cm; |
|
1815 } |
|
1816 } |
|
1817 |
|
1818 void |
1827
|
1819 TC_REP::convert_to_matrix_type (bool make_complex) |
1558
|
1820 { |
|
1821 switch (type_tag) |
|
1822 { |
|
1823 case complex_scalar_constant: |
|
1824 { |
|
1825 Complex *old_complex = complex_scalar; |
|
1826 complex_matrix = new ComplexMatrix (1, 1, *complex_scalar); |
|
1827 type_tag = complex_matrix_constant; |
|
1828 delete old_complex; |
|
1829 } |
|
1830 break; |
|
1831 |
|
1832 case scalar_constant: |
|
1833 { |
1581
|
1834 if (make_complex) |
|
1835 { |
|
1836 complex_matrix = new ComplexMatrix (1, 1, scalar); |
|
1837 type_tag = complex_matrix_constant; |
|
1838 } |
|
1839 else |
|
1840 { |
|
1841 matrix = new Matrix (1, 1, scalar); |
|
1842 type_tag = matrix_constant; |
|
1843 } |
1558
|
1844 } |
|
1845 break; |
|
1846 |
|
1847 case unknown_constant: |
|
1848 { |
1581
|
1849 if (make_complex) |
|
1850 { |
|
1851 complex_matrix = new ComplexMatrix (); |
|
1852 type_tag = complex_matrix_constant; |
|
1853 } |
|
1854 else |
|
1855 { |
|
1856 matrix = new Matrix (); |
|
1857 type_tag = matrix_constant; |
|
1858 } |
1558
|
1859 } |
|
1860 break; |
|
1861 |
1572
|
1862 case range_constant: |
|
1863 { |
1581
|
1864 if (make_complex) |
|
1865 { |
|
1866 ComplexMatrix *tmp = new ComplexMatrix (range->matrix_value ()); |
|
1867 delete range; |
|
1868 complex_matrix = tmp; |
|
1869 type_tag = complex_matrix_constant; |
|
1870 } |
|
1871 else |
|
1872 { |
|
1873 Matrix *tmp = new Matrix (range->matrix_value ()); |
|
1874 delete range; |
|
1875 matrix = tmp; |
|
1876 type_tag = matrix_constant; |
|
1877 } |
1572
|
1878 } |
|
1879 break; |
|
1880 |
1558
|
1881 default: |
|
1882 panic_impossible (); |
|
1883 break; |
|
1884 } |
|
1885 } |
|
1886 |
|
1887 void |
1827
|
1888 TC_REP::force_numeric (bool force_string_conv) |
1299
|
1889 { |
|
1890 switch (type_tag) |
|
1891 { |
|
1892 case scalar_constant: |
|
1893 case matrix_constant: |
|
1894 case complex_scalar_constant: |
|
1895 case complex_matrix_constant: |
1572
|
1896 case char_matrix_constant: |
1299
|
1897 break; |
|
1898 |
1572
|
1899 case char_matrix_constant_str: |
1299
|
1900 { |
1827
|
1901 if (! force_string_conv && ! user_pref.implicit_str_to_num_ok) |
1299
|
1902 { |
1490
|
1903 ::error ("string to numeric conversion failed --\ |
|
1904 default conversion turned off"); |
1299
|
1905 return; |
|
1906 } |
|
1907 |
1572
|
1908 int nr = char_matrix->rows (); |
|
1909 int nc = char_matrix->cols (); |
1355
|
1910 |
|
1911 if (nr == 1 && nc == 1) |
1299
|
1912 { |
|
1913 type_tag = scalar_constant; |
1572
|
1914 double tmp = toascii ((int) char_matrix->elem (0, 0)); |
|
1915 delete char_matrix; |
|
1916 scalar = tmp; |
1299
|
1917 } |
1355
|
1918 else if (nr == 0 || nc == 0) |
1299
|
1919 { |
1572
|
1920 delete char_matrix; |
1299
|
1921 type_tag = matrix_constant; |
|
1922 matrix = new Matrix (0, 0); |
|
1923 } |
1355
|
1924 else if (nr > 0 && nc > 0) |
|
1925 { |
|
1926 type_tag = matrix_constant; |
|
1927 |
|
1928 Matrix *tm = new Matrix (nr, nc); |
|
1929 |
|
1930 for (int i = 0; i < nr; i++) |
|
1931 { |
|
1932 for (int j = 0; j < nc; j++) |
|
1933 { |
1572
|
1934 int c = (int) char_matrix->elem (i, j); |
1355
|
1935 tm->elem (i, j) = toascii (c); |
|
1936 } |
|
1937 } |
1572
|
1938 delete char_matrix; |
1355
|
1939 matrix = tm; |
|
1940 } |
1299
|
1941 else |
|
1942 panic_impossible (); |
|
1943 } |
|
1944 break; |
|
1945 |
|
1946 case range_constant: |
|
1947 { |
|
1948 int len = range->nelem (); |
|
1949 if (len > 1) |
|
1950 { |
|
1951 type_tag = matrix_constant; |
|
1952 Matrix *tm = new Matrix (1, len); |
|
1953 double b = range->base (); |
|
1954 double increment = range->inc (); |
|
1955 for (int i = 0; i < len; i++) |
|
1956 tm->elem (0, i) = b + i * increment; |
1572
|
1957 delete range; |
1299
|
1958 matrix = tm; |
|
1959 } |
|
1960 else if (len == 1) |
|
1961 { |
|
1962 type_tag = scalar_constant; |
|
1963 scalar = range->base (); |
|
1964 } |
|
1965 } |
|
1966 break; |
|
1967 |
|
1968 default: |
|
1969 gripe_invalid_conversion (type_as_string (), "numeric type"); |
|
1970 break; |
|
1971 } |
|
1972 } |
|
1973 |
|
1974 tree_constant |
1827
|
1975 TC_REP::make_numeric (bool force_string_conv) const |
1299
|
1976 { |
|
1977 tree_constant retval; |
|
1978 |
|
1979 switch (type_tag) |
|
1980 { |
|
1981 case scalar_constant: |
1355
|
1982 retval = scalar; |
1299
|
1983 break; |
|
1984 |
|
1985 case matrix_constant: |
1355
|
1986 retval = *matrix; |
1299
|
1987 break; |
|
1988 |
|
1989 case complex_scalar_constant: |
1355
|
1990 retval = *complex_scalar; |
1299
|
1991 break; |
|
1992 |
|
1993 case complex_matrix_constant: |
1355
|
1994 retval = *complex_matrix; |
1299
|
1995 break; |
|
1996 |
1572
|
1997 case char_matrix_constant: |
|
1998 retval = *char_matrix; |
|
1999 break; |
|
2000 |
|
2001 case char_matrix_constant_str: |
|
2002 { |
1827
|
2003 int flag = force_string_conv; |
1572
|
2004 if (! flag) |
|
2005 flag = user_pref.implicit_str_to_num_ok; |
|
2006 |
|
2007 if (flag < 0) |
|
2008 warn_implicit_conversion ("string", "char matrix"); |
|
2009 |
|
2010 if (flag) |
|
2011 { |
|
2012 retval = *char_matrix; |
1827
|
2013 retval.force_numeric (force_string_conv); |
1572
|
2014 } |
|
2015 else |
|
2016 gripe_invalid_conversion ("string", "char matrix"); |
|
2017 } |
1299
|
2018 break; |
|
2019 |
|
2020 case range_constant: |
1355
|
2021 retval = *range; |
1827
|
2022 retval.force_numeric (force_string_conv); |
1299
|
2023 break; |
|
2024 |
|
2025 default: |
|
2026 gripe_invalid_conversion (type_as_string (), "numeric value"); |
|
2027 break; |
|
2028 } |
|
2029 |
|
2030 return retval; |
|
2031 } |
|
2032 |
|
2033 void |
|
2034 TC_REP::bump_value (tree_expression::type etype) |
|
2035 { |
|
2036 switch (etype) |
|
2037 { |
|
2038 case tree_expression::increment: |
|
2039 switch (type_tag) |
|
2040 { |
|
2041 case scalar_constant: |
|
2042 scalar++; |
|
2043 break; |
|
2044 |
|
2045 case matrix_constant: |
|
2046 *matrix = *matrix + 1.0; |
|
2047 break; |
|
2048 |
|
2049 case complex_scalar_constant: |
|
2050 *complex_scalar = *complex_scalar + 1.0; |
|
2051 break; |
|
2052 |
|
2053 case complex_matrix_constant: |
|
2054 *complex_matrix = *complex_matrix + 1.0; |
|
2055 break; |
|
2056 |
|
2057 case range_constant: |
|
2058 range->set_base (range->base () + 1.0); |
|
2059 range->set_limit (range->limit () + 1.0); |
|
2060 break; |
|
2061 |
|
2062 default: |
|
2063 gripe_wrong_type_arg ("operator ++", type_as_string ()); |
|
2064 break; |
|
2065 } |
|
2066 break; |
|
2067 |
|
2068 case tree_expression::decrement: |
|
2069 switch (type_tag) |
|
2070 { |
|
2071 case scalar_constant: |
|
2072 scalar--; |
|
2073 break; |
|
2074 |
|
2075 case matrix_constant: |
|
2076 *matrix = *matrix - 1.0; |
|
2077 break; |
|
2078 |
|
2079 case range_constant: |
|
2080 range->set_base (range->base () - 1.0); |
|
2081 range->set_limit (range->limit () - 1.0); |
|
2082 break; |
|
2083 |
|
2084 default: |
|
2085 gripe_wrong_type_arg ("operator --", type_as_string ()); |
|
2086 break; |
|
2087 } |
|
2088 break; |
|
2089 |
|
2090 default: |
|
2091 panic_impossible (); |
|
2092 break; |
|
2093 } |
|
2094 } |
|
2095 |
|
2096 void |
|
2097 TC_REP::resize (int i, int j) |
|
2098 { |
|
2099 switch (type_tag) |
|
2100 { |
|
2101 case matrix_constant: |
|
2102 matrix->resize (i, j); |
|
2103 break; |
|
2104 |
|
2105 case complex_matrix_constant: |
|
2106 complex_matrix->resize (i, j); |
|
2107 break; |
|
2108 |
|
2109 default: |
|
2110 gripe_wrong_type_arg ("resize", type_as_string ()); |
|
2111 break; |
|
2112 } |
|
2113 } |
|
2114 |
|
2115 void |
|
2116 TC_REP::resize (int i, int j, double val) |
|
2117 { |
|
2118 switch (type_tag) |
|
2119 { |
|
2120 case matrix_constant: |
|
2121 matrix->resize (i, j, val); |
|
2122 break; |
|
2123 |
|
2124 case complex_matrix_constant: |
|
2125 complex_matrix->resize (i, j, val); |
|
2126 break; |
|
2127 |
|
2128 default: |
|
2129 gripe_wrong_type_arg ("resize", type_as_string ()); |
|
2130 break; |
|
2131 } |
|
2132 } |
|
2133 |
|
2134 void |
1755
|
2135 TC_REP::stash_original_text (const string &s) |
1299
|
2136 { |
1755
|
2137 orig_text = s; |
1299
|
2138 } |
|
2139 |
|
2140 void |
|
2141 TC_REP::maybe_mutate (void) |
|
2142 { |
|
2143 switch (type_tag) |
|
2144 { |
|
2145 case complex_scalar_constant: |
|
2146 if (::imag (*complex_scalar) == 0.0) |
|
2147 { |
|
2148 double d = ::real (*complex_scalar); |
|
2149 delete complex_scalar; |
|
2150 scalar = d; |
|
2151 type_tag = scalar_constant; |
|
2152 } |
|
2153 break; |
|
2154 |
|
2155 case complex_matrix_constant: |
|
2156 if (! any_element_is_complex (*complex_matrix)) |
|
2157 { |
|
2158 Matrix *m = new Matrix (::real (*complex_matrix)); |
|
2159 delete complex_matrix; |
|
2160 matrix = m; |
|
2161 type_tag = matrix_constant; |
|
2162 } |
|
2163 break; |
|
2164 |
|
2165 default: |
|
2166 break; |
|
2167 } |
|
2168 |
1358
|
2169 // Avoid calling rows() and columns() for things like magic_colon. |
1299
|
2170 |
|
2171 int nr = 1; |
|
2172 int nc = 1; |
|
2173 if (type_tag == matrix_constant |
|
2174 || type_tag == complex_matrix_constant |
|
2175 || type_tag == range_constant) |
|
2176 { |
|
2177 nr = rows (); |
|
2178 nc = columns (); |
|
2179 } |
|
2180 |
|
2181 switch (type_tag) |
|
2182 { |
|
2183 case matrix_constant: |
|
2184 if (nr == 1 && nc == 1) |
|
2185 { |
|
2186 double d = matrix->elem (0, 0); |
|
2187 delete matrix; |
|
2188 scalar = d; |
|
2189 type_tag = scalar_constant; |
|
2190 } |
|
2191 break; |
|
2192 |
|
2193 case complex_matrix_constant: |
|
2194 if (nr == 1 && nc == 1) |
|
2195 { |
|
2196 Complex c = complex_matrix->elem (0, 0); |
|
2197 delete complex_matrix; |
|
2198 complex_scalar = new Complex (c); |
|
2199 type_tag = complex_scalar_constant; |
|
2200 } |
|
2201 break; |
|
2202 |
|
2203 case range_constant: |
|
2204 if (nr == 1 && nc == 1) |
|
2205 { |
|
2206 double d = range->base (); |
|
2207 delete range; |
|
2208 scalar = d; |
|
2209 type_tag = scalar_constant; |
|
2210 } |
|
2211 break; |
|
2212 |
|
2213 default: |
|
2214 break; |
|
2215 } |
|
2216 } |
|
2217 |
|
2218 void |
|
2219 TC_REP::print (ostream& output_buf) |
|
2220 { |
|
2221 if (error_state) |
|
2222 return; |
|
2223 |
|
2224 switch (type_tag) |
|
2225 { |
|
2226 case scalar_constant: |
1973
|
2227 octave_print_internal (output_buf, scalar, false); |
1299
|
2228 break; |
|
2229 |
|
2230 case matrix_constant: |
1973
|
2231 octave_print_internal (output_buf, *matrix, false, |
|
2232 struct_indent); |
1299
|
2233 break; |
|
2234 |
|
2235 case complex_scalar_constant: |
1973
|
2236 octave_print_internal (output_buf, *complex_scalar, false); |
1299
|
2237 break; |
|
2238 |
|
2239 case complex_matrix_constant: |
1973
|
2240 octave_print_internal (output_buf, *complex_matrix, false, |
|
2241 struct_indent); |
1299
|
2242 break; |
|
2243 |
1572
|
2244 case char_matrix_constant: |
1973
|
2245 octave_print_internal (output_buf, *char_matrix, false, |
|
2246 struct_indent); |
1572
|
2247 break; |
|
2248 |
|
2249 case char_matrix_constant_str: |
1973
|
2250 octave_print_internal (output_buf, *char_matrix, false, true, |
|
2251 struct_indent); |
1299
|
2252 break; |
|
2253 |
|
2254 case range_constant: |
1973
|
2255 octave_print_internal (output_buf, *range, false, struct_indent); |
1299
|
2256 break; |
|
2257 |
|
2258 case map_constant: |
|
2259 { |
1358
|
2260 // XXX FIXME XXX -- would be nice to print the output in some |
|
2261 // standard order. Maybe all substructures first, maybe |
|
2262 // alphabetize entries, etc. |
|
2263 |
1299
|
2264 begin_unwind_frame ("TC_REP_print"); |
|
2265 |
1973
|
2266 unwind_protect_int (struct_indent); |
1299
|
2267 unwind_protect_int (user_pref.struct_levels_to_print); |
|
2268 |
|
2269 if (user_pref.struct_levels_to_print-- > 0) |
|
2270 { |
1973
|
2271 output_buf.form ("\n%*s{\n", struct_indent, ""); |
|
2272 |
|
2273 increment_struct_indent (); |
|
2274 |
|
2275 Pix p = a_map->first (); |
|
2276 |
|
2277 while (p) |
1299
|
2278 { |
1973
|
2279 bool pad_after = false; |
|
2280 |
1755
|
2281 string key = a_map->key (p); |
1299
|
2282 tree_constant val = a_map->contents (p); |
|
2283 |
1973
|
2284 a_map->next (p); |
|
2285 |
|
2286 output_buf.form ("%*s%s =", struct_indent, |
1755
|
2287 "", key.c_str ()); |
|
2288 |
1973
|
2289 if (val.print_as_scalar ()) |
|
2290 output_buf << " "; |
|
2291 else if (val.print_as_structure ()) |
|
2292 { |
|
2293 if (p) |
|
2294 pad_after = true; |
|
2295 } |
|
2296 else |
|
2297 { |
|
2298 if (p) |
|
2299 pad_after = true; |
|
2300 |
|
2301 output_buf << "\n\n"; |
|
2302 } |
1299
|
2303 |
|
2304 val.print (output_buf); |
1973
|
2305 |
|
2306 if (pad_after) |
|
2307 output_buf << "\n"; |
1299
|
2308 } |
|
2309 |
1973
|
2310 decrement_struct_indent (); |
|
2311 |
|
2312 output_buf.form ("%*s%s", struct_indent, "", "}\n"); |
1299
|
2313 } |
|
2314 else |
1973
|
2315 output_buf << " <structure>\n"; |
1299
|
2316 |
|
2317 run_unwind_frame ("TC_REP_print"); |
|
2318 } |
|
2319 break; |
|
2320 |
|
2321 case unknown_constant: |
|
2322 case magic_colon: |
|
2323 case all_va_args: |
|
2324 panic_impossible (); |
|
2325 break; |
|
2326 } |
|
2327 } |
|
2328 |
|
2329 void |
|
2330 TC_REP::print_code (ostream& os) |
|
2331 { |
|
2332 switch (type_tag) |
|
2333 { |
|
2334 case scalar_constant: |
1755
|
2335 if (orig_text.empty ()) |
|
2336 octave_print_internal (os, scalar, 1); |
|
2337 else |
1299
|
2338 os << orig_text; |
|
2339 break; |
|
2340 |
|
2341 case matrix_constant: |
|
2342 octave_print_internal (os, *matrix, 1); |
|
2343 break; |
|
2344 |
|
2345 case complex_scalar_constant: |
|
2346 { |
|
2347 double re = complex_scalar->real (); |
|
2348 double im = complex_scalar->imag (); |
|
2349 |
1358
|
2350 // If we have the original text and a pure imaginary, just |
|
2351 // print the original text, because this must be a constant |
|
2352 // that was parsed as part of a function. |
1299
|
2353 |
1755
|
2354 if (! orig_text.empty () && re == 0.0 && im > 0.0) |
1299
|
2355 os << orig_text; |
|
2356 else |
|
2357 octave_print_internal (os, *complex_scalar, 1); |
|
2358 } |
|
2359 break; |
|
2360 |
|
2361 case complex_matrix_constant: |
|
2362 octave_print_internal (os, *complex_matrix, 1); |
|
2363 break; |
|
2364 |
1572
|
2365 case char_matrix_constant: |
|
2366 octave_print_internal (os, *char_matrix, 1); |
|
2367 break; |
|
2368 |
|
2369 case char_matrix_constant_str: |
|
2370 octave_print_internal (os, *char_matrix, 1, 1); |
1299
|
2371 break; |
|
2372 |
|
2373 case range_constant: |
|
2374 octave_print_internal (os, *range, 1); |
|
2375 break; |
|
2376 |
|
2377 case magic_colon: |
|
2378 os << ":"; |
|
2379 break; |
|
2380 |
|
2381 case all_va_args: |
|
2382 os << "all_va_args"; |
|
2383 break; |
|
2384 |
|
2385 case map_constant: |
|
2386 case unknown_constant: |
|
2387 panic_impossible (); |
|
2388 break; |
|
2389 } |
|
2390 } |
|
2391 |
|
2392 void |
|
2393 TC_REP::gripe_wrong_type_arg (const char *name, |
|
2394 const tree_constant_rep& tcr) const |
|
2395 { |
|
2396 if (name) |
|
2397 ::error ("%s: wrong type argument `%s'", name, tcr.type_as_string ()); |
|
2398 else |
|
2399 ::error ("wrong type argument `%s'", name, tcr.type_as_string ()); |
|
2400 } |
|
2401 |
|
2402 char * |
|
2403 TC_REP::type_as_string (void) const |
|
2404 { |
|
2405 switch (type_tag) |
|
2406 { |
|
2407 case scalar_constant: |
|
2408 return "real scalar"; |
|
2409 |
|
2410 case matrix_constant: |
|
2411 return "real matrix"; |
|
2412 |
|
2413 case complex_scalar_constant: |
|
2414 return "complex scalar"; |
|
2415 |
|
2416 case complex_matrix_constant: |
|
2417 return "complex matrix"; |
|
2418 |
1572
|
2419 case char_matrix_constant: |
|
2420 return "char matrix"; |
|
2421 |
|
2422 case char_matrix_constant_str: |
1299
|
2423 return "string"; |
|
2424 |
|
2425 case range_constant: |
|
2426 return "range"; |
|
2427 |
|
2428 case map_constant: |
|
2429 return "structure"; |
|
2430 |
|
2431 default: |
|
2432 return "<unknown type>"; |
|
2433 } |
|
2434 } |
|
2435 |
|
2436 tree_constant |
|
2437 do_binary_op (tree_constant& a, tree_constant& b, tree_expression::type t) |
|
2438 { |
|
2439 tree_constant retval; |
|
2440 |
1827
|
2441 bool first_empty = (a.rows () == 0 || a.columns () == 0); |
|
2442 bool second_empty = (b.rows () == 0 || b.columns () == 0); |
1299
|
2443 |
|
2444 if (first_empty || second_empty) |
|
2445 { |
|
2446 int flag = user_pref.propagate_empty_matrices; |
|
2447 if (flag < 0) |
|
2448 warning ("binary operation on empty matrix"); |
|
2449 else if (flag == 0) |
|
2450 { |
|
2451 ::error ("invalid binary operation on empty matrix"); |
|
2452 return retval; |
|
2453 } |
|
2454 } |
|
2455 |
|
2456 tree_constant tmp_a = a.make_numeric (); |
|
2457 |
|
2458 if (error_state) |
|
2459 return retval; |
|
2460 |
|
2461 tree_constant tmp_b = b.make_numeric (); |
|
2462 |
|
2463 if (error_state) |
|
2464 return retval; |
|
2465 |
|
2466 TC_REP::constant_type a_type = tmp_a.const_type (); |
|
2467 TC_REP::constant_type b_type = tmp_b.const_type (); |
|
2468 |
|
2469 double d1, d2; |
|
2470 Matrix m1, m2; |
|
2471 Complex c1, c2; |
|
2472 ComplexMatrix cm1, cm2; |
|
2473 |
|
2474 switch (a_type) |
|
2475 { |
|
2476 case TC_REP::scalar_constant: |
|
2477 |
|
2478 d1 = tmp_a.double_value (); |
|
2479 |
|
2480 switch (b_type) |
|
2481 { |
|
2482 case TC_REP::scalar_constant: |
|
2483 d2 = tmp_b.double_value (); |
|
2484 retval = do_binary_op (d1, d2, t); |
|
2485 break; |
|
2486 |
|
2487 case TC_REP::matrix_constant: |
1572
|
2488 case TC_REP::char_matrix_constant: |
1299
|
2489 m2 = tmp_b.matrix_value (); |
|
2490 retval = do_binary_op (d1, m2, t); |
|
2491 break; |
|
2492 |
|
2493 case TC_REP::complex_scalar_constant: |
|
2494 c2 = tmp_b.complex_value (); |
|
2495 retval = do_binary_op (d1, c2, t); |
|
2496 break; |
|
2497 |
|
2498 case TC_REP::complex_matrix_constant: |
|
2499 cm2 = tmp_b.complex_matrix_value (); |
|
2500 retval = do_binary_op (d1, cm2, t); |
|
2501 break; |
|
2502 |
|
2503 default: |
|
2504 gripe_wrong_type_arg_for_binary_op (tmp_b); |
|
2505 break; |
|
2506 } |
|
2507 break; |
|
2508 |
|
2509 case TC_REP::matrix_constant: |
1572
|
2510 case TC_REP::char_matrix_constant: |
1299
|
2511 |
|
2512 m1 = tmp_a.matrix_value (); |
|
2513 |
|
2514 switch (b_type) |
|
2515 { |
|
2516 case TC_REP::scalar_constant: |
|
2517 d2 = tmp_b.double_value (); |
|
2518 retval = do_binary_op (m1, d2, t); |
|
2519 break; |
|
2520 |
|
2521 case TC_REP::matrix_constant: |
1572
|
2522 case TC_REP::char_matrix_constant: |
1299
|
2523 m2 = tmp_b.matrix_value (); |
|
2524 retval = do_binary_op (m1, m2, t); |
|
2525 break; |
|
2526 |
|
2527 case TC_REP::complex_scalar_constant: |
|
2528 c2 = tmp_b.complex_value (); |
|
2529 retval = do_binary_op (m1, c2, t); |
|
2530 break; |
|
2531 |
|
2532 case TC_REP::complex_matrix_constant: |
|
2533 cm2 = tmp_b.complex_matrix_value (); |
|
2534 retval = do_binary_op (m1, cm2, t); |
|
2535 break; |
|
2536 |
|
2537 default: |
|
2538 gripe_wrong_type_arg_for_binary_op (tmp_b); |
|
2539 break; |
|
2540 } |
|
2541 break; |
|
2542 |
|
2543 case TC_REP::complex_scalar_constant: |
|
2544 |
|
2545 c1 = tmp_a.complex_value (); |
|
2546 |
|
2547 switch (b_type) |
|
2548 { |
|
2549 case TC_REP::scalar_constant: |
|
2550 d2 = tmp_b.double_value (); |
|
2551 retval = do_binary_op (c1, d2, t); |
|
2552 break; |
|
2553 |
|
2554 case TC_REP::matrix_constant: |
1572
|
2555 case TC_REP::char_matrix_constant: |
1299
|
2556 m2 = tmp_b.matrix_value (); |
|
2557 retval = do_binary_op (c1, m2, t); |
|
2558 break; |
|
2559 |
|
2560 case TC_REP::complex_scalar_constant: |
|
2561 c2 = tmp_b.complex_value (); |
|
2562 retval = do_binary_op (c1, c2, t); |
|
2563 break; |
|
2564 |
|
2565 case TC_REP::complex_matrix_constant: |
|
2566 cm2 = tmp_b.complex_matrix_value (); |
|
2567 retval = do_binary_op (c1, cm2, t); |
|
2568 break; |
|
2569 |
|
2570 default: |
|
2571 gripe_wrong_type_arg_for_binary_op (tmp_b); |
|
2572 break; |
|
2573 } |
|
2574 break; |
|
2575 |
|
2576 case TC_REP::complex_matrix_constant: |
|
2577 |
|
2578 cm1 = tmp_a.complex_matrix_value (); |
|
2579 |
|
2580 switch (b_type) |
|
2581 { |
|
2582 case TC_REP::scalar_constant: |
|
2583 d2 = tmp_b.double_value (); |
|
2584 retval = do_binary_op (cm1, d2, t); |
|
2585 break; |
|
2586 |
|
2587 case TC_REP::matrix_constant: |
1572
|
2588 case TC_REP::char_matrix_constant: |
1299
|
2589 m2 = tmp_b.matrix_value (); |
|
2590 retval = do_binary_op (cm1, m2, t); |
|
2591 break; |
|
2592 |
|
2593 case TC_REP::complex_scalar_constant: |
|
2594 c2 = tmp_b.complex_value (); |
|
2595 retval = do_binary_op (cm1, c2, t); |
|
2596 break; |
|
2597 |
|
2598 case TC_REP::complex_matrix_constant: |
|
2599 cm2 = tmp_b.complex_matrix_value (); |
|
2600 retval = do_binary_op (cm1, cm2, t); |
|
2601 break; |
|
2602 |
|
2603 default: |
|
2604 gripe_wrong_type_arg_for_binary_op (tmp_b); |
|
2605 break; |
|
2606 } |
|
2607 break; |
|
2608 |
|
2609 default: |
|
2610 gripe_wrong_type_arg_for_binary_op (tmp_a); |
|
2611 break; |
|
2612 } |
|
2613 |
|
2614 return retval; |
|
2615 } |
|
2616 |
|
2617 tree_constant |
|
2618 do_unary_op (tree_constant& a, tree_expression::type t) |
|
2619 { |
|
2620 tree_constant retval; |
|
2621 |
|
2622 if (a.rows () == 0 || a.columns () == 0) |
|
2623 { |
|
2624 int flag = user_pref.propagate_empty_matrices; |
|
2625 if (flag < 0) |
|
2626 warning ("unary operation on empty matrix"); |
|
2627 else if (flag == 0) |
|
2628 { |
|
2629 ::error ("invalid unary operation on empty matrix"); |
|
2630 return retval; |
|
2631 } |
|
2632 } |
|
2633 |
|
2634 tree_constant tmp_a = a.make_numeric (); |
|
2635 |
|
2636 if (error_state) |
|
2637 return retval; |
|
2638 |
|
2639 switch (tmp_a.const_type ()) |
|
2640 { |
|
2641 case TC_REP::scalar_constant: |
|
2642 retval = do_unary_op (tmp_a.double_value (), t); |
|
2643 break; |
|
2644 |
|
2645 case TC_REP::matrix_constant: |
|
2646 { |
|
2647 Matrix m = tmp_a.matrix_value (); |
|
2648 retval = do_unary_op (m, t); |
|
2649 } |
|
2650 break; |
|
2651 |
|
2652 case TC_REP::complex_scalar_constant: |
|
2653 retval = do_unary_op (tmp_a.complex_value (), t); |
|
2654 break; |
|
2655 |
|
2656 case TC_REP::complex_matrix_constant: |
|
2657 { |
|
2658 ComplexMatrix m = tmp_a.complex_matrix_value (); |
|
2659 retval = do_unary_op (m, t); |
|
2660 } |
|
2661 break; |
|
2662 |
|
2663 default: |
|
2664 gripe_wrong_type_arg_for_unary_op (tmp_a); |
|
2665 break; |
|
2666 } |
|
2667 |
|
2668 return retval; |
|
2669 } |
|
2670 |
|
2671 // Indexing operations for the tree-constant representation class. |
1558
|
2672 |
|
2673 void |
|
2674 TC_REP::clear_index (void) |
|
2675 { |
|
2676 switch (type_tag) |
|
2677 { |
|
2678 case matrix_constant: |
|
2679 matrix->clear_index (); |
|
2680 break; |
|
2681 |
|
2682 case TC_REP::complex_matrix_constant: |
|
2683 complex_matrix->clear_index (); |
|
2684 break; |
|
2685 |
1572
|
2686 case char_matrix_constant: |
|
2687 case char_matrix_constant_str: |
|
2688 char_matrix->clear_index (); |
|
2689 break; |
|
2690 |
1558
|
2691 default: |
|
2692 panic_impossible (); |
|
2693 break; |
|
2694 } |
|
2695 } |
|
2696 |
|
2697 #if 0 |
|
2698 void |
|
2699 TC_REP::set_index (double d) |
|
2700 { |
|
2701 switch (type_tag) |
|
2702 { |
|
2703 case matrix_constant: |
|
2704 matrix->set_index (d); |
|
2705 break; |
|
2706 |
|
2707 case TC_REP::complex_matrix_constant: |
|
2708 complex_matrix->set_index (d); |
|
2709 break; |
|
2710 |
1572
|
2711 case TC_REP::char_matrix_constant: |
|
2712 case TC_REP::char_matrix_constant_str: |
|
2713 char_matrix->set_index (d); |
|
2714 break; |
|
2715 |
1558
|
2716 default: |
|
2717 panic_impossible (); |
|
2718 break; |
|
2719 } |
|
2720 } |
|
2721 #endif |
|
2722 |
|
2723 void |
|
2724 TC_REP::set_index (const Range& r) |
|
2725 { |
|
2726 switch (type_tag) |
|
2727 { |
|
2728 case matrix_constant: |
|
2729 matrix->set_index (r); |
|
2730 break; |
|
2731 |
|
2732 case TC_REP::complex_matrix_constant: |
|
2733 complex_matrix->set_index (r); |
|
2734 break; |
|
2735 |
1572
|
2736 case TC_REP::char_matrix_constant: |
|
2737 case TC_REP::char_matrix_constant_str: |
|
2738 char_matrix->set_index (r); |
|
2739 break; |
|
2740 |
1558
|
2741 default: |
|
2742 panic_impossible (); |
|
2743 break; |
|
2744 } |
|
2745 } |
|
2746 |
|
2747 void |
|
2748 TC_REP::set_index (const ColumnVector& v) |
|
2749 { |
|
2750 switch (type_tag) |
|
2751 { |
|
2752 case matrix_constant: |
|
2753 matrix->set_index (v); |
|
2754 break; |
|
2755 |
|
2756 case TC_REP::complex_matrix_constant: |
|
2757 complex_matrix->set_index (v); |
|
2758 break; |
|
2759 |
1572
|
2760 case TC_REP::char_matrix_constant: |
|
2761 case TC_REP::char_matrix_constant_str: |
|
2762 char_matrix->set_index (v); |
|
2763 break; |
|
2764 |
1558
|
2765 default: |
|
2766 panic_impossible (); |
|
2767 break; |
|
2768 } |
|
2769 } |
|
2770 |
|
2771 void |
|
2772 TC_REP::set_index (const Matrix& m) |
|
2773 { |
|
2774 int nr = m.rows (); |
|
2775 int nc = m.cols (); |
|
2776 |
|
2777 if (nr <= 1 || nc <= 1 |
|
2778 || user_pref.do_fortran_indexing) |
|
2779 { |
|
2780 switch (type_tag) |
|
2781 { |
|
2782 case matrix_constant: |
|
2783 matrix->set_index (m); |
|
2784 break; |
|
2785 |
|
2786 case TC_REP::complex_matrix_constant: |
|
2787 complex_matrix->set_index (m); |
|
2788 break; |
|
2789 |
1572
|
2790 case TC_REP::char_matrix_constant: |
|
2791 case TC_REP::char_matrix_constant_str: |
|
2792 char_matrix->set_index (m); |
|
2793 break; |
|
2794 |
1558
|
2795 default: |
|
2796 panic_impossible (); |
|
2797 break; |
|
2798 } |
|
2799 } |
|
2800 else |
|
2801 ::error ("invalid matrix used as index"); |
|
2802 } |
|
2803 |
|
2804 // XXX FIXME XXX -- this should probably be handled some other way... |
|
2805 // The arg here is expected to be ':'. |
|
2806 void |
|
2807 TC_REP::set_index (char c) |
|
2808 { |
|
2809 switch (type_tag) |
|
2810 { |
|
2811 case matrix_constant: |
|
2812 matrix->set_index (c); |
|
2813 break; |
|
2814 |
|
2815 case TC_REP::complex_matrix_constant: |
|
2816 complex_matrix->set_index (c); |
|
2817 break; |
|
2818 |
1572
|
2819 case TC_REP::char_matrix_constant: |
|
2820 case TC_REP::char_matrix_constant_str: |
|
2821 char_matrix->set_index (c); |
|
2822 break; |
|
2823 |
1558
|
2824 default: |
|
2825 panic_impossible (); |
|
2826 break; |
|
2827 } |
|
2828 } |
|
2829 |
|
2830 void |
1827
|
2831 TC_REP::set_index (const Octave_object& args, bool rhs_is_complex) |
1558
|
2832 { |
|
2833 switch (type_tag) |
|
2834 { |
|
2835 case unknown_constant: |
|
2836 case scalar_constant: |
|
2837 case complex_scalar_constant: |
|
2838 case range_constant: |
1581
|
2839 convert_to_matrix_type (rhs_is_complex); |
1558
|
2840 break; |
|
2841 |
|
2842 default: |
|
2843 break; |
|
2844 } |
|
2845 |
|
2846 int n = args.length (); |
|
2847 |
|
2848 for (int i = 0; i < n; i++) |
|
2849 { |
|
2850 tree_constant arg = args (i); |
|
2851 |
|
2852 switch (arg.const_type ()) |
|
2853 { |
|
2854 case range_constant: |
|
2855 set_index (arg.range_value ()); |
|
2856 break; |
|
2857 |
|
2858 case magic_colon: |
|
2859 set_index (':'); |
|
2860 break; |
|
2861 |
|
2862 default: |
|
2863 set_index (arg.matrix_value ()); |
|
2864 break; |
|
2865 } |
|
2866 |
|
2867 if (error_state) |
|
2868 { |
|
2869 clear_index (); |
|
2870 break; |
|
2871 } |
|
2872 } |
|
2873 } |
|
2874 |
1827
|
2875 static inline bool |
1558
|
2876 valid_scalar_indices (const Octave_object& args) |
|
2877 { |
|
2878 int nargin = args.length (); |
|
2879 |
|
2880 for (int i = 0; i < nargin; i++) |
|
2881 if (! args(i).valid_as_scalar_index ()) |
1827
|
2882 return false; |
|
2883 |
|
2884 return true; |
1558
|
2885 } |
1299
|
2886 |
|
2887 tree_constant |
|
2888 TC_REP::do_index (const Octave_object& args) |
|
2889 { |
|
2890 tree_constant retval; |
|
2891 |
|
2892 if (error_state) |
|
2893 return retval; |
|
2894 |
1827
|
2895 bool originally_scalar_type = is_scalar_type (); |
1558
|
2896 |
|
2897 if (originally_scalar_type && valid_scalar_indices (args)) |
1299
|
2898 { |
1558
|
2899 switch (type_tag) |
1299
|
2900 { |
1558
|
2901 case scalar_constant: |
|
2902 retval = scalar; |
|
2903 break; |
|
2904 |
|
2905 case complex_scalar_constant: |
|
2906 retval = *complex_scalar; |
1299
|
2907 break; |
|
2908 |
|
2909 default: |
1558
|
2910 panic_impossible (); |
1299
|
2911 break; |
|
2912 } |
|
2913 } |
|
2914 else |
|
2915 { |
1558
|
2916 set_index (args); |
|
2917 |
|
2918 if (! error_state) |
|
2919 { |
|
2920 switch (type_tag) |
|
2921 { |
|
2922 case range_constant: |
|
2923 force_numeric (); |
|
2924 // Fall through... |
|
2925 |
|
2926 case matrix_constant: |
|
2927 retval = Matrix (matrix->value ()); |
|
2928 break; |
|
2929 |
|
2930 case complex_matrix_constant: |
|
2931 retval = ComplexMatrix (complex_matrix->value ()); |
|
2932 break; |
|
2933 |
1572
|
2934 case char_matrix_constant: |
|
2935 retval = charMatrix (char_matrix->value ()); |
|
2936 break; |
|
2937 |
|
2938 case char_matrix_constant_str: |
|
2939 retval = tree_constant (charMatrix (char_matrix->value ()), 1); |
|
2940 break; |
|
2941 |
1558
|
2942 default: |
|
2943 error ("can't index %s variables", type_as_string ()); |
|
2944 break; |
|
2945 } |
|
2946 } |
1299
|
2947 } |
|
2948 |
|
2949 return retval; |
|
2950 } |
|
2951 |
1558
|
2952 void |
|
2953 TC_REP::maybe_widen (TC_REP::constant_type rhs_type) |
1299
|
2954 { |
1558
|
2955 switch (type_tag) |
1299
|
2956 { |
|
2957 case matrix_constant: |
1558
|
2958 switch (rhs_type) |
|
2959 { |
|
2960 case complex_scalar_constant: |
|
2961 case complex_matrix_constant: |
1299
|
2962 { |
1558
|
2963 ComplexMatrix *cm = new ComplexMatrix (*matrix); |
|
2964 delete matrix; |
|
2965 complex_matrix = cm; |
|
2966 type_tag = complex_matrix_constant; |
1299
|
2967 } |
1558
|
2968 break; |
|
2969 |
|
2970 default: |
|
2971 break; |
|
2972 } |
1299
|
2973 break; |
|
2974 |
1572
|
2975 case char_matrix_constant: |
|
2976 switch (rhs_type) |
|
2977 { |
|
2978 case scalar_constant: |
|
2979 case matrix_constant: |
|
2980 { |
|
2981 Matrix *m = new Matrix (*char_matrix); |
|
2982 delete matrix; |
|
2983 matrix = m; |
|
2984 type_tag = matrix_constant; |
|
2985 } |
|
2986 break; |
|
2987 |
|
2988 case complex_scalar_constant: |
|
2989 case complex_matrix_constant: |
|
2990 { |
|
2991 ComplexMatrix *cm = new ComplexMatrix (*char_matrix); |
|
2992 delete matrix; |
|
2993 complex_matrix = cm; |
|
2994 type_tag = complex_matrix_constant; |
|
2995 } |
|
2996 break; |
|
2997 |
|
2998 default: |
|
2999 break; |
|
3000 } |
|
3001 break; |
|
3002 |
1299
|
3003 default: |
|
3004 break; |
|
3005 } |
|
3006 } |
|
3007 |
|
3008 // Assignment operations for the tree-constant representation class. |
|
3009 |
|
3010 // Top-level tree-constant function that handles assignments. Only |
|
3011 // decide if the left-hand side is currently a scalar or a matrix and |
|
3012 // hand off to other functions to do the real work. |
|
3013 |
1572
|
3014 // XXX FIXME XXX -- need some other way to make these functions |
|
3015 // visible here (they should be in some header file...) |
|
3016 |
1558
|
3017 extern void assign (Array2<Complex>&, const Array2<Complex>&); |
|
3018 extern void assign (Array2<Complex>&, const Array2<double>&); |
1572
|
3019 extern void assign (Array2<Complex>&, const Array2<char>&); |
|
3020 |
1558
|
3021 extern void assign (Array2<double>&, const Array2<double>&); |
1572
|
3022 extern void assign (Array2<double>&, const Array2<char>&); |
|
3023 |
|
3024 extern void assign (Array2<char>&, const Array2<char>&); |
1558
|
3025 |
1299
|
3026 void |
|
3027 TC_REP::assign (tree_constant& rhs, const Octave_object& args) |
|
3028 { |
1593
|
3029 // XXX FIXME XXX -- we should probably have special cases for rhs |
|
3030 // being a range type, since converting to a matrix can waste a lot |
|
3031 // of memory. |
|
3032 |
|
3033 tree_constant rhs_tmp = rhs; |
|
3034 |
1652
|
3035 if (! (is_string () |
|
3036 && (rhs_tmp.is_string () |
|
3037 || rhs_tmp.is_zero_by_zero ()))) |
|
3038 { |
|
3039 rhs_tmp.force_numeric (); |
|
3040 |
|
3041 if (error_state) |
|
3042 return; |
|
3043 } |
|
3044 |
|
3045 if (rhs_tmp.is_string |
|
3046 && rhs_tmp.rows () == 1 |
|
3047 && rhs_tmp.columns () == 0) |
|
3048 { |
|
3049 rhs_tmp.force_numeric (1); |
|
3050 |
|
3051 if (error_state) |
|
3052 return; |
|
3053 } |
1299
|
3054 |
1572
|
3055 // An assignment to a range will normally require a conversion to a |
|
3056 // vector in the end anyway, since it will normally destroy the |
|
3057 // equally-spaced property of the range elements. This is not as |
|
3058 // memory efficient as possible, but it is much simpler than writing |
|
3059 // additional indexing and assignment functions especially for |
|
3060 // Ranges. |
|
3061 |
|
3062 if (is_defined () && ! (is_numeric_type () || is_string ())) |
1652
|
3063 { |
|
3064 force_numeric (); |
|
3065 |
|
3066 if (error_state) |
|
3067 return; |
|
3068 } |
|
3069 |
|
3070 if (! rhs_tmp.is_zero_by_zero ()) |
|
3071 { |
|
3072 maybe_widen (rhs_tmp.const_type ()); |
|
3073 |
|
3074 if (error_state) |
|
3075 return; |
|
3076 } |
|
3077 |
|
3078 set_index (args, rhs_tmp.is_complex_type ()); |
1299
|
3079 |
|
3080 if (error_state) |
|
3081 return; |
|
3082 |
1652
|
3083 switch (type_tag) |
1299
|
3084 { |
1652
|
3085 case complex_matrix_constant: |
|
3086 { |
|
3087 switch (rhs_tmp.const_type ()) |
1299
|
3088 { |
1652
|
3089 case complex_scalar_constant: |
|
3090 case complex_matrix_constant: |
|
3091 ::assign (*complex_matrix, rhs_tmp.complex_matrix_value ()); |
|
3092 break; |
|
3093 |
|
3094 case scalar_constant: |
|
3095 case matrix_constant: |
|
3096 ::assign (*complex_matrix, rhs_tmp.matrix_value ()); |
|
3097 break; |
|
3098 |
|
3099 default: |
|
3100 panic_impossible ();; |
|
3101 break; |
1299
|
3102 } |
1652
|
3103 } |
|
3104 break; |
|
3105 |
|
3106 case scalar_constant: |
|
3107 case matrix_constant: |
|
3108 { |
|
3109 switch (rhs_tmp.const_type ()) |
1572
|
3110 { |
1652
|
3111 case scalar_constant: |
|
3112 case matrix_constant: |
|
3113 ::assign (*matrix, rhs_tmp.matrix_value ()); |
|
3114 break; |
|
3115 |
|
3116 case char_matrix_constant: |
|
3117 ::assign (*matrix, rhs_tmp.char_matrix_value ()); |
|
3118 break; |
|
3119 |
|
3120 default: |
|
3121 panic_impossible (); |
|
3122 break; |
1572
|
3123 } |
1652
|
3124 } |
|
3125 break; |
|
3126 |
|
3127 case char_matrix_constant: |
|
3128 ::assign (*char_matrix, rhs_tmp.char_matrix_value ()); |
|
3129 break; |
|
3130 |
|
3131 case char_matrix_constant_str: |
|
3132 ::assign (*char_matrix, rhs_tmp.char_matrix_value ()); |
|
3133 if (char_matrix->rows () == 0 && char_matrix->columns () == 0) |
|
3134 char_matrix->resize (1, 0); |
|
3135 break; |
|
3136 |
|
3137 default: |
|
3138 panic_impossible (); |
|
3139 break; |
1299
|
3140 } |
1688
|
3141 |
|
3142 // Do the right thing for assignments like `x(1) = pi' when x is |
|
3143 // undefined before the assignment. |
|
3144 |
|
3145 if (is_matrix_type () || is_range ()) |
|
3146 maybe_mutate (); |
1299
|
3147 } |
|
3148 |
1827
|
3149 bool |
1755
|
3150 TC_REP::print_as_scalar (void) |
|
3151 { |
|
3152 int nr = rows (); |
|
3153 int nc = columns (); |
1827
|
3154 |
1755
|
3155 return (is_scalar_type () |
|
3156 || (is_string () && nr <= 1) |
|
3157 || (is_matrix_type () |
|
3158 && ((nr == 1 && nc == 1) |
|
3159 || nr == 0 |
|
3160 || nc == 0))); |
|
3161 } |
|
3162 |
1827
|
3163 bool |
1755
|
3164 TC_REP::print_as_structure (void) |
|
3165 { |
|
3166 return is_map (); |
|
3167 } |
|
3168 |
96
|
3169 /* |
1
|
3170 ;;; Local Variables: *** |
|
3171 ;;; mode: C++ *** |
|
3172 ;;; End: *** |
|
3173 */ |