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