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