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