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