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