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 |
5848
|
139 bool takes_var_return (void) const; |
2974
|
140 |
|
141 octave_value_list octave_all_va_args (void); |
|
142 |
4748
|
143 void stash_function_name (const std::string& s) { my_name = s; } |
2974
|
144 |
4238
|
145 void mark_as_nested_function (void) { nested_function = true; } |
|
146 |
|
147 bool is_nested_function (void) const { return nested_function; } |
|
148 |
6149
|
149 void mark_as_inline_function (void) { inline_function = true; } |
|
150 |
|
151 bool is_inline_function (void) const { return inline_function; } |
|
152 |
3875
|
153 void save_args_passed (const octave_value_list& args) |
|
154 { |
|
155 if (call_depth > 1) |
|
156 saved_args.push (args_passed); |
|
157 |
|
158 args_passed = args; |
|
159 } |
|
160 |
|
161 void restore_args_passed (void) |
|
162 { |
3933
|
163 if (saved_args.empty ()) |
|
164 args_passed = octave_value_list (); |
|
165 else |
4214
|
166 { |
|
167 args_passed = saved_args.top (); |
|
168 saved_args.pop (); |
|
169 } |
3875
|
170 } |
3239
|
171 |
4661
|
172 octave_value subsref (const std::string&, |
|
173 const std::list<octave_value_list>&) |
4271
|
174 { |
|
175 panic_impossible (); |
|
176 return octave_value (); |
|
177 } |
|
178 |
4247
|
179 octave_value_list subsref (const std::string& type, |
4219
|
180 const std::list<octave_value_list>& idx, |
3933
|
181 int nargout); |
|
182 |
3544
|
183 octave_value_list |
|
184 do_multi_index_op (int nargout, const octave_value_list& args); |
2974
|
185 |
4346
|
186 void traceback_error (void) const; |
2974
|
187 |
|
188 tree_parameter_list *parameter_list (void) { return param_list; } |
|
189 |
|
190 tree_parameter_list *return_list (void) { return ret_list; } |
|
191 |
|
192 tree_statement_list *body (void) { return cmd_list; } |
|
193 |
3665
|
194 octave_comment_list *leading_comment (void) { return lead_comm; } |
|
195 |
|
196 octave_comment_list *trailing_comment (void) { return trail_comm; } |
|
197 |
2974
|
198 void accept (tree_walker& tw); |
|
199 |
3933
|
200 void print_symtab_info (std::ostream& os) const; |
|
201 |
2974
|
202 private: |
|
203 |
|
204 // List of arguments for this function. These are local variables. |
|
205 tree_parameter_list *param_list; |
|
206 |
|
207 // List of parameters we return. These are also local variables in |
|
208 // this function. |
|
209 tree_parameter_list *ret_list; |
|
210 |
|
211 // The list of commands that make up the body of this function. |
|
212 tree_statement_list *cmd_list; |
|
213 |
|
214 // The local symbol table for this function. |
|
215 symbol_table *sym_tab; |
|
216 |
3665
|
217 // The comments preceding the FUNCTION token. |
|
218 octave_comment_list *lead_comm; |
|
219 |
|
220 // The comments preceding the ENDFUNCTION token. |
|
221 octave_comment_list *trail_comm; |
|
222 |
2974
|
223 // The name of the file we parsed |
3523
|
224 std::string file_name; |
2974
|
225 |
|
226 // The time the file was parsed. |
3255
|
227 octave_time t_parsed; |
2974
|
228 |
3165
|
229 // The time the file was last checked to see if it needs to be |
|
230 // parsed again. |
3255
|
231 octave_time t_checked; |
3165
|
232 |
2974
|
233 // True if this function came from a file that is considered to be a |
|
234 // system function. This affects whether we check the time stamp |
|
235 // on the file to see if it has changed. |
|
236 bool system_fcn_file; |
|
237 |
|
238 // Used to keep track of recursion depth. |
|
239 int call_depth; |
|
240 |
|
241 // The number of arguments that have names. |
|
242 int num_named_args; |
|
243 |
4238
|
244 // TRUE means this is a nested function. |
|
245 bool nested_function; |
|
246 |
6149
|
247 // TRUE means this is an inline function. |
|
248 bool inline_function; |
|
249 |
2974
|
250 // The values that were passed as arguments. |
|
251 octave_value_list args_passed; |
|
252 |
3875
|
253 // A place to store the passed args for recursive calls. |
4214
|
254 std::stack<octave_value_list> saved_args; |
3875
|
255 |
2974
|
256 // The number of arguments passed in. |
|
257 int num_args_passed; |
|
258 |
|
259 // The symbol record for this function. |
|
260 symbol_record *symtab_entry; |
|
261 |
|
262 // The symbol record for argn in the local symbol table. |
|
263 symbol_record *argn_sr; |
|
264 |
|
265 // The symbol record for nargin in the local symbol table. |
|
266 symbol_record *nargin_sr; |
|
267 |
|
268 // The symbol record for nargout in the local symbol table. |
|
269 symbol_record *nargout_sr; |
|
270 |
3974
|
271 // The symbol record for varargin in the local symbol table. |
|
272 symbol_record *varargin_sr; |
|
273 |
2974
|
274 void print_code_function_header (void); |
|
275 |
|
276 void print_code_function_trailer (void); |
|
277 |
|
278 void install_automatic_vars (void); |
|
279 |
|
280 void bind_automatic_vars (const string_vector& arg_names, int nargin, |
3974
|
281 int nargout, const octave_value_list& va_args); |
3219
|
282 |
4645
|
283 // No copying! |
|
284 |
|
285 octave_user_function (const octave_user_function& fn); |
|
286 |
|
287 octave_user_function& operator = (const octave_user_function& fn); |
|
288 |
4612
|
289 DECLARE_OCTAVE_ALLOCATOR |
3219
|
290 |
4612
|
291 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2974
|
292 }; |
|
293 |
|
294 #endif |
|
295 |
|
296 /* |
|
297 ;;; Local Variables: *** |
|
298 ;;; mode: C++ *** |
|
299 ;;; End: *** |
|
300 */ |