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