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" |
2979
|
35 #include "oct-lvalue.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. |
3196
|
118 bool Vresize_on_range_error; |
2948
|
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 |
3189
|
368 octave_value::octave_value (char c) |
|
369 : rep (new octave_char_matrix_str (c)) |
|
370 { |
|
371 rep->count = 1; |
|
372 maybe_mutate (); |
|
373 } |
|
374 |
2376
|
375 octave_value::octave_value (const char *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& 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 string_vector& s) |
2423
|
390 : rep (new octave_char_matrix_str (s)) |
|
391 { |
|
392 rep->count = 1; |
|
393 maybe_mutate (); |
|
394 } |
2376
|
395 |
|
396 octave_value::octave_value (const charMatrix& chm, bool is_string) |
2409
|
397 : rep (0) |
|
398 { |
|
399 if (is_string) |
|
400 rep = new octave_char_matrix_str (chm); |
|
401 else |
|
402 rep = new octave_char_matrix (chm); |
2376
|
403 |
2409
|
404 rep->count = 1; |
2423
|
405 maybe_mutate (); |
2409
|
406 } |
2376
|
407 |
|
408 octave_value::octave_value (double base, double limit, double inc) |
2423
|
409 : rep (new octave_range (base, limit, inc)) |
|
410 { |
|
411 rep->count = 1; |
|
412 maybe_mutate (); |
|
413 } |
2376
|
414 |
|
415 octave_value::octave_value (const Range& r) |
2423
|
416 : rep (new octave_range (r)) |
|
417 { |
|
418 rep->count = 1; |
|
419 maybe_mutate (); |
|
420 } |
2376
|
421 |
|
422 octave_value::octave_value (const Octave_map& m) |
2825
|
423 : rep (new octave_struct (m)) |
|
424 { |
|
425 rep->count = 1; |
2880
|
426 } |
|
427 |
2903
|
428 octave_value::octave_value (octave_stream *s, int n) |
|
429 : rep (new octave_file (s, n)) |
|
430 { |
|
431 rep->count = 1; |
|
432 } |
|
433 |
2974
|
434 octave_value::octave_value (octave_function *f) |
|
435 : rep (f) |
|
436 { |
|
437 rep->count = 1; |
|
438 } |
|
439 |
2880
|
440 octave_value::octave_value (const octave_value_list& l) |
|
441 : rep (new octave_list (l)) |
|
442 { |
|
443 rep->count = 1; |
|
444 } |
2376
|
445 |
|
446 octave_value::octave_value (octave_value::magic_colon) |
2825
|
447 : rep (new octave_magic_colon ()) |
|
448 { |
|
449 rep->count = 1; |
|
450 } |
2376
|
451 |
|
452 octave_value::octave_value (octave_value::all_va_args) |
2825
|
453 : rep (new octave_all_va_args ()) |
|
454 { |
|
455 rep->count = 1; |
|
456 } |
2376
|
457 |
|
458 octave_value::octave_value (octave_value *new_rep) |
2825
|
459 : rep (new_rep) |
|
460 { |
|
461 rep->count = 1; |
|
462 } |
2376
|
463 |
|
464 octave_value::~octave_value (void) |
|
465 { |
|
466 #if defined (MDEBUG) |
|
467 cerr << "~octave_value: rep: " << rep |
|
468 << " rep->count: " << rep->count << "\n"; |
|
469 #endif |
|
470 |
|
471 if (rep && --rep->count == 0) |
|
472 { |
|
473 delete rep; |
|
474 rep = 0; |
|
475 } |
|
476 } |
|
477 |
2880
|
478 octave_value * |
|
479 octave_value::clone (void) |
|
480 { |
|
481 panic_impossible (); |
|
482 } |
|
483 |
2409
|
484 void |
|
485 octave_value::maybe_mutate (void) |
|
486 { |
2410
|
487 octave_value *tmp = rep->try_narrowing_conversion (); |
2409
|
488 |
|
489 if (tmp && tmp != rep) |
|
490 { |
|
491 if (--rep->count == 0) |
|
492 delete rep; |
|
493 |
|
494 rep = tmp; |
|
495 rep->count = 1; |
|
496 } |
|
497 } |
|
498 |
2974
|
499 octave_value_list |
|
500 octave_value::do_index_op (int nargout, const octave_value_list& idx) |
|
501 { |
|
502 return rep->do_index_op (nargout, idx); |
|
503 } |
|
504 |
2376
|
505 static void |
|
506 gripe_no_conversion (const string& tn1, const string& tn2) |
|
507 { |
2903
|
508 error ("no suitable conversion found for assignment of `%s' to indexed `%s'", |
2376
|
509 tn2.c_str (), tn1.c_str ()); |
|
510 } |
|
511 |
2963
|
512 void |
2880
|
513 octave_value::assign (assign_op, const octave_value& rhs) |
|
514 { |
|
515 // XXX FIXME XXX -- make this work for ops other than `='. |
|
516 |
2963
|
517 operator = (rhs); |
2880
|
518 } |
|
519 |
2963
|
520 void |
2880
|
521 octave_value::assign (octave_value::assign_op op, |
|
522 const octave_value_list& idx, |
|
523 const octave_value& rhs) |
2409
|
524 { |
2948
|
525 if (Vresize_on_range_error || is_defined ()) |
|
526 { |
|
527 make_unique (); |
|
528 |
|
529 bool assignment_ok = try_assignment (op, idx, rhs); |
|
530 |
|
531 if (! (error_state || assignment_ok)) |
|
532 { |
|
533 assignment_ok = try_assignment_with_conversion (op,idx, rhs); |
|
534 |
|
535 if (! (error_state || assignment_ok)) |
|
536 gripe_no_conversion (type_name (), rhs.type_name ()); |
|
537 } |
|
538 |
|
539 if (! error_state) |
|
540 maybe_mutate (); |
|
541 } |
|
542 else |
|
543 { |
|
544 error ("indexed assignment to previously undefined variables"); |
|
545 error ("is only possible when resize_on_range_error is true"); |
|
546 } |
|
547 } |
|
548 |
|
549 void |
|
550 octave_value::assign_struct_elt (assign_op op, const string& elt_nm, |
|
551 const octave_value& rhs) |
|
552 { |
2409
|
553 make_unique (); |
2376
|
554 |
2948
|
555 rep->assign_struct_elt (op, elt_nm, rhs); |
|
556 } |
|
557 |
2409
|
558 |
2948
|
559 void |
|
560 octave_value::assign_struct_elt (assign_op op, const string& elt_nm, |
|
561 const octave_value_list& idx, |
|
562 const octave_value& rhs) |
|
563 { |
|
564 make_unique (); |
2409
|
565 |
2948
|
566 rep->assign_struct_elt (op, elt_nm, idx, rhs); |
|
567 } |
2376
|
568 |
2979
|
569 octave_lvalue |
2948
|
570 octave_value::struct_elt_ref (const string& nm) |
|
571 { |
|
572 return rep->struct_elt_ref (this, nm); |
|
573 } |
2409
|
574 |
2979
|
575 octave_lvalue |
2948
|
576 octave_value::struct_elt_ref (octave_value *, const string&) |
|
577 { |
|
578 panic_impossible (); |
|
579 |
2979
|
580 return octave_lvalue (); |
2376
|
581 } |
|
582 |
|
583 Octave_map |
|
584 octave_value::map_value (void) const |
|
585 { |
|
586 return rep->map_value (); |
|
587 } |
|
588 |
2903
|
589 octave_stream * |
|
590 octave_value::stream_value (void) const |
|
591 { |
|
592 return rep->stream_value (); |
|
593 } |
|
594 |
|
595 int |
|
596 octave_value::stream_number (void) const |
|
597 { |
|
598 return rep->stream_number (); |
|
599 } |
|
600 |
2974
|
601 octave_function * |
|
602 octave_value::function_value (bool silent) |
|
603 { |
|
604 return rep->function_value (silent); |
|
605 } |
|
606 |
2880
|
607 octave_value_list |
|
608 octave_value::list_value (void) const |
|
609 { |
|
610 return rep->list_value (); |
|
611 } |
|
612 |
2376
|
613 ColumnVector |
|
614 octave_value::vector_value (bool force_string_conv, |
|
615 bool force_vector_conversion) const |
|
616 { |
|
617 ColumnVector retval; |
|
618 |
|
619 Matrix m = matrix_value (force_string_conv); |
|
620 |
|
621 if (error_state) |
|
622 return retval; |
|
623 |
|
624 int nr = m.rows (); |
|
625 int nc = m.columns (); |
|
626 |
|
627 if (nr == 1) |
|
628 { |
|
629 retval.resize (nc); |
|
630 for (int i = 0; i < nc; i++) |
|
631 retval (i) = m (0, i); |
|
632 } |
|
633 else if (nc == 1) |
|
634 { |
|
635 retval.resize (nr); |
|
636 for (int i = 0; i < nr; i++) |
|
637 retval (i) = m (i, 0); |
|
638 } |
|
639 else if (nr > 0 && nc > 0 |
|
640 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
641 { |
|
642 retval.resize (nr * nc); |
|
643 int k = 0; |
|
644 for (int j = 0; j < nc; j++) |
|
645 for (int i = 0; i < nr; i++) |
|
646 retval (k++) = m (i, j); |
|
647 } |
|
648 else |
|
649 { |
|
650 string tn = type_name (); |
|
651 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
652 } |
|
653 |
|
654 return retval; |
|
655 } |
|
656 |
|
657 ComplexColumnVector |
|
658 octave_value::complex_vector_value (bool force_string_conv, |
|
659 bool force_vector_conversion) const |
|
660 { |
|
661 ComplexColumnVector retval; |
|
662 |
|
663 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
664 |
|
665 if (error_state) |
|
666 return retval; |
|
667 |
|
668 int nr = m.rows (); |
|
669 int nc = m.columns (); |
|
670 |
|
671 if (nr == 1) |
|
672 { |
|
673 retval.resize (nc); |
|
674 for (int i = 0; i < nc; i++) |
|
675 retval (i) = m (0, i); |
|
676 } |
|
677 else if (nc == 1) |
|
678 { |
|
679 retval.resize (nr); |
|
680 for (int i = 0; i < nr; i++) |
|
681 retval (i) = m (i, 0); |
|
682 } |
|
683 else if (nr > 0 && nc > 0 |
|
684 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
685 { |
|
686 retval.resize (nr * nc); |
|
687 int k = 0; |
|
688 for (int j = 0; j < nc; j++) |
|
689 for (int i = 0; i < nr; i++) |
|
690 retval (k++) = m (i, j); |
|
691 } |
|
692 else |
|
693 { |
|
694 string tn = type_name (); |
|
695 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
|
696 } |
|
697 |
|
698 return retval; |
|
699 } |
|
700 |
|
701 void |
|
702 octave_value::print_with_name (ostream& output_buf, const string& name, |
2903
|
703 bool print_padding) const |
2376
|
704 { |
2903
|
705 bool pad_after = print_name_tag (output_buf, name); |
2376
|
706 |
|
707 print (output_buf); |
|
708 |
|
709 if (print_padding && pad_after) |
2903
|
710 newline (output_buf); |
2376
|
711 } |
|
712 |
|
713 static void |
2413
|
714 gripe_indexed_assignment (const string& tn1, const string& tn2) |
|
715 { |
2903
|
716 error ("assignment of `%s' to indexed `%s' not implemented", |
2413
|
717 tn2.c_str (), tn1.c_str ()); |
|
718 } |
|
719 |
|
720 static void |
|
721 gripe_conversion_failed (const string& tn1, const string& tn2) |
|
722 { |
2903
|
723 error ("type conversion for assignment of `%s' to indexed `%s' failed", |
2413
|
724 tn2.c_str (), tn1.c_str ()); |
|
725 } |
|
726 |
|
727 bool |
2880
|
728 octave_value::convert_and_assign (octave_value::assign_op op, |
|
729 const octave_value_list& idx, |
2413
|
730 const octave_value& rhs) |
|
731 { |
|
732 bool assignment_ok = false; |
|
733 |
|
734 int t_lhs = type_id (); |
|
735 int t_rhs = rhs.type_id (); |
|
736 |
|
737 int t_result |
|
738 = octave_value_typeinfo::lookup_pref_assign_conv (t_lhs, t_rhs); |
|
739 |
|
740 if (t_result >= 0) |
|
741 { |
2427
|
742 type_conv_fcn cf |
2413
|
743 = octave_value_typeinfo::lookup_widening_op (t_lhs, t_result); |
|
744 |
|
745 if (cf) |
|
746 { |
|
747 octave_value *tmp = cf (*rep); |
|
748 |
|
749 if (tmp) |
|
750 { |
2589
|
751 octave_value *old_rep = rep; |
|
752 rep = tmp; |
|
753 rep->count = 1; |
|
754 |
2880
|
755 assignment_ok = try_assignment (op, idx, rhs); |
2589
|
756 |
|
757 if (! assignment_ok && old_rep) |
2413
|
758 { |
|
759 if (--rep->count == 0) |
|
760 delete rep; |
|
761 |
2589
|
762 rep = old_rep; |
|
763 old_rep = 0; |
2413
|
764 } |
|
765 |
2589
|
766 if (old_rep && --old_rep->count == 0) |
|
767 delete old_rep; |
2413
|
768 } |
|
769 else |
|
770 gripe_conversion_failed (type_name (), rhs.type_name ()); |
|
771 } |
|
772 else |
|
773 gripe_indexed_assignment (type_name (), rhs.type_name ()); |
|
774 } |
|
775 |
|
776 return (assignment_ok && ! error_state); |
|
777 } |
|
778 |
|
779 bool |
2880
|
780 octave_value::try_assignment_with_conversion (octave_value::assign_op op, |
|
781 const octave_value_list& idx, |
2413
|
782 const octave_value& rhs) |
|
783 { |
2880
|
784 bool assignment_ok = convert_and_assign (op, idx, rhs); |
2413
|
785 |
|
786 if (! (error_state || assignment_ok)) |
|
787 { |
|
788 octave_value tmp_rhs; |
2427
|
789 type_conv_fcn cf_rhs = rhs.numeric_conversion_function (); |
2413
|
790 |
|
791 if (cf_rhs) |
|
792 tmp_rhs = octave_value (cf_rhs (*rhs.rep)); |
|
793 else |
|
794 tmp_rhs = rhs; |
|
795 |
|
796 octave_value *old_rep = 0; |
2427
|
797 type_conv_fcn cf_this = numeric_conversion_function (); |
2413
|
798 |
|
799 if (cf_this) |
|
800 { |
|
801 old_rep = rep; |
|
802 rep = cf_this (*rep); |
|
803 rep->count = 1; |
|
804 } |
|
805 |
|
806 if (cf_this || cf_rhs) |
|
807 { |
2880
|
808 assignment_ok = try_assignment (op, idx, tmp_rhs); |
2413
|
809 |
|
810 if (! (error_state || assignment_ok)) |
2880
|
811 assignment_ok = convert_and_assign (op, idx, tmp_rhs); |
2413
|
812 } |
|
813 |
|
814 if (! assignment_ok && old_rep) |
|
815 { |
|
816 if (--rep->count == 0) |
|
817 delete rep; |
|
818 |
|
819 rep = old_rep; |
|
820 old_rep = 0; |
|
821 } |
|
822 |
|
823 if (old_rep && --old_rep->count == 0) |
|
824 delete old_rep; |
|
825 } |
|
826 |
|
827 return (assignment_ok && ! error_state); |
|
828 } |
|
829 |
|
830 bool |
2880
|
831 octave_value::try_assignment (octave_value::assign_op op, |
|
832 const octave_value_list& idx, |
2413
|
833 const octave_value& rhs) |
|
834 { |
|
835 bool retval = false; |
|
836 |
|
837 int t_lhs = type_id (); |
|
838 int t_rhs = rhs.type_id (); |
|
839 |
2880
|
840 assign_op_fcn f |
|
841 = octave_value_typeinfo::lookup_assign_op (op, t_lhs, t_rhs); |
2413
|
842 |
|
843 if (f) |
|
844 { |
|
845 f (*rep, idx, *(rhs.rep)); |
|
846 |
|
847 retval = (! error_state); |
|
848 } |
3196
|
849 else |
|
850 { |
|
851 f = octave_value_typeinfo::lookup_assignany_op (op, t_lhs); |
|
852 |
|
853 if (f) |
|
854 { |
|
855 f (*rep, idx, rhs); |
|
856 |
|
857 retval = (! error_state); |
|
858 } |
|
859 } |
2413
|
860 |
|
861 return retval; |
|
862 } |
|
863 |
|
864 static void |
2376
|
865 gripe_binary_op (const string& on, const string& tn1, const string& tn2) |
|
866 { |
2903
|
867 error ("binary operator `%s' not implemented for `%s' by `%s' operations", |
2376
|
868 on.c_str (), tn1.c_str (), tn2.c_str ()); |
|
869 } |
|
870 |
|
871 octave_value |
|
872 do_binary_op (octave_value::binary_op op, const octave_value& v1, |
|
873 const octave_value& v2) |
|
874 { |
|
875 octave_value retval; |
|
876 |
|
877 int t1 = v1.type_id (); |
|
878 int t2 = v2.type_id (); |
|
879 |
2427
|
880 binary_op_fcn f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
881 |
|
882 if (f) |
|
883 retval = f (*v1.rep, *v2.rep); |
|
884 else |
|
885 { |
|
886 octave_value tv1; |
2427
|
887 type_conv_fcn cf1 = v1.numeric_conversion_function (); |
2376
|
888 |
|
889 if (cf1) |
|
890 { |
|
891 tv1 = octave_value (cf1 (*v1.rep)); |
|
892 t1 = tv1.type_id (); |
|
893 } |
|
894 else |
|
895 tv1 = v1; |
|
896 |
|
897 octave_value tv2; |
2427
|
898 type_conv_fcn cf2 = v2.numeric_conversion_function (); |
2376
|
899 |
|
900 if (cf2) |
|
901 { |
|
902 tv2 = octave_value (cf2 (*v2.rep)); |
|
903 t2 = tv2.type_id (); |
|
904 } |
|
905 else |
|
906 tv2 = v2; |
|
907 |
|
908 if (cf1 || cf2) |
|
909 { |
2427
|
910 binary_op_fcn f |
2376
|
911 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
|
912 |
|
913 if (f) |
|
914 retval = f (*tv1.rep, *tv2.rep); |
|
915 else |
|
916 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
917 v1.type_name (), v2.type_name ()); |
|
918 } |
|
919 else |
|
920 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
921 v1.type_name (), v2.type_name ()); |
|
922 } |
|
923 |
|
924 return retval; |
|
925 } |
|
926 |
2903
|
927 // Current indentation. |
|
928 int octave_value::curr_print_indent_level = 0; |
|
929 |
3018
|
930 // TRUE means we are at the beginning of a line. |
2903
|
931 bool octave_value::beginning_of_line = true; |
|
932 |
|
933 // Each print() function should call this before printing anything. |
|
934 // |
|
935 // This doesn't need to be fast, but isn't there a better way? |
|
936 |
|
937 void |
|
938 octave_value::indent (ostream& os) const |
|
939 { |
|
940 assert (curr_print_indent_level >= 0); |
|
941 |
|
942 if (beginning_of_line) |
|
943 { |
|
944 // XXX FIXME XXX -- do we need this? |
|
945 // os << prefix; |
|
946 |
|
947 for (int i = 0; i < curr_print_indent_level; i++) |
|
948 os << " "; |
|
949 |
|
950 beginning_of_line = false; |
|
951 } |
|
952 } |
|
953 |
|
954 // All print() functions should use this to print new lines. |
|
955 |
|
956 void |
|
957 octave_value::newline (ostream& os) const |
|
958 { |
|
959 os << "\n"; |
|
960 |
|
961 beginning_of_line = true; |
|
962 } |
|
963 |
|
964 // For ressetting print state. |
|
965 |
|
966 void |
|
967 octave_value::reset (void) const |
|
968 { |
|
969 beginning_of_line = true; |
|
970 curr_print_indent_level = 0; |
|
971 } |
|
972 |
2376
|
973 void |
|
974 install_types (void) |
|
975 { |
|
976 octave_base_value::register_type (); |
|
977 octave_scalar::register_type (); |
|
978 octave_complex::register_type (); |
|
979 octave_matrix::register_type (); |
|
980 octave_complex_matrix::register_type (); |
|
981 octave_range::register_type (); |
2825
|
982 octave_bool::register_type (); |
|
983 octave_bool_matrix::register_type (); |
2376
|
984 octave_char_matrix::register_type (); |
|
985 octave_char_matrix_str::register_type (); |
|
986 octave_struct::register_type (); |
2903
|
987 octave_file::register_type (); |
2880
|
988 octave_list::register_type (); |
2376
|
989 octave_all_va_args::register_type (); |
|
990 octave_magic_colon::register_type (); |
2974
|
991 octave_builtin::register_type (); |
|
992 octave_mapper::register_type (); |
|
993 octave_user_function::register_type (); |
2376
|
994 } |
|
995 |
|
996 static int |
|
997 do_fortran_indexing (void) |
|
998 { |
|
999 Vdo_fortran_indexing = check_preference ("do_fortran_indexing"); |
|
1000 |
|
1001 liboctave_dfi_flag = Vdo_fortran_indexing; |
|
1002 |
|
1003 return 0; |
|
1004 } |
|
1005 |
|
1006 static int |
|
1007 implicit_str_to_num_ok (void) |
|
1008 { |
|
1009 Vimplicit_str_to_num_ok = check_preference ("implicit_str_to_num_ok"); |
|
1010 |
|
1011 return 0; |
|
1012 } |
|
1013 |
|
1014 static int |
|
1015 ok_to_lose_imaginary_part (void) |
|
1016 { |
|
1017 Vok_to_lose_imaginary_part = check_preference ("ok_to_lose_imaginary_part"); |
|
1018 |
|
1019 return 0; |
|
1020 } |
|
1021 |
|
1022 static int |
|
1023 prefer_column_vectors (void) |
|
1024 { |
|
1025 Vprefer_column_vectors |
|
1026 = check_preference ("prefer_column_vectors"); |
|
1027 |
|
1028 liboctave_pcv_flag = Vprefer_column_vectors; |
|
1029 |
|
1030 return 0; |
|
1031 } |
|
1032 |
|
1033 static int |
|
1034 print_answer_id_name (void) |
|
1035 { |
|
1036 Vprint_answer_id_name = check_preference ("print_answer_id_name"); |
|
1037 |
|
1038 return 0; |
|
1039 } |
|
1040 |
|
1041 static int |
|
1042 propagate_empty_matrices (void) |
|
1043 { |
|
1044 Vpropagate_empty_matrices = check_preference ("propagate_empty_matrices"); |
|
1045 |
|
1046 return 0; |
|
1047 } |
|
1048 |
|
1049 static int |
|
1050 resize_on_range_error (void) |
|
1051 { |
|
1052 Vresize_on_range_error = check_preference ("resize_on_range_error"); |
|
1053 |
|
1054 liboctave_rre_flag = Vresize_on_range_error; |
|
1055 |
|
1056 return 0; |
|
1057 } |
|
1058 |
|
1059 static int |
|
1060 struct_levels_to_print (void) |
|
1061 { |
|
1062 double val; |
|
1063 if (builtin_real_scalar_variable ("struct_levels_to_print", val) |
|
1064 && ! xisnan (val)) |
|
1065 { |
|
1066 int ival = NINT (val); |
2800
|
1067 if (ival >= 0 && ival == val) |
2376
|
1068 { |
|
1069 Vstruct_levels_to_print = ival; |
|
1070 return 0; |
|
1071 } |
|
1072 } |
|
1073 gripe_invalid_value_specified ("struct_levels_to_print"); |
|
1074 return -1; |
|
1075 } |
|
1076 |
|
1077 static int |
|
1078 warn_divide_by_zero (void) |
|
1079 { |
|
1080 Vwarn_divide_by_zero = check_preference ("warn_divide_by_zero"); |
|
1081 |
|
1082 return 0; |
|
1083 } |
|
1084 |
|
1085 void |
2909
|
1086 symbols_of_ov (void) |
2376
|
1087 { |
|
1088 DEFVAR (do_fortran_indexing, 0.0, 0, do_fortran_indexing, |
|
1089 "allow single indices for matrices"); |
|
1090 |
|
1091 DEFVAR (implicit_str_to_num_ok, 0.0, 0, implicit_str_to_num_ok, |
|
1092 "allow implicit string to number conversion"); |
|
1093 |
|
1094 DEFVAR (ok_to_lose_imaginary_part, "warn", 0, ok_to_lose_imaginary_part, |
|
1095 "silently convert from complex to real by dropping imaginary part"); |
|
1096 |
|
1097 DEFVAR (prefer_column_vectors, 1.0, 0, prefer_column_vectors, |
|
1098 "prefer column/row vectors"); |
|
1099 |
|
1100 DEFVAR (print_answer_id_name, 1.0, 0, print_answer_id_name, |
|
1101 "set output style to print `var_name = ...'"); |
|
1102 |
|
1103 DEFVAR (propagate_empty_matrices, 1.0, 0, propagate_empty_matrices, |
|
1104 "operations on empty matrices return an empty matrix, not an error"); |
|
1105 |
|
1106 DEFVAR (resize_on_range_error, 1.0, 0, resize_on_range_error, |
|
1107 "enlarge matrices on assignment"); |
|
1108 |
|
1109 DEFVAR (struct_levels_to_print, 2.0, 0, struct_levels_to_print, |
|
1110 "number of levels of structure elements to print"); |
|
1111 |
|
1112 DEFVAR (warn_divide_by_zero, 1.0, 0, warn_divide_by_zero, |
2957
|
1113 "if TRUE, warn about division by zero"); |
2376
|
1114 } |
|
1115 |
|
1116 /* |
|
1117 ;;; Local Variables: *** |
|
1118 ;;; mode: C++ *** |
|
1119 ;;; End: *** |
|
1120 */ |