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