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