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