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