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