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