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