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