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