Mercurial > octave
annotate src/ov-builtin.cc @ 10887:f10d0bc8f9cc
make isargout available to DEFUNs
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 13 Aug 2010 10:14:52 +0200 |
parents | 57a59eae83cc |
children | fd0a3ac60b0e |
rev | line source |
---|---|
2974 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, |
8920 | 4 2006, 2007, 2008, 2009 John W. Eaton |
2974 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2974 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2974 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "error.h" | |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7487
diff
changeset
|
29 #include "gripes.h" |
2974 | 30 #include "oct-obj.h" |
31 #include "ov-builtin.h" | |
32 #include "ov.h" | |
4748 | 33 #include "toplev.h" |
34 #include "unwind-prot.h" | |
2974 | 35 |
3219 | 36 DEFINE_OCTAVE_ALLOCATOR (octave_builtin); |
2974 | 37 |
4612 | 38 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_builtin, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
39 "built-in function", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
40 "built-in function"); |
2974 | 41 |
42 octave_value_list | |
4247 | 43 octave_builtin::subsref (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
44 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
45 int nargout) |
3933 | 46 { |
10887
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
47 return octave_builtin::subsref (type, idx, nargout, 0); |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
48 } |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
49 |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
50 octave_value_list |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
51 octave_builtin::subsref (const std::string& type, |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
52 const std::list<octave_value_list>& idx, |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
53 int nargout, const std::list<octave_lvalue>* lvalue_list) |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
54 { |
3933 | 55 octave_value_list retval; |
56 | |
57 switch (type[0]) | |
58 { | |
59 case '(': | |
5154 | 60 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
61 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5154 | 62 |
10887
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
63 retval = do_multi_index_op (tmp_nargout, idx.front (), |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
64 idx.size () == 1 ? lvalue_list : 0); |
5154 | 65 } |
3933 | 66 break; |
67 | |
68 case '{': | |
69 case '.': | |
70 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
71 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
72 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
3933 | 73 } |
74 break; | |
75 | |
76 default: | |
77 panic_impossible (); | |
78 } | |
79 | |
5775 | 80 // FIXME -- perhaps there should be an |
4059 | 81 // octave_value_list::next_subsref member function? See also |
82 // octave_user_function::subsref. | |
83 // | |
5775 | 84 // FIXME -- Note that if a function call returns multiple |
4059 | 85 // values, and there is further indexing to perform, then we are |
86 // ignoring all but the first value. Is this really what we want to | |
87 // do? If it is not, then what should happen for stat("file").size, | |
88 // for exmaple? | |
3933 | 89 |
4219 | 90 if (idx.size () > 1) |
4994 | 91 retval = retval(0).next_subsref (nargout, type, idx); |
4059 | 92 |
93 return retval; | |
3933 | 94 } |
95 | |
96 octave_value_list | |
3544 | 97 octave_builtin::do_multi_index_op (int nargout, const octave_value_list& args) |
2974 | 98 { |
10887
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
99 return octave_builtin::do_multi_index_op (nargout, args, 0); |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
100 } |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
101 |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
102 octave_value_list |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
103 octave_builtin::do_multi_index_op (int nargout, const octave_value_list& args, |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
104 const std::list<octave_lvalue> *lvalue_list) |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
105 { |
2974 | 106 octave_value_list retval; |
107 | |
108 if (error_state) | |
109 return retval; | |
110 | |
5864 | 111 if (args.has_magic_colon ()) |
2974 | 112 ::error ("invalid use of colon in function argument list"); |
113 else | |
4748 | 114 { |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
115 unwind_protect frame; |
4987 | 116 |
5743 | 117 octave_call_stack::push (this); |
4975 | 118 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
119 frame.add_fcn (octave_call_stack::pop); |
4748 | 120 |
10887
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
121 if (lvalue_list || curr_lvalue_list) |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
122 { |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
123 frame.protect_var (curr_lvalue_list); |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
124 curr_lvalue_list = lvalue_list; |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
125 } |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
126 |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
127 try |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
128 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
129 retval = (*f) (args, nargout); |
10184
b39bd23019eb
partially revert a668fbd32e34
Jaroslav Hajek <highegg@gmail.com>
parents:
10181
diff
changeset
|
130 // Do not allow null values to be returned from functions. |
b39bd23019eb
partially revert a668fbd32e34
Jaroslav Hajek <highegg@gmail.com>
parents:
10181
diff
changeset
|
131 // FIXME -- perhaps true builtins should be allowed? |
b39bd23019eb
partially revert a668fbd32e34
Jaroslav Hajek <highegg@gmail.com>
parents:
10181
diff
changeset
|
132 retval.make_storable_values (); |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
133 // Fix the case of a single undefined value. |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
134 // This happens when a compiled function uses |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
135 // octave_value retval; |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
136 // instead of |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
137 // octave_value_list retval; |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
138 // the idiom is very common, so we solve that here. |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
139 if (retval.length () == 1 && retval.xelem (0).is_undefined ()) |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10160
diff
changeset
|
140 retval.clear (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
141 } |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
142 catch (octave_execution_exception) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
143 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
144 gripe_library_execution_error (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10184
diff
changeset
|
145 } |
4748 | 146 } |
2974 | 147 |
148 return retval; | |
149 } | |
10887
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
150 |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
151 |
f10d0bc8f9cc
make isargout available to DEFUNs
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
152 const std::list<octave_lvalue> *octave_builtin::curr_lvalue_list = 0; |