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