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