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