529
|
1 // tc-rep.cc -*- C++ -*- |
492
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
492
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
492
|
26 #endif |
|
27 |
|
28 #include <ctype.h> |
|
29 #include <string.h> |
|
30 #include <fstream.h> |
|
31 #include <iostream.h> |
|
32 |
|
33 #include "mx-base.h" |
|
34 #include "Range.h" |
|
35 |
504
|
36 #include "arith-ops.h" |
492
|
37 #include "variables.h" |
631
|
38 #include "sysdep.h" |
492
|
39 #include "error.h" |
|
40 #include "gripes.h" |
|
41 #include "user-prefs.h" |
|
42 #include "utils.h" |
|
43 #include "pr-output.h" |
|
44 #include "tree-const.h" |
|
45 #include "idx-vector.h" |
1199
|
46 #include "unwind-prot.h" |
745
|
47 #include "oct-map.h" |
492
|
48 |
759
|
49 #include "tc-inlines.h" |
492
|
50 |
1168
|
51 // The following three variables could be made static members of the |
|
52 // TC_REP class. |
|
53 |
|
54 // Pointer to the blocks of memory we manage. |
1170
|
55 static TC_REP *newlist = 0; |
1168
|
56 |
|
57 // Multiplier for allocating new blocks. |
|
58 static const int newlist_grow_size = 128; |
|
59 |
1199
|
60 // Indentation level for structures. |
|
61 static int structure_indent_level = 0; |
|
62 |
1200
|
63 static void |
|
64 increment_structure_indent_level (void) |
|
65 { |
|
66 structure_indent_level += 2; |
|
67 } |
|
68 |
|
69 static void |
|
70 decrement_structure_indent_level (void) |
|
71 { |
|
72 structure_indent_level -= 2; |
|
73 } |
|
74 |
492
|
75 static int |
|
76 any_element_is_complex (const ComplexMatrix& a) |
|
77 { |
|
78 int nr = a.rows (); |
|
79 int nc = a.columns (); |
|
80 for (int j = 0; j < nc; j++) |
|
81 for (int i = 0; i < nr; i++) |
|
82 if (imag (a.elem (i, j)) != 0.0) |
|
83 return 1; |
|
84 return 0; |
|
85 } |
|
86 |
581
|
87 // The real representation of constants. |
|
88 |
631
|
89 TC_REP::tree_constant_rep (void) |
492
|
90 { |
|
91 type_tag = unknown_constant; |
581
|
92 orig_text = 0; |
492
|
93 } |
|
94 |
631
|
95 TC_REP::tree_constant_rep (double d) |
492
|
96 { |
|
97 scalar = d; |
|
98 type_tag = scalar_constant; |
581
|
99 orig_text = 0; |
492
|
100 } |
|
101 |
631
|
102 TC_REP::tree_constant_rep (const Matrix& m) |
492
|
103 { |
|
104 if (m.rows () == 1 && m.columns () == 1) |
|
105 { |
|
106 scalar = m.elem (0, 0); |
|
107 type_tag = scalar_constant; |
|
108 } |
|
109 else |
|
110 { |
|
111 matrix = new Matrix (m); |
|
112 type_tag = matrix_constant; |
|
113 } |
581
|
114 orig_text = 0; |
492
|
115 } |
|
116 |
631
|
117 TC_REP::tree_constant_rep (const DiagMatrix& d) |
492
|
118 { |
|
119 if (d.rows () == 1 && d.columns () == 1) |
|
120 { |
|
121 scalar = d.elem (0, 0); |
|
122 type_tag = scalar_constant; |
|
123 } |
|
124 else |
|
125 { |
|
126 matrix = new Matrix (d); |
|
127 type_tag = matrix_constant; |
|
128 } |
581
|
129 orig_text = 0; |
492
|
130 } |
|
131 |
631
|
132 TC_REP::tree_constant_rep (const RowVector& v, int prefer_column_vector) |
492
|
133 { |
|
134 int len = v.capacity (); |
|
135 if (len == 1) |
|
136 { |
|
137 scalar = v.elem (0); |
|
138 type_tag = scalar_constant; |
|
139 } |
|
140 else |
|
141 { |
|
142 int pcv = (prefer_column_vector < 0) |
|
143 ? user_pref.prefer_column_vectors |
|
144 : prefer_column_vector; |
|
145 |
|
146 if (pcv) |
|
147 { |
|
148 Matrix m (len, 1); |
|
149 for (int i = 0; i < len; i++) |
|
150 m.elem (i, 0) = v.elem (i); |
|
151 matrix = new Matrix (m); |
|
152 type_tag = matrix_constant; |
|
153 } |
|
154 else |
|
155 { |
|
156 Matrix m (1, len); |
|
157 for (int i = 0; i < len; i++) |
|
158 m.elem (0, i) = v.elem (i); |
|
159 matrix = new Matrix (m); |
|
160 type_tag = matrix_constant; |
|
161 } |
|
162 } |
581
|
163 orig_text = 0; |
492
|
164 } |
|
165 |
631
|
166 TC_REP::tree_constant_rep (const ColumnVector& v, int prefer_column_vector) |
492
|
167 { |
|
168 int len = v.capacity (); |
|
169 if (len == 1) |
|
170 { |
|
171 scalar = v.elem (0); |
|
172 type_tag = scalar_constant; |
|
173 } |
|
174 else |
|
175 { |
|
176 int pcv = (prefer_column_vector < 0) |
|
177 ? user_pref.prefer_column_vectors |
|
178 : prefer_column_vector; |
|
179 |
|
180 if (pcv) |
|
181 { |
|
182 Matrix m (len, 1); |
|
183 for (int i = 0; i < len; i++) |
|
184 m.elem (i, 0) = v.elem (i); |
|
185 matrix = new Matrix (m); |
|
186 type_tag = matrix_constant; |
|
187 } |
|
188 else |
|
189 { |
|
190 Matrix m (1, len); |
|
191 for (int i = 0; i < len; i++) |
|
192 m.elem (0, i) = v.elem (i); |
|
193 matrix = new Matrix (m); |
|
194 type_tag = matrix_constant; |
|
195 } |
|
196 } |
581
|
197 orig_text = 0; |
492
|
198 } |
|
199 |
631
|
200 TC_REP::tree_constant_rep (const Complex& c) |
492
|
201 { |
|
202 complex_scalar = new Complex (c); |
|
203 type_tag = complex_scalar_constant; |
581
|
204 orig_text = 0; |
492
|
205 } |
|
206 |
631
|
207 TC_REP::tree_constant_rep (const ComplexMatrix& m) |
492
|
208 { |
|
209 if (m.rows () == 1 && m.columns () == 1) |
|
210 { |
|
211 complex_scalar = new Complex (m.elem (0, 0)); |
|
212 type_tag = complex_scalar_constant; |
|
213 } |
|
214 else |
|
215 { |
|
216 complex_matrix = new ComplexMatrix (m); |
|
217 type_tag = complex_matrix_constant; |
|
218 } |
581
|
219 orig_text = 0; |
492
|
220 } |
|
221 |
631
|
222 TC_REP::tree_constant_rep (const ComplexDiagMatrix& d) |
492
|
223 { |
|
224 if (d.rows () == 1 && d.columns () == 1) |
|
225 { |
|
226 complex_scalar = new Complex (d.elem (0, 0)); |
|
227 type_tag = complex_scalar_constant; |
|
228 } |
|
229 else |
|
230 { |
|
231 complex_matrix = new ComplexMatrix (d); |
|
232 type_tag = complex_matrix_constant; |
|
233 } |
581
|
234 orig_text = 0; |
492
|
235 } |
|
236 |
631
|
237 TC_REP::tree_constant_rep (const ComplexRowVector& v, |
|
238 int prefer_column_vector) |
492
|
239 { |
|
240 int len = v.capacity (); |
|
241 if (len == 1) |
|
242 { |
|
243 complex_scalar = new Complex (v.elem (0)); |
|
244 type_tag = complex_scalar_constant; |
|
245 } |
|
246 else |
|
247 { |
|
248 int pcv = (prefer_column_vector < 0) |
|
249 ? user_pref.prefer_column_vectors |
|
250 : prefer_column_vector; |
|
251 |
|
252 if (pcv) |
|
253 { |
|
254 ComplexMatrix m (len, 1); |
|
255 for (int i = 0; i < len; i++) |
|
256 m.elem (i, 0) = v.elem (i); |
|
257 complex_matrix = new ComplexMatrix (m); |
|
258 type_tag = complex_matrix_constant; |
|
259 } |
|
260 else |
|
261 { |
|
262 ComplexMatrix m (1, len); |
|
263 for (int i = 0; i < len; i++) |
|
264 m.elem (0, i) = v.elem (i); |
|
265 complex_matrix = new ComplexMatrix (m); |
|
266 type_tag = complex_matrix_constant; |
|
267 } |
|
268 } |
581
|
269 orig_text = 0; |
492
|
270 } |
|
271 |
631
|
272 TC_REP::tree_constant_rep (const ComplexColumnVector& v, int |
|
273 prefer_column_vector) |
492
|
274 { |
|
275 int len = v.capacity (); |
|
276 if (len == 1) |
|
277 { |
|
278 complex_scalar = new Complex (v.elem (0)); |
|
279 type_tag = complex_scalar_constant; |
|
280 } |
|
281 else |
|
282 { |
|
283 int pcv = (prefer_column_vector < 0) |
|
284 ? user_pref.prefer_column_vectors |
|
285 : prefer_column_vector; |
|
286 |
|
287 if (pcv) |
|
288 { |
|
289 ComplexMatrix m (len, 1); |
|
290 for (int i = 0; i < len; i++) |
|
291 m.elem (i, 0) = v.elem (i); |
|
292 complex_matrix = new ComplexMatrix (m); |
|
293 type_tag = complex_matrix_constant; |
|
294 } |
|
295 else |
|
296 { |
|
297 ComplexMatrix m (1, len); |
|
298 for (int i = 0; i < len; i++) |
|
299 m.elem (0, i) = v.elem (i); |
|
300 complex_matrix = new ComplexMatrix (m); |
|
301 type_tag = complex_matrix_constant; |
|
302 } |
|
303 } |
581
|
304 orig_text = 0; |
492
|
305 } |
|
306 |
631
|
307 TC_REP::tree_constant_rep (const char *s) |
492
|
308 { |
|
309 string = strsave (s); |
|
310 type_tag = string_constant; |
581
|
311 orig_text = 0; |
492
|
312 } |
|
313 |
631
|
314 TC_REP::tree_constant_rep (double b, double l, double i) |
492
|
315 { |
|
316 range = new Range (b, l, i); |
|
317 int nel = range->nelem (); |
777
|
318 if (nel > 1) |
492
|
319 type_tag = range_constant; |
|
320 else |
|
321 { |
|
322 delete range; |
|
323 if (nel == 1) |
|
324 { |
|
325 scalar = b; |
|
326 type_tag = scalar_constant; |
|
327 } |
|
328 else if (nel == 0) |
|
329 { |
|
330 matrix = new Matrix (); |
|
331 type_tag = matrix_constant; |
|
332 } |
|
333 else |
777
|
334 { |
|
335 type_tag = unknown_constant; |
|
336 if (nel == -1) |
|
337 ::error ("number of elements in range exceeds INT_MAX"); |
|
338 else |
|
339 ::error ("invalid range"); |
|
340 } |
492
|
341 } |
581
|
342 orig_text = 0; |
492
|
343 } |
|
344 |
631
|
345 TC_REP::tree_constant_rep (const Range& r) |
492
|
346 { |
777
|
347 int nel = r.nelem (); |
|
348 if (nel > 1) |
492
|
349 { |
|
350 range = new Range (r); |
|
351 type_tag = range_constant; |
|
352 } |
777
|
353 else if (nel == 1) |
492
|
354 { |
|
355 scalar = r.base (); |
|
356 type_tag = scalar_constant; |
|
357 } |
777
|
358 else if (nel == 0) |
492
|
359 { |
|
360 matrix = new Matrix (); |
|
361 type_tag = matrix_constant; |
|
362 } |
|
363 else |
777
|
364 { |
|
365 type_tag = unknown_constant; |
|
366 if (nel == -1) |
|
367 ::error ("number of elements in range exceeds INT_MAX"); |
|
368 else |
|
369 ::error ("invalid range"); |
|
370 } |
581
|
371 |
|
372 orig_text = 0; |
492
|
373 } |
|
374 |
745
|
375 TC_REP::tree_constant_rep (const Octave_map& m) |
|
376 { |
|
377 a_map = new Octave_map (m); |
|
378 type_tag = map_constant; |
|
379 orig_text = 0; |
|
380 } |
|
381 |
631
|
382 TC_REP::tree_constant_rep (TC_REP::constant_type t) |
492
|
383 { |
922
|
384 assert (t == magic_colon || t == all_va_args); |
|
385 type_tag = t; |
581
|
386 orig_text = 0; |
492
|
387 } |
|
388 |
631
|
389 TC_REP::tree_constant_rep (const tree_constant_rep& t) |
492
|
390 { |
|
391 type_tag = t.type_tag; |
|
392 |
|
393 switch (t.type_tag) |
|
394 { |
|
395 case unknown_constant: |
|
396 break; |
631
|
397 |
492
|
398 case scalar_constant: |
|
399 scalar = t.scalar; |
|
400 break; |
631
|
401 |
492
|
402 case matrix_constant: |
|
403 matrix = new Matrix (*(t.matrix)); |
|
404 break; |
631
|
405 |
492
|
406 case string_constant: |
|
407 string = strsave (t.string); |
|
408 break; |
631
|
409 |
492
|
410 case complex_matrix_constant: |
|
411 complex_matrix = new ComplexMatrix (*(t.complex_matrix)); |
|
412 break; |
631
|
413 |
492
|
414 case complex_scalar_constant: |
|
415 complex_scalar = new Complex (*(t.complex_scalar)); |
|
416 break; |
631
|
417 |
492
|
418 case range_constant: |
|
419 range = new Range (*(t.range)); |
|
420 break; |
631
|
421 |
745
|
422 case map_constant: |
|
423 a_map = new Octave_map (*(t.a_map)); |
|
424 break; |
|
425 |
492
|
426 case magic_colon: |
922
|
427 case all_va_args: |
492
|
428 break; |
|
429 } |
581
|
430 |
|
431 orig_text = strsave (t.orig_text); |
492
|
432 } |
|
433 |
631
|
434 TC_REP::~tree_constant_rep (void) |
492
|
435 { |
|
436 switch (type_tag) |
|
437 { |
|
438 case matrix_constant: |
|
439 delete matrix; |
|
440 break; |
631
|
441 |
492
|
442 case complex_scalar_constant: |
|
443 delete complex_scalar; |
|
444 break; |
631
|
445 |
492
|
446 case complex_matrix_constant: |
|
447 delete complex_matrix; |
|
448 break; |
631
|
449 |
492
|
450 case string_constant: |
|
451 delete [] string; |
|
452 break; |
631
|
453 |
492
|
454 case range_constant: |
|
455 delete range; |
|
456 break; |
631
|
457 |
745
|
458 case map_constant: |
|
459 delete a_map; |
|
460 break; |
|
461 |
777
|
462 case unknown_constant: |
|
463 case scalar_constant: |
|
464 case magic_colon: |
922
|
465 case all_va_args: |
492
|
466 break; |
|
467 } |
581
|
468 |
|
469 delete [] orig_text; |
492
|
470 } |
|
471 |
|
472 void * |
631
|
473 TC_REP::operator new (size_t size) |
492
|
474 { |
1168
|
475 assert (size == sizeof (TC_REP)); |
|
476 |
|
477 if (! newlist) |
|
478 { |
|
479 int block_size = newlist_grow_size * sizeof (TC_REP); |
|
480 newlist = (TC_REP *) new char [block_size]; |
|
481 |
|
482 for (int i = 0; i < newlist_grow_size - 1; i++) |
|
483 newlist[i].freeptr = &newlist[i+1]; |
|
484 |
|
485 newlist[i].freeptr = 0; |
|
486 } |
|
487 |
|
488 TC_REP *tmp = newlist; |
|
489 newlist = newlist->freeptr; |
|
490 return tmp; |
492
|
491 } |
|
492 |
|
493 void |
631
|
494 TC_REP::operator delete (void *p, size_t size) |
492
|
495 { |
1168
|
496 TC_REP *tmp = (TC_REP *) p; |
|
497 tmp->freeptr = newlist; |
|
498 newlist = tmp; |
492
|
499 } |
|
500 |
631
|
501 int |
|
502 TC_REP::rows (void) const |
492
|
503 { |
631
|
504 int retval = -1; |
581
|
505 |
|
506 switch (type_tag) |
|
507 { |
|
508 case scalar_constant: |
|
509 case complex_scalar_constant: |
631
|
510 retval = 1; |
|
511 break; |
|
512 |
581
|
513 case string_constant: |
|
514 case range_constant: |
631
|
515 retval = (columns () > 0); |
|
516 break; |
|
517 |
|
518 case matrix_constant: |
|
519 retval = matrix->rows (); |
|
520 break; |
|
521 |
|
522 case complex_matrix_constant: |
|
523 retval = complex_matrix->rows (); |
|
524 break; |
|
525 |
777
|
526 default: |
631
|
527 break; |
492
|
528 } |
|
529 |
|
530 return retval; |
|
531 } |
|
532 |
|
533 int |
631
|
534 TC_REP::columns (void) const |
492
|
535 { |
|
536 int retval = -1; |
631
|
537 |
492
|
538 switch (type_tag) |
|
539 { |
|
540 case scalar_constant: |
|
541 case complex_scalar_constant: |
|
542 retval = 1; |
|
543 break; |
631
|
544 |
492
|
545 case matrix_constant: |
631
|
546 retval = matrix->columns (); |
|
547 break; |
|
548 |
492
|
549 case complex_matrix_constant: |
631
|
550 retval = complex_matrix->columns (); |
|
551 break; |
|
552 |
|
553 case string_constant: |
|
554 retval = strlen (string); |
|
555 break; |
|
556 |
|
557 case range_constant: |
|
558 retval = range->nelem (); |
|
559 break; |
|
560 |
777
|
561 default: |
492
|
562 break; |
|
563 } |
631
|
564 |
492
|
565 return retval; |
|
566 } |
|
567 |
|
568 tree_constant |
631
|
569 TC_REP::all (void) const |
492
|
570 { |
777
|
571 tree_constant retval; |
|
572 |
|
573 if (error_state) |
|
574 return retval; |
|
575 |
|
576 if (! is_numeric_type ()) |
492
|
577 { |
|
578 tree_constant tmp = make_numeric (); |
915
|
579 |
|
580 if (error_state) |
|
581 return retval; |
|
582 |
492
|
583 return tmp.all (); |
|
584 } |
|
585 |
|
586 switch (type_tag) |
|
587 { |
|
588 case scalar_constant: |
|
589 { |
|
590 double status = (scalar != 0.0); |
|
591 retval = tree_constant (status); |
|
592 } |
|
593 break; |
631
|
594 |
492
|
595 case matrix_constant: |
|
596 { |
|
597 Matrix m = matrix->all (); |
|
598 retval = tree_constant (m); |
|
599 } |
|
600 break; |
631
|
601 |
492
|
602 case complex_scalar_constant: |
|
603 { |
|
604 double status = (*complex_scalar != 0.0); |
|
605 retval = tree_constant (status); |
|
606 } |
|
607 break; |
631
|
608 |
492
|
609 case complex_matrix_constant: |
|
610 { |
|
611 Matrix m = complex_matrix->all (); |
|
612 retval = tree_constant (m); |
|
613 } |
|
614 break; |
631
|
615 |
492
|
616 default: |
777
|
617 gripe_wrong_type_arg ("all", *this); |
492
|
618 break; |
|
619 } |
631
|
620 |
492
|
621 return retval; |
|
622 } |
|
623 |
|
624 tree_constant |
631
|
625 TC_REP::any (void) const |
492
|
626 { |
777
|
627 tree_constant retval; |
|
628 |
|
629 if (error_state) |
|
630 return retval; |
|
631 |
|
632 if (! is_numeric_type ()) |
492
|
633 { |
|
634 tree_constant tmp = make_numeric (); |
915
|
635 |
|
636 if (error_state) |
|
637 return retval; |
|
638 |
492
|
639 return tmp.any (); |
|
640 } |
|
641 |
|
642 switch (type_tag) |
|
643 { |
|
644 case scalar_constant: |
|
645 { |
|
646 double status = (scalar != 0.0); |
|
647 retval = tree_constant (status); |
|
648 } |
|
649 break; |
631
|
650 |
492
|
651 case matrix_constant: |
|
652 { |
|
653 Matrix m = matrix->any (); |
|
654 retval = tree_constant (m); |
|
655 } |
|
656 break; |
631
|
657 |
492
|
658 case complex_scalar_constant: |
|
659 { |
|
660 double status = (*complex_scalar != 0.0); |
|
661 retval = tree_constant (status); |
|
662 } |
|
663 break; |
631
|
664 |
492
|
665 case complex_matrix_constant: |
|
666 { |
|
667 Matrix m = complex_matrix->any (); |
|
668 retval = tree_constant (m); |
|
669 } |
|
670 break; |
631
|
671 |
|
672 default: |
777
|
673 gripe_wrong_type_arg ("any", *this); |
631
|
674 break; |
|
675 } |
|
676 |
|
677 return retval; |
|
678 } |
|
679 |
|
680 int |
|
681 TC_REP::valid_as_scalar_index (void) const |
|
682 { |
|
683 return (type_tag == magic_colon |
1086
|
684 || (type_tag == scalar_constant |
|
685 && ! xisnan (scalar) |
|
686 && NINT (scalar) == 1) |
631
|
687 || (type_tag == range_constant |
1086
|
688 && range->nelem () == 1 |
|
689 && ! xisnan (range->base ()) |
|
690 && NINT (range->base ()) == 1)); |
631
|
691 } |
|
692 |
|
693 int |
1041
|
694 TC_REP::valid_as_zero_index (void) const |
|
695 { |
1086
|
696 return ((type_tag == scalar_constant |
|
697 && ! xisnan (scalar) |
|
698 && NINT (scalar) == 0) |
1041
|
699 || (type_tag == matrix_constant |
1086
|
700 && matrix->rows () == 0 |
|
701 && matrix->columns () == 0) |
1041
|
702 || (type_tag == range_constant |
1086
|
703 && range->nelem () == 1 |
|
704 && ! xisnan (range->base ()) |
|
705 && NINT (range->base ()) == 0)); |
1041
|
706 } |
|
707 |
|
708 int |
631
|
709 TC_REP::is_true (void) const |
|
710 { |
777
|
711 int retval = 0; |
|
712 |
|
713 if (error_state) |
|
714 return retval; |
|
715 |
|
716 if (! is_numeric_type ()) |
631
|
717 { |
|
718 tree_constant tmp = make_numeric (); |
915
|
719 |
|
720 if (error_state) |
|
721 return retval; |
|
722 |
631
|
723 return tmp.is_true (); |
|
724 } |
|
725 |
|
726 switch (type_tag) |
|
727 { |
|
728 case scalar_constant: |
|
729 retval = (scalar != 0.0); |
|
730 break; |
|
731 |
|
732 case matrix_constant: |
|
733 { |
|
734 Matrix m = (matrix->all ()) . all (); |
|
735 retval = (m.rows () == 1 |
|
736 && m.columns () == 1 |
|
737 && m.elem (0, 0) != 0.0); |
|
738 } |
|
739 break; |
|
740 |
|
741 case complex_scalar_constant: |
|
742 retval = (*complex_scalar != 0.0); |
|
743 break; |
|
744 |
|
745 case complex_matrix_constant: |
|
746 { |
|
747 Matrix m = (complex_matrix->all ()) . all (); |
|
748 retval = (m.rows () == 1 |
|
749 && m.columns () == 1 |
|
750 && m.elem (0, 0) != 0.0); |
|
751 } |
|
752 break; |
|
753 |
492
|
754 default: |
777
|
755 gripe_wrong_type_arg (0, *this); |
492
|
756 break; |
|
757 } |
631
|
758 |
|
759 return retval; |
|
760 } |
|
761 |
|
762 static void |
|
763 warn_implicit_conversion (const char *from, const char *to) |
|
764 { |
|
765 warning ("implicit conversion from %s to %s", from, to); |
|
766 } |
|
767 |
|
768 double |
|
769 TC_REP::double_value (int force_string_conversion) const |
|
770 { |
|
771 double retval = octave_NaN; |
|
772 |
|
773 switch (type_tag) |
|
774 { |
|
775 case scalar_constant: |
|
776 retval = scalar; |
|
777 break; |
|
778 |
|
779 case matrix_constant: |
|
780 { |
|
781 if (user_pref.do_fortran_indexing && rows () > 0 && columns () > 0) |
|
782 retval = matrix->elem (0, 0); |
|
783 else |
|
784 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
785 } |
|
786 break; |
|
787 |
|
788 case complex_matrix_constant: |
|
789 case complex_scalar_constant: |
|
790 { |
|
791 int flag = user_pref.ok_to_lose_imaginary_part; |
|
792 |
|
793 if (flag < 0) |
|
794 warn_implicit_conversion ("complex scalar", "real scalar"); |
|
795 |
|
796 if (flag) |
|
797 { |
|
798 if (type_tag == complex_scalar_constant) |
|
799 retval = ::real (*complex_scalar); |
|
800 else if (type_tag == complex_matrix_constant) |
|
801 { |
|
802 if (user_pref.do_fortran_indexing |
|
803 && rows () > 0 && columns () > 0) |
|
804 retval = ::real (complex_matrix->elem (0, 0)); |
|
805 else |
|
806 gripe_invalid_conversion ("complex matrix", "real scalar"); |
|
807 } |
|
808 else |
|
809 panic_impossible (); |
|
810 } |
|
811 else |
|
812 gripe_invalid_conversion ("complex scalar", "real scalar"); |
|
813 } |
|
814 break; |
|
815 |
|
816 case string_constant: |
|
817 { |
|
818 int flag = force_string_conversion; |
|
819 if (! flag) |
|
820 flag = user_pref.implicit_str_to_num_ok; |
|
821 |
|
822 if (flag < 0) |
|
823 warn_implicit_conversion ("string", "real scalar"); |
|
824 |
|
825 int len = strlen (string); |
|
826 if (flag && (len == 1 || (len > 1 && user_pref.do_fortran_indexing))) |
|
827 retval = toascii ((int) string[0]); |
|
828 else |
|
829 gripe_invalid_conversion ("string", "real scalar"); |
|
830 } |
|
831 break; |
|
832 |
|
833 case range_constant: |
|
834 { |
|
835 int nel = range->nelem (); |
|
836 if (nel == 1 || (nel > 1 && user_pref.do_fortran_indexing)) |
|
837 retval = range->base (); |
|
838 else |
|
839 gripe_invalid_conversion ("range", "real scalar"); |
|
840 } |
|
841 break; |
|
842 |
|
843 default: |
|
844 gripe_invalid_conversion (type_as_string (), "real scalar"); |
|
845 break; |
|
846 } |
|
847 |
|
848 return retval; |
|
849 } |
|
850 |
|
851 Matrix |
|
852 TC_REP::matrix_value (int force_string_conversion) const |
|
853 { |
|
854 Matrix retval; |
|
855 |
|
856 switch (type_tag) |
|
857 { |
|
858 case scalar_constant: |
|
859 retval = Matrix (1, 1, scalar); |
|
860 break; |
|
861 |
|
862 case matrix_constant: |
|
863 retval = *matrix; |
|
864 break; |
|
865 |
|
866 case complex_scalar_constant: |
|
867 case complex_matrix_constant: |
|
868 { |
|
869 int flag = user_pref.ok_to_lose_imaginary_part; |
|
870 if (flag < 0) |
|
871 warn_implicit_conversion ("complex matrix", "real matrix"); |
|
872 |
|
873 if (flag) |
|
874 { |
|
875 if (type_tag == complex_scalar_constant) |
|
876 retval = Matrix (1, 1, ::real (*complex_scalar)); |
|
877 else if (type_tag == complex_matrix_constant) |
|
878 retval = ::real (*complex_matrix); |
|
879 else |
|
880 panic_impossible (); |
|
881 } |
|
882 else |
|
883 gripe_invalid_conversion ("complex matrix", "real matrix"); |
|
884 } |
|
885 break; |
|
886 |
|
887 case string_constant: |
|
888 { |
|
889 int flag = force_string_conversion; |
|
890 if (! flag) |
|
891 flag = user_pref.implicit_str_to_num_ok; |
|
892 |
|
893 if (flag < 0) |
|
894 warn_implicit_conversion ("string", "real matrix"); |
|
895 |
|
896 if (flag) |
|
897 { |
|
898 int len = strlen (string); |
|
899 |
983
|
900 if (len > 0) |
|
901 { |
|
902 retval.resize (1, len); |
631
|
903 |
|
904 for (int i = 0; i < len; i++) |
|
905 retval.elem (0, i) = toascii ((int) string[i]); |
|
906 } |
|
907 else |
983
|
908 retval = Matrix (); |
631
|
909 } |
|
910 else |
|
911 gripe_invalid_conversion ("string", "real matrix"); |
|
912 } |
|
913 break; |
|
914 |
|
915 case range_constant: |
|
916 retval = range->matrix_value (); |
|
917 break; |
|
918 |
|
919 default: |
|
920 gripe_invalid_conversion (type_as_string (), "real matrix"); |
|
921 break; |
|
922 } |
|
923 |
|
924 return retval; |
|
925 } |
|
926 |
|
927 Complex |
|
928 TC_REP::complex_value (int force_string_conversion) const |
|
929 { |
636
|
930 Complex retval (octave_NaN, octave_NaN); |
631
|
931 |
|
932 switch (type_tag) |
|
933 { |
|
934 case complex_scalar_constant: |
|
935 retval = *complex_scalar; |
|
936 break; |
|
937 |
|
938 case scalar_constant: |
|
939 retval = scalar; |
|
940 break; |
|
941 |
|
942 case complex_matrix_constant: |
|
943 case matrix_constant: |
|
944 { |
|
945 if (user_pref.do_fortran_indexing && rows () > 0 && columns () > 0) |
|
946 { |
|
947 if (type_tag == complex_matrix_constant) |
|
948 retval = complex_matrix->elem (0, 0); |
|
949 else |
|
950 retval = matrix->elem (0, 0); |
|
951 } |
|
952 else |
|
953 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
954 } |
|
955 break; |
|
956 |
|
957 case string_constant: |
|
958 { |
|
959 int flag = force_string_conversion; |
|
960 if (! flag) |
|
961 flag = user_pref.implicit_str_to_num_ok; |
|
962 |
|
963 if (flag < 0) |
|
964 warn_implicit_conversion ("string", "complex scalar"); |
|
965 |
|
966 int len = strlen (string); |
|
967 if (flag && (len == 1 || (len > 1 && user_pref.do_fortran_indexing))) |
|
968 retval = toascii ((int) string[0]); |
|
969 else |
|
970 gripe_invalid_conversion ("string", "complex scalar"); |
|
971 } |
|
972 break; |
|
973 |
|
974 case range_constant: |
|
975 { |
|
976 int nel = range->nelem (); |
|
977 if (nel == 1 || (nel > 1 && user_pref.do_fortran_indexing)) |
|
978 retval = range->base (); |
|
979 else |
|
980 gripe_invalid_conversion ("range", "complex scalar"); |
|
981 } |
|
982 break; |
|
983 |
|
984 default: |
|
985 gripe_invalid_conversion (type_as_string (), "complex scalar"); |
|
986 break; |
|
987 } |
|
988 |
|
989 return retval; |
|
990 } |
|
991 |
|
992 ComplexMatrix |
|
993 TC_REP::complex_matrix_value (int force_string_conversion) const |
|
994 { |
|
995 ComplexMatrix retval; |
|
996 |
|
997 switch (type_tag) |
|
998 { |
|
999 case scalar_constant: |
|
1000 retval = ComplexMatrix (1, 1, Complex (scalar)); |
|
1001 break; |
|
1002 |
|
1003 case complex_scalar_constant: |
|
1004 retval = ComplexMatrix (1, 1, *complex_scalar); |
|
1005 break; |
|
1006 |
|
1007 case matrix_constant: |
|
1008 retval = ComplexMatrix (*matrix); |
|
1009 break; |
|
1010 |
|
1011 case complex_matrix_constant: |
|
1012 retval = *complex_matrix; |
|
1013 break; |
|
1014 |
|
1015 case string_constant: |
|
1016 { |
|
1017 int flag = force_string_conversion; |
|
1018 if (! flag) |
|
1019 flag = user_pref.implicit_str_to_num_ok; |
|
1020 |
|
1021 if (flag < 0) |
|
1022 warn_implicit_conversion ("string", "complex matrix"); |
|
1023 |
|
1024 if (flag) |
|
1025 { |
|
1026 int len = strlen (string); |
|
1027 |
|
1028 retval.resize (1, len); |
|
1029 |
|
1030 if (len > 1) |
|
1031 { |
|
1032 for (int i = 0; i < len; i++) |
|
1033 retval.elem (0, i) = toascii ((int) string[i]); |
|
1034 } |
|
1035 else if (len == 1) |
|
1036 retval.elem (0, 0) = toascii ((int) string[0]); |
|
1037 else |
|
1038 panic_impossible (); |
|
1039 } |
|
1040 else |
|
1041 gripe_invalid_conversion ("string", "real matrix"); |
|
1042 } |
|
1043 break; |
|
1044 |
|
1045 case range_constant: |
|
1046 retval = range->matrix_value (); |
|
1047 break; |
|
1048 |
|
1049 default: |
|
1050 gripe_invalid_conversion (type_as_string (), "complex matrix"); |
|
1051 break; |
|
1052 } |
|
1053 |
|
1054 return retval; |
|
1055 } |
|
1056 |
|
1057 char * |
|
1058 TC_REP::string_value (void) const |
|
1059 { |
636
|
1060 if (type_tag == string_constant) |
|
1061 return string; |
|
1062 else |
|
1063 { |
|
1064 gripe_invalid_conversion (type_as_string (), "string"); |
|
1065 return 0; |
|
1066 } |
631
|
1067 } |
|
1068 |
|
1069 Range |
|
1070 TC_REP::range_value (void) const |
|
1071 { |
|
1072 assert (type_tag == range_constant); |
|
1073 return *range; |
|
1074 } |
|
1075 |
745
|
1076 Octave_map |
|
1077 TC_REP::map_value (void) const |
|
1078 { |
|
1079 assert (type_tag == map_constant); |
|
1080 return *a_map; |
|
1081 } |
|
1082 |
|
1083 tree_constant& |
1277
|
1084 TC_REP::lookup_map_element (const char *name, int insert, int silent) |
745
|
1085 { |
|
1086 static tree_constant retval; |
|
1087 |
|
1088 if (type_tag == map_constant) |
|
1089 { |
|
1090 Pix idx = a_map->seek (name); |
|
1091 |
|
1092 if (idx) |
|
1093 return a_map->contents (idx); |
|
1094 else if (insert) |
|
1095 return (*a_map) [name]; |
1277
|
1096 else if (! silent) |
745
|
1097 error ("structure has no member `%s'", name); |
|
1098 } |
1277
|
1099 else if (! silent) |
745
|
1100 error ("invalid structure access attempted"); |
|
1101 |
|
1102 return retval; |
|
1103 } |
|
1104 |
631
|
1105 // This could be made more efficient by doing all the work here rather |
|
1106 // than relying on matrix_value() to do any possible type conversions. |
|
1107 |
|
1108 ColumnVector |
|
1109 TC_REP::vector_value (int force_string_conversion, |
|
1110 int force_vector_conversion) const |
|
1111 { |
|
1112 ColumnVector retval; |
|
1113 |
|
1114 Matrix m = matrix_value (force_string_conversion); |
|
1115 |
|
1116 if (error_state) |
|
1117 return retval; |
|
1118 |
|
1119 int nr = m.rows (); |
|
1120 int nc = m.columns (); |
|
1121 if (nr == 1) |
|
1122 { |
|
1123 retval.resize (nc); |
|
1124 for (int i = 0; i < nc; i++) |
|
1125 retval.elem (i) = m (0, i); |
|
1126 } |
|
1127 else if (nc == 1) |
|
1128 { |
|
1129 retval.resize (nr); |
|
1130 for (int i = 0; i < nr; i++) |
|
1131 retval.elem (i) = m.elem (i, 0); |
|
1132 } |
|
1133 else if (nr > 0 && nc > 0 |
|
1134 && (user_pref.do_fortran_indexing || force_vector_conversion)) |
|
1135 { |
|
1136 retval.resize (nr * nc); |
|
1137 int k = 0; |
|
1138 for (int j = 0; j < nc; j++) |
|
1139 for (int i = 0; i < nr; i++) |
|
1140 retval.elem (k++) = m.elem (i, j); |
|
1141 } |
|
1142 else |
|
1143 gripe_invalid_conversion ("real matrix", "real vector"); |
|
1144 |
|
1145 return retval; |
|
1146 } |
|
1147 |
|
1148 // This could be made more efficient by doing all the work here rather |
|
1149 // than relying on complex_matrix_value() to do any possible type |
|
1150 // conversions. |
|
1151 |
|
1152 ComplexColumnVector |
|
1153 TC_REP::complex_vector_value (int force_string_conversion, |
|
1154 int force_vector_conversion) const |
|
1155 { |
|
1156 ComplexColumnVector retval; |
|
1157 |
|
1158 ComplexMatrix m = complex_matrix_value (force_string_conversion); |
|
1159 |
|
1160 if (error_state) |
|
1161 return retval; |
|
1162 |
|
1163 int nr = m.rows (); |
|
1164 int nc = m.columns (); |
|
1165 if (nr == 1) |
|
1166 { |
|
1167 retval.resize (nc); |
|
1168 for (int i = 0; i < nc; i++) |
|
1169 retval.elem (i) = m (0, i); |
|
1170 } |
|
1171 else if (nc == 1) |
|
1172 { |
|
1173 retval.resize (nr); |
|
1174 for (int i = 0; i < nr; i++) |
|
1175 retval.elem (i) = m.elem (i, 0); |
|
1176 } |
|
1177 else if (nr > 0 && nc > 0 |
|
1178 && (user_pref.do_fortran_indexing || force_vector_conversion)) |
|
1179 { |
|
1180 retval.resize (nr * nc); |
|
1181 int k = 0; |
|
1182 for (int j = 0; j < nc; j++) |
|
1183 for (int i = 0; i < nr; i++) |
|
1184 retval.elem (k++) = m.elem (i, j); |
|
1185 } |
|
1186 else |
|
1187 gripe_invalid_conversion ("complex matrix", "complex vector"); |
|
1188 |
492
|
1189 return retval; |
|
1190 } |
|
1191 |
|
1192 tree_constant |
745
|
1193 TC_REP::convert_to_str (void) const |
492
|
1194 { |
|
1195 tree_constant retval; |
|
1196 |
|
1197 switch (type_tag) |
|
1198 { |
|
1199 case complex_scalar_constant: |
|
1200 case scalar_constant: |
|
1201 { |
|
1202 double d = double_value (); |
1086
|
1203 |
|
1204 if (xisnan (d)) |
|
1205 { |
|
1206 ::error ("invalid conversion from NaN to character"); |
|
1207 return retval; |
|
1208 } |
|
1209 else |
|
1210 { |
|
1211 int i = NINT (d); |
492
|
1212 // Warn about out of range conversions? |
1086
|
1213 char s[2]; |
|
1214 s[0] = (char) i; |
|
1215 s[1] = '\0'; |
|
1216 retval = tree_constant (s); |
|
1217 } |
492
|
1218 } |
|
1219 break; |
631
|
1220 |
492
|
1221 case complex_matrix_constant: |
|
1222 case matrix_constant: |
|
1223 { |
767
|
1224 if (rows () == 0 && columns () == 0) |
|
1225 { |
|
1226 char s = '\0'; |
|
1227 retval = tree_constant (&s); |
|
1228 } |
492
|
1229 else |
|
1230 { |
767
|
1231 ColumnVector v = vector_value (); |
|
1232 int len = v.length (); |
|
1233 if (len == 0) |
|
1234 { |
|
1235 char s = '\0'; |
|
1236 retval = tree_constant (&s); |
|
1237 } |
|
1238 else |
492
|
1239 { |
767
|
1240 char *s = new char [len+1]; |
|
1241 s[len] = '\0'; |
|
1242 for (int i = 0; i < len; i++) |
|
1243 { |
|
1244 double d = v.elem (i); |
1086
|
1245 |
|
1246 if (xisnan (d)) |
|
1247 { |
|
1248 ::error ("invalid conversion from NaN to character"); |
|
1249 delete [] s; |
|
1250 return retval; |
|
1251 } |
|
1252 else |
|
1253 { |
|
1254 int ival = NINT (d); |
492
|
1255 // Warn about out of range conversions? |
1086
|
1256 s[i] = (char) ival; |
|
1257 } |
767
|
1258 } |
|
1259 retval = tree_constant (s); |
|
1260 delete [] s; |
492
|
1261 } |
|
1262 } |
|
1263 } |
|
1264 break; |
631
|
1265 |
492
|
1266 case range_constant: |
|
1267 { |
|
1268 Range r = range_value (); |
|
1269 double b = r.base (); |
|
1270 double incr = r.inc (); |
|
1271 int nel = r.nelem (); |
|
1272 char *s = new char [nel+1]; |
|
1273 s[nel] = '\0'; |
|
1274 for (int i = 0; i < nel; i++) |
|
1275 { |
|
1276 double d = b + i * incr; |
1086
|
1277 |
|
1278 if (xisnan (d)) |
|
1279 { |
|
1280 ::error ("invalid conversion from NaN to character"); |
|
1281 delete [] s; |
|
1282 return retval; |
|
1283 } |
|
1284 else |
|
1285 { |
|
1286 int ival = NINT (d); |
492
|
1287 // Warn about out of range conversions? |
1086
|
1288 s[i] = (char) ival; |
|
1289 } |
492
|
1290 } |
|
1291 retval = tree_constant (s); |
|
1292 delete [] s; |
|
1293 } |
|
1294 break; |
631
|
1295 |
492
|
1296 case string_constant: |
581
|
1297 retval = string; |
492
|
1298 break; |
631
|
1299 |
492
|
1300 default: |
777
|
1301 gripe_invalid_conversion (type_as_string (), "string"); |
492
|
1302 break; |
|
1303 } |
631
|
1304 |
492
|
1305 return retval; |
|
1306 } |
|
1307 |
|
1308 void |
631
|
1309 TC_REP::convert_to_row_or_column_vector (void) |
492
|
1310 { |
|
1311 assert (type_tag == matrix_constant || type_tag == complex_matrix_constant); |
|
1312 |
|
1313 int nr = rows (); |
|
1314 int nc = columns (); |
|
1315 |
509
|
1316 if (nr == 1 || nc == 1) |
|
1317 return; |
|
1318 |
492
|
1319 int len = nr * nc; |
|
1320 |
|
1321 assert (len > 0); |
|
1322 |
|
1323 int new_nr = 1; |
|
1324 int new_nc = 1; |
|
1325 |
|
1326 if (user_pref.prefer_column_vectors) |
|
1327 new_nr = len; |
|
1328 else |
|
1329 new_nc = len; |
|
1330 |
|
1331 if (type_tag == matrix_constant) |
|
1332 { |
|
1333 Matrix *m = new Matrix (new_nr, new_nc); |
|
1334 |
|
1335 double *cop_out = matrix->fortran_vec (); |
|
1336 |
|
1337 for (int i = 0; i < len; i++) |
|
1338 { |
|
1339 if (new_nr == 1) |
|
1340 m->elem (0, i) = *cop_out++; |
|
1341 else |
|
1342 m->elem (i, 0) = *cop_out++; |
|
1343 } |
|
1344 |
|
1345 delete matrix; |
|
1346 matrix = m; |
|
1347 } |
|
1348 else |
|
1349 { |
|
1350 ComplexMatrix *cm = new ComplexMatrix (new_nr, new_nc); |
|
1351 |
|
1352 Complex *cop_out = complex_matrix->fortran_vec (); |
|
1353 |
|
1354 for (int i = 0; i < len; i++) |
|
1355 { |
|
1356 if (new_nr == 1) |
|
1357 cm->elem (0, i) = *cop_out++; |
|
1358 else |
|
1359 cm->elem (i, 0) = *cop_out++; |
|
1360 } |
|
1361 |
|
1362 delete complex_matrix; |
|
1363 complex_matrix = cm; |
|
1364 } |
|
1365 } |
|
1366 |
631
|
1367 void |
|
1368 TC_REP::force_numeric (int force_str_conv) |
492
|
1369 { |
|
1370 switch (type_tag) |
|
1371 { |
|
1372 case scalar_constant: |
|
1373 case matrix_constant: |
|
1374 case complex_scalar_constant: |
|
1375 case complex_matrix_constant: |
631
|
1376 break; |
|
1377 |
|
1378 case string_constant: |
492
|
1379 { |
631
|
1380 if (! force_str_conv && ! user_pref.implicit_str_to_num_ok) |
|
1381 { |
|
1382 ::error ("failed to convert `%s' to a numeric type --", string); |
|
1383 ::error ("default conversion turned off"); |
915
|
1384 |
|
1385 return; |
631
|
1386 } |
|
1387 |
|
1388 int len = strlen (string); |
|
1389 if (len > 1) |
|
1390 { |
|
1391 type_tag = matrix_constant; |
|
1392 Matrix *tm = new Matrix (1, len); |
|
1393 for (int i = 0; i < len; i++) |
|
1394 tm->elem (0, i) = toascii ((int) string[i]); |
|
1395 matrix = tm; |
|
1396 } |
|
1397 else if (len == 1) |
|
1398 { |
|
1399 type_tag = scalar_constant; |
|
1400 scalar = toascii ((int) string[0]); |
|
1401 } |
|
1402 else if (len == 0) |
|
1403 { |
|
1404 type_tag = matrix_constant; |
|
1405 matrix = new Matrix (0, 0); |
|
1406 } |
|
1407 else |
|
1408 panic_impossible (); |
492
|
1409 } |
|
1410 break; |
631
|
1411 |
492
|
1412 case range_constant: |
631
|
1413 { |
|
1414 int len = range->nelem (); |
|
1415 if (len > 1) |
|
1416 { |
|
1417 type_tag = matrix_constant; |
|
1418 Matrix *tm = new Matrix (1, len); |
|
1419 double b = range->base (); |
|
1420 double increment = range->inc (); |
|
1421 for (int i = 0; i < len; i++) |
|
1422 tm->elem (0, i) = b + i * increment; |
|
1423 matrix = tm; |
|
1424 } |
|
1425 else if (len == 1) |
|
1426 { |
|
1427 type_tag = scalar_constant; |
|
1428 scalar = range->base (); |
|
1429 } |
|
1430 } |
|
1431 break; |
|
1432 |
492
|
1433 default: |
777
|
1434 gripe_invalid_conversion (type_as_string (), "numeric type"); |
492
|
1435 break; |
|
1436 } |
|
1437 } |
|
1438 |
|
1439 tree_constant |
631
|
1440 TC_REP::make_numeric (int force_str_conv) const |
492
|
1441 { |
|
1442 tree_constant retval; |
631
|
1443 |
492
|
1444 switch (type_tag) |
|
1445 { |
|
1446 case scalar_constant: |
|
1447 retval = tree_constant (scalar); |
|
1448 break; |
631
|
1449 |
492
|
1450 case matrix_constant: |
631
|
1451 retval = tree_constant (*matrix); |
|
1452 break; |
|
1453 |
492
|
1454 case complex_scalar_constant: |
|
1455 retval = tree_constant (*complex_scalar); |
|
1456 break; |
631
|
1457 |
492
|
1458 case complex_matrix_constant: |
631
|
1459 retval = tree_constant (*complex_matrix); |
|
1460 break; |
|
1461 |
492
|
1462 case string_constant: |
631
|
1463 retval = tree_constant (string); |
|
1464 retval.force_numeric (force_str_conv); |
|
1465 break; |
|
1466 |
492
|
1467 case range_constant: |
631
|
1468 retval = tree_constant (*range); |
|
1469 retval.force_numeric (force_str_conv); |
|
1470 break; |
|
1471 |
492
|
1472 default: |
777
|
1473 gripe_invalid_conversion (type_as_string (), "numeric value"); |
492
|
1474 break; |
|
1475 } |
631
|
1476 |
|
1477 return retval; |
|
1478 } |
|
1479 |
|
1480 void |
|
1481 TC_REP::bump_value (tree_expression::type etype) |
|
1482 { |
|
1483 switch (etype) |
|
1484 { |
|
1485 case tree_expression::increment: |
|
1486 switch (type_tag) |
|
1487 { |
|
1488 case scalar_constant: |
|
1489 scalar++; |
|
1490 break; |
|
1491 |
|
1492 case matrix_constant: |
|
1493 *matrix = *matrix + 1.0; |
|
1494 break; |
|
1495 |
|
1496 case complex_scalar_constant: |
|
1497 *complex_scalar = *complex_scalar + 1.0; |
|
1498 break; |
|
1499 |
|
1500 case complex_matrix_constant: |
|
1501 *complex_matrix = *complex_matrix + 1.0; |
|
1502 break; |
|
1503 |
|
1504 case range_constant: |
|
1505 range->set_base (range->base () + 1.0); |
|
1506 range->set_limit (range->limit () + 1.0); |
|
1507 break; |
|
1508 |
|
1509 default: |
777
|
1510 gripe_wrong_type_arg ("operator ++", type_as_string ()); |
631
|
1511 break; |
|
1512 } |
|
1513 break; |
|
1514 |
|
1515 case tree_expression::decrement: |
|
1516 switch (type_tag) |
|
1517 { |
|
1518 case scalar_constant: |
|
1519 scalar--; |
|
1520 break; |
|
1521 |
|
1522 case matrix_constant: |
|
1523 *matrix = *matrix - 1.0; |
|
1524 break; |
|
1525 |
|
1526 case range_constant: |
|
1527 range->set_base (range->base () - 1.0); |
|
1528 range->set_limit (range->limit () - 1.0); |
|
1529 break; |
|
1530 |
|
1531 default: |
777
|
1532 gripe_wrong_type_arg ("operator --", type_as_string ()); |
631
|
1533 break; |
|
1534 } |
|
1535 break; |
|
1536 |
|
1537 default: |
|
1538 panic_impossible (); |
|
1539 break; |
|
1540 } |
|
1541 } |
|
1542 |
|
1543 void |
|
1544 TC_REP::resize (int i, int j) |
|
1545 { |
|
1546 switch (type_tag) |
|
1547 { |
|
1548 case matrix_constant: |
|
1549 matrix->resize (i, j); |
|
1550 break; |
|
1551 |
|
1552 case complex_matrix_constant: |
|
1553 complex_matrix->resize (i, j); |
|
1554 break; |
|
1555 |
|
1556 default: |
777
|
1557 gripe_wrong_type_arg ("resize", type_as_string ()); |
631
|
1558 break; |
|
1559 } |
|
1560 } |
|
1561 |
|
1562 void |
|
1563 TC_REP::resize (int i, int j, double val) |
|
1564 { |
|
1565 switch (type_tag) |
|
1566 { |
|
1567 case matrix_constant: |
|
1568 matrix->resize (i, j, val); |
|
1569 break; |
|
1570 |
|
1571 case complex_matrix_constant: |
|
1572 complex_matrix->resize (i, j, val); |
|
1573 break; |
|
1574 |
|
1575 default: |
777
|
1576 gripe_wrong_type_arg ("resize", type_as_string ()); |
631
|
1577 break; |
|
1578 } |
|
1579 } |
|
1580 |
|
1581 void |
|
1582 TC_REP::maybe_resize (int i, int j) |
|
1583 { |
|
1584 int nr = rows (); |
|
1585 int nc = columns (); |
|
1586 |
|
1587 i++; |
|
1588 j++; |
|
1589 |
|
1590 assert (i > 0 && j > 0); |
|
1591 |
|
1592 if (i > nr || j > nc) |
|
1593 { |
|
1594 if (user_pref.resize_on_range_error) |
|
1595 resize (MAX (i, nr), MAX (j, nc), 0.0); |
|
1596 else |
|
1597 { |
|
1598 if (i > nr) |
|
1599 ::error ("row index = %d exceeds max row dimension = %d", i, nr); |
|
1600 |
|
1601 if (j > nc) |
|
1602 ::error ("column index = %d exceeds max column dimension = %d", |
|
1603 j, nc); |
|
1604 } |
|
1605 } |
|
1606 } |
|
1607 |
|
1608 void |
|
1609 TC_REP::maybe_resize (int i, force_orient f_orient) |
|
1610 { |
|
1611 int nr = rows (); |
|
1612 int nc = columns (); |
|
1613 |
|
1614 i++; |
|
1615 |
|
1616 assert (i >= 0 && (nr <= 1 || nc <= 1)); |
|
1617 |
|
1618 // This function never reduces the size of a vector, and all vectors |
|
1619 // have dimensions of at least 0x0. If i is 0, it is either because |
|
1620 // a vector has been indexed with a vector of all zeros (in which case |
|
1621 // the index vector is empty and nothing will happen) or a vector has |
|
1622 // been indexed with 0 (an error which will be caught elsewhere). |
|
1623 if (i == 0) |
|
1624 return; |
|
1625 |
|
1626 if (nr <= 1 && nc <= 1 && i >= 1) |
|
1627 { |
|
1628 if (user_pref.resize_on_range_error) |
|
1629 { |
|
1630 if (f_orient == row_orient) |
|
1631 resize (1, i, 0.0); |
|
1632 else if (f_orient == column_orient) |
|
1633 resize (i, 1, 0.0); |
|
1634 else if (user_pref.prefer_column_vectors) |
|
1635 resize (i, 1, 0.0); |
|
1636 else |
|
1637 resize (1, i, 0.0); |
|
1638 } |
|
1639 else |
|
1640 ::error ("matrix index = %d exceeds max dimension = %d", i, nc); |
|
1641 } |
|
1642 else if (nr == 1 && i > nc) |
|
1643 { |
|
1644 if (user_pref.resize_on_range_error) |
|
1645 resize (1, i, 0.0); |
|
1646 else |
|
1647 ::error ("matrix index = %d exceeds max dimension = %d", i, nc); |
|
1648 } |
|
1649 else if (nc == 1 && i > nr) |
|
1650 { |
|
1651 if (user_pref.resize_on_range_error) |
|
1652 resize (i, 1, 0.0); |
|
1653 else |
|
1654 ::error ("matrix index = %d exceeds max dimension = ", i, nc); |
|
1655 } |
|
1656 } |
|
1657 |
|
1658 void |
|
1659 TC_REP::stash_original_text (char *s) |
|
1660 { |
|
1661 orig_text = strsave (s); |
|
1662 } |
|
1663 |
|
1664 void |
|
1665 TC_REP::maybe_mutate (void) |
492
|
1666 { |
631
|
1667 if (error_state) |
|
1668 return; |
581
|
1669 |
|
1670 switch (type_tag) |
|
1671 { |
|
1672 case complex_scalar_constant: |
631
|
1673 if (::imag (*complex_scalar) == 0.0) |
|
1674 { |
|
1675 double d = ::real (*complex_scalar); |
|
1676 delete complex_scalar; |
|
1677 scalar = d; |
|
1678 type_tag = scalar_constant; |
|
1679 } |
|
1680 break; |
|
1681 |
|
1682 case complex_matrix_constant: |
|
1683 if (! any_element_is_complex (*complex_matrix)) |
|
1684 { |
|
1685 Matrix *m = new Matrix (::real (*complex_matrix)); |
|
1686 delete complex_matrix; |
|
1687 matrix = m; |
|
1688 type_tag = matrix_constant; |
|
1689 } |
|
1690 break; |
|
1691 |
|
1692 default: |
|
1693 break; |
|
1694 } |
|
1695 |
|
1696 // Avoid calling rows() and columns() for things like magic_colon. |
|
1697 |
|
1698 int nr = 1; |
|
1699 int nc = 1; |
|
1700 if (type_tag == matrix_constant |
|
1701 || type_tag == complex_matrix_constant |
|
1702 || type_tag == range_constant) |
|
1703 { |
|
1704 nr = rows (); |
|
1705 nc = columns (); |
|
1706 } |
|
1707 |
|
1708 switch (type_tag) |
|
1709 { |
|
1710 case matrix_constant: |
|
1711 if (nr == 1 && nc == 1) |
|
1712 { |
|
1713 double d = matrix->elem (0, 0); |
|
1714 delete matrix; |
|
1715 scalar = d; |
|
1716 type_tag = scalar_constant; |
|
1717 } |
|
1718 break; |
|
1719 |
581
|
1720 case complex_matrix_constant: |
631
|
1721 if (nr == 1 && nc == 1) |
|
1722 { |
|
1723 Complex c = complex_matrix->elem (0, 0); |
|
1724 delete complex_matrix; |
|
1725 complex_scalar = new Complex (c); |
|
1726 type_tag = complex_scalar_constant; |
|
1727 } |
|
1728 break; |
|
1729 |
|
1730 case range_constant: |
|
1731 if (nr == 1 && nc == 1) |
|
1732 { |
|
1733 double d = range->base (); |
|
1734 delete range; |
|
1735 scalar = d; |
|
1736 type_tag = scalar_constant; |
|
1737 } |
|
1738 break; |
|
1739 |
|
1740 default: |
|
1741 break; |
|
1742 } |
|
1743 } |
|
1744 |
|
1745 void |
1199
|
1746 TC_REP::print (ostream& output_buf) |
631
|
1747 { |
|
1748 if (error_state) |
|
1749 return; |
|
1750 |
961
|
1751 switch (type_tag) |
|
1752 { |
|
1753 case scalar_constant: |
|
1754 octave_print_internal (output_buf, scalar); |
|
1755 break; |
|
1756 |
|
1757 case matrix_constant: |
|
1758 octave_print_internal (output_buf, *matrix); |
|
1759 break; |
631
|
1760 |
961
|
1761 case complex_scalar_constant: |
|
1762 octave_print_internal (output_buf, *complex_scalar); |
|
1763 break; |
631
|
1764 |
961
|
1765 case complex_matrix_constant: |
|
1766 octave_print_internal (output_buf, *complex_matrix); |
|
1767 break; |
631
|
1768 |
961
|
1769 case string_constant: |
|
1770 output_buf << string << "\n"; |
|
1771 break; |
631
|
1772 |
961
|
1773 case range_constant: |
|
1774 octave_print_internal (output_buf, *range); |
|
1775 break; |
631
|
1776 |
961
|
1777 case map_constant: |
|
1778 { |
1199
|
1779 // XXX FIXME XXX -- would be nice to print the output in some standard |
|
1780 // order. Maybe all substructures first, maybe alphabetize entries, |
|
1781 // etc. |
|
1782 begin_unwind_frame ("TC_REP_print"); |
|
1783 |
|
1784 unwind_protect_int (structure_indent_level); |
|
1785 unwind_protect_int (user_pref.struct_levels_to_print); |
|
1786 |
1202
|
1787 if (user_pref.struct_levels_to_print-- > 0) |
745
|
1788 { |
1202
|
1789 output_buf << "{\n"; |
1199
|
1790 |
1200
|
1791 increment_structure_indent_level (); |
|
1792 |
1199
|
1793 for (Pix p = a_map->first (); p != 0; a_map->next (p)) |
745
|
1794 { |
1199
|
1795 const char *key = a_map->key (p); |
|
1796 tree_constant val = a_map->contents (p); |
|
1797 |
|
1798 output_buf.form ("%*s%s = ", structure_indent_level, |
|
1799 "", key); |
|
1800 |
1202
|
1801 if (! (print_as_scalar (val) || print_as_structure (val))) |
1199
|
1802 output_buf << "\n"; |
|
1803 |
|
1804 val.print (output_buf); |
745
|
1805 } |
1199
|
1806 |
1200
|
1807 decrement_structure_indent_level (); |
|
1808 |
1199
|
1809 output_buf.form ("%*s%s", structure_indent_level, "", "}\n"); |
745
|
1810 } |
1199
|
1811 else |
|
1812 output_buf << "<structure>\n"; |
|
1813 |
|
1814 run_unwind_frame ("TC_REP_print"); |
961
|
1815 } |
|
1816 break; |
745
|
1817 |
961
|
1818 case unknown_constant: |
|
1819 case magic_colon: |
|
1820 case all_va_args: |
|
1821 panic_impossible (); |
|
1822 break; |
|
1823 } |
631
|
1824 } |
|
1825 |
|
1826 void |
|
1827 TC_REP::print_code (ostream& os) |
|
1828 { |
|
1829 switch (type_tag) |
|
1830 { |
|
1831 case scalar_constant: |
|
1832 if (orig_text) |
|
1833 os << orig_text; |
|
1834 else |
|
1835 octave_print_internal (os, scalar, 1); |
|
1836 break; |
|
1837 |
|
1838 case matrix_constant: |
|
1839 octave_print_internal (os, *matrix, 1); |
|
1840 break; |
|
1841 |
|
1842 case complex_scalar_constant: |
|
1843 { |
|
1844 double re = complex_scalar->real (); |
|
1845 double im = complex_scalar->imag (); |
|
1846 |
|
1847 // If we have the original text and a pure imaginary, just print the |
|
1848 // original text, because this must be a constant that was parsed as |
|
1849 // part of a function. |
|
1850 |
|
1851 if (orig_text && re == 0.0 && im > 0.0) |
|
1852 os << orig_text; |
|
1853 else |
|
1854 octave_print_internal (os, *complex_scalar, 1); |
|
1855 } |
|
1856 break; |
|
1857 |
|
1858 case complex_matrix_constant: |
|
1859 octave_print_internal (os, *complex_matrix, 1); |
|
1860 break; |
|
1861 |
|
1862 case string_constant: |
|
1863 { |
|
1864 os << "\""; |
|
1865 char *s, *t = string; |
1215
|
1866 while ((s = undo_string_escape (*t++))) |
631
|
1867 os << s; |
|
1868 os << "\""; |
|
1869 } |
|
1870 break; |
|
1871 |
|
1872 case range_constant: |
|
1873 octave_print_internal (os, *range, 1); |
|
1874 break; |
|
1875 |
|
1876 case magic_colon: |
|
1877 os << ":"; |
|
1878 break; |
|
1879 |
922
|
1880 case all_va_args: |
|
1881 os << "all_va_args"; |
|
1882 break; |
|
1883 |
777
|
1884 case map_constant: |
|
1885 case unknown_constant: |
631
|
1886 panic_impossible (); |
|
1887 break; |
|
1888 } |
|
1889 } |
|
1890 |
777
|
1891 void |
|
1892 TC_REP::gripe_wrong_type_arg (const char *name, |
|
1893 const tree_constant_rep& tcr) const |
|
1894 { |
|
1895 if (name) |
|
1896 ::error ("%s: wrong type argument `%s'", name, tcr.type_as_string ()); |
|
1897 else |
|
1898 ::error ("wrong type argument `%s'", name, tcr.type_as_string ()); |
|
1899 } |
|
1900 |
631
|
1901 char * |
|
1902 TC_REP::type_as_string (void) const |
|
1903 { |
|
1904 switch (type_tag) |
|
1905 { |
|
1906 case scalar_constant: |
|
1907 return "real scalar"; |
|
1908 |
581
|
1909 case matrix_constant: |
631
|
1910 return "real matrix"; |
|
1911 |
|
1912 case complex_scalar_constant: |
|
1913 return "complex scalar"; |
|
1914 |
|
1915 case complex_matrix_constant: |
|
1916 return "complex matrix"; |
|
1917 |
|
1918 case string_constant: |
|
1919 return "string"; |
|
1920 |
581
|
1921 case range_constant: |
631
|
1922 return "range"; |
|
1923 |
777
|
1924 case map_constant: |
|
1925 return "structure"; |
|
1926 |
631
|
1927 default: |
|
1928 return "<unknown type>"; |
|
1929 } |
|
1930 } |
|
1931 |
|
1932 tree_constant |
|
1933 do_binary_op (tree_constant& a, tree_constant& b, tree_expression::type t) |
|
1934 { |
777
|
1935 tree_constant retval; |
631
|
1936 |
|
1937 int first_empty = (a.rows () == 0 || a.columns () == 0); |
|
1938 int second_empty = (b.rows () == 0 || b.columns () == 0); |
|
1939 |
|
1940 if (first_empty || second_empty) |
|
1941 { |
|
1942 int flag = user_pref.propagate_empty_matrices; |
|
1943 if (flag < 0) |
|
1944 warning ("binary operation on empty matrix"); |
|
1945 else if (flag == 0) |
|
1946 { |
|
1947 ::error ("invalid binary operation on empty matrix"); |
777
|
1948 return retval; |
631
|
1949 } |
|
1950 } |
|
1951 |
|
1952 tree_constant tmp_a = a.make_numeric (); |
777
|
1953 |
|
1954 if (error_state) |
|
1955 return retval; |
|
1956 |
631
|
1957 tree_constant tmp_b = b.make_numeric (); |
|
1958 |
777
|
1959 if (error_state) |
|
1960 return retval; |
|
1961 |
631
|
1962 TC_REP::constant_type a_type = tmp_a.const_type (); |
|
1963 TC_REP::constant_type b_type = tmp_b.const_type (); |
|
1964 |
|
1965 double d1, d2; |
|
1966 Matrix m1, m2; |
|
1967 Complex c1, c2; |
|
1968 ComplexMatrix cm1, cm2; |
|
1969 |
|
1970 switch (a_type) |
|
1971 { |
|
1972 case TC_REP::scalar_constant: |
|
1973 |
|
1974 d1 = tmp_a.double_value (); |
|
1975 |
|
1976 switch (b_type) |
|
1977 { |
|
1978 case TC_REP::scalar_constant: |
|
1979 d2 = tmp_b.double_value (); |
777
|
1980 retval = do_binary_op (d1, d2, t); |
631
|
1981 break; |
|
1982 |
|
1983 case TC_REP::matrix_constant: |
|
1984 m2 = tmp_b.matrix_value (); |
777
|
1985 retval = do_binary_op (d1, m2, t); |
631
|
1986 break; |
|
1987 |
|
1988 case TC_REP::complex_scalar_constant: |
|
1989 c2 = tmp_b.complex_value (); |
777
|
1990 retval = do_binary_op (d1, c2, t); |
631
|
1991 break; |
|
1992 |
|
1993 case TC_REP::complex_matrix_constant: |
|
1994 cm2 = tmp_b.complex_matrix_value (); |
777
|
1995 retval = do_binary_op (d1, cm2, t); |
631
|
1996 break; |
|
1997 |
|
1998 default: |
777
|
1999 gripe_wrong_type_arg_for_binary_op (tmp_b); |
631
|
2000 break; |
|
2001 } |
|
2002 break; |
|
2003 |
|
2004 case TC_REP::matrix_constant: |
|
2005 |
|
2006 m1 = tmp_a.matrix_value (); |
|
2007 |
|
2008 switch (b_type) |
|
2009 { |
|
2010 case TC_REP::scalar_constant: |
|
2011 d2 = tmp_b.double_value (); |
777
|
2012 retval = do_binary_op (m1, d2, t); |
631
|
2013 break; |
|
2014 |
|
2015 case TC_REP::matrix_constant: |
|
2016 m2 = tmp_b.matrix_value (); |
777
|
2017 retval = do_binary_op (m1, m2, t); |
631
|
2018 break; |
|
2019 |
|
2020 case TC_REP::complex_scalar_constant: |
|
2021 c2 = tmp_b.complex_value (); |
777
|
2022 retval = do_binary_op (m1, c2, t); |
631
|
2023 break; |
|
2024 |
|
2025 case TC_REP::complex_matrix_constant: |
|
2026 cm2 = tmp_b.complex_matrix_value (); |
777
|
2027 retval = do_binary_op (m1, cm2, t); |
631
|
2028 break; |
|
2029 |
|
2030 default: |
777
|
2031 gripe_wrong_type_arg_for_binary_op (tmp_b); |
631
|
2032 break; |
|
2033 } |
|
2034 break; |
|
2035 |
|
2036 case TC_REP::complex_scalar_constant: |
|
2037 |
|
2038 c1 = tmp_a.complex_value (); |
|
2039 |
|
2040 switch (b_type) |
|
2041 { |
|
2042 case TC_REP::scalar_constant: |
|
2043 d2 = tmp_b.double_value (); |
777
|
2044 retval = do_binary_op (c1, d2, t); |
631
|
2045 break; |
|
2046 |
|
2047 case TC_REP::matrix_constant: |
|
2048 m2 = tmp_b.matrix_value (); |
777
|
2049 retval = do_binary_op (c1, m2, t); |
631
|
2050 break; |
|
2051 |
|
2052 case TC_REP::complex_scalar_constant: |
|
2053 c2 = tmp_b.complex_value (); |
777
|
2054 retval = do_binary_op (c1, c2, t); |
631
|
2055 break; |
|
2056 |
|
2057 case TC_REP::complex_matrix_constant: |
|
2058 cm2 = tmp_b.complex_matrix_value (); |
777
|
2059 retval = do_binary_op (c1, cm2, t); |
631
|
2060 break; |
|
2061 |
|
2062 default: |
777
|
2063 gripe_wrong_type_arg_for_binary_op (tmp_b); |
631
|
2064 break; |
|
2065 } |
|
2066 break; |
|
2067 |
|
2068 case TC_REP::complex_matrix_constant: |
|
2069 |
|
2070 cm1 = tmp_a.complex_matrix_value (); |
|
2071 |
|
2072 switch (b_type) |
|
2073 { |
|
2074 case TC_REP::scalar_constant: |
|
2075 d2 = tmp_b.double_value (); |
777
|
2076 retval = do_binary_op (cm1, d2, t); |
631
|
2077 break; |
|
2078 |
|
2079 case TC_REP::matrix_constant: |
|
2080 m2 = tmp_b.matrix_value (); |
777
|
2081 retval = do_binary_op (cm1, m2, t); |
631
|
2082 break; |
|
2083 |
|
2084 case TC_REP::complex_scalar_constant: |
|
2085 c2 = tmp_b.complex_value (); |
777
|
2086 retval = do_binary_op (cm1, c2, t); |
631
|
2087 break; |
|
2088 |
|
2089 case TC_REP::complex_matrix_constant: |
|
2090 cm2 = tmp_b.complex_matrix_value (); |
777
|
2091 retval = do_binary_op (cm1, cm2, t); |
631
|
2092 break; |
|
2093 |
|
2094 default: |
777
|
2095 gripe_wrong_type_arg_for_binary_op (tmp_b); |
631
|
2096 break; |
|
2097 } |
|
2098 break; |
|
2099 |
|
2100 default: |
777
|
2101 gripe_wrong_type_arg_for_binary_op (tmp_a); |
631
|
2102 break; |
|
2103 } |
|
2104 |
777
|
2105 return retval; |
631
|
2106 } |
|
2107 |
|
2108 tree_constant |
|
2109 do_unary_op (tree_constant& a, tree_expression::type t) |
|
2110 { |
777
|
2111 tree_constant retval; |
631
|
2112 |
|
2113 if (a.rows () == 0 || a.columns () == 0) |
|
2114 { |
|
2115 int flag = user_pref.propagate_empty_matrices; |
|
2116 if (flag < 0) |
|
2117 warning ("unary operation on empty matrix"); |
|
2118 else if (flag == 0) |
|
2119 { |
|
2120 ::error ("invalid unary operation on empty matrix"); |
777
|
2121 return retval; |
631
|
2122 } |
|
2123 } |
|
2124 |
|
2125 tree_constant tmp_a = a.make_numeric (); |
|
2126 |
777
|
2127 if (error_state) |
|
2128 return retval; |
|
2129 |
631
|
2130 switch (tmp_a.const_type ()) |
|
2131 { |
|
2132 case TC_REP::scalar_constant: |
777
|
2133 retval = do_unary_op (tmp_a.double_value (), t); |
631
|
2134 break; |
|
2135 |
|
2136 case TC_REP::matrix_constant: |
|
2137 { |
|
2138 Matrix m = tmp_a.matrix_value (); |
777
|
2139 retval = do_unary_op (m, t); |
631
|
2140 } |
|
2141 break; |
|
2142 |
|
2143 case TC_REP::complex_scalar_constant: |
777
|
2144 retval = do_unary_op (tmp_a.complex_value (), t); |
631
|
2145 break; |
|
2146 |
|
2147 case TC_REP::complex_matrix_constant: |
|
2148 { |
|
2149 ComplexMatrix m = tmp_a.complex_matrix_value (); |
777
|
2150 retval = do_unary_op (m, t); |
631
|
2151 } |
|
2152 break; |
|
2153 |
|
2154 default: |
777
|
2155 gripe_wrong_type_arg_for_unary_op (tmp_a); |
631
|
2156 break; |
|
2157 } |
|
2158 |
777
|
2159 return retval; |
631
|
2160 } |
|
2161 |
492
|
2162 /* |
|
2163 ;;; Local Variables: *** |
|
2164 ;;; mode: C++ *** |
|
2165 ;;; page-delimiter: "^/\\*" *** |
|
2166 ;;; End: *** |
|
2167 */ |