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