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 |
6323
|
111 void stash_parent_fcn_name (const std::string& p) { parent_name = p; } |
|
112 |
3665
|
113 void stash_leading_comment (octave_comment_list *lc) { lead_comm = lc; } |
|
114 |
|
115 void stash_trailing_comment (octave_comment_list *tc) { trail_comm = tc; } |
|
116 |
3325
|
117 void mark_fcn_file_up_to_date (const octave_time& t) { t_checked = t; } |
3165
|
118 |
3255
|
119 void stash_fcn_file_time (const octave_time& t) |
3165
|
120 { |
|
121 t_parsed = t; |
|
122 mark_fcn_file_up_to_date (t); |
|
123 } |
2974
|
124 |
4346
|
125 void stash_symtab_ptr (symbol_record *sr) { symtab_entry = sr; } |
2974
|
126 |
4346
|
127 std::string fcn_file_name (void) const { return file_name; } |
2974
|
128 |
6323
|
129 std::string parent_fcn_name (void) const { return parent_name; } |
|
130 |
4346
|
131 octave_time time_parsed (void) const { return t_parsed; } |
2974
|
132 |
4346
|
133 octave_time time_checked (void) const { return t_checked; } |
3165
|
134 |
2974
|
135 void mark_as_system_fcn_file (void); |
|
136 |
4346
|
137 bool is_system_fcn_file (void) const { return system_fcn_file; } |
2974
|
138 |
4748
|
139 bool is_user_function (void) const { return true; } |
|
140 |
2974
|
141 bool takes_varargs (void) const; |
|
142 |
5848
|
143 bool takes_var_return (void) const; |
2974
|
144 |
|
145 octave_value_list octave_all_va_args (void); |
|
146 |
4748
|
147 void stash_function_name (const std::string& s) { my_name = s; } |
2974
|
148 |
4238
|
149 void mark_as_nested_function (void) { nested_function = true; } |
|
150 |
|
151 bool is_nested_function (void) const { return nested_function; } |
|
152 |
6149
|
153 void mark_as_inline_function (void) { inline_function = true; } |
|
154 |
|
155 bool is_inline_function (void) const { return inline_function; } |
|
156 |
3875
|
157 void save_args_passed (const octave_value_list& args) |
|
158 { |
|
159 if (call_depth > 1) |
|
160 saved_args.push (args_passed); |
|
161 |
|
162 args_passed = args; |
|
163 } |
|
164 |
|
165 void restore_args_passed (void) |
|
166 { |
3933
|
167 if (saved_args.empty ()) |
|
168 args_passed = octave_value_list (); |
|
169 else |
4214
|
170 { |
|
171 args_passed = saved_args.top (); |
|
172 saved_args.pop (); |
|
173 } |
3875
|
174 } |
3239
|
175 |
4661
|
176 octave_value subsref (const std::string&, |
|
177 const std::list<octave_value_list>&) |
4271
|
178 { |
|
179 panic_impossible (); |
|
180 return octave_value (); |
|
181 } |
|
182 |
4247
|
183 octave_value_list subsref (const std::string& type, |
4219
|
184 const std::list<octave_value_list>& idx, |
3933
|
185 int nargout); |
|
186 |
3544
|
187 octave_value_list |
|
188 do_multi_index_op (int nargout, const octave_value_list& args); |
2974
|
189 |
4346
|
190 void traceback_error (void) const; |
2974
|
191 |
|
192 tree_parameter_list *parameter_list (void) { return param_list; } |
|
193 |
|
194 tree_parameter_list *return_list (void) { return ret_list; } |
|
195 |
|
196 tree_statement_list *body (void) { return cmd_list; } |
|
197 |
6505
|
198 symbol_table *sym_tab (void) { return local_sym_tab; } |
|
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. |
6505
|
221 symbol_table *local_sym_tab; |
2974
|
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 |
6323
|
229 // The name of the file we parsed. |
3523
|
230 std::string file_name; |
2974
|
231 |
6323
|
232 // The name of the parent function, if any. |
|
233 std::string parent_name; |
|
234 |
2974
|
235 // The time the file was parsed. |
3255
|
236 octave_time t_parsed; |
2974
|
237 |
3165
|
238 // The time the file was last checked to see if it needs to be |
|
239 // parsed again. |
3255
|
240 octave_time t_checked; |
3165
|
241 |
2974
|
242 // True if this function came from a file that is considered to be a |
|
243 // system function. This affects whether we check the time stamp |
|
244 // on the file to see if it has changed. |
|
245 bool system_fcn_file; |
|
246 |
|
247 // Used to keep track of recursion depth. |
|
248 int call_depth; |
|
249 |
|
250 // The number of arguments that have names. |
|
251 int num_named_args; |
|
252 |
4238
|
253 // TRUE means this is a nested function. |
|
254 bool nested_function; |
|
255 |
6149
|
256 // TRUE means this is an inline function. |
|
257 bool inline_function; |
|
258 |
2974
|
259 // The values that were passed as arguments. |
|
260 octave_value_list args_passed; |
|
261 |
3875
|
262 // A place to store the passed args for recursive calls. |
4214
|
263 std::stack<octave_value_list> saved_args; |
3875
|
264 |
2974
|
265 // The number of arguments passed in. |
|
266 int num_args_passed; |
|
267 |
|
268 // The symbol record for this function. |
|
269 symbol_record *symtab_entry; |
|
270 |
|
271 // The symbol record for argn in the local symbol table. |
|
272 symbol_record *argn_sr; |
|
273 |
|
274 // The symbol record for nargin in the local symbol table. |
|
275 symbol_record *nargin_sr; |
|
276 |
|
277 // The symbol record for nargout in the local symbol table. |
|
278 symbol_record *nargout_sr; |
|
279 |
3974
|
280 // The symbol record for varargin in the local symbol table. |
|
281 symbol_record *varargin_sr; |
|
282 |
2974
|
283 void print_code_function_header (void); |
|
284 |
|
285 void print_code_function_trailer (void); |
|
286 |
|
287 void install_automatic_vars (void); |
|
288 |
|
289 void bind_automatic_vars (const string_vector& arg_names, int nargin, |
3974
|
290 int nargout, const octave_value_list& va_args); |
3219
|
291 |
4645
|
292 // No copying! |
|
293 |
|
294 octave_user_function (const octave_user_function& fn); |
|
295 |
|
296 octave_user_function& operator = (const octave_user_function& fn); |
|
297 |
4612
|
298 DECLARE_OCTAVE_ALLOCATOR |
3219
|
299 |
4612
|
300 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2974
|
301 }; |
|
302 |
|
303 #endif |
|
304 |
|
305 /* |
|
306 ;;; Local Variables: *** |
|
307 ;;; mode: C++ *** |
|
308 ;;; End: *** |
|
309 */ |