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