2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "Array-flags.h" |
|
32 |
|
33 #include "ov.h" |
|
34 #include "ov-base.h" |
2825
|
35 #include "ov-bool.h" |
|
36 #include "ov-bool-mat.h" |
2376
|
37 #include "ov-scalar.h" |
|
38 #include "ov-re-mat.h" |
|
39 #include "ov-complex.h" |
|
40 #include "ov-cx-mat.h" |
|
41 #include "ov-ch-mat.h" |
|
42 #include "ov-str-mat.h" |
|
43 #include "ov-range.h" |
|
44 #include "ov-struct.h" |
2880
|
45 #include "ov-list.h" |
2376
|
46 #include "ov-colon.h" |
|
47 #include "ov-va-args.h" |
|
48 #include "ov-typeinfo.h" |
|
49 |
|
50 #include "defun.h" |
2880
|
51 #include "error.h" |
2376
|
52 #include "gripes.h" |
|
53 #include "pager.h" |
|
54 #include "pr-output.h" |
|
55 #include "utils.h" |
|
56 #include "variables.h" |
|
57 |
2477
|
58 // We are likely to have a lot of octave_value objects to allocate, so |
|
59 // make the grow_size large. |
|
60 octave_allocator |
|
61 octave_value::allocator (sizeof (octave_value), 1024); |
|
62 |
2376
|
63 // If TRUE, allow assignments like |
|
64 // |
|
65 // octave> A(1) = 3; A(2) = 5 |
|
66 // |
|
67 // for A already defined and a matrix type. |
|
68 bool Vdo_fortran_indexing; |
|
69 |
|
70 // Should we allow things like: |
|
71 // |
|
72 // octave> 'abc' + 0 |
|
73 // 97 98 99 |
|
74 // |
|
75 // to happen? A positive value means yes. A negative value means |
|
76 // yes, but print a warning message. Zero means it should be |
|
77 // considered an error. |
|
78 int Vimplicit_str_to_num_ok; |
|
79 |
|
80 // Should we allow silent conversion of complex to real when a real |
|
81 // type is what we're really looking for? A positive value means yes. |
|
82 // A negative value means yes, but print a warning message. Zero |
|
83 // means it should be considered an error. |
|
84 int Vok_to_lose_imaginary_part; |
|
85 |
|
86 // If TRUE, create column vectors when doing assignments like: |
|
87 // |
|
88 // octave> A(1) = 3; A(2) = 5 |
|
89 // |
|
90 // (for A undefined). Only matters when resize_on_range_error is also |
|
91 // TRUE. |
|
92 bool Vprefer_column_vectors; |
|
93 |
|
94 // If TRUE, print the name along with the value. |
|
95 bool Vprint_answer_id_name; |
|
96 |
|
97 // Should operations on empty matrices return empty matrices or an |
|
98 // error? A positive value means yes. A negative value means yes, |
|
99 // but print a warning message. Zero means it should be considered an |
|
100 // error. |
|
101 int Vpropagate_empty_matrices; |
|
102 |
|
103 // If TRUE, resize matrices when performing and indexed assignment and |
|
104 // the indices are outside the current bounds. |
|
105 bool Vresize_on_range_error; |
|
106 |
|
107 // How many levels of structure elements should we print? |
|
108 int Vstruct_levels_to_print; |
|
109 |
|
110 // Allow divide by zero errors to be suppressed. |
|
111 bool Vwarn_divide_by_zero; |
|
112 |
|
113 // Indentation level for structures. |
|
114 int struct_indent = 0; |
|
115 |
|
116 // XXX FIXME XXX |
|
117 void |
|
118 increment_struct_indent (void) |
|
119 { |
|
120 struct_indent += 2; |
|
121 } |
|
122 |
|
123 void |
|
124 decrement_struct_indent (void) |
|
125 { |
|
126 struct_indent -= 2; |
|
127 } |
|
128 |
2880
|
129 // Indentation level for lists. |
|
130 int list_indent = 0; |
|
131 |
|
132 void |
|
133 increment_list_indent (void) |
|
134 { |
|
135 list_indent += 2; |
|
136 } |
|
137 |
|
138 void |
|
139 decrement_list_indent (void) |
|
140 { |
|
141 list_indent -= 2; |
|
142 } |
|
143 |
|
144 // XXX FIXME XXX |
|
145 |
2376
|
146 // Octave's value type. |
|
147 |
|
148 string |
|
149 octave_value::binary_op_as_string (binary_op op) |
|
150 { |
|
151 string retval; |
|
152 |
|
153 switch (op) |
|
154 { |
|
155 case add: |
|
156 retval = "+"; |
|
157 break; |
|
158 |
|
159 case sub: |
|
160 retval = "-"; |
|
161 break; |
|
162 |
|
163 case mul: |
|
164 retval = "*"; |
|
165 break; |
|
166 |
|
167 case div: |
|
168 retval = "/"; |
|
169 break; |
|
170 |
|
171 case pow: |
|
172 retval = "^"; |
|
173 break; |
|
174 |
|
175 case ldiv: |
|
176 retval = "\\"; |
|
177 break; |
|
178 |
|
179 case lt: |
|
180 retval = "<"; |
|
181 break; |
|
182 |
|
183 case le: |
|
184 retval = "<="; |
|
185 break; |
|
186 |
|
187 case eq: |
|
188 retval = "=="; |
|
189 break; |
|
190 |
|
191 case ge: |
|
192 retval = ">="; |
|
193 break; |
|
194 |
|
195 case gt: |
|
196 retval = ">"; |
|
197 break; |
|
198 |
|
199 case ne: |
|
200 retval = "!="; |
|
201 break; |
|
202 |
|
203 case el_mul: |
|
204 retval = ".*"; |
|
205 break; |
|
206 |
|
207 case el_div: |
|
208 retval = "./"; |
|
209 break; |
|
210 |
|
211 case el_pow: |
|
212 retval = ".^"; |
|
213 break; |
|
214 |
|
215 case el_ldiv: |
|
216 retval = ".\\"; |
|
217 break; |
|
218 |
|
219 case el_and: |
|
220 retval = "&"; |
|
221 break; |
|
222 |
|
223 case el_or: |
|
224 retval = "|"; |
|
225 break; |
|
226 |
|
227 case struct_ref: |
|
228 retval = "."; |
|
229 break; |
|
230 |
|
231 default: |
|
232 retval = "<unknown>"; |
|
233 } |
|
234 |
|
235 return retval; |
|
236 } |
|
237 |
2880
|
238 string |
|
239 octave_value::assign_op_as_string (assign_op op) |
|
240 { |
|
241 string retval; |
|
242 |
|
243 switch (op) |
|
244 { |
|
245 case asn_eq: |
|
246 retval = "="; |
|
247 break; |
|
248 |
|
249 case add_eq: |
|
250 retval = "+="; |
|
251 break; |
|
252 |
|
253 case sub_eq: |
|
254 retval = "-="; |
|
255 break; |
|
256 |
|
257 case mul_eq: |
|
258 retval = "*="; |
|
259 break; |
|
260 |
|
261 case div_eq: |
|
262 retval = "/="; |
|
263 break; |
|
264 |
|
265 case el_mul_eq: |
|
266 retval = ".*="; |
|
267 break; |
|
268 |
|
269 case el_div_eq: |
|
270 retval = "./="; |
|
271 break; |
|
272 |
|
273 case el_and_eq: |
|
274 retval = "&="; |
|
275 break; |
|
276 |
|
277 case el_or_eq: |
|
278 retval = "|="; |
|
279 break; |
|
280 |
|
281 default: |
|
282 retval = "<unknown>"; |
|
283 } |
|
284 |
|
285 return retval; |
|
286 } |
|
287 |
2376
|
288 octave_value::octave_value (void) |
2825
|
289 : rep (new octave_base_value ()) |
|
290 { |
|
291 rep->count = 1; |
|
292 } |
2376
|
293 |
|
294 octave_value::octave_value (double d) |
2825
|
295 : rep (new octave_scalar (d)) |
|
296 { |
|
297 rep->count = 1; |
|
298 } |
2376
|
299 |
|
300 octave_value::octave_value (const Matrix& m) |
2423
|
301 : rep (new octave_matrix (m)) |
|
302 { |
|
303 rep->count = 1; |
|
304 maybe_mutate (); |
|
305 } |
2376
|
306 |
|
307 octave_value::octave_value (const DiagMatrix& d) |
2423
|
308 : rep (new octave_matrix (d)) |
|
309 { |
|
310 rep->count = 1; |
|
311 maybe_mutate (); |
|
312 } |
2376
|
313 |
|
314 octave_value::octave_value (const RowVector& v, int pcv) |
2423
|
315 : rep (new octave_matrix (v, pcv)) |
|
316 { |
|
317 rep->count = 1; |
|
318 maybe_mutate (); |
|
319 } |
2376
|
320 |
|
321 octave_value::octave_value (const ColumnVector& v, int pcv) |
2423
|
322 : rep (new octave_matrix (v, pcv)) |
|
323 { |
|
324 rep->count = 1; |
|
325 maybe_mutate (); |
|
326 } |
2376
|
327 |
|
328 octave_value::octave_value (const Complex& C) |
2423
|
329 : rep (new octave_complex (C)) |
|
330 { |
|
331 rep->count = 1; |
|
332 maybe_mutate (); |
|
333 } |
2376
|
334 |
|
335 octave_value::octave_value (const ComplexMatrix& m) |
2423
|
336 : rep (new octave_complex_matrix (m)) |
|
337 { |
|
338 rep->count = 1; |
|
339 maybe_mutate (); |
|
340 } |
2376
|
341 |
|
342 octave_value::octave_value (const ComplexDiagMatrix& d) |
2423
|
343 : rep (new octave_complex_matrix (d)) |
|
344 { |
|
345 rep->count = 1; |
|
346 maybe_mutate (); |
|
347 } |
2376
|
348 |
|
349 octave_value::octave_value (const ComplexRowVector& v, int pcv) |
2423
|
350 : rep (new octave_complex_matrix (v, pcv)) |
|
351 { |
|
352 rep->count = 1; |
|
353 maybe_mutate (); |
|
354 } |
2376
|
355 |
|
356 octave_value::octave_value (const ComplexColumnVector& v, int pcv) |
2423
|
357 : rep (new octave_complex_matrix (v, pcv)) |
|
358 { |
|
359 rep->count = 1; |
|
360 maybe_mutate (); |
|
361 } |
2376
|
362 |
2825
|
363 octave_value::octave_value (bool b) |
|
364 : rep (new octave_bool (b)) |
|
365 { |
|
366 rep->count = 1; |
|
367 } |
|
368 |
|
369 octave_value::octave_value (const boolMatrix& bm) |
|
370 : rep (new octave_bool_matrix (bm)) |
|
371 { |
|
372 rep->count = 1; |
|
373 maybe_mutate (); |
|
374 } |
|
375 |
2376
|
376 octave_value::octave_value (const char *s) |
2423
|
377 : rep (new octave_char_matrix_str (s)) |
|
378 { |
|
379 rep->count = 1; |
|
380 maybe_mutate (); |
|
381 } |
2376
|
382 |
|
383 octave_value::octave_value (const string& s) |
2423
|
384 : rep (new octave_char_matrix_str (s)) |
|
385 { |
|
386 rep->count = 1; |
|
387 maybe_mutate (); |
|
388 } |
2376
|
389 |
|
390 octave_value::octave_value (const string_vector& s) |
2423
|
391 : rep (new octave_char_matrix_str (s)) |
|
392 { |
|
393 rep->count = 1; |
|
394 maybe_mutate (); |
|
395 } |
2376
|
396 |
|
397 octave_value::octave_value (const charMatrix& chm, bool is_string) |
2409
|
398 : rep (0) |
|
399 { |
|
400 if (is_string) |
|
401 rep = new octave_char_matrix_str (chm); |
|
402 else |
|
403 rep = new octave_char_matrix (chm); |
2376
|
404 |
2409
|
405 rep->count = 1; |
2423
|
406 maybe_mutate (); |
2409
|
407 } |
2376
|
408 |
|
409 octave_value::octave_value (double base, double limit, double inc) |
2423
|
410 : rep (new octave_range (base, limit, inc)) |
|
411 { |
|
412 rep->count = 1; |
|
413 maybe_mutate (); |
|
414 } |
2376
|
415 |
|
416 octave_value::octave_value (const Range& r) |
2423
|
417 : rep (new octave_range (r)) |
|
418 { |
|
419 rep->count = 1; |
|
420 maybe_mutate (); |
|
421 } |
2376
|
422 |
|
423 octave_value::octave_value (const Octave_map& m) |
2825
|
424 : rep (new octave_struct (m)) |
|
425 { |
|
426 rep->count = 1; |
2880
|
427 } |
|
428 |
|
429 octave_value::octave_value (const octave_value_list& l) |
|
430 : rep (new octave_list (l)) |
|
431 { |
|
432 rep->count = 1; |
|
433 } |
2376
|
434 |
|
435 octave_value::octave_value (octave_value::magic_colon) |
2825
|
436 : rep (new octave_magic_colon ()) |
|
437 { |
|
438 rep->count = 1; |
|
439 } |
2376
|
440 |
|
441 octave_value::octave_value (octave_value::all_va_args) |
2825
|
442 : rep (new octave_all_va_args ()) |
|
443 { |
|
444 rep->count = 1; |
|
445 } |
2376
|
446 |
|
447 octave_value::octave_value (octave_value *new_rep) |
2825
|
448 : rep (new_rep) |
|
449 { |
|
450 rep->count = 1; |
|
451 } |
2376
|
452 |
|
453 octave_value::~octave_value (void) |
|
454 { |
|
455 #if defined (MDEBUG) |
|
456 cerr << "~octave_value: rep: " << rep |
|
457 << " rep->count: " << rep->count << "\n"; |
|
458 #endif |
|
459 |
|
460 if (rep && --rep->count == 0) |
|
461 { |
|
462 delete rep; |
|
463 rep = 0; |
|
464 } |
|
465 } |
|
466 |
2880
|
467 octave_value * |
|
468 octave_value::clone (void) |
|
469 { |
|
470 panic_impossible (); |
|
471 } |
|
472 |
2409
|
473 void |
|
474 octave_value::maybe_mutate (void) |
|
475 { |
2410
|
476 octave_value *tmp = rep->try_narrowing_conversion (); |
2409
|
477 |
|
478 if (tmp && tmp != rep) |
|
479 { |
|
480 if (--rep->count == 0) |
|
481 delete rep; |
|
482 |
|
483 rep = tmp; |
|
484 rep->count = 1; |
|
485 } |
|
486 } |
|
487 |
2376
|
488 static void |
|
489 gripe_no_conversion (const string& tn1, const string& tn2) |
|
490 { |
|
491 error ("no suitable conversion found for assignment of %s to indexed %s", |
|
492 tn2.c_str (), tn1.c_str ()); |
|
493 } |
|
494 |
2409
|
495 octave_value& |
2880
|
496 octave_value::assign (assign_op, const octave_value& rhs) |
|
497 { |
|
498 // XXX FIXME XXX -- make this work for ops other than `='. |
|
499 |
|
500 return operator = (rhs); |
|
501 } |
|
502 |
|
503 octave_value& |
|
504 octave_value::assign (octave_value::assign_op op, |
|
505 const octave_value_list& idx, |
|
506 const octave_value& rhs) |
2409
|
507 { |
|
508 make_unique (); |
2376
|
509 |
2880
|
510 bool assignment_ok = try_assignment (op, idx, rhs); |
2409
|
511 |
|
512 if (! (error_state || assignment_ok)) |
|
513 { |
2880
|
514 assignment_ok = try_assignment_with_conversion (op,idx, rhs); |
2409
|
515 |
|
516 if (! (error_state || assignment_ok)) |
2376
|
517 gripe_no_conversion (type_name (), rhs.type_name ()); |
|
518 } |
|
519 |
2409
|
520 if (! error_state) |
|
521 maybe_mutate (); |
|
522 |
2376
|
523 return *this; |
|
524 } |
|
525 |
|
526 Octave_map |
|
527 octave_value::map_value (void) const |
|
528 { |
|
529 return rep->map_value (); |
|
530 } |
|
531 |
2880
|
532 octave_value_list |
|
533 octave_value::list_value (void) const |
|
534 { |
|
535 return rep->list_value (); |
|
536 } |
|
537 |
2376
|
538 ColumnVector |
|
539 octave_value::vector_value (bool force_string_conv, |
|
540 bool force_vector_conversion) const |
|
541 { |
|
542 ColumnVector retval; |
|
543 |
|
544 Matrix m = matrix_value (force_string_conv); |
|
545 |
|
546 if (error_state) |
|
547 return retval; |
|
548 |
|
549 int nr = m.rows (); |
|
550 int nc = m.columns (); |
|
551 |
|
552 if (nr == 1) |
|
553 { |
|
554 retval.resize (nc); |
|
555 for (int i = 0; i < nc; i++) |
|
556 retval (i) = m (0, i); |
|
557 } |
|
558 else if (nc == 1) |
|
559 { |
|
560 retval.resize (nr); |
|
561 for (int i = 0; i < nr; i++) |
|
562 retval (i) = m (i, 0); |
|
563 } |
|
564 else if (nr > 0 && nc > 0 |
|
565 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
566 { |
|
567 retval.resize (nr * nc); |
|
568 int k = 0; |
|
569 for (int j = 0; j < nc; j++) |
|
570 for (int i = 0; i < nr; i++) |
|
571 retval (k++) = m (i, j); |
|
572 } |
|
573 else |
|
574 { |
|
575 string tn = type_name (); |
|
576 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
577 } |
|
578 |
|
579 return retval; |
|
580 } |
|
581 |
|
582 ComplexColumnVector |
|
583 octave_value::complex_vector_value (bool force_string_conv, |
|
584 bool force_vector_conversion) const |
|
585 { |
|
586 ComplexColumnVector retval; |
|
587 |
|
588 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
589 |
|
590 if (error_state) |
|
591 return retval; |
|
592 |
|
593 int nr = m.rows (); |
|
594 int nc = m.columns (); |
|
595 |
|
596 if (nr == 1) |
|
597 { |
|
598 retval.resize (nc); |
|
599 for (int i = 0; i < nc; i++) |
|
600 retval (i) = m (0, i); |
|
601 } |
|
602 else if (nc == 1) |
|
603 { |
|
604 retval.resize (nr); |
|
605 for (int i = 0; i < nr; i++) |
|
606 retval (i) = m (i, 0); |
|
607 } |
|
608 else if (nr > 0 && nc > 0 |
|
609 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
610 { |
|
611 retval.resize (nr * nc); |
|
612 int k = 0; |
|
613 for (int j = 0; j < nc; j++) |
|
614 for (int i = 0; i < nr; i++) |
|
615 retval (k++) = m (i, j); |
|
616 } |
|
617 else |
|
618 { |
|
619 string tn = type_name (); |
|
620 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
|
621 } |
|
622 |
|
623 return retval; |
|
624 } |
|
625 |
|
626 void |
2466
|
627 octave_value::print (bool pr_as_read_syntax) |
2376
|
628 { |
2466
|
629 print (octave_stdout, pr_as_read_syntax); |
2376
|
630 } |
|
631 |
|
632 void |
|
633 octave_value::print_with_name (const string& name, bool print_padding) |
|
634 { |
|
635 print_with_name (octave_stdout, name, print_padding); |
|
636 } |
|
637 |
|
638 void |
|
639 octave_value::print_with_name (ostream& output_buf, const string& name, |
|
640 bool print_padding) |
|
641 { |
|
642 bool pad_after = false; |
|
643 |
|
644 if (Vprint_answer_id_name) |
|
645 { |
|
646 if (print_as_scalar ()) |
|
647 output_buf << name << " = "; |
2475
|
648 else if (is_map ()) |
2376
|
649 { |
|
650 pad_after = true; |
|
651 output_buf << name << " ="; |
|
652 } |
|
653 else |
|
654 { |
|
655 pad_after = true; |
|
656 output_buf << name << " =\n\n"; |
|
657 } |
|
658 } |
|
659 |
|
660 print (output_buf); |
|
661 |
|
662 if (print_padding && pad_after) |
|
663 output_buf << "\n"; |
|
664 } |
|
665 |
|
666 bool |
|
667 octave_value::print_as_scalar (void) |
|
668 { |
|
669 int nr = rows (); |
|
670 int nc = columns (); |
|
671 |
|
672 return (is_scalar_type () |
|
673 || (is_string () && nr <= 1) |
|
674 || (is_matrix_type () |
|
675 && ((nr == 1 && nc == 1) |
|
676 || nr == 0 |
|
677 || nc == 0))); |
|
678 } |
|
679 |
|
680 static void |
2413
|
681 gripe_indexed_assignment (const string& tn1, const string& tn2) |
|
682 { |
|
683 error ("assignment of %s to indexed %s not implemented", |
|
684 tn2.c_str (), tn1.c_str ()); |
|
685 } |
|
686 |
|
687 static void |
|
688 gripe_conversion_failed (const string& tn1, const string& tn2) |
|
689 { |
|
690 error ("type conversion for assignment of %s to indexed %s failed", |
|
691 tn2.c_str (), tn1.c_str ()); |
|
692 } |
|
693 |
|
694 bool |
2880
|
695 octave_value::convert_and_assign (octave_value::assign_op op, |
|
696 const octave_value_list& idx, |
2413
|
697 const octave_value& rhs) |
|
698 { |
|
699 bool assignment_ok = false; |
|
700 |
|
701 int t_lhs = type_id (); |
|
702 int t_rhs = rhs.type_id (); |
|
703 |
|
704 int t_result |
|
705 = octave_value_typeinfo::lookup_pref_assign_conv (t_lhs, t_rhs); |
|
706 |
|
707 if (t_result >= 0) |
|
708 { |
2427
|
709 type_conv_fcn cf |
2413
|
710 = octave_value_typeinfo::lookup_widening_op (t_lhs, t_result); |
|
711 |
|
712 if (cf) |
|
713 { |
|
714 octave_value *tmp = cf (*rep); |
|
715 |
|
716 if (tmp) |
|
717 { |
2589
|
718 octave_value *old_rep = rep; |
|
719 rep = tmp; |
|
720 rep->count = 1; |
|
721 |
2880
|
722 assignment_ok = try_assignment (op, idx, rhs); |
2589
|
723 |
|
724 if (! assignment_ok && old_rep) |
2413
|
725 { |
|
726 if (--rep->count == 0) |
|
727 delete rep; |
|
728 |
2589
|
729 rep = old_rep; |
|
730 old_rep = 0; |
2413
|
731 } |
|
732 |
2589
|
733 if (old_rep && --old_rep->count == 0) |
|
734 delete old_rep; |
2413
|
735 } |
|
736 else |
|
737 gripe_conversion_failed (type_name (), rhs.type_name ()); |
|
738 } |
|
739 else |
|
740 gripe_indexed_assignment (type_name (), rhs.type_name ()); |
|
741 } |
|
742 |
|
743 return (assignment_ok && ! error_state); |
|
744 } |
|
745 |
|
746 bool |
2880
|
747 octave_value::try_assignment_with_conversion (octave_value::assign_op op, |
|
748 const octave_value_list& idx, |
2413
|
749 const octave_value& rhs) |
|
750 { |
2880
|
751 bool assignment_ok = convert_and_assign (op, idx, rhs); |
2413
|
752 |
|
753 if (! (error_state || assignment_ok)) |
|
754 { |
|
755 octave_value tmp_rhs; |
2427
|
756 type_conv_fcn cf_rhs = rhs.numeric_conversion_function (); |
2413
|
757 |
|
758 if (cf_rhs) |
|
759 tmp_rhs = octave_value (cf_rhs (*rhs.rep)); |
|
760 else |
|
761 tmp_rhs = rhs; |
|
762 |
|
763 octave_value *old_rep = 0; |
2427
|
764 type_conv_fcn cf_this = numeric_conversion_function (); |
2413
|
765 |
|
766 if (cf_this) |
|
767 { |
|
768 old_rep = rep; |
|
769 rep = cf_this (*rep); |
|
770 rep->count = 1; |
|
771 } |
|
772 |
|
773 if (cf_this || cf_rhs) |
|
774 { |
2880
|
775 assignment_ok = try_assignment (op, idx, tmp_rhs); |
2413
|
776 |
|
777 if (! (error_state || assignment_ok)) |
2880
|
778 assignment_ok = convert_and_assign (op, idx, tmp_rhs); |
2413
|
779 } |
|
780 |
|
781 if (! assignment_ok && old_rep) |
|
782 { |
|
783 if (--rep->count == 0) |
|
784 delete rep; |
|
785 |
|
786 rep = old_rep; |
|
787 old_rep = 0; |
|
788 } |
|
789 |
|
790 if (old_rep && --old_rep->count == 0) |
|
791 delete old_rep; |
|
792 } |
|
793 |
|
794 return (assignment_ok && ! error_state); |
|
795 } |
|
796 |
|
797 bool |
2880
|
798 octave_value::try_assignment (octave_value::assign_op op, |
|
799 const octave_value_list& idx, |
2413
|
800 const octave_value& rhs) |
|
801 { |
|
802 bool retval = false; |
|
803 |
|
804 int t_lhs = type_id (); |
|
805 int t_rhs = rhs.type_id (); |
|
806 |
2880
|
807 assign_op_fcn f |
|
808 = octave_value_typeinfo::lookup_assign_op (op, t_lhs, t_rhs); |
2413
|
809 |
|
810 if (f) |
|
811 { |
|
812 f (*rep, idx, *(rhs.rep)); |
|
813 |
|
814 retval = (! error_state); |
|
815 } |
|
816 |
|
817 return retval; |
|
818 } |
|
819 |
|
820 static void |
2376
|
821 gripe_binary_op (const string& on, const string& tn1, const string& tn2) |
|
822 { |
|
823 error ("binary operator %s not implemented for %s by %s operations", |
|
824 on.c_str (), tn1.c_str (), tn2.c_str ()); |
|
825 } |
|
826 |
|
827 octave_value |
|
828 do_binary_op (octave_value::binary_op op, const octave_value& v1, |
|
829 const octave_value& v2) |
|
830 { |
|
831 octave_value retval; |
|
832 |
|
833 int t1 = v1.type_id (); |
|
834 int t2 = v2.type_id (); |
|
835 |
2427
|
836 binary_op_fcn f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
837 |
|
838 if (f) |
|
839 retval = f (*v1.rep, *v2.rep); |
|
840 else |
|
841 { |
|
842 octave_value tv1; |
2427
|
843 type_conv_fcn cf1 = v1.numeric_conversion_function (); |
2376
|
844 |
|
845 if (cf1) |
|
846 { |
|
847 tv1 = octave_value (cf1 (*v1.rep)); |
|
848 t1 = tv1.type_id (); |
|
849 } |
|
850 else |
|
851 tv1 = v1; |
|
852 |
|
853 octave_value tv2; |
2427
|
854 type_conv_fcn cf2 = v2.numeric_conversion_function (); |
2376
|
855 |
|
856 if (cf2) |
|
857 { |
|
858 tv2 = octave_value (cf2 (*v2.rep)); |
|
859 t2 = tv2.type_id (); |
|
860 } |
|
861 else |
|
862 tv2 = v2; |
|
863 |
|
864 if (cf1 || cf2) |
|
865 { |
2427
|
866 binary_op_fcn f |
2376
|
867 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
|
868 |
|
869 if (f) |
|
870 retval = f (*tv1.rep, *tv2.rep); |
|
871 else |
|
872 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
873 v1.type_name (), v2.type_name ()); |
|
874 } |
|
875 else |
|
876 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
877 v1.type_name (), v2.type_name ()); |
|
878 } |
|
879 |
|
880 return retval; |
|
881 } |
|
882 |
|
883 void |
|
884 install_types (void) |
|
885 { |
|
886 octave_base_value::register_type (); |
|
887 octave_scalar::register_type (); |
|
888 octave_complex::register_type (); |
|
889 octave_matrix::register_type (); |
|
890 octave_complex_matrix::register_type (); |
|
891 octave_range::register_type (); |
2825
|
892 octave_bool::register_type (); |
|
893 octave_bool_matrix::register_type (); |
2376
|
894 octave_char_matrix::register_type (); |
|
895 octave_char_matrix_str::register_type (); |
|
896 octave_struct::register_type (); |
2880
|
897 octave_list::register_type (); |
2376
|
898 octave_all_va_args::register_type (); |
|
899 octave_magic_colon::register_type (); |
|
900 } |
|
901 |
|
902 static int |
|
903 do_fortran_indexing (void) |
|
904 { |
|
905 Vdo_fortran_indexing = check_preference ("do_fortran_indexing"); |
|
906 |
|
907 liboctave_dfi_flag = Vdo_fortran_indexing; |
|
908 |
|
909 return 0; |
|
910 } |
|
911 |
|
912 static int |
|
913 implicit_str_to_num_ok (void) |
|
914 { |
|
915 Vimplicit_str_to_num_ok = check_preference ("implicit_str_to_num_ok"); |
|
916 |
|
917 return 0; |
|
918 } |
|
919 |
|
920 static int |
|
921 ok_to_lose_imaginary_part (void) |
|
922 { |
|
923 Vok_to_lose_imaginary_part = check_preference ("ok_to_lose_imaginary_part"); |
|
924 |
|
925 return 0; |
|
926 } |
|
927 |
|
928 static int |
|
929 prefer_column_vectors (void) |
|
930 { |
|
931 Vprefer_column_vectors |
|
932 = check_preference ("prefer_column_vectors"); |
|
933 |
|
934 liboctave_pcv_flag = Vprefer_column_vectors; |
|
935 |
|
936 return 0; |
|
937 } |
|
938 |
|
939 static int |
|
940 print_answer_id_name (void) |
|
941 { |
|
942 Vprint_answer_id_name = check_preference ("print_answer_id_name"); |
|
943 |
|
944 return 0; |
|
945 } |
|
946 |
|
947 static int |
|
948 propagate_empty_matrices (void) |
|
949 { |
|
950 Vpropagate_empty_matrices = check_preference ("propagate_empty_matrices"); |
|
951 |
|
952 return 0; |
|
953 } |
|
954 |
|
955 static int |
|
956 resize_on_range_error (void) |
|
957 { |
|
958 Vresize_on_range_error = check_preference ("resize_on_range_error"); |
|
959 |
|
960 liboctave_rre_flag = Vresize_on_range_error; |
|
961 |
|
962 return 0; |
|
963 } |
|
964 |
|
965 static int |
|
966 struct_levels_to_print (void) |
|
967 { |
|
968 double val; |
|
969 if (builtin_real_scalar_variable ("struct_levels_to_print", val) |
|
970 && ! xisnan (val)) |
|
971 { |
|
972 int ival = NINT (val); |
2800
|
973 if (ival >= 0 && ival == val) |
2376
|
974 { |
|
975 Vstruct_levels_to_print = ival; |
|
976 return 0; |
|
977 } |
|
978 } |
|
979 gripe_invalid_value_specified ("struct_levels_to_print"); |
|
980 return -1; |
|
981 } |
|
982 |
|
983 static int |
|
984 warn_divide_by_zero (void) |
|
985 { |
|
986 Vwarn_divide_by_zero = check_preference ("warn_divide_by_zero"); |
|
987 |
|
988 return 0; |
|
989 } |
|
990 |
|
991 void |
|
992 symbols_of_value (void) |
|
993 { |
|
994 DEFVAR (do_fortran_indexing, 0.0, 0, do_fortran_indexing, |
|
995 "allow single indices for matrices"); |
|
996 |
|
997 DEFVAR (implicit_str_to_num_ok, 0.0, 0, implicit_str_to_num_ok, |
|
998 "allow implicit string to number conversion"); |
|
999 |
|
1000 DEFVAR (ok_to_lose_imaginary_part, "warn", 0, ok_to_lose_imaginary_part, |
|
1001 "silently convert from complex to real by dropping imaginary part"); |
|
1002 |
|
1003 DEFVAR (prefer_column_vectors, 1.0, 0, prefer_column_vectors, |
|
1004 "prefer column/row vectors"); |
|
1005 |
|
1006 DEFVAR (print_answer_id_name, 1.0, 0, print_answer_id_name, |
|
1007 "set output style to print `var_name = ...'"); |
|
1008 |
|
1009 DEFVAR (propagate_empty_matrices, 1.0, 0, propagate_empty_matrices, |
|
1010 "operations on empty matrices return an empty matrix, not an error"); |
|
1011 |
|
1012 DEFVAR (resize_on_range_error, 1.0, 0, resize_on_range_error, |
|
1013 "enlarge matrices on assignment"); |
|
1014 |
|
1015 DEFVAR (struct_levels_to_print, 2.0, 0, struct_levels_to_print, |
|
1016 "number of levels of structure elements to print"); |
|
1017 |
|
1018 DEFVAR (warn_divide_by_zero, 1.0, 0, warn_divide_by_zero, |
|
1019 "If TRUE, warn about division by zero"); |
|
1020 } |
|
1021 |
|
1022 /* |
|
1023 ;;; Local Variables: *** |
|
1024 ;;; mode: C++ *** |
|
1025 ;;; End: *** |
|
1026 */ |