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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2376
|
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" |
4153
|
33 #include "quit.h" |
2376
|
34 |
2974
|
35 #include "oct-obj.h" |
2376
|
36 #include "ov.h" |
|
37 #include "ov-base.h" |
2825
|
38 #include "ov-bool.h" |
|
39 #include "ov-bool-mat.h" |
3351
|
40 #include "ov-cell.h" |
2376
|
41 #include "ov-scalar.h" |
|
42 #include "ov-re-mat.h" |
|
43 #include "ov-complex.h" |
|
44 #include "ov-cx-mat.h" |
|
45 #include "ov-ch-mat.h" |
|
46 #include "ov-str-mat.h" |
|
47 #include "ov-range.h" |
|
48 #include "ov-struct.h" |
2903
|
49 #include "ov-file.h" |
2880
|
50 #include "ov-list.h" |
3977
|
51 #include "ov-cs-list.h" |
2376
|
52 #include "ov-colon.h" |
|
53 #include "ov-va-args.h" |
2974
|
54 #include "ov-builtin.h" |
|
55 #include "ov-mapper.h" |
|
56 #include "ov-usr-fcn.h" |
4342
|
57 #include "ov-fcn-handle.h" |
4433
|
58 #include "ov-re-nd-array.h" |
2376
|
59 #include "ov-typeinfo.h" |
|
60 |
|
61 #include "defun.h" |
2880
|
62 #include "error.h" |
2376
|
63 #include "gripes.h" |
|
64 #include "pager.h" |
4005
|
65 #include "parse.h" |
2376
|
66 #include "pr-output.h" |
|
67 #include "utils.h" |
|
68 #include "variables.h" |
|
69 |
2477
|
70 // We are likely to have a lot of octave_value objects to allocate, so |
|
71 // make the grow_size large. |
3219
|
72 DEFINE_OCTAVE_ALLOCATOR2(octave_value, 1024); |
2477
|
73 |
4005
|
74 // If TRUE, turn off printing of results in functions (as if a |
|
75 // semicolon has been appended to each statement). |
|
76 static bool Vsilent_functions; |
|
77 |
4455
|
78 // If TRUE, print a warning for assignments like |
2376
|
79 // |
|
80 // octave> A(1) = 3; A(2) = 5 |
|
81 // |
|
82 // for A already defined and a matrix type. |
4455
|
83 bool Vwarn_fortran_indexing; |
2376
|
84 |
4452
|
85 // Should we warn about conversions from complex to real? |
|
86 int Vwarn_imag_to_real; |
4257
|
87 |
4452
|
88 // Should we print a warning when converting `[97, 98, 99, "123"]' |
|
89 // to a character string? |
|
90 bool Vwarn_num_to_str; |
|
91 |
|
92 // If TRUE, warn for operations like |
2376
|
93 // |
|
94 // octave> 'abc' + 0 |
|
95 // 97 98 99 |
|
96 // |
4452
|
97 int Vwarn_str_to_num; |
2376
|
98 |
|
99 // If TRUE, print the name along with the value. |
|
100 bool Vprint_answer_id_name; |
|
101 |
|
102 // Should operations on empty matrices return empty matrices or an |
|
103 // error? A positive value means yes. A negative value means yes, |
|
104 // but print a warning message. Zero means it should be considered an |
|
105 // error. |
|
106 int Vpropagate_empty_matrices; |
|
107 |
|
108 // How many levels of structure elements should we print? |
|
109 int Vstruct_levels_to_print; |
|
110 |
|
111 // Allow divide by zero errors to be suppressed. |
|
112 bool Vwarn_divide_by_zero; |
|
113 |
2948
|
114 // If TRUE, resize matrices when performing and indexed assignment and |
|
115 // the indices are outside the current bounds. |
3196
|
116 bool Vresize_on_range_error; |
2948
|
117 |
2880
|
118 // XXX FIXME XXX |
|
119 |
2376
|
120 // Octave's value type. |
|
121 |
3536
|
122 std::string |
3203
|
123 octave_value::unary_op_as_string (unary_op op) |
|
124 { |
3523
|
125 std::string retval; |
3203
|
126 |
|
127 switch (op) |
|
128 { |
3533
|
129 case op_not: |
3203
|
130 retval = "!"; |
|
131 break; |
|
132 |
3533
|
133 case op_uminus: |
3203
|
134 retval = "-"; |
|
135 break; |
|
136 |
3533
|
137 case op_transpose: |
3203
|
138 retval = ".'"; |
|
139 break; |
|
140 |
3533
|
141 case op_hermitian: |
3203
|
142 retval = "'"; |
|
143 break; |
|
144 |
3533
|
145 case op_incr: |
3203
|
146 retval = "++"; |
|
147 break; |
|
148 |
3533
|
149 case op_decr: |
3203
|
150 retval = "--"; |
|
151 break; |
|
152 |
|
153 default: |
|
154 retval = "<unknown>"; |
|
155 } |
|
156 |
|
157 return retval; |
|
158 } |
|
159 |
3536
|
160 std::string |
2376
|
161 octave_value::binary_op_as_string (binary_op op) |
|
162 { |
3523
|
163 std::string retval; |
2376
|
164 |
|
165 switch (op) |
|
166 { |
3533
|
167 case op_add: |
2376
|
168 retval = "+"; |
|
169 break; |
|
170 |
3533
|
171 case op_sub: |
2376
|
172 retval = "-"; |
|
173 break; |
|
174 |
3533
|
175 case op_mul: |
2376
|
176 retval = "*"; |
|
177 break; |
|
178 |
3533
|
179 case op_div: |
2376
|
180 retval = "/"; |
|
181 break; |
|
182 |
3533
|
183 case op_pow: |
2376
|
184 retval = "^"; |
|
185 break; |
|
186 |
3533
|
187 case op_ldiv: |
2376
|
188 retval = "\\"; |
|
189 break; |
|
190 |
3533
|
191 case op_lshift: |
2903
|
192 retval = "<<"; |
|
193 break; |
|
194 |
3533
|
195 case op_rshift: |
2903
|
196 retval = ">>"; |
|
197 break; |
|
198 |
3533
|
199 case op_lt: |
2376
|
200 retval = "<"; |
|
201 break; |
|
202 |
3533
|
203 case op_le: |
2376
|
204 retval = "<="; |
|
205 break; |
|
206 |
3533
|
207 case op_eq: |
2376
|
208 retval = "=="; |
|
209 break; |
|
210 |
3533
|
211 case op_ge: |
2376
|
212 retval = ">="; |
|
213 break; |
|
214 |
3533
|
215 case op_gt: |
2376
|
216 retval = ">"; |
|
217 break; |
|
218 |
3533
|
219 case op_ne: |
2376
|
220 retval = "!="; |
|
221 break; |
|
222 |
3533
|
223 case op_el_mul: |
2376
|
224 retval = ".*"; |
|
225 break; |
|
226 |
3533
|
227 case op_el_div: |
2376
|
228 retval = "./"; |
|
229 break; |
|
230 |
3533
|
231 case op_el_pow: |
2376
|
232 retval = ".^"; |
|
233 break; |
|
234 |
3533
|
235 case op_el_ldiv: |
2376
|
236 retval = ".\\"; |
|
237 break; |
|
238 |
3533
|
239 case op_el_and: |
2376
|
240 retval = "&"; |
|
241 break; |
|
242 |
3533
|
243 case op_el_or: |
2376
|
244 retval = "|"; |
|
245 break; |
|
246 |
3533
|
247 case op_struct_ref: |
2376
|
248 retval = "."; |
|
249 break; |
|
250 |
|
251 default: |
|
252 retval = "<unknown>"; |
|
253 } |
|
254 |
|
255 return retval; |
|
256 } |
|
257 |
3536
|
258 std::string |
2880
|
259 octave_value::assign_op_as_string (assign_op op) |
|
260 { |
3523
|
261 std::string retval; |
2880
|
262 |
|
263 switch (op) |
|
264 { |
3533
|
265 case op_asn_eq: |
2880
|
266 retval = "="; |
|
267 break; |
|
268 |
3533
|
269 case op_add_eq: |
2880
|
270 retval = "+="; |
|
271 break; |
|
272 |
3533
|
273 case op_sub_eq: |
2880
|
274 retval = "-="; |
|
275 break; |
|
276 |
3533
|
277 case op_mul_eq: |
2880
|
278 retval = "*="; |
|
279 break; |
|
280 |
3533
|
281 case op_div_eq: |
2880
|
282 retval = "/="; |
|
283 break; |
|
284 |
3533
|
285 case op_ldiv_eq: |
3204
|
286 retval = "\\="; |
|
287 break; |
|
288 |
4018
|
289 case op_pow_eq: |
|
290 retval = "^="; |
|
291 break; |
|
292 |
3533
|
293 case op_lshift_eq: |
2903
|
294 retval = "<<="; |
|
295 break; |
|
296 |
3533
|
297 case op_rshift_eq: |
2903
|
298 retval = ">>="; |
|
299 break; |
|
300 |
3533
|
301 case op_el_mul_eq: |
2880
|
302 retval = ".*="; |
|
303 break; |
|
304 |
3533
|
305 case op_el_div_eq: |
2880
|
306 retval = "./="; |
|
307 break; |
|
308 |
3533
|
309 case op_el_ldiv_eq: |
3204
|
310 retval = ".\\="; |
|
311 break; |
|
312 |
4018
|
313 case op_el_pow_eq: |
|
314 retval = ".^="; |
|
315 break; |
|
316 |
3533
|
317 case op_el_and_eq: |
2880
|
318 retval = "&="; |
|
319 break; |
|
320 |
3533
|
321 case op_el_or_eq: |
2880
|
322 retval = "|="; |
|
323 break; |
|
324 |
|
325 default: |
|
326 retval = "<unknown>"; |
|
327 } |
|
328 |
|
329 return retval; |
|
330 } |
|
331 |
2376
|
332 octave_value::octave_value (void) |
2825
|
333 : rep (new octave_base_value ()) |
|
334 { |
|
335 rep->count = 1; |
|
336 } |
2376
|
337 |
4254
|
338 octave_value::octave_value (short int i) |
|
339 : rep (new octave_scalar (i)) |
|
340 { |
|
341 rep->count = 1; |
|
342 } |
|
343 |
|
344 octave_value::octave_value (unsigned short int i) |
|
345 : rep (new octave_scalar (i)) |
|
346 { |
|
347 rep->count = 1; |
|
348 } |
|
349 |
4233
|
350 octave_value::octave_value (int i) |
|
351 : rep (new octave_scalar (i)) |
|
352 { |
|
353 rep->count = 1; |
|
354 } |
|
355 |
4254
|
356 octave_value::octave_value (unsigned int i) |
|
357 : rep (new octave_scalar (i)) |
|
358 { |
|
359 rep->count = 1; |
|
360 } |
|
361 |
|
362 octave_value::octave_value (long int i) |
|
363 : rep (new octave_scalar (i)) |
|
364 { |
|
365 rep->count = 1; |
|
366 } |
|
367 |
|
368 octave_value::octave_value (unsigned long int i) |
|
369 : rep (new octave_scalar (i)) |
|
370 { |
|
371 rep->count = 1; |
|
372 } |
|
373 |
4353
|
374 #if defined (HAVE_LONG_LONG_INT) |
|
375 octave_value::octave_value (long long int i) |
|
376 : rep (new octave_scalar (i)) |
|
377 { |
|
378 rep->count = 1; |
|
379 } |
|
380 #endif |
|
381 |
4355
|
382 #if defined (HAVE_UNSIGNED_LONG_LONG_INT) |
4353
|
383 octave_value::octave_value (unsigned long long int i) |
|
384 : rep (new octave_scalar (i)) |
|
385 { |
|
386 rep->count = 1; |
|
387 } |
|
388 #endif |
|
389 |
4254
|
390 octave_value::octave_value (octave_time t) |
|
391 : rep (new octave_scalar (t)) |
|
392 { |
|
393 rep->count = 1; |
|
394 } |
|
395 |
2376
|
396 octave_value::octave_value (double d) |
2825
|
397 : rep (new octave_scalar (d)) |
|
398 { |
|
399 rep->count = 1; |
|
400 } |
2376
|
401 |
3351
|
402 octave_value::octave_value (const Cell& c) |
|
403 : rep (new octave_cell (c)) |
|
404 { |
|
405 rep->count = 1; |
|
406 maybe_mutate (); |
|
407 } |
|
408 |
2376
|
409 octave_value::octave_value (const Matrix& m) |
2423
|
410 : rep (new octave_matrix (m)) |
|
411 { |
|
412 rep->count = 1; |
|
413 maybe_mutate (); |
|
414 } |
2376
|
415 |
|
416 octave_value::octave_value (const DiagMatrix& d) |
2423
|
417 : rep (new octave_matrix (d)) |
|
418 { |
|
419 rep->count = 1; |
|
420 maybe_mutate (); |
|
421 } |
2376
|
422 |
3418
|
423 octave_value::octave_value (const RowVector& v) |
|
424 : rep (new octave_matrix (v)) |
2423
|
425 { |
|
426 rep->count = 1; |
|
427 maybe_mutate (); |
|
428 } |
2376
|
429 |
3418
|
430 octave_value::octave_value (const ColumnVector& v) |
|
431 : rep (new octave_matrix (v)) |
2423
|
432 { |
|
433 rep->count = 1; |
|
434 maybe_mutate (); |
|
435 } |
2376
|
436 |
|
437 octave_value::octave_value (const Complex& C) |
2423
|
438 : rep (new octave_complex (C)) |
|
439 { |
|
440 rep->count = 1; |
|
441 maybe_mutate (); |
|
442 } |
2376
|
443 |
|
444 octave_value::octave_value (const ComplexMatrix& m) |
2423
|
445 : rep (new octave_complex_matrix (m)) |
|
446 { |
|
447 rep->count = 1; |
|
448 maybe_mutate (); |
|
449 } |
2376
|
450 |
|
451 octave_value::octave_value (const ComplexDiagMatrix& d) |
2423
|
452 : rep (new octave_complex_matrix (d)) |
|
453 { |
|
454 rep->count = 1; |
|
455 maybe_mutate (); |
|
456 } |
2376
|
457 |
3418
|
458 octave_value::octave_value (const ComplexRowVector& v) |
|
459 : rep (new octave_complex_matrix (v)) |
2423
|
460 { |
|
461 rep->count = 1; |
|
462 maybe_mutate (); |
|
463 } |
2376
|
464 |
3418
|
465 octave_value::octave_value (const ComplexColumnVector& v) |
|
466 : rep (new octave_complex_matrix (v)) |
2423
|
467 { |
|
468 rep->count = 1; |
|
469 maybe_mutate (); |
|
470 } |
2376
|
471 |
2825
|
472 octave_value::octave_value (bool b) |
|
473 : rep (new octave_bool (b)) |
|
474 { |
|
475 rep->count = 1; |
|
476 } |
|
477 |
|
478 octave_value::octave_value (const boolMatrix& bm) |
|
479 : rep (new octave_bool_matrix (bm)) |
|
480 { |
|
481 rep->count = 1; |
|
482 maybe_mutate (); |
|
483 } |
|
484 |
3189
|
485 octave_value::octave_value (char c) |
|
486 : rep (new octave_char_matrix_str (c)) |
|
487 { |
|
488 rep->count = 1; |
|
489 maybe_mutate (); |
|
490 } |
|
491 |
2376
|
492 octave_value::octave_value (const char *s) |
2423
|
493 : rep (new octave_char_matrix_str (s)) |
|
494 { |
|
495 rep->count = 1; |
|
496 maybe_mutate (); |
|
497 } |
2376
|
498 |
3523
|
499 octave_value::octave_value (const std::string& s) |
2423
|
500 : rep (new octave_char_matrix_str (s)) |
|
501 { |
|
502 rep->count = 1; |
|
503 maybe_mutate (); |
|
504 } |
2376
|
505 |
|
506 octave_value::octave_value (const string_vector& s) |
2423
|
507 : rep (new octave_char_matrix_str (s)) |
|
508 { |
|
509 rep->count = 1; |
|
510 maybe_mutate (); |
|
511 } |
2376
|
512 |
|
513 octave_value::octave_value (const charMatrix& chm, bool is_string) |
2409
|
514 : rep (0) |
|
515 { |
|
516 if (is_string) |
|
517 rep = new octave_char_matrix_str (chm); |
|
518 else |
|
519 rep = new octave_char_matrix (chm); |
2376
|
520 |
2409
|
521 rep->count = 1; |
2423
|
522 maybe_mutate (); |
2409
|
523 } |
2376
|
524 |
|
525 octave_value::octave_value (double base, double limit, double inc) |
2423
|
526 : rep (new octave_range (base, limit, inc)) |
|
527 { |
|
528 rep->count = 1; |
|
529 maybe_mutate (); |
|
530 } |
2376
|
531 |
|
532 octave_value::octave_value (const Range& r) |
2423
|
533 : rep (new octave_range (r)) |
|
534 { |
|
535 rep->count = 1; |
|
536 maybe_mutate (); |
|
537 } |
2376
|
538 |
|
539 octave_value::octave_value (const Octave_map& m) |
2825
|
540 : rep (new octave_struct (m)) |
|
541 { |
|
542 rep->count = 1; |
2880
|
543 } |
|
544 |
3340
|
545 octave_value::octave_value (const octave_stream& s, int n) |
2903
|
546 : rep (new octave_file (s, n)) |
|
547 { |
|
548 rep->count = 1; |
|
549 } |
|
550 |
2974
|
551 octave_value::octave_value (octave_function *f) |
|
552 : rep (f) |
|
553 { |
|
554 rep->count = 1; |
|
555 } |
|
556 |
4342
|
557 octave_value::octave_value (const octave_fcn_handle& fh) |
|
558 : rep (new octave_fcn_handle (fh)) |
|
559 { |
|
560 rep->count = 1; |
|
561 } |
|
562 |
3977
|
563 octave_value::octave_value (const octave_value_list& l, bool is_cs_list) |
|
564 : rep (0) |
2880
|
565 { |
4038
|
566 rep = is_cs_list ? new octave_cs_list (l) : new octave_list (l); |
3977
|
567 |
2880
|
568 rep->count = 1; |
|
569 } |
2376
|
570 |
|
571 octave_value::octave_value (octave_value::magic_colon) |
2825
|
572 : rep (new octave_magic_colon ()) |
|
573 { |
|
574 rep->count = 1; |
|
575 } |
2376
|
576 |
|
577 octave_value::octave_value (octave_value::all_va_args) |
2825
|
578 : rep (new octave_all_va_args ()) |
|
579 { |
|
580 rep->count = 1; |
|
581 } |
2376
|
582 |
4276
|
583 octave_value::octave_value (octave_value *new_rep, int cnt) |
2825
|
584 : rep (new_rep) |
|
585 { |
4276
|
586 rep->count = cnt; |
2825
|
587 } |
2376
|
588 |
|
589 octave_value::~octave_value (void) |
|
590 { |
|
591 #if defined (MDEBUG) |
3531
|
592 std::cerr << "~octave_value: rep: " << rep |
|
593 << " rep->count: " << rep->count << "\n"; |
2376
|
594 #endif |
|
595 |
|
596 if (rep && --rep->count == 0) |
|
597 { |
|
598 delete rep; |
|
599 rep = 0; |
|
600 } |
|
601 } |
|
602 |
2880
|
603 octave_value * |
3933
|
604 octave_value::clone (void) const |
2880
|
605 { |
|
606 panic_impossible (); |
3546
|
607 return 0; |
2880
|
608 } |
|
609 |
2409
|
610 void |
|
611 octave_value::maybe_mutate (void) |
|
612 { |
2410
|
613 octave_value *tmp = rep->try_narrowing_conversion (); |
2409
|
614 |
|
615 if (tmp && tmp != rep) |
|
616 { |
|
617 if (--rep->count == 0) |
|
618 delete rep; |
|
619 |
|
620 rep = tmp; |
|
621 rep->count = 1; |
|
622 } |
|
623 } |
|
624 |
4247
|
625 octave_value |
4271
|
626 octave_value::single_subsref (const std::string& type, |
|
627 const octave_value_list& idx) |
4247
|
628 { |
|
629 std::list<octave_value_list> i; |
|
630 |
|
631 i.push_back (idx); |
|
632 |
|
633 return rep->subsref (type, i); |
|
634 } |
|
635 |
2974
|
636 octave_value_list |
4247
|
637 octave_value::subsref (const std::string& type, |
4219
|
638 const std::list<octave_value_list>& idx, int nargout) |
3933
|
639 { |
|
640 if (is_constant ()) |
|
641 return rep->subsref (type, idx); |
|
642 else |
|
643 return rep->subsref (type, idx, nargout); |
|
644 } |
|
645 |
|
646 octave_value |
4247
|
647 octave_value::next_subsref (const std::string& type, |
4219
|
648 const std::list<octave_value_list>& idx, |
4233
|
649 size_t skip) |
3933
|
650 { |
4219
|
651 if (idx.size () > skip) |
3933
|
652 { |
4219
|
653 std::list<octave_value_list> new_idx (idx); |
4233
|
654 for (size_t i = 0; i < skip; i++) |
4219
|
655 new_idx.erase (new_idx.begin ()); |
3933
|
656 return subsref (type.substr (skip), new_idx); |
|
657 } |
|
658 else |
|
659 return *this; |
|
660 } |
|
661 |
|
662 octave_value_list |
3544
|
663 octave_value::do_multi_index_op (int nargout, const octave_value_list& idx) |
2974
|
664 { |
3544
|
665 return rep->do_multi_index_op (nargout, idx); |
2974
|
666 } |
|
667 |
2376
|
668 static void |
3933
|
669 gripe_no_conversion (const std::string& on, const std::string& tn1, |
|
670 const std::string& tn2) |
2376
|
671 { |
3203
|
672 error ("operator %s: no conversion for assignment of `%s' to indexed `%s'", |
|
673 on.c_str (), tn2.c_str (), tn1.c_str ()); |
2376
|
674 } |
|
675 |
3933
|
676 #if 0 |
3204
|
677 static void |
3933
|
678 gripe_assign_failed (const std::string& on, const std::string& tn1, |
|
679 const std::string& tn2) |
3204
|
680 { |
|
681 error ("assignment failed for `%s %s %s'", |
|
682 tn1.c_str (), on.c_str (), tn2.c_str ()); |
|
683 } |
3933
|
684 #endif |
3204
|
685 |
|
686 static void |
3933
|
687 gripe_assign_failed_or_no_method (const std::string& on, |
|
688 const std::string& tn1, |
3523
|
689 const std::string& tn2) |
3204
|
690 { |
|
691 error ("assignment failed, or no method for `%s %s %s'", |
|
692 tn1.c_str (), on.c_str (), tn2.c_str ()); |
|
693 } |
|
694 |
3933
|
695 octave_value |
4247
|
696 octave_value::subsasgn (const std::string& type, |
4219
|
697 const std::list<octave_value_list>& idx, |
3933
|
698 const octave_value& rhs) |
|
699 { |
|
700 return rep->subsasgn (type, idx, rhs); |
|
701 } |
|
702 |
|
703 octave_value |
4247
|
704 octave_value::assign (assign_op op, const std::string& type, |
4219
|
705 const std::list<octave_value_list>& idx, |
3933
|
706 const octave_value& rhs) |
|
707 { |
|
708 octave_value retval; |
|
709 |
|
710 make_unique (); |
|
711 |
|
712 octave_value t_rhs = rhs; |
|
713 |
|
714 if (op != op_asn_eq) |
|
715 { |
|
716 // XXX FIXME XXX -- only do the following stuff if we can't find |
|
717 // a specific function to call to handle the op= operation for |
|
718 // the types we have. |
|
719 |
|
720 octave_value t = subsref (type, idx); |
|
721 |
|
722 if (! error_state) |
|
723 { |
|
724 binary_op binop = op_eq_to_binary_op (op); |
|
725 |
|
726 if (! error_state) |
|
727 t_rhs = do_binary_op (binop, t, rhs); |
|
728 } |
|
729 } |
|
730 |
|
731 if (! error_state) |
|
732 { |
|
733 if (type[0] == '.' && ! is_map ()) |
|
734 { |
|
735 octave_value tmp = Octave_map (); |
|
736 retval = tmp.subsasgn (type, idx, t_rhs); |
|
737 } |
|
738 else |
|
739 retval = subsasgn (type, idx, t_rhs); |
|
740 } |
|
741 |
|
742 if (error_state) |
|
743 gripe_assign_failed_or_no_method (assign_op_as_string (op), |
|
744 type_name (), rhs.type_name ()); |
|
745 |
|
746 return retval; |
|
747 } |
|
748 |
|
749 const octave_value& |
3203
|
750 octave_value::assign (assign_op op, const octave_value& rhs) |
2880
|
751 { |
3533
|
752 if (op == op_asn_eq) |
3203
|
753 operator = (rhs); |
|
754 else |
|
755 { |
3204
|
756 // XXX FIXME XXX -- only do the following stuff if we can't find |
|
757 // a specific function to call to handle the op= operation for |
|
758 // the types we have. |
|
759 |
|
760 binary_op binop = op_eq_to_binary_op (op); |
|
761 |
|
762 if (! error_state) |
|
763 { |
|
764 octave_value t = do_binary_op (binop, *this, rhs); |
|
765 |
|
766 if (! error_state) |
|
767 operator = (t); |
|
768 } |
|
769 |
|
770 if (error_state) |
|
771 gripe_assign_failed_or_no_method (assign_op_as_string (op), |
|
772 type_name (), rhs.type_name ()); |
|
773 } |
|
774 |
3933
|
775 return *this; |
2376
|
776 } |
|
777 |
3351
|
778 Cell |
|
779 octave_value::cell_value (void) const |
|
780 { |
|
781 return rep->cell_value (); |
|
782 } |
|
783 |
2376
|
784 Octave_map |
|
785 octave_value::map_value (void) const |
|
786 { |
|
787 return rep->map_value (); |
|
788 } |
|
789 |
3340
|
790 octave_stream |
2903
|
791 octave_value::stream_value (void) const |
|
792 { |
|
793 return rep->stream_value (); |
|
794 } |
|
795 |
|
796 int |
|
797 octave_value::stream_number (void) const |
|
798 { |
|
799 return rep->stream_number (); |
|
800 } |
|
801 |
2974
|
802 octave_function * |
|
803 octave_value::function_value (bool silent) |
|
804 { |
|
805 return rep->function_value (silent); |
|
806 } |
|
807 |
4346
|
808 octave_fcn_handle * |
4343
|
809 octave_value::fcn_handle_value (bool silent) |
|
810 { |
|
811 return rep->fcn_handle_value (silent); |
|
812 } |
|
813 |
2880
|
814 octave_value_list |
|
815 octave_value::list_value (void) const |
|
816 { |
|
817 return rep->list_value (); |
|
818 } |
|
819 |
2376
|
820 ColumnVector |
3419
|
821 octave_value::column_vector_value (bool force_string_conv, |
|
822 bool force_vector_conversion) const |
|
823 { |
|
824 ColumnVector retval; |
|
825 |
|
826 Matrix m = matrix_value (force_string_conv); |
|
827 |
|
828 if (error_state) |
|
829 return retval; |
|
830 |
|
831 int nr = m.rows (); |
|
832 int nc = m.columns (); |
|
833 |
|
834 if (nc == 1) |
|
835 { |
|
836 retval.resize (nr); |
|
837 for (int i = 0; i < nr; i++) |
|
838 retval (i) = m (i, 0); |
|
839 } |
|
840 else |
|
841 { |
3523
|
842 std::string tn = type_name (); |
3419
|
843 gripe_invalid_conversion (tn.c_str (), "real column vector"); |
|
844 } |
|
845 |
|
846 return retval; |
|
847 } |
|
848 |
|
849 ComplexColumnVector |
|
850 octave_value::complex_column_vector_value (bool force_string_conv, |
|
851 bool force_vector_conversion) const |
|
852 { |
|
853 ComplexColumnVector retval; |
|
854 |
|
855 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
856 |
|
857 if (error_state) |
|
858 return retval; |
|
859 |
|
860 int nr = m.rows (); |
|
861 int nc = m.columns (); |
|
862 |
|
863 if (nc == 1) |
|
864 { |
3465
|
865 retval.resize (nr); |
|
866 for (int i = 0; i < nr; i++) |
3419
|
867 retval (i) = m (i, 0); |
|
868 } |
|
869 else |
|
870 { |
3523
|
871 std::string tn = type_name (); |
3419
|
872 gripe_invalid_conversion (tn.c_str (), "complex column vector"); |
|
873 } |
|
874 |
|
875 return retval; |
|
876 } |
|
877 |
|
878 RowVector |
|
879 octave_value::row_vector_value (bool force_string_conv, |
|
880 bool force_vector_conversion) const |
|
881 { |
|
882 RowVector retval; |
|
883 |
|
884 Matrix m = matrix_value (force_string_conv); |
|
885 |
|
886 if (error_state) |
|
887 return retval; |
|
888 |
|
889 int nr = m.rows (); |
|
890 int nc = m.columns (); |
|
891 |
|
892 if (nr == 1) |
|
893 { |
|
894 retval.resize (nc); |
|
895 for (int i = 0; i < nc; i++) |
|
896 retval (i) = m (0, i); |
|
897 } |
|
898 else |
|
899 { |
3523
|
900 std::string tn = type_name (); |
3419
|
901 gripe_invalid_conversion (tn.c_str (), "real row vector"); |
|
902 } |
|
903 |
|
904 return retval; |
|
905 } |
|
906 |
|
907 ComplexRowVector |
|
908 octave_value::complex_row_vector_value (bool force_string_conv, |
|
909 bool force_vector_conversion) const |
|
910 { |
|
911 ComplexRowVector retval; |
|
912 |
|
913 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
914 |
|
915 if (error_state) |
|
916 return retval; |
|
917 |
|
918 int nr = m.rows (); |
|
919 int nc = m.columns (); |
|
920 |
|
921 if (nr == 1) |
|
922 { |
|
923 retval.resize (nc); |
|
924 for (int i = 0; i < nc; i++) |
|
925 retval (i) = m (0, i); |
|
926 } |
|
927 else |
|
928 { |
3523
|
929 std::string tn = type_name (); |
3419
|
930 gripe_invalid_conversion (tn.c_str (), "complex row vector"); |
|
931 } |
|
932 |
|
933 return retval; |
|
934 } |
|
935 |
|
936 // Sloppy... |
|
937 |
|
938 Array<double> |
2376
|
939 octave_value::vector_value (bool force_string_conv, |
|
940 bool force_vector_conversion) const |
|
941 { |
3419
|
942 Array<double> retval; |
2376
|
943 |
|
944 Matrix m = matrix_value (force_string_conv); |
|
945 |
|
946 if (error_state) |
|
947 return retval; |
|
948 |
|
949 int nr = m.rows (); |
|
950 int nc = m.columns (); |
|
951 |
|
952 if (nr == 1) |
|
953 { |
|
954 retval.resize (nc); |
|
955 for (int i = 0; i < nc; i++) |
|
956 retval (i) = m (0, i); |
|
957 } |
|
958 else if (nc == 1) |
|
959 { |
|
960 retval.resize (nr); |
|
961 for (int i = 0; i < nr; i++) |
|
962 retval (i) = m (i, 0); |
|
963 } |
4455
|
964 else if (nr > 0 && nc > 0) |
2376
|
965 { |
4455
|
966 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
967 if (! force_vector_conversion && Vwarn_fortran_indexing) |
|
968 gripe_implicit_conversion (type_name (), "real vector"); |
|
969 |
2376
|
970 retval.resize (nr * nc); |
|
971 int k = 0; |
|
972 for (int j = 0; j < nc; j++) |
|
973 for (int i = 0; i < nr; i++) |
4153
|
974 { |
|
975 OCTAVE_QUIT; |
|
976 |
|
977 retval (k++) = m (i, j); |
|
978 } |
2376
|
979 } |
|
980 else |
|
981 { |
3523
|
982 std::string tn = type_name (); |
2376
|
983 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
984 } |
|
985 |
|
986 return retval; |
|
987 } |
|
988 |
4044
|
989 Array<int> |
|
990 octave_value::int_vector_value (bool force_string_conv, bool require_int, |
|
991 bool force_vector_conversion) const |
|
992 { |
|
993 Array<int> retval; |
|
994 |
|
995 Matrix m = matrix_value (force_string_conv); |
|
996 |
|
997 if (error_state) |
|
998 return retval; |
|
999 |
|
1000 int nr = m.rows (); |
|
1001 int nc = m.columns (); |
|
1002 |
|
1003 if (nr == 1) |
|
1004 { |
|
1005 retval.resize (nc); |
|
1006 for (int i = 0; i < nc; i++) |
|
1007 { |
4153
|
1008 OCTAVE_QUIT; |
|
1009 |
4044
|
1010 double d = m (0, i); |
|
1011 |
|
1012 if (require_int && D_NINT (d) != d) |
|
1013 { |
|
1014 error ("conversion to integer value failed"); |
|
1015 return retval; |
|
1016 } |
|
1017 |
|
1018 retval (i) = static_cast<int> (d); |
|
1019 } |
|
1020 } |
|
1021 else if (nc == 1) |
|
1022 { |
|
1023 retval.resize (nr); |
|
1024 for (int i = 0; i < nr; i++) |
|
1025 { |
4153
|
1026 OCTAVE_QUIT; |
|
1027 |
4044
|
1028 double d = m (i, 0); |
|
1029 |
|
1030 if (require_int && D_NINT (d) != d) |
|
1031 { |
|
1032 error ("conversion to integer value failed"); |
|
1033 return retval; |
|
1034 } |
|
1035 |
|
1036 retval (i) = static_cast<int> (d); |
|
1037 } |
|
1038 } |
4455
|
1039 else if (nr > 0 && nc > 0) |
4044
|
1040 { |
4455
|
1041 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
1042 if (! force_vector_conversion && Vwarn_fortran_indexing) |
|
1043 gripe_implicit_conversion (type_name (), "real vector"); |
|
1044 |
4044
|
1045 retval.resize (nr * nc); |
|
1046 int k = 0; |
|
1047 for (int j = 0; j < nc; j++) |
|
1048 { |
|
1049 for (int i = 0; i < nr; i++) |
|
1050 { |
4153
|
1051 OCTAVE_QUIT; |
|
1052 |
4044
|
1053 double d = m (i, j); |
|
1054 |
|
1055 if (require_int && D_NINT (d) != d) |
|
1056 { |
|
1057 error ("conversion to integer value failed"); |
|
1058 return retval; |
|
1059 } |
|
1060 |
|
1061 retval (k++) = static_cast<int> (d); |
|
1062 } |
|
1063 } |
|
1064 } |
|
1065 else |
|
1066 { |
|
1067 std::string tn = type_name (); |
|
1068 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
1069 } |
|
1070 |
|
1071 return retval; |
|
1072 } |
|
1073 |
3419
|
1074 Array<Complex> |
2376
|
1075 octave_value::complex_vector_value (bool force_string_conv, |
|
1076 bool force_vector_conversion) const |
|
1077 { |
3419
|
1078 Array<Complex> retval; |
2376
|
1079 |
|
1080 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
1081 |
|
1082 if (error_state) |
|
1083 return retval; |
|
1084 |
|
1085 int nr = m.rows (); |
|
1086 int nc = m.columns (); |
|
1087 |
|
1088 if (nr == 1) |
|
1089 { |
|
1090 retval.resize (nc); |
|
1091 for (int i = 0; i < nc; i++) |
4153
|
1092 { |
|
1093 OCTAVE_QUIT; |
|
1094 retval (i) = m (0, i); |
|
1095 } |
2376
|
1096 } |
|
1097 else if (nc == 1) |
|
1098 { |
|
1099 retval.resize (nr); |
|
1100 for (int i = 0; i < nr; i++) |
4153
|
1101 { |
|
1102 OCTAVE_QUIT; |
|
1103 retval (i) = m (i, 0); |
|
1104 } |
2376
|
1105 } |
4455
|
1106 else if (nr > 0 && nc > 0) |
2376
|
1107 { |
4455
|
1108 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
1109 if (! force_vector_conversion && Vwarn_fortran_indexing) |
|
1110 gripe_implicit_conversion (type_name (), "complex vector"); |
|
1111 |
2376
|
1112 retval.resize (nr * nc); |
|
1113 int k = 0; |
|
1114 for (int j = 0; j < nc; j++) |
|
1115 for (int i = 0; i < nr; i++) |
4153
|
1116 { |
|
1117 OCTAVE_QUIT; |
|
1118 retval (k++) = m (i, j); |
|
1119 } |
2376
|
1120 } |
|
1121 else |
|
1122 { |
3523
|
1123 std::string tn = type_name (); |
2376
|
1124 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
|
1125 } |
|
1126 |
|
1127 return retval; |
|
1128 } |
|
1129 |
4452
|
1130 octave_value |
|
1131 octave_value::convert_to_str (bool pad) const |
|
1132 { |
|
1133 octave_value retval = convert_to_str_internal (pad); |
|
1134 |
|
1135 if (is_numeric_type () && Vwarn_num_to_str) |
|
1136 gripe_implicit_conversion (type_name (), retval.type_name ()); |
|
1137 |
|
1138 return retval; |
|
1139 } |
|
1140 |
2376
|
1141 void |
4005
|
1142 octave_value::print_with_name (std::ostream& output_buf, |
|
1143 const std::string& name, |
2903
|
1144 bool print_padding) const |
2376
|
1145 { |
4005
|
1146 if (! (evaluating_function_body && Vsilent_functions)) |
|
1147 { |
|
1148 bool pad_after = print_name_tag (output_buf, name); |
2376
|
1149 |
4005
|
1150 print (output_buf); |
2376
|
1151 |
4005
|
1152 if (print_padding && pad_after) |
|
1153 newline (output_buf); |
|
1154 } |
2376
|
1155 } |
|
1156 |
|
1157 static void |
3523
|
1158 gripe_indexed_assignment (const std::string& tn1, const std::string& tn2) |
2413
|
1159 { |
2903
|
1160 error ("assignment of `%s' to indexed `%s' not implemented", |
2413
|
1161 tn2.c_str (), tn1.c_str ()); |
|
1162 } |
|
1163 |
|
1164 static void |
3933
|
1165 gripe_assign_conversion_failed (const std::string& tn1, |
|
1166 const std::string& tn2) |
2413
|
1167 { |
2903
|
1168 error ("type conversion for assignment of `%s' to indexed `%s' failed", |
2413
|
1169 tn2.c_str (), tn1.c_str ()); |
|
1170 } |
|
1171 |
3933
|
1172 octave_value |
4247
|
1173 octave_value::numeric_assign (const std::string& type, |
4219
|
1174 const std::list<octave_value_list>& idx, |
2413
|
1175 const octave_value& rhs) |
|
1176 { |
3933
|
1177 octave_value retval; |
2413
|
1178 |
|
1179 int t_lhs = type_id (); |
|
1180 int t_rhs = rhs.type_id (); |
|
1181 |
2880
|
1182 assign_op_fcn f |
3933
|
1183 = octave_value_typeinfo::lookup_assign_op (op_asn_eq, t_lhs, t_rhs); |
|
1184 |
|
1185 bool done = false; |
2413
|
1186 |
|
1187 if (f) |
|
1188 { |
3933
|
1189 f (*this, idx.front (), rhs.get_rep ()); |
2413
|
1190 |
3933
|
1191 done = (! error_state); |
2413
|
1192 } |
3933
|
1193 |
|
1194 if (done) |
|
1195 retval = octave_value (this, count + 1); |
3196
|
1196 else |
|
1197 { |
3933
|
1198 int t_result |
|
1199 = octave_value_typeinfo::lookup_pref_assign_conv (t_lhs, t_rhs); |
|
1200 |
|
1201 if (t_result >= 0) |
|
1202 { |
|
1203 type_conv_fcn cf |
|
1204 = octave_value_typeinfo::lookup_widening_op (t_lhs, t_result); |
|
1205 |
|
1206 if (cf) |
|
1207 { |
|
1208 octave_value *tmp (cf (*this)); |
3196
|
1209 |
3933
|
1210 if (tmp) |
|
1211 { |
|
1212 retval = tmp->subsasgn (type, idx, rhs); |
|
1213 |
|
1214 done = (! error_state); |
|
1215 } |
|
1216 else |
|
1217 gripe_assign_conversion_failed (type_name (), |
|
1218 rhs.type_name ()); |
|
1219 } |
|
1220 else |
|
1221 gripe_indexed_assignment (type_name (), rhs.type_name ()); |
|
1222 } |
|
1223 |
|
1224 if (! (done || error_state)) |
3196
|
1225 { |
3933
|
1226 octave_value tmp_rhs; |
|
1227 type_conv_fcn cf_rhs = rhs.numeric_conversion_function (); |
|
1228 |
|
1229 if (cf_rhs) |
|
1230 { |
|
1231 octave_value *tmp = cf_rhs (rhs.get_rep ()); |
|
1232 |
|
1233 if (tmp) |
|
1234 tmp_rhs = octave_value (tmp); |
|
1235 else |
|
1236 { |
|
1237 gripe_assign_conversion_failed (type_name (), |
|
1238 rhs.type_name ()); |
|
1239 return octave_value (); |
|
1240 } |
|
1241 } |
|
1242 else |
|
1243 tmp_rhs = rhs; |
|
1244 |
|
1245 type_conv_fcn cf_this = numeric_conversion_function (); |
|
1246 |
|
1247 octave_value *tmp_lhs = this; |
3196
|
1248 |
3933
|
1249 if (cf_this) |
|
1250 { |
|
1251 octave_value *tmp = cf_this (*this); |
|
1252 |
|
1253 if (tmp) |
|
1254 tmp_lhs = tmp; |
|
1255 else |
|
1256 { |
|
1257 gripe_assign_conversion_failed (type_name (), |
|
1258 rhs.type_name ()); |
|
1259 return octave_value (); |
|
1260 } |
|
1261 } |
|
1262 |
|
1263 if (cf_this || cf_rhs) |
|
1264 { |
|
1265 retval = tmp_lhs->subsasgn (type, idx, tmp_rhs); |
|
1266 |
|
1267 done = (! error_state); |
|
1268 } |
|
1269 else |
|
1270 gripe_no_conversion (assign_op_as_string (op_asn_eq), |
|
1271 type_name (), rhs.type_name ()); |
3196
|
1272 } |
|
1273 } |
2413
|
1274 |
|
1275 return retval; |
|
1276 } |
|
1277 |
|
1278 static void |
3933
|
1279 gripe_binary_op (const std::string& on, const std::string& tn1, |
|
1280 const std::string& tn2) |
2376
|
1281 { |
2903
|
1282 error ("binary operator `%s' not implemented for `%s' by `%s' operations", |
2376
|
1283 on.c_str (), tn1.c_str (), tn2.c_str ()); |
|
1284 } |
|
1285 |
3203
|
1286 static void |
3523
|
1287 gripe_binary_op_conv (const std::string& on) |
3203
|
1288 { |
|
1289 error ("type conversion failed for binary operator `%s'", on.c_str ()); |
|
1290 } |
|
1291 |
2376
|
1292 octave_value |
3933
|
1293 do_binary_op (octave_value::binary_op op, |
|
1294 const octave_value& v1, const octave_value& v2) |
2376
|
1295 { |
|
1296 octave_value retval; |
|
1297 |
|
1298 int t1 = v1.type_id (); |
|
1299 int t2 = v2.type_id (); |
|
1300 |
2427
|
1301 binary_op_fcn f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
1302 |
|
1303 if (f) |
|
1304 retval = f (*v1.rep, *v2.rep); |
|
1305 else |
|
1306 { |
|
1307 octave_value tv1; |
2427
|
1308 type_conv_fcn cf1 = v1.numeric_conversion_function (); |
2376
|
1309 |
|
1310 if (cf1) |
|
1311 { |
3203
|
1312 octave_value *tmp = cf1 (*v1.rep); |
|
1313 |
|
1314 if (tmp) |
|
1315 { |
|
1316 tv1 = octave_value (tmp); |
|
1317 t1 = tv1.type_id (); |
|
1318 } |
|
1319 else |
|
1320 { |
|
1321 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); |
|
1322 return retval; |
|
1323 } |
2376
|
1324 } |
|
1325 else |
|
1326 tv1 = v1; |
|
1327 |
|
1328 octave_value tv2; |
2427
|
1329 type_conv_fcn cf2 = v2.numeric_conversion_function (); |
2376
|
1330 |
|
1331 if (cf2) |
|
1332 { |
3203
|
1333 octave_value *tmp = cf2 (*v2.rep); |
|
1334 |
|
1335 if (tmp) |
|
1336 { |
|
1337 tv2 = octave_value (tmp); |
|
1338 t2 = tv2.type_id (); |
|
1339 } |
|
1340 else |
|
1341 { |
|
1342 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); |
|
1343 return retval; |
|
1344 } |
2376
|
1345 } |
|
1346 else |
|
1347 tv2 = v2; |
|
1348 |
|
1349 if (cf1 || cf2) |
|
1350 { |
2427
|
1351 binary_op_fcn f |
2376
|
1352 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
|
1353 |
|
1354 if (f) |
|
1355 retval = f (*tv1.rep, *tv2.rep); |
|
1356 else |
|
1357 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
1358 v1.type_name (), v2.type_name ()); |
|
1359 } |
|
1360 else |
|
1361 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
1362 v1.type_name (), v2.type_name ()); |
|
1363 } |
|
1364 |
|
1365 return retval; |
|
1366 } |
|
1367 |
3933
|
1368 void |
|
1369 octave_value::print_info (std::ostream& os, const std::string& prefix) const |
|
1370 { |
|
1371 os << prefix << "type_name: " << type_name () << "\n" |
|
1372 << prefix << "count: " << get_count () << "\n" |
|
1373 << prefix << "rep info: "; |
|
1374 |
|
1375 rep->print_info (os, prefix + " "); |
|
1376 } |
|
1377 |
3203
|
1378 static void |
3523
|
1379 gripe_unary_op (const std::string& on, const std::string& tn) |
3203
|
1380 { |
|
1381 error ("unary operator `%s' not implemented for `%s' operands", |
|
1382 on.c_str (), tn.c_str ()); |
|
1383 } |
|
1384 |
|
1385 static void |
3523
|
1386 gripe_unary_op_conv (const std::string& on) |
3203
|
1387 { |
|
1388 error ("type conversion failed for unary operator `%s'", on.c_str ()); |
|
1389 } |
|
1390 |
|
1391 octave_value |
|
1392 do_unary_op (octave_value::unary_op op, const octave_value& v) |
|
1393 { |
|
1394 octave_value retval; |
|
1395 |
|
1396 int t = v.type_id (); |
|
1397 |
|
1398 unary_op_fcn f = octave_value_typeinfo::lookup_unary_op (op, t); |
|
1399 |
|
1400 if (f) |
|
1401 retval = f (*v.rep); |
|
1402 else |
|
1403 { |
|
1404 octave_value tv; |
|
1405 type_conv_fcn cf = v.numeric_conversion_function (); |
|
1406 |
|
1407 if (cf) |
|
1408 { |
|
1409 octave_value *tmp = cf (*v.rep); |
|
1410 |
|
1411 if (tmp) |
|
1412 { |
|
1413 tv = octave_value (tmp); |
|
1414 t = tv.type_id (); |
|
1415 |
|
1416 unary_op_fcn f = octave_value_typeinfo::lookup_unary_op (op, t); |
|
1417 |
|
1418 if (f) |
|
1419 retval = f (*tv.rep); |
|
1420 else |
|
1421 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1422 v.type_name ()); |
|
1423 } |
|
1424 else |
|
1425 gripe_unary_op_conv (octave_value::unary_op_as_string (op)); |
|
1426 } |
|
1427 else |
|
1428 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1429 v.type_name ()); |
|
1430 } |
|
1431 |
|
1432 return retval; |
|
1433 } |
|
1434 |
|
1435 static void |
3933
|
1436 gripe_unary_op_conversion_failed (const std::string& op, |
|
1437 const std::string& tn) |
3203
|
1438 { |
|
1439 error ("operator %s: type conversion for `%s' failed", |
|
1440 op.c_str (), tn.c_str ()); |
|
1441 } |
|
1442 |
3933
|
1443 const octave_value& |
|
1444 octave_value::do_non_const_unary_op (unary_op op) |
3203
|
1445 { |
|
1446 octave_value retval; |
|
1447 |
|
1448 int t = type_id (); |
|
1449 |
|
1450 non_const_unary_op_fcn f |
|
1451 = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
|
1452 |
|
1453 if (f) |
|
1454 { |
|
1455 make_unique (); |
|
1456 |
|
1457 f (*rep); |
|
1458 } |
|
1459 else |
|
1460 { |
|
1461 type_conv_fcn cf = numeric_conversion_function (); |
|
1462 |
|
1463 if (cf) |
|
1464 { |
|
1465 octave_value *tmp = cf (*rep); |
|
1466 |
|
1467 if (tmp) |
|
1468 { |
|
1469 octave_value *old_rep = rep; |
|
1470 rep = tmp; |
|
1471 rep->count = 1; |
|
1472 |
|
1473 t = type_id (); |
|
1474 |
|
1475 f = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
|
1476 |
|
1477 if (f) |
|
1478 { |
|
1479 f (*rep); |
|
1480 |
|
1481 if (old_rep && --old_rep->count == 0) |
|
1482 delete old_rep; |
|
1483 } |
|
1484 else |
|
1485 { |
|
1486 if (old_rep) |
|
1487 { |
|
1488 if (--rep->count == 0) |
|
1489 delete rep; |
|
1490 |
|
1491 rep = old_rep; |
|
1492 } |
|
1493 |
|
1494 gripe_unary_op (octave_value::unary_op_as_string (op), |
|
1495 type_name ()); |
|
1496 } |
|
1497 } |
|
1498 else |
|
1499 gripe_unary_op_conversion_failed |
|
1500 (octave_value::unary_op_as_string (op), type_name ()); |
|
1501 } |
|
1502 else |
|
1503 gripe_unary_op (octave_value::unary_op_as_string (op), type_name ()); |
|
1504 } |
3933
|
1505 |
|
1506 return *this; |
3203
|
1507 } |
|
1508 |
3933
|
1509 #if 0 |
3205
|
1510 static void |
3933
|
1511 gripe_unary_op_failed_or_no_method (const std::string& on, |
|
1512 const std::string& tn) |
3205
|
1513 { |
|
1514 error ("operator %s: no method, or unable to evaluate for %s operand", |
|
1515 on.c_str (), tn.c_str ()); |
|
1516 } |
3933
|
1517 #endif |
3205
|
1518 |
|
1519 void |
3933
|
1520 octave_value::do_non_const_unary_op (unary_op op, const octave_value_list& idx) |
3205
|
1521 { |
3933
|
1522 abort (); |
|
1523 } |
|
1524 |
|
1525 octave_value |
4247
|
1526 octave_value::do_non_const_unary_op (unary_op op, const std::string& type, |
4219
|
1527 const std::list<octave_value_list>& idx) |
3933
|
1528 { |
|
1529 octave_value retval; |
|
1530 |
|
1531 if (idx.empty ()) |
|
1532 { |
|
1533 do_non_const_unary_op (op); |
3205
|
1534 |
3933
|
1535 retval = *this; |
|
1536 } |
|
1537 else |
|
1538 { |
|
1539 // XXX FIXME XXX -- only do the following stuff if we can't find a |
|
1540 // specific function to call to handle the op= operation for the |
|
1541 // types we have. |
3205
|
1542 |
3933
|
1543 assign_op assop = unary_op_to_assign_op (op); |
|
1544 |
|
1545 retval = assign (assop, type, idx, 1.0); |
|
1546 } |
|
1547 |
|
1548 return retval; |
3205
|
1549 } |
|
1550 |
2903
|
1551 // Current indentation. |
|
1552 int octave_value::curr_print_indent_level = 0; |
|
1553 |
3018
|
1554 // TRUE means we are at the beginning of a line. |
2903
|
1555 bool octave_value::beginning_of_line = true; |
|
1556 |
|
1557 // Each print() function should call this before printing anything. |
|
1558 // |
|
1559 // This doesn't need to be fast, but isn't there a better way? |
|
1560 |
|
1561 void |
3523
|
1562 octave_value::indent (std::ostream& os) const |
2903
|
1563 { |
|
1564 assert (curr_print_indent_level >= 0); |
|
1565 |
|
1566 if (beginning_of_line) |
|
1567 { |
|
1568 // XXX FIXME XXX -- do we need this? |
|
1569 // os << prefix; |
|
1570 |
|
1571 for (int i = 0; i < curr_print_indent_level; i++) |
|
1572 os << " "; |
|
1573 |
|
1574 beginning_of_line = false; |
|
1575 } |
|
1576 } |
|
1577 |
|
1578 // All print() functions should use this to print new lines. |
|
1579 |
|
1580 void |
3523
|
1581 octave_value::newline (std::ostream& os) const |
2903
|
1582 { |
|
1583 os << "\n"; |
|
1584 |
|
1585 beginning_of_line = true; |
|
1586 } |
|
1587 |
|
1588 // For ressetting print state. |
|
1589 |
|
1590 void |
|
1591 octave_value::reset (void) const |
|
1592 { |
|
1593 beginning_of_line = true; |
|
1594 curr_print_indent_level = 0; |
|
1595 } |
|
1596 |
3205
|
1597 octave_value::assign_op |
|
1598 octave_value::unary_op_to_assign_op (unary_op op) |
|
1599 { |
|
1600 assign_op binop = unknown_assign_op; |
|
1601 |
|
1602 switch (op) |
|
1603 { |
3533
|
1604 case op_incr: |
|
1605 binop = op_add_eq; |
3205
|
1606 break; |
|
1607 |
3533
|
1608 case op_decr: |
|
1609 binop = op_sub_eq; |
3205
|
1610 break; |
|
1611 |
|
1612 default: |
|
1613 { |
3523
|
1614 std::string on = unary_op_as_string (op); |
3205
|
1615 error ("operator %s: no assign operator found", on.c_str ()); |
|
1616 } |
|
1617 } |
|
1618 |
|
1619 return binop; |
|
1620 } |
|
1621 |
3204
|
1622 octave_value::binary_op |
|
1623 octave_value::op_eq_to_binary_op (assign_op op) |
|
1624 { |
|
1625 binary_op binop = unknown_binary_op; |
|
1626 |
|
1627 switch (op) |
|
1628 { |
3533
|
1629 case op_add_eq: |
|
1630 binop = op_add; |
3204
|
1631 break; |
|
1632 |
3533
|
1633 case op_sub_eq: |
|
1634 binop = op_sub; |
3204
|
1635 break; |
|
1636 |
3533
|
1637 case op_mul_eq: |
|
1638 binop = op_mul; |
3204
|
1639 break; |
|
1640 |
3533
|
1641 case op_div_eq: |
|
1642 binop = op_div; |
3204
|
1643 break; |
|
1644 |
3533
|
1645 case op_ldiv_eq: |
|
1646 binop = op_ldiv; |
3204
|
1647 break; |
|
1648 |
4018
|
1649 case op_pow_eq: |
|
1650 binop = op_pow; |
|
1651 break; |
|
1652 |
3533
|
1653 case op_lshift_eq: |
|
1654 binop = op_lshift; |
3204
|
1655 break; |
|
1656 |
3533
|
1657 case op_rshift_eq: |
|
1658 binop = op_rshift; |
3204
|
1659 break; |
|
1660 |
3533
|
1661 case op_el_mul_eq: |
|
1662 binop = op_el_mul; |
3204
|
1663 break; |
|
1664 |
3533
|
1665 case op_el_div_eq: |
|
1666 binop = op_el_div; |
3204
|
1667 break; |
|
1668 |
3533
|
1669 case op_el_ldiv_eq: |
|
1670 binop = op_el_ldiv; |
3204
|
1671 break; |
|
1672 |
4018
|
1673 case op_el_pow_eq: |
|
1674 binop = op_el_pow; |
|
1675 break; |
|
1676 |
3533
|
1677 case op_el_and_eq: |
|
1678 binop = op_el_and; |
3204
|
1679 break; |
|
1680 |
3533
|
1681 case op_el_or_eq: |
|
1682 binop = op_el_or; |
3204
|
1683 break; |
|
1684 |
|
1685 default: |
|
1686 { |
3523
|
1687 std::string on = assign_op_as_string (op); |
3204
|
1688 error ("operator %s: no binary operator found", on.c_str ()); |
|
1689 } |
|
1690 } |
|
1691 |
|
1692 return binop; |
|
1693 } |
|
1694 |
3933
|
1695 octave_value |
|
1696 octave_value::empty_conv (const std::string& type, const octave_value& rhs) |
|
1697 { |
|
1698 octave_value retval; |
|
1699 |
|
1700 if (type.length () > 0) |
|
1701 { |
|
1702 switch (type[0]) |
|
1703 { |
|
1704 case '(': |
|
1705 { |
|
1706 if (type.length () > 1 && type[1] == '.') |
|
1707 retval = Octave_map (); |
|
1708 else |
|
1709 retval = octave_value (rhs.empty_clone ()); |
|
1710 } |
|
1711 break; |
|
1712 |
|
1713 case '{': |
|
1714 retval = Cell (); |
|
1715 break; |
|
1716 |
|
1717 case '.': |
|
1718 retval = Octave_map (); |
|
1719 break; |
|
1720 |
|
1721 default: |
|
1722 panic_impossible (); |
|
1723 } |
|
1724 } |
|
1725 else |
|
1726 retval = octave_value (rhs.empty_clone ()); |
|
1727 |
|
1728 return retval; |
|
1729 } |
|
1730 |
2376
|
1731 void |
|
1732 install_types (void) |
|
1733 { |
|
1734 octave_base_value::register_type (); |
3928
|
1735 octave_cell::register_type (); |
2376
|
1736 octave_scalar::register_type (); |
|
1737 octave_complex::register_type (); |
|
1738 octave_matrix::register_type (); |
|
1739 octave_complex_matrix::register_type (); |
|
1740 octave_range::register_type (); |
2825
|
1741 octave_bool::register_type (); |
|
1742 octave_bool_matrix::register_type (); |
2376
|
1743 octave_char_matrix::register_type (); |
|
1744 octave_char_matrix_str::register_type (); |
|
1745 octave_struct::register_type (); |
2903
|
1746 octave_file::register_type (); |
2880
|
1747 octave_list::register_type (); |
3977
|
1748 octave_cs_list::register_type (); |
2376
|
1749 octave_all_va_args::register_type (); |
|
1750 octave_magic_colon::register_type (); |
2974
|
1751 octave_builtin::register_type (); |
|
1752 octave_mapper::register_type (); |
|
1753 octave_user_function::register_type (); |
2376
|
1754 } |
|
1755 |
|
1756 static int |
4455
|
1757 warn_fortran_indexing (void) |
2376
|
1758 { |
4455
|
1759 Vwarn_fortran_indexing = check_preference ("warn_fortran_indexing"); |
2376
|
1760 |
4455
|
1761 liboctave_wfi_flag = Vwarn_fortran_indexing; |
2376
|
1762 |
|
1763 return 0; |
|
1764 } |
|
1765 |
|
1766 static int |
4452
|
1767 warn_imag_to_real (void) |
4257
|
1768 { |
4452
|
1769 Vwarn_imag_to_real = check_preference ("warn_imag_to_real"); |
4257
|
1770 |
|
1771 return 0; |
|
1772 } |
|
1773 |
|
1774 static int |
4452
|
1775 warn_num_to_str (void) |
2376
|
1776 { |
4452
|
1777 Vwarn_num_to_str = check_preference ("warn_num_to_str"); |
2376
|
1778 |
|
1779 return 0; |
|
1780 } |
|
1781 |
|
1782 static int |
4452
|
1783 warn_str_to_num (void) |
2376
|
1784 { |
4452
|
1785 Vwarn_str_to_num = check_preference ("warn_str_to_num"); |
2376
|
1786 |
|
1787 return 0; |
|
1788 } |
|
1789 |
|
1790 static int |
|
1791 print_answer_id_name (void) |
|
1792 { |
|
1793 Vprint_answer_id_name = check_preference ("print_answer_id_name"); |
|
1794 |
|
1795 return 0; |
|
1796 } |
|
1797 |
|
1798 static int |
|
1799 propagate_empty_matrices (void) |
|
1800 { |
|
1801 Vpropagate_empty_matrices = check_preference ("propagate_empty_matrices"); |
|
1802 |
|
1803 return 0; |
|
1804 } |
|
1805 |
|
1806 static int |
|
1807 resize_on_range_error (void) |
|
1808 { |
|
1809 Vresize_on_range_error = check_preference ("resize_on_range_error"); |
|
1810 |
|
1811 liboctave_rre_flag = Vresize_on_range_error; |
|
1812 |
|
1813 return 0; |
|
1814 } |
|
1815 |
|
1816 static int |
4005
|
1817 silent_functions (void) |
|
1818 { |
|
1819 Vsilent_functions = check_preference ("silent_functions"); |
|
1820 |
|
1821 return 0; |
|
1822 } |
|
1823 |
|
1824 static int |
2376
|
1825 struct_levels_to_print (void) |
|
1826 { |
|
1827 double val; |
|
1828 if (builtin_real_scalar_variable ("struct_levels_to_print", val) |
|
1829 && ! xisnan (val)) |
|
1830 { |
|
1831 int ival = NINT (val); |
3961
|
1832 if (ival == val) |
2376
|
1833 { |
|
1834 Vstruct_levels_to_print = ival; |
|
1835 return 0; |
|
1836 } |
|
1837 } |
|
1838 gripe_invalid_value_specified ("struct_levels_to_print"); |
|
1839 return -1; |
|
1840 } |
|
1841 |
|
1842 static int |
|
1843 warn_divide_by_zero (void) |
|
1844 { |
|
1845 Vwarn_divide_by_zero = check_preference ("warn_divide_by_zero"); |
|
1846 |
|
1847 return 0; |
|
1848 } |
|
1849 |
|
1850 void |
2909
|
1851 symbols_of_ov (void) |
2376
|
1852 { |
4233
|
1853 DEFVAR (print_answer_id_name, true, print_answer_id_name, |
3372
|
1854 "-*- texinfo -*-\n\ |
|
1855 @defvr {Built-in Variable} print_answer_id_name\n\ |
|
1856 If the value of @code{print_answer_id_name} is nonzero, variable\n\ |
|
1857 names are printed along with the result. Otherwise, only the result\n\ |
|
1858 values are printed. The default value is 1.\n\ |
|
1859 @end defvr"); |
2376
|
1860 |
4233
|
1861 DEFVAR (propagate_empty_matrices, true, propagate_empty_matrices, |
3321
|
1862 "-*- texinfo -*-\n\ |
|
1863 @defvr {Built-in Variable} propagate_empty_matrices\n\ |
|
1864 If the value of @code{propagate_empty_matrices} is nonzero,\n\ |
|
1865 functions like @code{inverse} and @code{svd} will return an empty matrix\n\ |
|
1866 if they are given one as an argument. The default value is 1.\n\ |
3333
|
1867 @end defvr"); |
2376
|
1868 |
4233
|
1869 DEFVAR (resize_on_range_error, true, resize_on_range_error, |
3371
|
1870 "-*- texinfo -*-\n\ |
|
1871 @defvr {Built-in Variable} resize_on_range_error\n\ |
|
1872 If the value of @code{resize_on_range_error} is nonzero, expressions\n\ |
|
1873 like\n\ |
|
1874 \n\ |
|
1875 @example\n\ |
|
1876 for i = 1:10\n\ |
|
1877 a (i) = sqrt (i);\n\ |
|
1878 endfor\n\ |
|
1879 @end example\n\ |
|
1880 \n\ |
|
1881 @noindent\n\ |
|
1882 (for @code{a} previously undefined) result in the variable @code{a}\n\ |
|
1883 being resized to be just large enough to hold the new value. New\n\ |
|
1884 elements that have not been given a value are set to zero. If the value\n\ |
|
1885 of @code{resize_on_range_error} is 0, an error message is printed and\n\ |
|
1886 control is returned to the top level. The default value is 1.\n\ |
|
1887 @end defvr"); |
2376
|
1888 |
4233
|
1889 DEFVAR (silent_functions, false, silent_functions, |
4005
|
1890 "-*- texinfo -*-\n\ |
|
1891 @defvr {Built-in Variable} silent_functions\n\ |
|
1892 If the value of @code{silent_functions} is nonzero, internal output\n\ |
|
1893 from a function is suppressed. Otherwise, the results of expressions\n\ |
|
1894 within a function body that are not terminated with a semicolon will\n\ |
|
1895 have their values printed. The default value is 0.\n\ |
|
1896 \n\ |
|
1897 For example, if the function\n\ |
|
1898 \n\ |
|
1899 @example\n\ |
|
1900 function f ()\n\ |
|
1901 2 + 2\n\ |
|
1902 endfunction\n\ |
|
1903 @end example\n\ |
|
1904 \n\ |
|
1905 @noindent\n\ |
|
1906 is executed, Octave will either print @samp{ans = 4} or nothing\n\ |
|
1907 depending on the value of @code{silent_functions}.\n\ |
|
1908 @end defvr"); |
|
1909 |
3258
|
1910 DEFVAR (struct_levels_to_print, 2.0, struct_levels_to_print, |
3361
|
1911 "-*- texinfo -*-\n\ |
|
1912 @defvr {Built-in Variable} struct_levels_to_print\n\ |
|
1913 You can tell Octave how many structure levels to display by setting the\n\ |
|
1914 built-in variable @code{struct_levels_to_print}. The default value is 2.\n\ |
3363
|
1915 @end defvr"); |
2376
|
1916 |
4233
|
1917 DEFVAR (warn_divide_by_zero, true, warn_divide_by_zero, |
3371
|
1918 "-*- texinfo -*-\n\ |
|
1919 @defvr {Built-in Variable} warn_divide_by_zero\n\ |
|
1920 If the value of @code{warn_divide_by_zero} is nonzero, a warning\n\ |
|
1921 is issued when Octave encounters a division by zero. If the value is\n\ |
|
1922 0, the warning is omitted. The default value is 1.\n\ |
|
1923 @end defvr"); |
4451
|
1924 |
4455
|
1925 DEFVAR (warn_fortran_indexing, false, warn_fortran_indexing, |
|
1926 "-*- texinfo -*-\n\ |
|
1927 @defvr {Built-in Variable} warn_fortran_indexing\n\ |
|
1928 If the value of @code{warn_fortran_indexing} is nonzero, a warning is\n\ |
|
1929 printed for expressions which select elements of a two-dimensional matrix\n\ |
|
1930 using a single index. The default value is 0.\n\ |
|
1931 @end defvr"); |
|
1932 |
4451
|
1933 DEFVAR (warn_imag_to_real, false, warn_imag_to_real, |
|
1934 "-*- texinfo -*-\n\ |
|
1935 @defvr {Built-in Variable} warn_imag_to_real\n\ |
|
1936 If the value of @code{warn_imag_to_real} is nonzero, a warning is\n\ |
|
1937 printed for implicit conversions of complex numbers to real numbers.\n\ |
|
1938 The default value is 0.\n\ |
|
1939 @end defvr"); |
4452
|
1940 |
|
1941 DEFVAR (warn_num_to_str, true, warn_num_to_str, |
|
1942 "-*- texinfo -*-\n\ |
|
1943 @defvr {Built-in Variable} warn_num_to_str\n\ |
|
1944 If the value of @code{warn_num_to_str} is nonzero, a warning is\n\ |
|
1945 printed for implicit conversions of numbers to their ASCII character\n\ |
|
1946 equivalents when strings are constructed using a mixture of strings and\n\ |
|
1947 numbers in matrix notation. For example,\n\ |
|
1948 \n\ |
|
1949 @example\n\ |
|
1950 @group\n\ |
|
1951 [ \"f\", 111, 111 ]\n\ |
|
1952 @result{} \"foo\"\n\ |
|
1953 @end group\n\ |
|
1954 @end example\n\ |
|
1955 elicits a warning if @code{warn_num_to_str} is nonzero. The default\n\ |
|
1956 value is 1.\n\ |
|
1957 @end defvr"); |
|
1958 |
|
1959 DEFVAR (warn_str_to_num, false, warn_str_to_num, |
|
1960 "-*- texinfo -*-\n\ |
|
1961 @defvr {Built-in Variable} warn_str_to_num\n\ |
|
1962 If the value of @code{warn_str_to_num} is nonzero, a warning is printed\n\ |
|
1963 for implicit conversions of strings to their numeric ASCII equivalents.\n\ |
|
1964 For example,\n\ |
|
1965 @example\n\ |
|
1966 @group\n\ |
|
1967 \"abc\" + 0\n\ |
|
1968 @result{} 97 98 99\n\ |
|
1969 @end group\n\ |
|
1970 @end example\n\ |
|
1971 elicits a warning if @code{warn_str_to_num} is nonzero. The default\n\ |
|
1972 value is 0.\n\ |
|
1973 @end defvr"); |
2376
|
1974 } |
|
1975 |
|
1976 /* |
|
1977 ;;; Local Variables: *** |
|
1978 ;;; mode: C++ *** |
|
1979 ;;; End: *** |
|
1980 */ |