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