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