Mercurial > octave-nkf
annotate src/ov-usr-fcn.cc @ 8874:bd1b1fe9c6e9 ss-3-1-53
bump version info for snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Feb 2009 18:35:47 -0500 |
parents | 70f5a0375afd |
children | eb63fbe60fab |
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 |
8669
33783e94fb16
line number fixes and other evaluator tweaks
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
131 unwind_protect_bool (tree_evaluator::in_fcn_or_script_body); |
33783e94fb16
line number fixes and other evaluator tweaks
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
132 tree_evaluator::in_fcn_or_script_body = true; |
33783e94fb16
line number fixes and other evaluator tweaks
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
133 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
134 cmd_list->accept (*current_evaluator); |
7336 | 135 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
136 if (tree_return_command::returning) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
137 tree_return_command::returning = 0; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
138 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
139 if (tree_break_command::breaking) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
140 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
|
141 |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
142 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
|
143 octave_call_stack::backtrace_error_message (); |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
144 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
145 else |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
146 ::error ("max_recursion_limit exceeded"); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
147 } |
7336 | 148 } |
149 else | |
150 error ("invalid call to script"); | |
151 } | |
152 | |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
153 unwind_protect::run_frame ("user_script_eval"); |
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
154 |
7336 | 155 return retval; |
156 } | |
157 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
158 void |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
159 octave_user_script::accept (tree_walker& tw) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
160 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
161 tw.visit_octave_user_script (*this); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
162 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
163 |
2974 | 164 // User defined functions. |
165 | |
3219 | 166 DEFINE_OCTAVE_ALLOCATOR (octave_user_function); |
2974 | 167 |
3219 | 168 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_function, |
4612 | 169 "user-defined function", |
3219 | 170 "user-defined function"); |
2974 | 171 |
172 // Ugh. This really needs to be simplified (code/data? | |
173 // extrinsic/intrinsic state?). | |
174 | |
175 octave_user_function::octave_user_function | |
7336 | 176 (symbol_table::scope_id sid, tree_parameter_list *pl, |
177 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
|
178 : octave_user_code (std::string (), std::string ()), |
2974 | 179 param_list (pl), ret_list (rl), cmd_list (cl), |
7336 | 180 lead_comm (), trail_comm (), file_name (), |
6323 | 181 parent_name (), t_parsed (static_cast<time_t> (0)), |
3255 | 182 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
|
183 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
|
184 num_named_args (param_list ? param_list->length () : 0), |
7336 | 185 nested_function (false), inline_function (false), |
8785
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8669
diff
changeset
|
186 class_constructor (false), class_method (false), args_passed (), |
70f5a0375afd
oct-map.h: fix think-o in previous change
John W. Eaton <jwe@octave.org>
parents:
8669
diff
changeset
|
187 num_args_passed (0), parent_scope (-1), local_scope (sid) |
7736 | 188 { |
189 if (cmd_list) | |
190 cmd_list->mark_as_function_body (); | |
191 } | |
2974 | 192 |
193 octave_user_function::~octave_user_function (void) | |
194 { | |
195 delete param_list; | |
196 delete ret_list; | |
197 delete cmd_list; | |
3665 | 198 delete lead_comm; |
199 delete trail_comm; | |
7336 | 200 |
201 symbol_table::erase_scope (local_scope); | |
2974 | 202 } |
203 | |
204 octave_user_function * | |
205 octave_user_function::define_ret_list (tree_parameter_list *t) | |
206 { | |
207 ret_list = t; | |
208 | |
209 return this; | |
210 } | |
211 | |
212 void | |
4343 | 213 octave_user_function::stash_fcn_file_name (const std::string& nm) |
2974 | 214 { |
4343 | 215 file_name = nm; |
2974 | 216 } |
217 | |
218 void | |
219 octave_user_function::mark_as_system_fcn_file (void) | |
220 { | |
221 if (! file_name.empty ()) | |
222 { | |
223 // We really should stash the whole path to the file we found, | |
224 // when we looked it up, to avoid possible race conditions... | |
5775 | 225 // FIXME |
2974 | 226 // |
227 // We probably also don't need to get the library directory | |
228 // every time, but since this function is only called when the | |
229 // function file is parsed, it probably doesn't matter that | |
230 // much. | |
231 | |
3523 | 232 std::string ff_name = fcn_file_in_path (file_name); |
2974 | 233 |
3565 | 234 if (Vfcn_file_dir == ff_name.substr (0, Vfcn_file_dir.length ())) |
2974 | 235 system_fcn_file = 1; |
236 } | |
237 else | |
238 system_fcn_file = 0; | |
239 } | |
240 | |
241 bool | |
242 octave_user_function::takes_varargs (void) const | |
243 { | |
244 return (param_list && param_list->takes_varargs ()); | |
245 } | |
246 | |
5848 | 247 bool |
248 octave_user_function::takes_var_return (void) const | |
2974 | 249 { |
5848 | 250 return (ret_list && ret_list->takes_varargs ()); |
2974 | 251 } |
252 | |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
253 void |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
254 octave_user_function::lock_subfunctions (void) |
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 symbol_table::lock_subfunctions (local_scope); |
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 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
259 void |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
260 octave_user_function::unlock_subfunctions (void) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
261 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
262 symbol_table::unlock_subfunctions (local_scope); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
263 } |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
264 |
2974 | 265 octave_value_list |
266 octave_user_function::octave_all_va_args (void) | |
267 { | |
268 octave_value_list retval; | |
269 | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8142
diff
changeset
|
270 octave_idx_type n = num_args_passed - num_named_args; |
2974 | 271 |
3178 | 272 if (n > 0) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8142
diff
changeset
|
273 retval = args_passed.slice (num_named_args, n); |
2974 | 274 |
275 return retval; | |
276 } | |
277 | |
278 static void | |
3239 | 279 clear_param_list (void *lst) |
280 { | |
281 tree_parameter_list *tmp = static_cast<tree_parameter_list *> (lst); | |
282 | |
283 if (tmp) | |
4219 | 284 tmp->undefine (); |
3239 | 285 } |
286 | |
287 static void | |
3875 | 288 restore_args_passed (void *fcn) |
3239 | 289 { |
290 octave_user_function *tmp = static_cast<octave_user_function *> (fcn); | |
291 | |
292 if (tmp) | |
3875 | 293 tmp->restore_args_passed (); |
3239 | 294 } |
295 | |
2974 | 296 octave_value_list |
4247 | 297 octave_user_function::subsref (const std::string& type, |
4219 | 298 const std::list<octave_value_list>& idx, |
3933 | 299 int nargout) |
300 { | |
301 octave_value_list retval; | |
302 | |
303 switch (type[0]) | |
304 { | |
305 case '(': | |
5154 | 306 { |
5155 | 307 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5154 | 308 |
309 retval = do_multi_index_op (tmp_nargout, idx.front ()); | |
310 } | |
3933 | 311 break; |
312 | |
313 case '{': | |
314 case '.': | |
315 { | |
316 std::string nm = type_name (); | |
317 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
318 } | |
319 break; | |
320 | |
321 default: | |
322 panic_impossible (); | |
323 } | |
324 | |
5775 | 325 // FIXME -- perhaps there should be an |
4059 | 326 // octave_value_list::next_subsref member function? See also |
327 // octave_builtin::subsref. | |
3933 | 328 |
4219 | 329 if (idx.size () > 1) |
4994 | 330 retval = retval(0).next_subsref (nargout, type, idx); |
4059 | 331 |
332 return retval; | |
3933 | 333 } |
334 | |
335 octave_value_list | |
3544 | 336 octave_user_function::do_multi_index_op (int nargout, |
337 const octave_value_list& args) | |
2974 | 338 { |
339 octave_value_list retval; | |
340 | |
341 if (error_state) | |
342 return retval; | |
343 | |
344 if (! cmd_list) | |
345 return retval; | |
346 | |
347 int nargin = args.length (); | |
348 | |
4987 | 349 unwind_protect::begin_frame ("user_func_eval"); |
2974 | 350 |
351 unwind_protect_int (call_depth); | |
352 call_depth++; | |
353 | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
354 if (call_depth >= Vmax_recursion_depth) |
3131 | 355 { |
356 ::error ("max_recursion_limit exceeded"); | |
4987 | 357 unwind_protect::run_frame ("user_func_eval"); |
3131 | 358 return retval; |
359 } | |
360 | |
7336 | 361 // Save old and set current symbol table context, for |
362 // eval_undefined_error(). | |
363 | |
364 symbol_table::push_scope (local_scope); | |
365 unwind_protect::add (symbol_table::pop_scope); | |
2974 | 366 |
7901 | 367 octave_call_stack::push (this, local_scope, call_depth); |
368 unwind_protect::add (octave_call_stack::unwind_pop, 0); | |
369 | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
370 if (call_depth > 0) |
2974 | 371 { |
7336 | 372 symbol_table::push_context (); |
2974 | 373 |
7336 | 374 unwind_protect::add (symbol_table::pop_context); |
375 } | |
2974 | 376 |
3875 | 377 // Save and restore args passed for recursive calls. |
2974 | 378 |
3875 | 379 save_args_passed (args); |
3239 | 380 |
3875 | 381 unwind_protect::add (::restore_args_passed, this); |
3239 | 382 |
2974 | 383 string_vector arg_names = args.name_tags (); |
384 | |
385 unwind_protect_int (num_args_passed); | |
386 num_args_passed = nargin; | |
387 | |
388 if (param_list && ! param_list->varargs_only ()) | |
389 { | |
390 param_list->define_from_arg_vector (args); | |
391 if (error_state) | |
392 goto abort; | |
393 } | |
394 | |
3239 | 395 // Force parameter list to be undefined when this function exits. |
396 // Doing so decrements the reference counts on the values of local | |
397 // variables that are also named function parameters. | |
398 | |
399 unwind_protect::add (clear_param_list, param_list); | |
400 | |
401 // Force return list to be undefined when this function exits. | |
402 // Doing so decrements the reference counts on the values of local | |
403 // variables that are also named values returned by this function. | |
404 | |
405 unwind_protect::add (clear_param_list, ret_list); | |
406 | |
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
|
407 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
|
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 // 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
|
410 // 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
|
411 // |
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 // 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
|
413 // 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
|
414 // 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
|
415 // 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
|
416 // 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
|
417 // 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
|
418 |
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 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
|
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 |
2974 | 422 // The following code is in a separate scope to avoid warnings from |
423 // G++ about `goto abort' crossing the initialization of some | |
424 // variables. | |
425 | |
426 { | |
3974 | 427 bind_automatic_vars (arg_names, nargin, nargout, octave_all_va_args ()); |
2974 | 428 |
429 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); | |
430 | |
431 if (echo_commands) | |
432 print_code_function_header (); | |
433 | |
434 // Evaluate the commands that make up the function. | |
435 | |
8669
33783e94fb16
line number fixes and other evaluator tweaks
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
436 unwind_protect_bool (tree_evaluator::in_fcn_or_script_body); |
33783e94fb16
line number fixes and other evaluator tweaks
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
437 tree_evaluator::in_fcn_or_script_body = true; |
3489 | 438 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
439 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
|
440 || 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
|
441 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
442 if (special_expr) |
6657 | 443 { |
444 assert (cmd_list->length () == 1); | |
445 | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
446 tree_statement *stmt = 0; |
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 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
|
449 && stmt->is_expression ()) |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
450 { |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
451 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
|
452 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
453 retval = expr->rvalue (nargout); |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
454 } |
6657 | 455 } |
456 else | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
457 cmd_list->accept (*current_evaluator); |
2974 | 458 |
459 if (echo_commands) | |
460 print_code_function_trailer (); | |
461 | |
4207 | 462 if (tree_return_command::returning) |
463 tree_return_command::returning = 0; | |
2974 | 464 |
4207 | 465 if (tree_break_command::breaking) |
466 tree_break_command::breaking--; | |
2974 | 467 |
468 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
|
469 { |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
470 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
|
471 goto abort; |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
472 } |
2974 | 473 |
474 // Copy return values out. | |
475 | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
476 if (ret_list && ! special_expr) |
3871 | 477 { |
4748 | 478 ret_list->initialize_undefined_elements (my_name, nargout, Matrix ()); |
3871 | 479 |
5848 | 480 Cell varargout; |
481 | |
6591 | 482 if (ret_list->takes_varargs ()) |
5848 | 483 { |
7336 | 484 octave_value varargout_varval = symbol_table::varval ("varargout"); |
6591 | 485 |
7336 | 486 if (varargout_varval.is_defined ()) |
6591 | 487 { |
7336 | 488 varargout = varargout_varval.cell_value (); |
5848 | 489 |
6591 | 490 if (error_state) |
491 error ("expecting varargout to be a cell array object"); | |
492 } | |
5848 | 493 } |
494 | |
495 if (! error_state) | |
496 retval = ret_list->convert_to_const_vector (varargout); | |
3871 | 497 } |
2974 | 498 } |
499 | |
500 abort: | |
4987 | 501 unwind_protect::run_frame ("user_func_eval"); |
2974 | 502 |
503 return retval; | |
504 } | |
505 | |
506 void | |
507 octave_user_function::accept (tree_walker& tw) | |
508 { | |
509 tw.visit_octave_user_function (*this); | |
510 } | |
511 | |
7336 | 512 #if 0 |
2974 | 513 void |
3933 | 514 octave_user_function::print_symtab_info (std::ostream& os) const |
515 { | |
7336 | 516 symbol_table::print_info (os, local_scope); |
3933 | 517 } |
7336 | 518 #endif |
3933 | 519 |
520 void | |
2974 | 521 octave_user_function::print_code_function_header (void) |
522 { | |
5794 | 523 tree_print_code tpc (octave_stdout, VPS4); |
2974 | 524 |
525 tpc.visit_octave_user_function_header (*this); | |
526 } | |
527 | |
528 void | |
529 octave_user_function::print_code_function_trailer (void) | |
530 { | |
5794 | 531 tree_print_code tpc (octave_stdout, VPS4); |
2974 | 532 |
533 tpc.visit_octave_user_function_trailer (*this); | |
534 } | |
535 | |
536 void | |
537 octave_user_function::bind_automatic_vars | |
3974 | 538 (const string_vector& arg_names, int nargin, int nargout, |
539 const octave_value_list& va_args) | |
2974 | 540 { |
541 if (! arg_names.empty ()) | |
7336 | 542 symbol_table::varref ("argn") = arg_names; |
2974 | 543 |
7336 | 544 symbol_table::varref (".nargin.") = nargin; |
545 symbol_table::varref (".nargout.") = nargout; | |
546 | |
547 symbol_table::mark_hidden (".nargin."); | |
548 symbol_table::mark_hidden (".nargout."); | |
3974 | 549 |
550 if (takes_varargs ()) | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8142
diff
changeset
|
551 symbol_table::varref ("varargin") = va_args.cell_value (); |
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 */ |