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 |
2409
|
489 octave_value& |
2880
|
490 octave_value::assign (assign_op, const octave_value& rhs) |
|
491 { |
|
492 // XXX FIXME XXX -- make this work for ops other than `='. |
|
493 |
|
494 return operator = (rhs); |
|
495 } |
|
496 |
|
497 octave_value& |
|
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 return *this; |
|
526 } |
|
527 |
|
528 void |
|
529 octave_value::assign_struct_elt (assign_op op, const string& elt_nm, |
|
530 const octave_value& rhs) |
|
531 { |
2409
|
532 make_unique (); |
2376
|
533 |
2948
|
534 rep->assign_struct_elt (op, elt_nm, rhs); |
|
535 } |
|
536 |
2409
|
537 |
2948
|
538 void |
|
539 octave_value::assign_struct_elt (assign_op op, const string& elt_nm, |
|
540 const octave_value_list& idx, |
|
541 const octave_value& rhs) |
|
542 { |
|
543 make_unique (); |
2409
|
544 |
2948
|
545 rep->assign_struct_elt (op, elt_nm, idx, rhs); |
|
546 } |
2376
|
547 |
2948
|
548 octave_variable_reference |
|
549 octave_value::struct_elt_ref (const string& nm) |
|
550 { |
|
551 return rep->struct_elt_ref (this, nm); |
|
552 } |
2409
|
553 |
2948
|
554 octave_variable_reference |
|
555 octave_value::struct_elt_ref (octave_value *, const string&) |
|
556 { |
|
557 panic_impossible (); |
|
558 |
|
559 return octave_variable_reference (); |
2376
|
560 } |
|
561 |
2891
|
562 octave_value_list |
|
563 octave_value::eval (int, const octave_value_list& idx) |
|
564 { |
|
565 return (idx.length () > 0) ? index (idx) : *this; |
|
566 } |
|
567 |
2376
|
568 Octave_map |
|
569 octave_value::map_value (void) const |
|
570 { |
|
571 return rep->map_value (); |
|
572 } |
|
573 |
2903
|
574 octave_stream * |
|
575 octave_value::stream_value (void) const |
|
576 { |
|
577 return rep->stream_value (); |
|
578 } |
|
579 |
|
580 int |
|
581 octave_value::stream_number (void) const |
|
582 { |
|
583 return rep->stream_number (); |
|
584 } |
|
585 |
2880
|
586 octave_value_list |
|
587 octave_value::list_value (void) const |
|
588 { |
|
589 return rep->list_value (); |
|
590 } |
|
591 |
2376
|
592 ColumnVector |
|
593 octave_value::vector_value (bool force_string_conv, |
|
594 bool force_vector_conversion) const |
|
595 { |
|
596 ColumnVector retval; |
|
597 |
|
598 Matrix m = 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 (), "real vector"); |
|
631 } |
|
632 |
|
633 return retval; |
|
634 } |
|
635 |
|
636 ComplexColumnVector |
|
637 octave_value::complex_vector_value (bool force_string_conv, |
|
638 bool force_vector_conversion) const |
|
639 { |
|
640 ComplexColumnVector retval; |
|
641 |
|
642 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
643 |
|
644 if (error_state) |
|
645 return retval; |
|
646 |
|
647 int nr = m.rows (); |
|
648 int nc = m.columns (); |
|
649 |
|
650 if (nr == 1) |
|
651 { |
|
652 retval.resize (nc); |
|
653 for (int i = 0; i < nc; i++) |
|
654 retval (i) = m (0, i); |
|
655 } |
|
656 else if (nc == 1) |
|
657 { |
|
658 retval.resize (nr); |
|
659 for (int i = 0; i < nr; i++) |
|
660 retval (i) = m (i, 0); |
|
661 } |
|
662 else if (nr > 0 && nc > 0 |
|
663 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
664 { |
|
665 retval.resize (nr * nc); |
|
666 int k = 0; |
|
667 for (int j = 0; j < nc; j++) |
|
668 for (int i = 0; i < nr; i++) |
|
669 retval (k++) = m (i, j); |
|
670 } |
|
671 else |
|
672 { |
|
673 string tn = type_name (); |
|
674 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
|
675 } |
|
676 |
|
677 return retval; |
|
678 } |
|
679 |
|
680 void |
|
681 octave_value::print_with_name (ostream& output_buf, const string& name, |
2903
|
682 bool print_padding) const |
2376
|
683 { |
2903
|
684 bool pad_after = print_name_tag (output_buf, name); |
2376
|
685 |
|
686 print (output_buf); |
|
687 |
|
688 if (print_padding && pad_after) |
2903
|
689 newline (output_buf); |
2376
|
690 } |
|
691 |
|
692 static void |
2413
|
693 gripe_indexed_assignment (const string& tn1, const string& tn2) |
|
694 { |
2903
|
695 error ("assignment of `%s' to indexed `%s' not implemented", |
2413
|
696 tn2.c_str (), tn1.c_str ()); |
|
697 } |
|
698 |
|
699 static void |
|
700 gripe_conversion_failed (const string& tn1, const string& tn2) |
|
701 { |
2903
|
702 error ("type conversion for assignment of `%s' to indexed `%s' failed", |
2413
|
703 tn2.c_str (), tn1.c_str ()); |
|
704 } |
|
705 |
|
706 bool |
2880
|
707 octave_value::convert_and_assign (octave_value::assign_op op, |
|
708 const octave_value_list& idx, |
2413
|
709 const octave_value& rhs) |
|
710 { |
|
711 bool assignment_ok = false; |
|
712 |
|
713 int t_lhs = type_id (); |
|
714 int t_rhs = rhs.type_id (); |
|
715 |
|
716 int t_result |
|
717 = octave_value_typeinfo::lookup_pref_assign_conv (t_lhs, t_rhs); |
|
718 |
|
719 if (t_result >= 0) |
|
720 { |
2427
|
721 type_conv_fcn cf |
2413
|
722 = octave_value_typeinfo::lookup_widening_op (t_lhs, t_result); |
|
723 |
|
724 if (cf) |
|
725 { |
|
726 octave_value *tmp = cf (*rep); |
|
727 |
|
728 if (tmp) |
|
729 { |
2589
|
730 octave_value *old_rep = rep; |
|
731 rep = tmp; |
|
732 rep->count = 1; |
|
733 |
2880
|
734 assignment_ok = try_assignment (op, idx, rhs); |
2589
|
735 |
|
736 if (! assignment_ok && old_rep) |
2413
|
737 { |
|
738 if (--rep->count == 0) |
|
739 delete rep; |
|
740 |
2589
|
741 rep = old_rep; |
|
742 old_rep = 0; |
2413
|
743 } |
|
744 |
2589
|
745 if (old_rep && --old_rep->count == 0) |
|
746 delete old_rep; |
2413
|
747 } |
|
748 else |
|
749 gripe_conversion_failed (type_name (), rhs.type_name ()); |
|
750 } |
|
751 else |
|
752 gripe_indexed_assignment (type_name (), rhs.type_name ()); |
|
753 } |
|
754 |
|
755 return (assignment_ok && ! error_state); |
|
756 } |
|
757 |
|
758 bool |
2880
|
759 octave_value::try_assignment_with_conversion (octave_value::assign_op op, |
|
760 const octave_value_list& idx, |
2413
|
761 const octave_value& rhs) |
|
762 { |
2880
|
763 bool assignment_ok = convert_and_assign (op, idx, rhs); |
2413
|
764 |
|
765 if (! (error_state || assignment_ok)) |
|
766 { |
|
767 octave_value tmp_rhs; |
2427
|
768 type_conv_fcn cf_rhs = rhs.numeric_conversion_function (); |
2413
|
769 |
|
770 if (cf_rhs) |
|
771 tmp_rhs = octave_value (cf_rhs (*rhs.rep)); |
|
772 else |
|
773 tmp_rhs = rhs; |
|
774 |
|
775 octave_value *old_rep = 0; |
2427
|
776 type_conv_fcn cf_this = numeric_conversion_function (); |
2413
|
777 |
|
778 if (cf_this) |
|
779 { |
|
780 old_rep = rep; |
|
781 rep = cf_this (*rep); |
|
782 rep->count = 1; |
|
783 } |
|
784 |
|
785 if (cf_this || cf_rhs) |
|
786 { |
2880
|
787 assignment_ok = try_assignment (op, idx, tmp_rhs); |
2413
|
788 |
|
789 if (! (error_state || assignment_ok)) |
2880
|
790 assignment_ok = convert_and_assign (op, idx, tmp_rhs); |
2413
|
791 } |
|
792 |
|
793 if (! assignment_ok && old_rep) |
|
794 { |
|
795 if (--rep->count == 0) |
|
796 delete rep; |
|
797 |
|
798 rep = old_rep; |
|
799 old_rep = 0; |
|
800 } |
|
801 |
|
802 if (old_rep && --old_rep->count == 0) |
|
803 delete old_rep; |
|
804 } |
|
805 |
|
806 return (assignment_ok && ! error_state); |
|
807 } |
|
808 |
|
809 bool |
2880
|
810 octave_value::try_assignment (octave_value::assign_op op, |
|
811 const octave_value_list& idx, |
2413
|
812 const octave_value& rhs) |
|
813 { |
|
814 bool retval = false; |
|
815 |
|
816 int t_lhs = type_id (); |
|
817 int t_rhs = rhs.type_id (); |
|
818 |
2880
|
819 assign_op_fcn f |
|
820 = octave_value_typeinfo::lookup_assign_op (op, t_lhs, t_rhs); |
2413
|
821 |
|
822 if (f) |
|
823 { |
|
824 f (*rep, idx, *(rhs.rep)); |
|
825 |
|
826 retval = (! error_state); |
|
827 } |
|
828 |
|
829 return retval; |
|
830 } |
|
831 |
|
832 static void |
2376
|
833 gripe_binary_op (const string& on, const string& tn1, const string& tn2) |
|
834 { |
2903
|
835 error ("binary operator `%s' not implemented for `%s' by `%s' operations", |
2376
|
836 on.c_str (), tn1.c_str (), tn2.c_str ()); |
|
837 } |
|
838 |
|
839 octave_value |
|
840 do_binary_op (octave_value::binary_op op, const octave_value& v1, |
|
841 const octave_value& v2) |
|
842 { |
|
843 octave_value retval; |
|
844 |
|
845 int t1 = v1.type_id (); |
|
846 int t2 = v2.type_id (); |
|
847 |
2427
|
848 binary_op_fcn f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
849 |
|
850 if (f) |
|
851 retval = f (*v1.rep, *v2.rep); |
|
852 else |
|
853 { |
|
854 octave_value tv1; |
2427
|
855 type_conv_fcn cf1 = v1.numeric_conversion_function (); |
2376
|
856 |
|
857 if (cf1) |
|
858 { |
|
859 tv1 = octave_value (cf1 (*v1.rep)); |
|
860 t1 = tv1.type_id (); |
|
861 } |
|
862 else |
|
863 tv1 = v1; |
|
864 |
|
865 octave_value tv2; |
2427
|
866 type_conv_fcn cf2 = v2.numeric_conversion_function (); |
2376
|
867 |
|
868 if (cf2) |
|
869 { |
|
870 tv2 = octave_value (cf2 (*v2.rep)); |
|
871 t2 = tv2.type_id (); |
|
872 } |
|
873 else |
|
874 tv2 = v2; |
|
875 |
|
876 if (cf1 || cf2) |
|
877 { |
2427
|
878 binary_op_fcn f |
2376
|
879 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
|
880 |
|
881 if (f) |
|
882 retval = f (*tv1.rep, *tv2.rep); |
|
883 else |
|
884 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
885 v1.type_name (), v2.type_name ()); |
|
886 } |
|
887 else |
|
888 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
889 v1.type_name (), v2.type_name ()); |
|
890 } |
|
891 |
|
892 return retval; |
|
893 } |
|
894 |
2903
|
895 // Current indentation. |
|
896 int octave_value::curr_print_indent_level = 0; |
|
897 |
|
898 // Nonzero means we are at the beginning of a line. |
|
899 bool octave_value::beginning_of_line = true; |
|
900 |
|
901 // Each print() function should call this before printing anything. |
|
902 // |
|
903 // This doesn't need to be fast, but isn't there a better way? |
|
904 |
|
905 void |
|
906 octave_value::indent (ostream& os) const |
|
907 { |
|
908 assert (curr_print_indent_level >= 0); |
|
909 |
|
910 if (beginning_of_line) |
|
911 { |
|
912 // XXX FIXME XXX -- do we need this? |
|
913 // os << prefix; |
|
914 |
|
915 for (int i = 0; i < curr_print_indent_level; i++) |
|
916 os << " "; |
|
917 |
|
918 beginning_of_line = false; |
|
919 } |
|
920 } |
|
921 |
|
922 // All print() functions should use this to print new lines. |
|
923 |
|
924 void |
|
925 octave_value::newline (ostream& os) const |
|
926 { |
|
927 os << "\n"; |
|
928 |
|
929 beginning_of_line = true; |
|
930 } |
|
931 |
|
932 // For ressetting print state. |
|
933 |
|
934 void |
|
935 octave_value::reset (void) const |
|
936 { |
|
937 beginning_of_line = true; |
|
938 curr_print_indent_level = 0; |
|
939 } |
|
940 |
2376
|
941 void |
|
942 install_types (void) |
|
943 { |
|
944 octave_base_value::register_type (); |
|
945 octave_scalar::register_type (); |
|
946 octave_complex::register_type (); |
|
947 octave_matrix::register_type (); |
|
948 octave_complex_matrix::register_type (); |
|
949 octave_range::register_type (); |
2825
|
950 octave_bool::register_type (); |
|
951 octave_bool_matrix::register_type (); |
2376
|
952 octave_char_matrix::register_type (); |
|
953 octave_char_matrix_str::register_type (); |
|
954 octave_struct::register_type (); |
2903
|
955 octave_file::register_type (); |
2880
|
956 octave_list::register_type (); |
2376
|
957 octave_all_va_args::register_type (); |
|
958 octave_magic_colon::register_type (); |
|
959 } |
|
960 |
|
961 static int |
|
962 do_fortran_indexing (void) |
|
963 { |
|
964 Vdo_fortran_indexing = check_preference ("do_fortran_indexing"); |
|
965 |
|
966 liboctave_dfi_flag = Vdo_fortran_indexing; |
|
967 |
|
968 return 0; |
|
969 } |
|
970 |
|
971 static int |
|
972 implicit_str_to_num_ok (void) |
|
973 { |
|
974 Vimplicit_str_to_num_ok = check_preference ("implicit_str_to_num_ok"); |
|
975 |
|
976 return 0; |
|
977 } |
|
978 |
|
979 static int |
|
980 ok_to_lose_imaginary_part (void) |
|
981 { |
|
982 Vok_to_lose_imaginary_part = check_preference ("ok_to_lose_imaginary_part"); |
|
983 |
|
984 return 0; |
|
985 } |
|
986 |
|
987 static int |
|
988 prefer_column_vectors (void) |
|
989 { |
|
990 Vprefer_column_vectors |
|
991 = check_preference ("prefer_column_vectors"); |
|
992 |
|
993 liboctave_pcv_flag = Vprefer_column_vectors; |
|
994 |
|
995 return 0; |
|
996 } |
|
997 |
|
998 static int |
|
999 print_answer_id_name (void) |
|
1000 { |
|
1001 Vprint_answer_id_name = check_preference ("print_answer_id_name"); |
|
1002 |
|
1003 return 0; |
|
1004 } |
|
1005 |
|
1006 static int |
|
1007 propagate_empty_matrices (void) |
|
1008 { |
|
1009 Vpropagate_empty_matrices = check_preference ("propagate_empty_matrices"); |
|
1010 |
|
1011 return 0; |
|
1012 } |
|
1013 |
|
1014 static int |
|
1015 resize_on_range_error (void) |
|
1016 { |
|
1017 Vresize_on_range_error = check_preference ("resize_on_range_error"); |
|
1018 |
|
1019 liboctave_rre_flag = Vresize_on_range_error; |
|
1020 |
|
1021 return 0; |
|
1022 } |
|
1023 |
|
1024 static int |
|
1025 struct_levels_to_print (void) |
|
1026 { |
|
1027 double val; |
|
1028 if (builtin_real_scalar_variable ("struct_levels_to_print", val) |
|
1029 && ! xisnan (val)) |
|
1030 { |
|
1031 int ival = NINT (val); |
2800
|
1032 if (ival >= 0 && ival == val) |
2376
|
1033 { |
|
1034 Vstruct_levels_to_print = ival; |
|
1035 return 0; |
|
1036 } |
|
1037 } |
|
1038 gripe_invalid_value_specified ("struct_levels_to_print"); |
|
1039 return -1; |
|
1040 } |
|
1041 |
|
1042 static int |
|
1043 warn_divide_by_zero (void) |
|
1044 { |
|
1045 Vwarn_divide_by_zero = check_preference ("warn_divide_by_zero"); |
|
1046 |
|
1047 return 0; |
|
1048 } |
|
1049 |
|
1050 void |
2909
|
1051 symbols_of_ov (void) |
2376
|
1052 { |
|
1053 DEFVAR (do_fortran_indexing, 0.0, 0, do_fortran_indexing, |
|
1054 "allow single indices for matrices"); |
|
1055 |
|
1056 DEFVAR (implicit_str_to_num_ok, 0.0, 0, implicit_str_to_num_ok, |
|
1057 "allow implicit string to number conversion"); |
|
1058 |
|
1059 DEFVAR (ok_to_lose_imaginary_part, "warn", 0, ok_to_lose_imaginary_part, |
|
1060 "silently convert from complex to real by dropping imaginary part"); |
|
1061 |
|
1062 DEFVAR (prefer_column_vectors, 1.0, 0, prefer_column_vectors, |
|
1063 "prefer column/row vectors"); |
|
1064 |
|
1065 DEFVAR (print_answer_id_name, 1.0, 0, print_answer_id_name, |
|
1066 "set output style to print `var_name = ...'"); |
|
1067 |
|
1068 DEFVAR (propagate_empty_matrices, 1.0, 0, propagate_empty_matrices, |
|
1069 "operations on empty matrices return an empty matrix, not an error"); |
|
1070 |
|
1071 DEFVAR (resize_on_range_error, 1.0, 0, resize_on_range_error, |
|
1072 "enlarge matrices on assignment"); |
|
1073 |
|
1074 DEFVAR (struct_levels_to_print, 2.0, 0, struct_levels_to_print, |
|
1075 "number of levels of structure elements to print"); |
|
1076 |
|
1077 DEFVAR (warn_divide_by_zero, 1.0, 0, warn_divide_by_zero, |
2957
|
1078 "if TRUE, warn about division by zero"); |
2376
|
1079 } |
|
1080 |
|
1081 /* |
|
1082 ;;; Local Variables: *** |
|
1083 ;;; mode: C++ *** |
|
1084 ;;; End: *** |
|
1085 */ |