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