2974
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2974
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_user_function_h) |
|
25 #define octave_user_function_h 1 |
|
26 |
|
27 #include <ctime> |
|
28 |
|
29 #include <string> |
4214
|
30 #include <stack> |
2974
|
31 |
3665
|
32 #include "comment-list.h" |
2974
|
33 #include "oct-obj.h" |
|
34 #include "ov-fcn.h" |
|
35 #include "ov-typeinfo.h" |
|
36 |
|
37 class string_vector; |
|
38 |
|
39 class octave_value; |
|
40 class tree_parameter_list; |
|
41 class tree_statement_list; |
|
42 class tree_va_return_list; |
|
43 class tree_walker; |
|
44 class symbol_table; |
|
45 class symbol_record; |
|
46 |
5744
|
47 // Scripts. |
|
48 |
|
49 class |
|
50 octave_user_script : public octave_function |
|
51 { |
|
52 public: |
|
53 |
|
54 octave_user_script (void) { } |
|
55 |
|
56 octave_user_script (const std::string& fnm, const std::string& nm, |
|
57 const std::string& ds) |
|
58 : octave_function (nm, ds), file_name (fnm) { } |
|
59 |
|
60 ~octave_user_script (void) { } |
|
61 |
|
62 // Scripts and user functions are both considered "scripts" because |
|
63 // they are written in Octave's scripting language. |
|
64 |
|
65 bool is_user_script (void) const { return true; } |
|
66 |
|
67 void stash_fcn_file_name (const std::string& nm) { file_name = nm; } |
|
68 |
|
69 std::string fcn_file_name (void) const { return file_name; } |
|
70 |
|
71 private: |
|
72 |
|
73 // The name of the file we parsed |
|
74 std::string file_name; |
|
75 |
|
76 // No copying! |
|
77 |
|
78 octave_user_script (const octave_user_script& f); |
|
79 |
|
80 octave_user_script& operator = (const octave_user_script& f); |
|
81 |
|
82 DECLARE_OCTAVE_ALLOCATOR |
|
83 |
|
84 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
|
85 }; |
|
86 |
|
87 // User-defined functions. |
2974
|
88 |
|
89 class |
|
90 octave_user_function : public octave_function |
|
91 { |
|
92 public: |
|
93 |
|
94 octave_user_function (tree_parameter_list *pl = 0, |
|
95 tree_parameter_list *rl = 0, |
|
96 tree_statement_list *cl = 0, |
|
97 symbol_table *st = 0); |
|
98 |
|
99 ~octave_user_function (void); |
|
100 |
4654
|
101 octave_function *function_value (bool = false) { return this; } |
2974
|
102 |
4700
|
103 octave_user_function *user_function_value (bool = false) { return this; } |
|
104 |
2974
|
105 octave_user_function *define_param_list (tree_parameter_list *t); |
|
106 |
|
107 octave_user_function *define_ret_list (tree_parameter_list *t); |
|
108 |
4343
|
109 void stash_fcn_file_name (const std::string& nm); |
2974
|
110 |
3665
|
111 void stash_leading_comment (octave_comment_list *lc) { lead_comm = lc; } |
|
112 |
|
113 void stash_trailing_comment (octave_comment_list *tc) { trail_comm = tc; } |
|
114 |
3325
|
115 void mark_fcn_file_up_to_date (const octave_time& t) { t_checked = t; } |
3165
|
116 |
3255
|
117 void stash_fcn_file_time (const octave_time& t) |
3165
|
118 { |
|
119 t_parsed = t; |
|
120 mark_fcn_file_up_to_date (t); |
|
121 } |
2974
|
122 |
4346
|
123 void stash_symtab_ptr (symbol_record *sr) { symtab_entry = sr; } |
2974
|
124 |
4346
|
125 std::string fcn_file_name (void) const { return file_name; } |
2974
|
126 |
4346
|
127 octave_time time_parsed (void) const { return t_parsed; } |
2974
|
128 |
4346
|
129 octave_time time_checked (void) const { return t_checked; } |
3165
|
130 |
2974
|
131 void mark_as_system_fcn_file (void); |
|
132 |
4346
|
133 bool is_system_fcn_file (void) const { return system_fcn_file; } |
2974
|
134 |
4748
|
135 bool is_user_function (void) const { return true; } |
|
136 |
2974
|
137 bool takes_varargs (void) const; |
|
138 |
4346
|
139 void octave_va_start (void) { curr_va_arg_number = num_named_args; } |
2974
|
140 |
|
141 octave_value octave_va_arg (void); |
|
142 |
|
143 octave_value_list octave_all_va_args (void); |
|
144 |
|
145 bool takes_var_return (void) const; |
|
146 |
|
147 void octave_vr_val (const octave_value& val); |
|
148 |
3974
|
149 void varargout_to_vr_val (void); |
|
150 |
|
151 bool has_varargout (void) const; |
|
152 |
4748
|
153 void stash_function_name (const std::string& s) { my_name = s; } |
2974
|
154 |
4238
|
155 void mark_as_nested_function (void) { nested_function = true; } |
|
156 |
|
157 bool is_nested_function (void) const { return nested_function; } |
|
158 |
3875
|
159 void save_args_passed (const octave_value_list& args) |
|
160 { |
|
161 if (call_depth > 1) |
|
162 saved_args.push (args_passed); |
|
163 |
|
164 args_passed = args; |
|
165 } |
|
166 |
|
167 void restore_args_passed (void) |
|
168 { |
3933
|
169 if (saved_args.empty ()) |
|
170 args_passed = octave_value_list (); |
|
171 else |
4214
|
172 { |
|
173 args_passed = saved_args.top (); |
|
174 saved_args.pop (); |
|
175 } |
3875
|
176 } |
3239
|
177 |
4661
|
178 octave_value subsref (const std::string&, |
|
179 const std::list<octave_value_list>&) |
4271
|
180 { |
|
181 panic_impossible (); |
|
182 return octave_value (); |
|
183 } |
|
184 |
4247
|
185 octave_value_list subsref (const std::string& type, |
4219
|
186 const std::list<octave_value_list>& idx, |
3933
|
187 int nargout); |
|
188 |
3544
|
189 octave_value_list |
|
190 do_multi_index_op (int nargout, const octave_value_list& args); |
2974
|
191 |
4346
|
192 void traceback_error (void) const; |
2974
|
193 |
|
194 tree_parameter_list *parameter_list (void) { return param_list; } |
|
195 |
|
196 tree_parameter_list *return_list (void) { return ret_list; } |
|
197 |
|
198 tree_statement_list *body (void) { return cmd_list; } |
|
199 |
3665
|
200 octave_comment_list *leading_comment (void) { return lead_comm; } |
|
201 |
|
202 octave_comment_list *trailing_comment (void) { return trail_comm; } |
|
203 |
2974
|
204 void accept (tree_walker& tw); |
|
205 |
3933
|
206 void print_symtab_info (std::ostream& os) const; |
|
207 |
2974
|
208 private: |
|
209 |
|
210 // List of arguments for this function. These are local variables. |
|
211 tree_parameter_list *param_list; |
|
212 |
|
213 // List of parameters we return. These are also local variables in |
|
214 // this function. |
|
215 tree_parameter_list *ret_list; |
|
216 |
|
217 // The list of commands that make up the body of this function. |
|
218 tree_statement_list *cmd_list; |
|
219 |
|
220 // The local symbol table for this function. |
|
221 symbol_table *sym_tab; |
|
222 |
3665
|
223 // The comments preceding the FUNCTION token. |
|
224 octave_comment_list *lead_comm; |
|
225 |
|
226 // The comments preceding the ENDFUNCTION token. |
|
227 octave_comment_list *trail_comm; |
|
228 |
2974
|
229 // The name of the file we parsed |
3523
|
230 std::string file_name; |
2974
|
231 |
|
232 // The time the file was parsed. |
3255
|
233 octave_time t_parsed; |
2974
|
234 |
3165
|
235 // The time the file was last checked to see if it needs to be |
|
236 // parsed again. |
3255
|
237 octave_time t_checked; |
3165
|
238 |
2974
|
239 // True if this function came from a file that is considered to be a |
|
240 // system function. This affects whether we check the time stamp |
|
241 // on the file to see if it has changed. |
|
242 bool system_fcn_file; |
|
243 |
|
244 // Used to keep track of recursion depth. |
|
245 int call_depth; |
|
246 |
|
247 // The number of arguments that have names. |
|
248 int num_named_args; |
|
249 |
4238
|
250 // TRUE means this is a nested function. |
|
251 bool nested_function; |
|
252 |
2974
|
253 // The values that were passed as arguments. |
|
254 octave_value_list args_passed; |
|
255 |
3875
|
256 // A place to store the passed args for recursive calls. |
4214
|
257 std::stack<octave_value_list> saved_args; |
3875
|
258 |
2974
|
259 // The number of arguments passed in. |
|
260 int num_args_passed; |
|
261 |
|
262 // Used to keep track of the current offset into the list of va_args. |
|
263 int curr_va_arg_number; |
|
264 |
|
265 // The list of return values when an unspecified number can be |
|
266 // returned. |
|
267 tree_va_return_list *vr_list; |
|
268 |
|
269 // The symbol record for this function. |
|
270 symbol_record *symtab_entry; |
|
271 |
|
272 // The symbol record for argn in the local symbol table. |
|
273 symbol_record *argn_sr; |
|
274 |
|
275 // The symbol record for nargin in the local symbol table. |
|
276 symbol_record *nargin_sr; |
|
277 |
|
278 // The symbol record for nargout in the local symbol table. |
|
279 symbol_record *nargout_sr; |
|
280 |
3974
|
281 // The symbol record for varargin in the local symbol table. |
|
282 symbol_record *varargin_sr; |
|
283 |
2974
|
284 void print_code_function_header (void); |
|
285 |
|
286 void print_code_function_trailer (void); |
|
287 |
|
288 void install_automatic_vars (void); |
|
289 |
|
290 void bind_automatic_vars (const string_vector& arg_names, int nargin, |
3974
|
291 int nargout, const octave_value_list& va_args); |
3219
|
292 |
4645
|
293 |
|
294 // No copying! |
|
295 |
|
296 octave_user_function (const octave_user_function& fn); |
|
297 |
|
298 octave_user_function& operator = (const octave_user_function& fn); |
|
299 |
4612
|
300 DECLARE_OCTAVE_ALLOCATOR |
3219
|
301 |
4612
|
302 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2974
|
303 }; |
|
304 |
|
305 #endif |
|
306 |
|
307 /* |
|
308 ;;; Local Variables: *** |
|
309 ;;; mode: C++ *** |
|
310 ;;; End: *** |
|
311 */ |