Mercurial > octave-dspies
annotate src/ov-usr-fcn.cc @ 8011:3100283874d7
improve backtrace error messages
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 05 Aug 2008 23:09:32 -0400 |
parents | 0d607e8dbbfa |
children | b3e667f1ab4c |
rev | line source |
---|---|
2974 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, |
4 2006, 2007 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 "str-vec.h" | |
29 | |
30 #include <defaults.h> | |
3974 | 31 #include "Cell.h" |
2974 | 32 #include "defun.h" |
33 #include "error.h" | |
34 #include "input.h" | |
35 #include "oct-obj.h" | |
36 #include "ov-usr-fcn.h" | |
37 #include "ov.h" | |
38 #include "pager.h" | |
2985 | 39 #include "pt-jump.h" |
2974 | 40 #include "pt-misc.h" |
41 #include "pt-pr-code.h" | |
2982 | 42 #include "pt-stmt.h" |
2974 | 43 #include "pt-walk.h" |
44 #include "symtab.h" | |
45 #include "toplev.h" | |
46 #include "unwind-prot.h" | |
47 #include "utils.h" | |
3489 | 48 #include "parse.h" |
2974 | 49 #include "variables.h" |
50 | |
3131 | 51 // Maximum nesting level for functions called recursively. |
5794 | 52 static int Vmax_recursion_depth = 256; |
3131 | 53 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
54 // User defined scripts. |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
55 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
56 DEFINE_OCTAVE_ALLOCATOR (octave_user_script); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
57 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_script, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
59 "user-defined script", |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
60 "user-defined script"); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
61 |
7731
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
62 octave_user_script::octave_user_script (void) |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
63 : octave_user_code (), cmd_list (0), file_name (), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
64 t_parsed (static_cast<time_t> (0)), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
65 t_checked (static_cast<time_t> (0)), |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
66 call_depth (-1) |
7731
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
67 { } |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
68 |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
69 octave_user_script::octave_user_script (const std::string& fnm, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
70 const std::string& nm, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
71 tree_statement_list *cmds, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
72 const std::string& ds) |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
73 : octave_user_code (nm, ds), cmd_list (cmds), file_name (fnm), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
74 t_parsed (static_cast<time_t> (0)), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
75 t_checked (static_cast<time_t> (0)), |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
76 call_depth (-1) |
7736 | 77 { |
78 if (cmd_list) | |
79 cmd_list->mark_as_script_body (); | |
80 } | |
7731
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
81 |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
82 octave_user_script::octave_user_script (const std::string& fnm, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
83 const std::string& nm, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
84 const std::string& ds) |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
85 : octave_user_code (nm, ds), cmd_list (0), file_name (fnm), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
86 t_parsed (static_cast<time_t> (0)), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
87 t_checked (static_cast<time_t> (0)), |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
88 call_depth (-1) |
7731
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
89 { } |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
90 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
91 octave_user_script::~octave_user_script (void) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
92 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
93 delete cmd_list; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
94 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
95 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
96 octave_value_list |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
97 octave_user_script::subsref (const std::string&, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
98 const std::list<octave_value_list>&, int) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
99 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
100 octave_value_list retval; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
101 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
102 ::error ("invalid use of script in index expression"); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
103 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
104 return retval; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
105 } |
7336 | 106 |
107 octave_value_list | |
108 octave_user_script::do_multi_index_op (int nargout, | |
109 const octave_value_list& args) | |
110 { | |
111 octave_value_list retval; | |
112 | |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
113 unwind_protect::begin_frame ("user_script_eval"); |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
114 |
7336 | 115 if (! error_state) |
116 { | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
117 if (args.length () == 0 && nargout == 0) |
7336 | 118 { |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
119 if (cmd_list) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
120 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
121 unwind_protect_int (call_depth); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
122 call_depth++; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
123 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
124 if (call_depth < Vmax_recursion_depth) |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
125 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
126 octave_call_stack::push (this); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
127 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
128 unwind_protect::add (octave_call_stack::unwind_pop, 0); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
129 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
130 cmd_list->eval (); |
7336 | 131 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
132 if (tree_return_command::returning) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
133 tree_return_command::returning = 0; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
134 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
135 if (tree_break_command::breaking) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
136 tree_break_command::breaking--; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
137 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
138 else |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
139 ::error ("max_recursion_limit exceeded"); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
140 } |
7336 | 141 } |
142 else | |
143 error ("invalid call to script"); | |
144 } | |
145 | |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
146 unwind_protect::run_frame ("user_script_eval"); |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
147 |
7336 | 148 return retval; |
149 } | |
150 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
151 void |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
152 octave_user_script::accept (tree_walker& tw) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
153 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
154 tw.visit_octave_user_script (*this); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
155 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
156 |
2974 | 157 // User defined functions. |
158 | |
3219 | 159 DEFINE_OCTAVE_ALLOCATOR (octave_user_function); |
2974 | 160 |
3219 | 161 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_function, |
4612 | 162 "user-defined function", |
3219 | 163 "user-defined function"); |
2974 | 164 |
165 // Ugh. This really needs to be simplified (code/data? | |
166 // extrinsic/intrinsic state?). | |
167 | |
168 octave_user_function::octave_user_function | |
7336 | 169 (symbol_table::scope_id sid, tree_parameter_list *pl, |
170 tree_parameter_list *rl, tree_statement_list *cl) | |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
171 : octave_user_code (std::string (), std::string ()), |
2974 | 172 param_list (pl), ret_list (rl), cmd_list (cl), |
7336 | 173 lead_comm (), trail_comm (), file_name (), |
6323 | 174 parent_name (), t_parsed (static_cast<time_t> (0)), |
3255 | 175 t_checked (static_cast<time_t> (0)), |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
176 system_fcn_file (false), call_depth (-1), |
7589
90fb25a5a3fa
minor cleanup in octave_user_function constructor
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
177 num_named_args (param_list ? param_list->length () : 0), |
7336 | 178 nested_function (false), inline_function (false), |
179 class_constructor (false), class_method (false), xdispatch_class (), | |
7968
0d607e8dbbfa
eliminate curr_parent_function; fix subfunction lookup
John W. Eaton <jwe@octave.org>
parents:
7901
diff
changeset
|
180 args_passed (), num_args_passed (0), parent_scope (-1), local_scope (sid) |
7736 | 181 { |
182 if (cmd_list) | |
183 cmd_list->mark_as_function_body (); | |
184 } | |
2974 | 185 |
186 octave_user_function::~octave_user_function (void) | |
187 { | |
188 delete param_list; | |
189 delete ret_list; | |
190 delete cmd_list; | |
3665 | 191 delete lead_comm; |
192 delete trail_comm; | |
7336 | 193 |
194 symbol_table::erase_scope (local_scope); | |
2974 | 195 } |
196 | |
197 octave_user_function * | |
198 octave_user_function::define_ret_list (tree_parameter_list *t) | |
199 { | |
200 ret_list = t; | |
201 | |
202 return this; | |
203 } | |
204 | |
205 void | |
4343 | 206 octave_user_function::stash_fcn_file_name (const std::string& nm) |
2974 | 207 { |
4343 | 208 file_name = nm; |
2974 | 209 } |
210 | |
211 void | |
212 octave_user_function::mark_as_system_fcn_file (void) | |
213 { | |
214 if (! file_name.empty ()) | |
215 { | |
216 // We really should stash the whole path to the file we found, | |
217 // when we looked it up, to avoid possible race conditions... | |
5775 | 218 // FIXME |
2974 | 219 // |
220 // We probably also don't need to get the library directory | |
221 // every time, but since this function is only called when the | |
222 // function file is parsed, it probably doesn't matter that | |
223 // much. | |
224 | |
3523 | 225 std::string ff_name = fcn_file_in_path (file_name); |
2974 | 226 |
3565 | 227 if (Vfcn_file_dir == ff_name.substr (0, Vfcn_file_dir.length ())) |
2974 | 228 system_fcn_file = 1; |
229 } | |
230 else | |
231 system_fcn_file = 0; | |
232 } | |
233 | |
234 bool | |
235 octave_user_function::takes_varargs (void) const | |
236 { | |
237 return (param_list && param_list->takes_varargs ()); | |
238 } | |
239 | |
5848 | 240 bool |
241 octave_user_function::takes_var_return (void) const | |
2974 | 242 { |
5848 | 243 return (ret_list && ret_list->takes_varargs ()); |
2974 | 244 } |
245 | |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
246 void |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
247 octave_user_function::lock_subfunctions (void) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
248 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
249 symbol_table::lock_subfunctions (local_scope); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
250 } |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
251 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
252 void |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
253 octave_user_function::unlock_subfunctions (void) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
254 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
255 symbol_table::unlock_subfunctions (local_scope); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
256 } |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
257 |
2974 | 258 octave_value_list |
259 octave_user_function::octave_all_va_args (void) | |
260 { | |
261 octave_value_list retval; | |
262 | |
3178 | 263 int n = num_args_passed - num_named_args; |
2974 | 264 |
3178 | 265 if (n > 0) |
266 { | |
267 retval.resize (n); | |
268 | |
269 int k = 0; | |
270 for (int i = num_named_args; i < num_args_passed; i++) | |
271 retval(k++) = args_passed(i); | |
272 } | |
2974 | 273 |
274 return retval; | |
275 } | |
276 | |
277 static void | |
3239 | 278 clear_param_list (void *lst) |
279 { | |
280 tree_parameter_list *tmp = static_cast<tree_parameter_list *> (lst); | |
281 | |
282 if (tmp) | |
4219 | 283 tmp->undefine (); |
3239 | 284 } |
285 | |
286 static void | |
3875 | 287 restore_args_passed (void *fcn) |
3239 | 288 { |
289 octave_user_function *tmp = static_cast<octave_user_function *> (fcn); | |
290 | |
291 if (tmp) | |
3875 | 292 tmp->restore_args_passed (); |
3239 | 293 } |
294 | |
2974 | 295 octave_value_list |
4247 | 296 octave_user_function::subsref (const std::string& type, |
4219 | 297 const std::list<octave_value_list>& idx, |
3933 | 298 int nargout) |
299 { | |
300 octave_value_list retval; | |
301 | |
302 switch (type[0]) | |
303 { | |
304 case '(': | |
5154 | 305 { |
5155 | 306 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5154 | 307 |
308 retval = do_multi_index_op (tmp_nargout, idx.front ()); | |
309 } | |
3933 | 310 break; |
311 | |
312 case '{': | |
313 case '.': | |
314 { | |
315 std::string nm = type_name (); | |
316 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
317 } | |
318 break; | |
319 | |
320 default: | |
321 panic_impossible (); | |
322 } | |
323 | |
5775 | 324 // FIXME -- perhaps there should be an |
4059 | 325 // octave_value_list::next_subsref member function? See also |
326 // octave_builtin::subsref. | |
3933 | 327 |
4219 | 328 if (idx.size () > 1) |
4994 | 329 retval = retval(0).next_subsref (nargout, type, idx); |
4059 | 330 |
331 return retval; | |
3933 | 332 } |
333 | |
334 octave_value_list | |
3544 | 335 octave_user_function::do_multi_index_op (int nargout, |
336 const octave_value_list& args) | |
2974 | 337 { |
338 octave_value_list retval; | |
339 | |
340 if (error_state) | |
341 return retval; | |
342 | |
343 if (! cmd_list) | |
344 return retval; | |
345 | |
346 int nargin = args.length (); | |
347 | |
4987 | 348 unwind_protect::begin_frame ("user_func_eval"); |
2974 | 349 |
350 unwind_protect_int (call_depth); | |
351 call_depth++; | |
352 | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
353 if (call_depth >= Vmax_recursion_depth) |
3131 | 354 { |
355 ::error ("max_recursion_limit exceeded"); | |
4987 | 356 unwind_protect::run_frame ("user_func_eval"); |
3131 | 357 return retval; |
358 } | |
359 | |
7336 | 360 // Save old and set current symbol table context, for |
361 // eval_undefined_error(). | |
362 | |
363 symbol_table::push_scope (local_scope); | |
364 unwind_protect::add (symbol_table::pop_scope); | |
2974 | 365 |
7901 | 366 octave_call_stack::push (this, local_scope, call_depth); |
367 unwind_protect::add (octave_call_stack::unwind_pop, 0); | |
368 | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
369 if (call_depth > 0) |
2974 | 370 { |
7336 | 371 symbol_table::push_context (); |
2974 | 372 |
7336 | 373 unwind_protect::add (symbol_table::pop_context); |
374 } | |
375 else | |
376 { | |
377 // Force symbols to be undefined again when this function exits. | |
378 unwind_protect::add (symbol_table::clear_variables); | |
379 } | |
2974 | 380 |
3875 | 381 // Save and restore args passed for recursive calls. |
2974 | 382 |
3875 | 383 save_args_passed (args); |
3239 | 384 |
3875 | 385 unwind_protect::add (::restore_args_passed, this); |
3239 | 386 |
2974 | 387 string_vector arg_names = args.name_tags (); |
388 | |
389 unwind_protect_int (num_args_passed); | |
390 num_args_passed = nargin; | |
391 | |
392 if (param_list && ! param_list->varargs_only ()) | |
393 { | |
394 param_list->define_from_arg_vector (args); | |
395 if (error_state) | |
396 goto abort; | |
397 } | |
398 | |
3239 | 399 // Force parameter list to be undefined when this function exits. |
400 // Doing so decrements the reference counts on the values of local | |
401 // variables that are also named function parameters. | |
402 | |
403 unwind_protect::add (clear_param_list, param_list); | |
404 | |
405 // Force return list to be undefined when this function exits. | |
406 // Doing so decrements the reference counts on the values of local | |
407 // variables that are also named values returned by this function. | |
408 | |
409 unwind_protect::add (clear_param_list, ret_list); | |
410 | |
2974 | 411 // The following code is in a separate scope to avoid warnings from |
412 // G++ about `goto abort' crossing the initialization of some | |
413 // variables. | |
414 | |
415 { | |
3974 | 416 bind_automatic_vars (arg_names, nargin, nargout, octave_all_va_args ()); |
2974 | 417 |
418 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); | |
419 | |
420 if (echo_commands) | |
421 print_code_function_header (); | |
422 | |
423 // Evaluate the commands that make up the function. | |
424 | |
3489 | 425 unwind_protect_bool (evaluating_function_body); |
426 evaluating_function_body = true; | |
427 | |
6657 | 428 if (is_inline_function ()) |
429 { | |
430 assert (cmd_list->length () == 1); | |
431 | |
432 retval = cmd_list->eval (false, nargout); | |
433 } | |
434 else | |
435 cmd_list->eval (); | |
2974 | 436 |
437 if (echo_commands) | |
438 print_code_function_trailer (); | |
439 | |
4207 | 440 if (tree_return_command::returning) |
441 tree_return_command::returning = 0; | |
2974 | 442 |
4207 | 443 if (tree_break_command::breaking) |
444 tree_break_command::breaking--; | |
2974 | 445 |
446 if (error_state) | |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
447 goto abort; |
2974 | 448 |
449 // Copy return values out. | |
450 | |
6657 | 451 if (ret_list && ! is_inline_function ()) |
3871 | 452 { |
4748 | 453 ret_list->initialize_undefined_elements (my_name, nargout, Matrix ()); |
3871 | 454 |
5848 | 455 Cell varargout; |
456 | |
6591 | 457 if (ret_list->takes_varargs ()) |
5848 | 458 { |
7336 | 459 octave_value varargout_varval = symbol_table::varval ("varargout"); |
6591 | 460 |
7336 | 461 if (varargout_varval.is_defined ()) |
6591 | 462 { |
7336 | 463 varargout = varargout_varval.cell_value (); |
5848 | 464 |
6591 | 465 if (error_state) |
466 error ("expecting varargout to be a cell array object"); | |
467 } | |
5848 | 468 } |
469 | |
470 if (! error_state) | |
471 retval = ret_list->convert_to_const_vector (varargout); | |
3871 | 472 } |
2974 | 473 } |
474 | |
475 abort: | |
4987 | 476 unwind_protect::run_frame ("user_func_eval"); |
2974 | 477 |
478 return retval; | |
479 } | |
480 | |
481 void | |
482 octave_user_function::accept (tree_walker& tw) | |
483 { | |
484 tw.visit_octave_user_function (*this); | |
485 } | |
486 | |
7336 | 487 #if 0 |
2974 | 488 void |
3933 | 489 octave_user_function::print_symtab_info (std::ostream& os) const |
490 { | |
7336 | 491 symbol_table::print_info (os, local_scope); |
3933 | 492 } |
7336 | 493 #endif |
3933 | 494 |
495 void | |
2974 | 496 octave_user_function::print_code_function_header (void) |
497 { | |
5794 | 498 tree_print_code tpc (octave_stdout, VPS4); |
2974 | 499 |
500 tpc.visit_octave_user_function_header (*this); | |
501 } | |
502 | |
503 void | |
504 octave_user_function::print_code_function_trailer (void) | |
505 { | |
5794 | 506 tree_print_code tpc (octave_stdout, VPS4); |
2974 | 507 |
508 tpc.visit_octave_user_function_trailer (*this); | |
509 } | |
510 | |
511 void | |
512 octave_user_function::bind_automatic_vars | |
3974 | 513 (const string_vector& arg_names, int nargin, int nargout, |
514 const octave_value_list& va_args) | |
2974 | 515 { |
516 if (! arg_names.empty ()) | |
7336 | 517 symbol_table::varref ("argn") = arg_names; |
2974 | 518 |
7336 | 519 symbol_table::varref (".nargin.") = nargin; |
520 symbol_table::varref (".nargout.") = nargout; | |
521 | |
522 symbol_table::mark_hidden (".nargin."); | |
523 symbol_table::mark_hidden (".nargout."); | |
3974 | 524 |
525 if (takes_varargs ()) | |
526 { | |
527 int n = va_args.length (); | |
528 | |
529 Cell varargin (1, n); | |
530 | |
531 for (int i = 0; i < n; i++) | |
532 varargin(0,i) = va_args(i); | |
533 | |
7336 | 534 symbol_table::varref ("varargin") = varargin; |
3974 | 535 } |
2974 | 536 } |
537 | |
4700 | 538 DEFUN (nargin, args, , |
539 "-*- texinfo -*-\n\ | |
540 @deftypefn {Built-in Function} {} nargin ()\n\ | |
541 @deftypefnx {Built-in Function} {} nargin (@var{fcn_name})\n\ | |
542 Within a function, return the number of arguments passed to the function.\n\ | |
543 At the top level, return the number of command line arguments passed to\n\ | |
544 Octave. If called with the optional argument @var{fcn_name}, return the\n\ | |
545 maximum number of arguments the named function can accept, or -1 if the\n\ | |
546 function accepts a variable number of arguments.\n\ | |
5642 | 547 @seealso{nargout, varargin, varargout}\n\ |
4700 | 548 @end deftypefn") |
549 { | |
550 octave_value retval; | |
551 | |
552 int nargin = args.length (); | |
553 | |
554 if (nargin == 1) | |
555 { | |
556 std::string fname = args(0).string_value (); | |
557 | |
558 if (! error_state) | |
559 { | |
7336 | 560 octave_value fcn_val = symbol_table::find_user_function (fname); |
4930 | 561 |
562 octave_user_function *fcn = fcn_val.user_function_value (true); | |
4700 | 563 |
564 if (fcn) | |
565 { | |
566 if (fcn->takes_varargs ()) | |
567 retval = -1; | |
568 else | |
569 { | |
570 tree_parameter_list *param_list = fcn->parameter_list (); | |
571 | |
572 retval = param_list ? param_list->length () : 0; | |
573 } | |
574 } | |
575 else | |
576 error ("nargin: invalid function"); | |
577 } | |
578 else | |
579 error ("nargin: expecting string as first argument"); | |
580 } | |
581 else if (nargin == 0) | |
582 { | |
7336 | 583 retval = symbol_table::varval (".nargin."); |
4700 | 584 |
7336 | 585 if (retval.is_undefined ()) |
586 retval = 0; | |
4700 | 587 } |
588 else | |
5823 | 589 print_usage (); |
4700 | 590 |
591 return retval; | |
592 } | |
593 | |
594 DEFUN (nargout, args, , | |
595 "-*- texinfo -*-\n\ | |
596 @deftypefn {Built-in Function} {} nargout ()\n\ | |
597 @deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ | |
598 Within a function, return the number of values the caller expects to\n\ | |
599 receive. If called with the optional argument @var{fcn_name}, return the\n\ | |
600 maximum number of values the named function can produce, or -1 if the\n\ | |
601 function can produce a variable number of values.\n\ | |
602 \n\ | |
603 For example,\n\ | |
604 \n\ | |
605 @example\n\ | |
606 f ()\n\ | |
607 @end example\n\ | |
608 \n\ | |
609 @noindent\n\ | |
4704 | 610 will cause @code{nargout} to return 0 inside the function @code{f} and\n\ |
4700 | 611 \n\ |
612 @example\n\ | |
613 [s, t] = f ()\n\ | |
614 @end example\n\ | |
615 \n\ | |
616 @noindent\n\ | |
617 will cause @code{nargout} to return 2 inside the function\n\ | |
618 @code{f}.\n\ | |
619 \n\ | |
620 At the top level, @code{nargout} is undefined.\n\ | |
5642 | 621 @seealso{nargin, varargin, varargout}\n\ |
4700 | 622 @end deftypefn") |
623 { | |
624 octave_value retval; | |
625 | |
626 int nargin = args.length (); | |
627 | |
628 if (nargin == 1) | |
629 { | |
630 std::string fname = args(0).string_value (); | |
631 | |
632 if (! error_state) | |
633 { | |
7336 | 634 octave_value fcn_val = symbol_table::find_user_function (fname); |
4930 | 635 |
636 octave_user_function *fcn = fcn_val.user_function_value (true); | |
4700 | 637 |
638 if (fcn) | |
639 { | |
640 if (fcn->takes_var_return ()) | |
641 retval = -1; | |
642 else | |
643 { | |
644 tree_parameter_list *ret_list = fcn->return_list (); | |
645 | |
646 retval = ret_list ? ret_list->length () : 0; | |
647 } | |
648 } | |
649 else | |
650 error ("nargout: invalid function"); | |
651 } | |
652 else | |
653 error ("nargout: expecting string as first argument"); | |
654 } | |
655 else if (nargin == 0) | |
656 { | |
7336 | 657 if (! symbol_table::at_top_level ()) |
4700 | 658 { |
7336 | 659 retval = symbol_table::varval (".nargout."); |
4700 | 660 |
7336 | 661 if (retval.is_undefined ()) |
662 retval = 0; | |
4700 | 663 } |
664 else | |
665 error ("nargout: invalid call at top level"); | |
666 } | |
667 else | |
5823 | 668 print_usage (); |
4700 | 669 |
670 return retval; | |
671 } | |
672 | |
5794 | 673 DEFUN (max_recursion_depth, args, nargout, |
674 "-*- texinfo -*-\n\ | |
675 @deftypefn {Built-in Function} {@var{val} =} max_recursion_depth ()\n\ | |
676 @deftypefnx {Built-in Function} {@var{old_val} =} max_recursion_depth (@var{new_val})\n\ | |
677 Query or set the internal limit on the number of times a function may\n\ | |
678 be called recursively. If the limit is exceeded, an error message is\n\ | |
679 printed and control returns to the top level.\n\ | |
680 @end deftypefn") | |
3131 | 681 { |
5794 | 682 return SET_INTERNAL_VARIABLE (max_recursion_depth); |
2974 | 683 } |
684 | |
685 /* | |
686 ;;; Local Variables: *** | |
687 ;;; mode: C++ *** | |
688 ;;; End: *** | |
689 */ |