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 |
3203
|
125 octave_value::unary_op_as_string (unary_op op) |
|
126 { |
|
127 string retval; |
|
128 |
|
129 switch (op) |
|
130 { |
|
131 case not: |
|
132 retval = "!"; |
|
133 break; |
|
134 |
|
135 case uminus: |
|
136 retval = "-"; |
|
137 break; |
|
138 |
|
139 case transpose: |
|
140 retval = ".'"; |
|
141 break; |
|
142 |
|
143 case hermitian: |
|
144 retval = "'"; |
|
145 break; |
|
146 |
|
147 case incr: |
|
148 retval = "++"; |
|
149 break; |
|
150 |
|
151 case decr: |
|
152 retval = "--"; |
|
153 break; |
|
154 |
|
155 default: |
|
156 retval = "<unknown>"; |
|
157 } |
|
158 |
|
159 return retval; |
|
160 } |
|
161 |
|
162 string |
2376
|
163 octave_value::binary_op_as_string (binary_op op) |
|
164 { |
|
165 string retval; |
|
166 |
|
167 switch (op) |
|
168 { |
|
169 case add: |
|
170 retval = "+"; |
|
171 break; |
|
172 |
|
173 case sub: |
|
174 retval = "-"; |
|
175 break; |
|
176 |
|
177 case mul: |
|
178 retval = "*"; |
|
179 break; |
|
180 |
|
181 case div: |
|
182 retval = "/"; |
|
183 break; |
|
184 |
|
185 case pow: |
|
186 retval = "^"; |
|
187 break; |
|
188 |
|
189 case ldiv: |
|
190 retval = "\\"; |
|
191 break; |
|
192 |
2903
|
193 case lshift: |
|
194 retval = "<<"; |
|
195 break; |
|
196 |
|
197 case rshift: |
|
198 retval = ">>"; |
|
199 break; |
|
200 |
2376
|
201 case lt: |
|
202 retval = "<"; |
|
203 break; |
|
204 |
|
205 case le: |
|
206 retval = "<="; |
|
207 break; |
|
208 |
|
209 case eq: |
|
210 retval = "=="; |
|
211 break; |
|
212 |
|
213 case ge: |
|
214 retval = ">="; |
|
215 break; |
|
216 |
|
217 case gt: |
|
218 retval = ">"; |
|
219 break; |
|
220 |
|
221 case ne: |
|
222 retval = "!="; |
|
223 break; |
|
224 |
|
225 case el_mul: |
|
226 retval = ".*"; |
|
227 break; |
|
228 |
|
229 case el_div: |
|
230 retval = "./"; |
|
231 break; |
|
232 |
|
233 case el_pow: |
|
234 retval = ".^"; |
|
235 break; |
|
236 |
|
237 case el_ldiv: |
|
238 retval = ".\\"; |
|
239 break; |
|
240 |
|
241 case el_and: |
|
242 retval = "&"; |
|
243 break; |
|
244 |
|
245 case el_or: |
|
246 retval = "|"; |
|
247 break; |
|
248 |
|
249 case struct_ref: |
|
250 retval = "."; |
|
251 break; |
|
252 |
|
253 default: |
|
254 retval = "<unknown>"; |
|
255 } |
|
256 |
|
257 return retval; |
|
258 } |
|
259 |
2880
|
260 string |
|
261 octave_value::assign_op_as_string (assign_op op) |
|
262 { |
|
263 string retval; |
|
264 |
|
265 switch (op) |
|
266 { |
|
267 case asn_eq: |
|
268 retval = "="; |
|
269 break; |
|
270 |
|
271 case add_eq: |
|
272 retval = "+="; |
|
273 break; |
|
274 |
|
275 case sub_eq: |
|
276 retval = "-="; |
|
277 break; |
|
278 |
|
279 case mul_eq: |
|
280 retval = "*="; |
|
281 break; |
|
282 |
|
283 case div_eq: |
|
284 retval = "/="; |
|
285 break; |
|
286 |
2903
|
287 case lshift_eq: |
|
288 retval = "<<="; |
|
289 break; |
|
290 |
|
291 case rshift_eq: |
|
292 retval = ">>="; |
|
293 break; |
|
294 |
2880
|
295 case el_mul_eq: |
|
296 retval = ".*="; |
|
297 break; |
|
298 |
|
299 case el_div_eq: |
|
300 retval = "./="; |
|
301 break; |
|
302 |
|
303 case el_and_eq: |
|
304 retval = "&="; |
|
305 break; |
|
306 |
|
307 case el_or_eq: |
|
308 retval = "|="; |
|
309 break; |
|
310 |
|
311 default: |
|
312 retval = "<unknown>"; |
|
313 } |
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
2376
|
318 octave_value::octave_value (void) |
2825
|
319 : rep (new octave_base_value ()) |
|
320 { |
|
321 rep->count = 1; |
|
322 } |
2376
|
323 |
|
324 octave_value::octave_value (double d) |
2825
|
325 : rep (new octave_scalar (d)) |
|
326 { |
|
327 rep->count = 1; |
|
328 } |
2376
|
329 |
|
330 octave_value::octave_value (const Matrix& m) |
2423
|
331 : rep (new octave_matrix (m)) |
|
332 { |
|
333 rep->count = 1; |
|
334 maybe_mutate (); |
|
335 } |
2376
|
336 |
|
337 octave_value::octave_value (const DiagMatrix& d) |
2423
|
338 : rep (new octave_matrix (d)) |
|
339 { |
|
340 rep->count = 1; |
|
341 maybe_mutate (); |
|
342 } |
2376
|
343 |
|
344 octave_value::octave_value (const RowVector& v, int pcv) |
2423
|
345 : rep (new octave_matrix (v, pcv)) |
|
346 { |
|
347 rep->count = 1; |
|
348 maybe_mutate (); |
|
349 } |
2376
|
350 |
|
351 octave_value::octave_value (const ColumnVector& v, int pcv) |
2423
|
352 : rep (new octave_matrix (v, pcv)) |
|
353 { |
|
354 rep->count = 1; |
|
355 maybe_mutate (); |
|
356 } |
2376
|
357 |
|
358 octave_value::octave_value (const Complex& C) |
2423
|
359 : rep (new octave_complex (C)) |
|
360 { |
|
361 rep->count = 1; |
|
362 maybe_mutate (); |
|
363 } |
2376
|
364 |
|
365 octave_value::octave_value (const ComplexMatrix& m) |
2423
|
366 : rep (new octave_complex_matrix (m)) |
|
367 { |
|
368 rep->count = 1; |
|
369 maybe_mutate (); |
|
370 } |
2376
|
371 |
|
372 octave_value::octave_value (const ComplexDiagMatrix& d) |
2423
|
373 : rep (new octave_complex_matrix (d)) |
|
374 { |
|
375 rep->count = 1; |
|
376 maybe_mutate (); |
|
377 } |
2376
|
378 |
|
379 octave_value::octave_value (const ComplexRowVector& v, int pcv) |
2423
|
380 : rep (new octave_complex_matrix (v, pcv)) |
|
381 { |
|
382 rep->count = 1; |
|
383 maybe_mutate (); |
|
384 } |
2376
|
385 |
|
386 octave_value::octave_value (const ComplexColumnVector& v, int pcv) |
2423
|
387 : rep (new octave_complex_matrix (v, pcv)) |
|
388 { |
|
389 rep->count = 1; |
|
390 maybe_mutate (); |
|
391 } |
2376
|
392 |
2825
|
393 octave_value::octave_value (bool b) |
|
394 : rep (new octave_bool (b)) |
|
395 { |
|
396 rep->count = 1; |
|
397 } |
|
398 |
|
399 octave_value::octave_value (const boolMatrix& bm) |
|
400 : rep (new octave_bool_matrix (bm)) |
|
401 { |
|
402 rep->count = 1; |
|
403 maybe_mutate (); |
|
404 } |
|
405 |
3189
|
406 octave_value::octave_value (char c) |
|
407 : rep (new octave_char_matrix_str (c)) |
|
408 { |
|
409 rep->count = 1; |
|
410 maybe_mutate (); |
|
411 } |
|
412 |
2376
|
413 octave_value::octave_value (const char *s) |
2423
|
414 : rep (new octave_char_matrix_str (s)) |
|
415 { |
|
416 rep->count = 1; |
|
417 maybe_mutate (); |
|
418 } |
2376
|
419 |
|
420 octave_value::octave_value (const string& s) |
2423
|
421 : rep (new octave_char_matrix_str (s)) |
|
422 { |
|
423 rep->count = 1; |
|
424 maybe_mutate (); |
|
425 } |
2376
|
426 |
|
427 octave_value::octave_value (const string_vector& s) |
2423
|
428 : rep (new octave_char_matrix_str (s)) |
|
429 { |
|
430 rep->count = 1; |
|
431 maybe_mutate (); |
|
432 } |
2376
|
433 |
|
434 octave_value::octave_value (const charMatrix& chm, bool is_string) |
2409
|
435 : rep (0) |
|
436 { |
|
437 if (is_string) |
|
438 rep = new octave_char_matrix_str (chm); |
|
439 else |
|
440 rep = new octave_char_matrix (chm); |
2376
|
441 |
2409
|
442 rep->count = 1; |
2423
|
443 maybe_mutate (); |
2409
|
444 } |
2376
|
445 |
|
446 octave_value::octave_value (double base, double limit, double inc) |
2423
|
447 : rep (new octave_range (base, limit, inc)) |
|
448 { |
|
449 rep->count = 1; |
|
450 maybe_mutate (); |
|
451 } |
2376
|
452 |
|
453 octave_value::octave_value (const Range& r) |
2423
|
454 : rep (new octave_range (r)) |
|
455 { |
|
456 rep->count = 1; |
|
457 maybe_mutate (); |
|
458 } |
2376
|
459 |
|
460 octave_value::octave_value (const Octave_map& m) |
2825
|
461 : rep (new octave_struct (m)) |
|
462 { |
|
463 rep->count = 1; |
2880
|
464 } |
|
465 |
2903
|
466 octave_value::octave_value (octave_stream *s, int n) |
|
467 : rep (new octave_file (s, n)) |
|
468 { |
|
469 rep->count = 1; |
|
470 } |
|
471 |
2974
|
472 octave_value::octave_value (octave_function *f) |
|
473 : rep (f) |
|
474 { |
|
475 rep->count = 1; |
|
476 } |
|
477 |
2880
|
478 octave_value::octave_value (const octave_value_list& l) |
|
479 : rep (new octave_list (l)) |
|
480 { |
|
481 rep->count = 1; |
|
482 } |
2376
|
483 |
|
484 octave_value::octave_value (octave_value::magic_colon) |
2825
|
485 : rep (new octave_magic_colon ()) |
|
486 { |
|
487 rep->count = 1; |
|
488 } |
2376
|
489 |
|
490 octave_value::octave_value (octave_value::all_va_args) |
2825
|
491 : rep (new octave_all_va_args ()) |
|
492 { |
|
493 rep->count = 1; |
|
494 } |
2376
|
495 |
|
496 octave_value::octave_value (octave_value *new_rep) |
2825
|
497 : rep (new_rep) |
|
498 { |
|
499 rep->count = 1; |
|
500 } |
2376
|
501 |
|
502 octave_value::~octave_value (void) |
|
503 { |
|
504 #if defined (MDEBUG) |
|
505 cerr << "~octave_value: rep: " << rep |
|
506 << " rep->count: " << rep->count << "\n"; |
|
507 #endif |
|
508 |
|
509 if (rep && --rep->count == 0) |
|
510 { |
|
511 delete rep; |
|
512 rep = 0; |
|
513 } |
|
514 } |
|
515 |
2880
|
516 octave_value * |
|
517 octave_value::clone (void) |
|
518 { |
|
519 panic_impossible (); |
|
520 } |
|
521 |
2409
|
522 void |
|
523 octave_value::maybe_mutate (void) |
|
524 { |
2410
|
525 octave_value *tmp = rep->try_narrowing_conversion (); |
2409
|
526 |
|
527 if (tmp && tmp != rep) |
|
528 { |
|
529 if (--rep->count == 0) |
|
530 delete rep; |
|
531 |
|
532 rep = tmp; |
|
533 rep->count = 1; |
|
534 } |
|
535 } |
|
536 |
2974
|
537 octave_value_list |
|
538 octave_value::do_index_op (int nargout, const octave_value_list& idx) |
|
539 { |
|
540 return rep->do_index_op (nargout, idx); |
|
541 } |
|
542 |
2376
|
543 static void |
3203
|
544 gripe_no_conversion (const string& on, const string& tn1, const string& tn2) |
2376
|
545 { |
3203
|
546 error ("operator %s: no conversion for assignment of `%s' to indexed `%s'", |
|
547 on.c_str (), tn2.c_str (), tn1.c_str ()); |
2376
|
548 } |
|
549 |
2963
|
550 void |
3203
|
551 octave_value::assign (assign_op op, const octave_value& rhs) |
2880
|
552 { |
|
553 // XXX FIXME XXX -- make this work for ops other than `='. |
|
554 |
3203
|
555 if (op == asn_eq) |
|
556 operator = (rhs); |
|
557 else |
|
558 { |
|
559 string on = assign_op_as_string (op); |
|
560 error ("operator `%s' not supported yet", on.c_str ()); |
|
561 } |
2880
|
562 } |
|
563 |
2963
|
564 void |
2880
|
565 octave_value::assign (octave_value::assign_op op, |
|
566 const octave_value_list& idx, |
|
567 const octave_value& rhs) |
2409
|
568 { |
2948
|
569 if (Vresize_on_range_error || is_defined ()) |
|
570 { |
|
571 make_unique (); |
|
572 |
|
573 bool assignment_ok = try_assignment (op, idx, rhs); |
|
574 |
|
575 if (! (error_state || assignment_ok)) |
|
576 { |
3203
|
577 assignment_ok = try_assignment_with_conversion (op, idx, rhs); |
2948
|
578 |
|
579 if (! (error_state || assignment_ok)) |
3203
|
580 gripe_no_conversion (assign_op_as_string (op), |
|
581 type_name (), rhs.type_name ()); |
2948
|
582 } |
|
583 |
|
584 if (! error_state) |
|
585 maybe_mutate (); |
|
586 } |
|
587 else |
|
588 { |
|
589 error ("indexed assignment to previously undefined variables"); |
|
590 error ("is only possible when resize_on_range_error is true"); |
|
591 } |
|
592 } |
|
593 |
|
594 void |
|
595 octave_value::assign_struct_elt (assign_op op, const string& elt_nm, |
|
596 const octave_value& rhs) |
|
597 { |
2409
|
598 make_unique (); |
2376
|
599 |
2948
|
600 rep->assign_struct_elt (op, elt_nm, rhs); |
|
601 } |
|
602 |
2409
|
603 |
2948
|
604 void |
|
605 octave_value::assign_struct_elt (assign_op op, const string& elt_nm, |
|
606 const octave_value_list& idx, |
|
607 const octave_value& rhs) |
|
608 { |
|
609 make_unique (); |
2409
|
610 |
2948
|
611 rep->assign_struct_elt (op, elt_nm, idx, rhs); |
|
612 } |
2376
|
613 |
2979
|
614 octave_lvalue |
2948
|
615 octave_value::struct_elt_ref (const string& nm) |
|
616 { |
|
617 return rep->struct_elt_ref (this, nm); |
|
618 } |
2409
|
619 |
2979
|
620 octave_lvalue |
2948
|
621 octave_value::struct_elt_ref (octave_value *, const string&) |
|
622 { |
|
623 panic_impossible (); |
|
624 |
2979
|
625 return octave_lvalue (); |
2376
|
626 } |
|
627 |
|
628 Octave_map |
|
629 octave_value::map_value (void) const |
|
630 { |
|
631 return rep->map_value (); |
|
632 } |
|
633 |
2903
|
634 octave_stream * |
|
635 octave_value::stream_value (void) const |
|
636 { |
|
637 return rep->stream_value (); |
|
638 } |
|
639 |
|
640 int |
|
641 octave_value::stream_number (void) const |
|
642 { |
|
643 return rep->stream_number (); |
|
644 } |
|
645 |
2974
|
646 octave_function * |
|
647 octave_value::function_value (bool silent) |
|
648 { |
|
649 return rep->function_value (silent); |
|
650 } |
|
651 |
2880
|
652 octave_value_list |
|
653 octave_value::list_value (void) const |
|
654 { |
|
655 return rep->list_value (); |
|
656 } |
|
657 |
2376
|
658 ColumnVector |
|
659 octave_value::vector_value (bool force_string_conv, |
|
660 bool force_vector_conversion) const |
|
661 { |
|
662 ColumnVector retval; |
|
663 |
|
664 Matrix m = matrix_value (force_string_conv); |
|
665 |
|
666 if (error_state) |
|
667 return retval; |
|
668 |
|
669 int nr = m.rows (); |
|
670 int nc = m.columns (); |
|
671 |
|
672 if (nr == 1) |
|
673 { |
|
674 retval.resize (nc); |
|
675 for (int i = 0; i < nc; i++) |
|
676 retval (i) = m (0, i); |
|
677 } |
|
678 else if (nc == 1) |
|
679 { |
|
680 retval.resize (nr); |
|
681 for (int i = 0; i < nr; i++) |
|
682 retval (i) = m (i, 0); |
|
683 } |
|
684 else if (nr > 0 && nc > 0 |
|
685 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
686 { |
|
687 retval.resize (nr * nc); |
|
688 int k = 0; |
|
689 for (int j = 0; j < nc; j++) |
|
690 for (int i = 0; i < nr; i++) |
|
691 retval (k++) = m (i, j); |
|
692 } |
|
693 else |
|
694 { |
|
695 string tn = type_name (); |
|
696 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
697 } |
|
698 |
|
699 return retval; |
|
700 } |
|
701 |
|
702 ComplexColumnVector |
|
703 octave_value::complex_vector_value (bool force_string_conv, |
|
704 bool force_vector_conversion) const |
|
705 { |
|
706 ComplexColumnVector retval; |
|
707 |
|
708 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
709 |
|
710 if (error_state) |
|
711 return retval; |
|
712 |
|
713 int nr = m.rows (); |
|
714 int nc = m.columns (); |
|
715 |
|
716 if (nr == 1) |
|
717 { |
|
718 retval.resize (nc); |
|
719 for (int i = 0; i < nc; i++) |
|
720 retval (i) = m (0, i); |
|
721 } |
|
722 else if (nc == 1) |
|
723 { |
|
724 retval.resize (nr); |
|
725 for (int i = 0; i < nr; i++) |
|
726 retval (i) = m (i, 0); |
|
727 } |
|
728 else if (nr > 0 && nc > 0 |
|
729 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
730 { |
|
731 retval.resize (nr * nc); |
|
732 int k = 0; |
|
733 for (int j = 0; j < nc; j++) |
|
734 for (int i = 0; i < nr; i++) |
|
735 retval (k++) = m (i, j); |
|
736 } |
|
737 else |
|
738 { |
|
739 string tn = type_name (); |
|
740 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
|
741 } |
|
742 |
|
743 return retval; |
|
744 } |
|
745 |
|
746 void |
|
747 octave_value::print_with_name (ostream& output_buf, const string& name, |
2903
|
748 bool print_padding) const |
2376
|
749 { |
2903
|
750 bool pad_after = print_name_tag (output_buf, name); |
2376
|
751 |
|
752 print (output_buf); |
|
753 |
|
754 if (print_padding && pad_after) |
2903
|
755 newline (output_buf); |
2376
|
756 } |
|
757 |
|
758 static void |
2413
|
759 gripe_indexed_assignment (const string& tn1, const string& tn2) |
|
760 { |
2903
|
761 error ("assignment of `%s' to indexed `%s' not implemented", |
2413
|
762 tn2.c_str (), tn1.c_str ()); |
|
763 } |
|
764 |
|
765 static void |
3203
|
766 gripe_assign_conversion_failed (const string& tn1, const string& tn2) |
2413
|
767 { |
2903
|
768 error ("type conversion for assignment of `%s' to indexed `%s' failed", |
2413
|
769 tn2.c_str (), tn1.c_str ()); |
|
770 } |
|
771 |
|
772 bool |
2880
|
773 octave_value::convert_and_assign (octave_value::assign_op op, |
|
774 const octave_value_list& idx, |
2413
|
775 const octave_value& rhs) |
|
776 { |
|
777 bool assignment_ok = false; |
|
778 |
|
779 int t_lhs = type_id (); |
|
780 int t_rhs = rhs.type_id (); |
|
781 |
|
782 int t_result |
|
783 = octave_value_typeinfo::lookup_pref_assign_conv (t_lhs, t_rhs); |
|
784 |
|
785 if (t_result >= 0) |
|
786 { |
2427
|
787 type_conv_fcn cf |
2413
|
788 = octave_value_typeinfo::lookup_widening_op (t_lhs, t_result); |
|
789 |
|
790 if (cf) |
|
791 { |
|
792 octave_value *tmp = cf (*rep); |
|
793 |
|
794 if (tmp) |
|
795 { |
2589
|
796 octave_value *old_rep = rep; |
|
797 rep = tmp; |
|
798 rep->count = 1; |
|
799 |
2880
|
800 assignment_ok = try_assignment (op, idx, rhs); |
2589
|
801 |
|
802 if (! assignment_ok && old_rep) |
2413
|
803 { |
|
804 if (--rep->count == 0) |
|
805 delete rep; |
|
806 |
2589
|
807 rep = old_rep; |
|
808 old_rep = 0; |
2413
|
809 } |
|
810 |
2589
|
811 if (old_rep && --old_rep->count == 0) |
|
812 delete old_rep; |
2413
|
813 } |
|
814 else |
3203
|
815 gripe_assign_conversion_failed (type_name (), rhs.type_name ()); |
2413
|
816 } |
|
817 else |
|
818 gripe_indexed_assignment (type_name (), rhs.type_name ()); |
|
819 } |
|
820 |
|
821 return (assignment_ok && ! error_state); |
|
822 } |
|
823 |
|
824 bool |
2880
|
825 octave_value::try_assignment_with_conversion (octave_value::assign_op op, |
|
826 const octave_value_list& idx, |
2413
|
827 const octave_value& rhs) |
|
828 { |
2880
|
829 bool assignment_ok = convert_and_assign (op, idx, rhs); |
2413
|
830 |
|
831 if (! (error_state || assignment_ok)) |
|
832 { |
|
833 octave_value tmp_rhs; |
2427
|
834 type_conv_fcn cf_rhs = rhs.numeric_conversion_function (); |
2413
|
835 |
|
836 if (cf_rhs) |
3203
|
837 { |
|
838 octave_value *tmp = cf_rhs (*rhs.rep); |
|
839 |
|
840 if (tmp) |
|
841 tmp_rhs = octave_value (tmp); |
|
842 else |
|
843 { |
|
844 gripe_assign_conversion_failed (type_name (), rhs.type_name ()); |
|
845 return false; |
|
846 } |
|
847 } |
2413
|
848 else |
|
849 tmp_rhs = rhs; |
|
850 |
|
851 octave_value *old_rep = 0; |
2427
|
852 type_conv_fcn cf_this = numeric_conversion_function (); |
2413
|
853 |
|
854 if (cf_this) |
|
855 { |
|
856 old_rep = rep; |
3203
|
857 |
|
858 octave_value *tmp = cf_this (*rep); |
|
859 |
|
860 if (tmp) |
|
861 { |
|
862 rep = tmp; |
|
863 rep->count = 1; |
|
864 } |
|
865 else |
|
866 { |
|
867 gripe_assign_conversion_failed (type_name (), rhs.type_name ()); |
|
868 return false; |
|
869 } |
2413
|
870 } |
|
871 |
|
872 if (cf_this || cf_rhs) |
|
873 { |
2880
|
874 assignment_ok = try_assignment (op, idx, tmp_rhs); |
2413
|
875 |
|
876 if (! (error_state || assignment_ok)) |
2880
|
877 assignment_ok = convert_and_assign (op, idx, tmp_rhs); |
2413
|
878 } |
|
879 |
|
880 if (! assignment_ok && old_rep) |
|
881 { |
|
882 if (--rep->count == 0) |
|
883 delete rep; |
|
884 |
|
885 rep = old_rep; |
|
886 old_rep = 0; |
|
887 } |
|
888 |
|
889 if (old_rep && --old_rep->count == 0) |
|
890 delete old_rep; |
|
891 } |
|
892 |
|
893 return (assignment_ok && ! error_state); |
|
894 } |
|
895 |
|
896 bool |
2880
|
897 octave_value::try_assignment (octave_value::assign_op op, |
|
898 const octave_value_list& idx, |
2413
|
899 const octave_value& rhs) |
|
900 { |
|
901 bool retval = false; |
|
902 |
|
903 int t_lhs = type_id (); |
|
904 int t_rhs = rhs.type_id (); |
|
905 |
2880
|
906 assign_op_fcn f |
|
907 = octave_value_typeinfo::lookup_assign_op (op, t_lhs, t_rhs); |
2413
|
908 |
|
909 if (f) |
|
910 { |
|
911 f (*rep, idx, *(rhs.rep)); |
|
912 |
|
913 retval = (! error_state); |
|
914 } |
3196
|
915 else |
|
916 { |
|
917 f = octave_value_typeinfo::lookup_assignany_op (op, t_lhs); |
|
918 |
|
919 if (f) |
|
920 { |
|
921 f (*rep, idx, rhs); |
|
922 |
|
923 retval = (! error_state); |
|
924 } |
|
925 } |
2413
|
926 |
|
927 return retval; |
|
928 } |
|
929 |
|
930 static void |
2376
|
931 gripe_binary_op (const string& on, const string& tn1, const string& tn2) |
|
932 { |
2903
|
933 error ("binary operator `%s' not implemented for `%s' by `%s' operations", |
2376
|
934 on.c_str (), tn1.c_str (), tn2.c_str ()); |
|
935 } |
|
936 |
3203
|
937 static void |
|
938 gripe_binary_op_conv (const string& on) |
|
939 { |
|
940 error ("type conversion failed for binary operator `%s'", on.c_str ()); |
|
941 } |
|
942 |
2376
|
943 octave_value |
|
944 do_binary_op (octave_value::binary_op op, const octave_value& v1, |
|
945 const octave_value& v2) |
|
946 { |
|
947 octave_value retval; |
|
948 |
|
949 int t1 = v1.type_id (); |
|
950 int t2 = v2.type_id (); |
|
951 |
2427
|
952 binary_op_fcn f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
953 |
|
954 if (f) |
|
955 retval = f (*v1.rep, *v2.rep); |
|
956 else |
|
957 { |
|
958 octave_value tv1; |
2427
|
959 type_conv_fcn cf1 = v1.numeric_conversion_function (); |
2376
|
960 |
|
961 if (cf1) |
|
962 { |
3203
|
963 octave_value *tmp = cf1 (*v1.rep); |
|
964 |
|
965 if (tmp) |
|
966 { |
|
967 tv1 = octave_value (tmp); |
|
968 t1 = tv1.type_id (); |
|
969 } |
|
970 else |
|
971 { |
|
972 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); |
|
973 return retval; |
|
974 } |
2376
|
975 } |
|
976 else |
|
977 tv1 = v1; |
|
978 |
|
979 octave_value tv2; |
2427
|
980 type_conv_fcn cf2 = v2.numeric_conversion_function (); |
2376
|
981 |
|
982 if (cf2) |
|
983 { |
3203
|
984 octave_value *tmp = cf2 (*v2.rep); |
|
985 |
|
986 if (tmp) |
|
987 { |
|
988 tv2 = octave_value (tmp); |
|
989 t2 = tv2.type_id (); |
|
990 } |
|
991 else |
|
992 { |
|
993 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); |
|
994 return retval; |
|
995 } |
2376
|
996 } |
|
997 else |
|
998 tv2 = v2; |
|
999 |
|
1000 if (cf1 || cf2) |
|
1001 { |
2427
|
1002 binary_op_fcn f |
2376
|
1003 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
|
1004 |
|
1005 if (f) |
|
1006 retval = f (*tv1.rep, *tv2.rep); |
|
1007 else |
|
1008 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
1009 v1.type_name (), v2.type_name ()); |
|
1010 } |
|
1011 else |
|
1012 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
1013 v1.type_name (), v2.type_name ()); |
|
1014 } |
|
1015 |
|
1016 return retval; |
|
1017 } |
|
1018 |
3203
|
1019 static void |
|
1020 gripe_unary_op (const string& on, const string& tn) |
|
1021 { |
|
1022 error ("unary operator `%s' not implemented for `%s' operands", |
|
1023 on.c_str (), tn.c_str ()); |
|
1024 } |
|
1025 |
|
1026 static void |
|
1027 gripe_unary_op_conv (const string& on) |
|
1028 { |
|
1029 error ("type conversion failed for unary operator `%s'", on.c_str ()); |
|
1030 } |
|
1031 |
|
1032 octave_value |
|
1033 do_unary_op (octave_value::unary_op op, const octave_value& v) |
|
1034 { |
|
1035 octave_value retval; |
|
1036 |
|
1037 int t = v.type_id (); |
|
1038 |
|
1039 unary_op_fcn f = octave_value_typeinfo::lookup_unary_op (op, t); |
|
1040 |
|
1041 if (f) |
|
1042 retval = f (*v.rep); |
|
1043 else |
|
1044 { |
|
1045 octave_value tv; |
|
1046 type_conv_fcn cf = v.numeric_conversion_function (); |
|
1047 |
|
1048 if (cf) |
|
1049 { |
|
1050 octave_value *tmp = cf (*v.rep); |
|
1051 |
|
1052 if (tmp) |
|
1053 { |
|
1054 tv = octave_value (tmp); |
|
1055 t = tv.type_id (); |
|
1056 |
|
1057 unary_op_fcn f = octave_value_typeinfo::lookup_unary_op (op, t); |
|
1058 |
|
1059 if (f) |
|
1060 retval = f (*tv.rep); |
|
1061 else |
|
1062 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1063 v.type_name ()); |
|
1064 } |
|
1065 else |
|
1066 gripe_unary_op_conv (octave_value::unary_op_as_string (op)); |
|
1067 } |
|
1068 else |
|
1069 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1070 v.type_name ()); |
|
1071 } |
|
1072 |
|
1073 return retval; |
|
1074 } |
|
1075 |
|
1076 static void |
|
1077 gripe_unary_op_conversion_failed (const string& op, const string& tn) |
|
1078 { |
|
1079 error ("operator %s: type conversion for `%s' failed", |
|
1080 op.c_str (), tn.c_str ()); |
|
1081 } |
|
1082 |
|
1083 void |
|
1084 octave_value::do_non_const_unary_op (octave_value::unary_op op) |
|
1085 { |
|
1086 octave_value retval; |
|
1087 |
|
1088 int t = type_id (); |
|
1089 |
|
1090 non_const_unary_op_fcn f |
|
1091 = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
|
1092 |
|
1093 if (f) |
|
1094 { |
|
1095 make_unique (); |
|
1096 |
|
1097 f (*rep); |
|
1098 } |
|
1099 else |
|
1100 { |
|
1101 type_conv_fcn cf = numeric_conversion_function (); |
|
1102 |
|
1103 if (cf) |
|
1104 { |
|
1105 octave_value *tmp = cf (*rep); |
|
1106 |
|
1107 if (tmp) |
|
1108 { |
|
1109 octave_value *old_rep = rep; |
|
1110 rep = tmp; |
|
1111 rep->count = 1; |
|
1112 |
|
1113 t = type_id (); |
|
1114 |
|
1115 f = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
|
1116 |
|
1117 if (f) |
|
1118 { |
|
1119 f (*rep); |
|
1120 |
|
1121 if (old_rep && --old_rep->count == 0) |
|
1122 delete old_rep; |
|
1123 } |
|
1124 else |
|
1125 { |
|
1126 if (old_rep) |
|
1127 { |
|
1128 if (--rep->count == 0) |
|
1129 delete rep; |
|
1130 |
|
1131 rep = old_rep; |
|
1132 } |
|
1133 |
|
1134 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1135 type_name ()); |
|
1136 } |
|
1137 } |
|
1138 else |
|
1139 gripe_unary_op_conversion_failed |
|
1140 (octave_value::unary_op_as_string (op), type_name ()); |
|
1141 } |
|
1142 else |
|
1143 gripe_unary_op (octave_value::unary_op_as_string (op), type_name ()); |
|
1144 } |
|
1145 } |
|
1146 |
2903
|
1147 // Current indentation. |
|
1148 int octave_value::curr_print_indent_level = 0; |
|
1149 |
3018
|
1150 // TRUE means we are at the beginning of a line. |
2903
|
1151 bool octave_value::beginning_of_line = true; |
|
1152 |
|
1153 // Each print() function should call this before printing anything. |
|
1154 // |
|
1155 // This doesn't need to be fast, but isn't there a better way? |
|
1156 |
|
1157 void |
|
1158 octave_value::indent (ostream& os) const |
|
1159 { |
|
1160 assert (curr_print_indent_level >= 0); |
|
1161 |
|
1162 if (beginning_of_line) |
|
1163 { |
|
1164 // XXX FIXME XXX -- do we need this? |
|
1165 // os << prefix; |
|
1166 |
|
1167 for (int i = 0; i < curr_print_indent_level; i++) |
|
1168 os << " "; |
|
1169 |
|
1170 beginning_of_line = false; |
|
1171 } |
|
1172 } |
|
1173 |
|
1174 // All print() functions should use this to print new lines. |
|
1175 |
|
1176 void |
|
1177 octave_value::newline (ostream& os) const |
|
1178 { |
|
1179 os << "\n"; |
|
1180 |
|
1181 beginning_of_line = true; |
|
1182 } |
|
1183 |
|
1184 // For ressetting print state. |
|
1185 |
|
1186 void |
|
1187 octave_value::reset (void) const |
|
1188 { |
|
1189 beginning_of_line = true; |
|
1190 curr_print_indent_level = 0; |
|
1191 } |
|
1192 |
2376
|
1193 void |
|
1194 install_types (void) |
|
1195 { |
|
1196 octave_base_value::register_type (); |
|
1197 octave_scalar::register_type (); |
|
1198 octave_complex::register_type (); |
|
1199 octave_matrix::register_type (); |
|
1200 octave_complex_matrix::register_type (); |
|
1201 octave_range::register_type (); |
2825
|
1202 octave_bool::register_type (); |
|
1203 octave_bool_matrix::register_type (); |
2376
|
1204 octave_char_matrix::register_type (); |
|
1205 octave_char_matrix_str::register_type (); |
|
1206 octave_struct::register_type (); |
2903
|
1207 octave_file::register_type (); |
2880
|
1208 octave_list::register_type (); |
2376
|
1209 octave_all_va_args::register_type (); |
|
1210 octave_magic_colon::register_type (); |
2974
|
1211 octave_builtin::register_type (); |
|
1212 octave_mapper::register_type (); |
|
1213 octave_user_function::register_type (); |
2376
|
1214 } |
|
1215 |
|
1216 static int |
|
1217 do_fortran_indexing (void) |
|
1218 { |
|
1219 Vdo_fortran_indexing = check_preference ("do_fortran_indexing"); |
|
1220 |
|
1221 liboctave_dfi_flag = Vdo_fortran_indexing; |
|
1222 |
|
1223 return 0; |
|
1224 } |
|
1225 |
|
1226 static int |
|
1227 implicit_str_to_num_ok (void) |
|
1228 { |
|
1229 Vimplicit_str_to_num_ok = check_preference ("implicit_str_to_num_ok"); |
|
1230 |
|
1231 return 0; |
|
1232 } |
|
1233 |
|
1234 static int |
|
1235 ok_to_lose_imaginary_part (void) |
|
1236 { |
|
1237 Vok_to_lose_imaginary_part = check_preference ("ok_to_lose_imaginary_part"); |
|
1238 |
|
1239 return 0; |
|
1240 } |
|
1241 |
|
1242 static int |
|
1243 prefer_column_vectors (void) |
|
1244 { |
|
1245 Vprefer_column_vectors |
|
1246 = check_preference ("prefer_column_vectors"); |
|
1247 |
|
1248 liboctave_pcv_flag = Vprefer_column_vectors; |
|
1249 |
|
1250 return 0; |
|
1251 } |
|
1252 |
|
1253 static int |
|
1254 print_answer_id_name (void) |
|
1255 { |
|
1256 Vprint_answer_id_name = check_preference ("print_answer_id_name"); |
|
1257 |
|
1258 return 0; |
|
1259 } |
|
1260 |
|
1261 static int |
|
1262 propagate_empty_matrices (void) |
|
1263 { |
|
1264 Vpropagate_empty_matrices = check_preference ("propagate_empty_matrices"); |
|
1265 |
|
1266 return 0; |
|
1267 } |
|
1268 |
|
1269 static int |
|
1270 resize_on_range_error (void) |
|
1271 { |
|
1272 Vresize_on_range_error = check_preference ("resize_on_range_error"); |
|
1273 |
|
1274 liboctave_rre_flag = Vresize_on_range_error; |
|
1275 |
|
1276 return 0; |
|
1277 } |
|
1278 |
|
1279 static int |
|
1280 struct_levels_to_print (void) |
|
1281 { |
|
1282 double val; |
|
1283 if (builtin_real_scalar_variable ("struct_levels_to_print", val) |
|
1284 && ! xisnan (val)) |
|
1285 { |
|
1286 int ival = NINT (val); |
2800
|
1287 if (ival >= 0 && ival == val) |
2376
|
1288 { |
|
1289 Vstruct_levels_to_print = ival; |
|
1290 return 0; |
|
1291 } |
|
1292 } |
|
1293 gripe_invalid_value_specified ("struct_levels_to_print"); |
|
1294 return -1; |
|
1295 } |
|
1296 |
|
1297 static int |
|
1298 warn_divide_by_zero (void) |
|
1299 { |
|
1300 Vwarn_divide_by_zero = check_preference ("warn_divide_by_zero"); |
|
1301 |
|
1302 return 0; |
|
1303 } |
|
1304 |
|
1305 void |
2909
|
1306 symbols_of_ov (void) |
2376
|
1307 { |
|
1308 DEFVAR (do_fortran_indexing, 0.0, 0, do_fortran_indexing, |
|
1309 "allow single indices for matrices"); |
|
1310 |
|
1311 DEFVAR (implicit_str_to_num_ok, 0.0, 0, implicit_str_to_num_ok, |
|
1312 "allow implicit string to number conversion"); |
|
1313 |
|
1314 DEFVAR (ok_to_lose_imaginary_part, "warn", 0, ok_to_lose_imaginary_part, |
|
1315 "silently convert from complex to real by dropping imaginary part"); |
|
1316 |
|
1317 DEFVAR (prefer_column_vectors, 1.0, 0, prefer_column_vectors, |
|
1318 "prefer column/row vectors"); |
|
1319 |
|
1320 DEFVAR (print_answer_id_name, 1.0, 0, print_answer_id_name, |
|
1321 "set output style to print `var_name = ...'"); |
|
1322 |
|
1323 DEFVAR (propagate_empty_matrices, 1.0, 0, propagate_empty_matrices, |
|
1324 "operations on empty matrices return an empty matrix, not an error"); |
|
1325 |
|
1326 DEFVAR (resize_on_range_error, 1.0, 0, resize_on_range_error, |
|
1327 "enlarge matrices on assignment"); |
|
1328 |
|
1329 DEFVAR (struct_levels_to_print, 2.0, 0, struct_levels_to_print, |
|
1330 "number of levels of structure elements to print"); |
|
1331 |
|
1332 DEFVAR (warn_divide_by_zero, 1.0, 0, warn_divide_by_zero, |
2957
|
1333 "if TRUE, warn about division by zero"); |
2376
|
1334 } |
|
1335 |
|
1336 /* |
|
1337 ;;; Local Variables: *** |
|
1338 ;;; mode: C++ *** |
|
1339 ;;; End: *** |
|
1340 */ |