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