Mercurial > octave
annotate src/ov-usr-fcn.h @ 7715:5b4d278ec828
parse scripts completely before executing
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 16 Apr 2008 15:09:56 -0400 |
parents | 443a8f5a50fd |
children | 87eda1f8faaa |
rev | line source |
---|---|
2974 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
4 2005, 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 #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" | |
7336 | 36 #include "symtab.h" |
2974 | 37 |
38 class string_vector; | |
39 | |
40 class octave_value; | |
41 class tree_parameter_list; | |
42 class tree_statement_list; | |
43 class tree_va_return_list; | |
44 class tree_walker; | |
45 | |
5744 | 46 // Scripts. |
47 | |
48 class | |
49 octave_user_script : public octave_function | |
50 { | |
51 public: | |
52 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
53 octave_user_script (void) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
54 : octave_function (), cmd_list (0), file_name () { } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
55 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
56 octave_user_script (const std::string& fnm, const std::string& nm, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
57 tree_statement_list *cmds, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
58 const std::string& ds = std::string ()) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
59 : octave_function (nm, ds), cmd_list (cmds), file_name (fnm) { } |
5744 | 60 |
61 octave_user_script (const std::string& fnm, const std::string& nm, | |
7336 | 62 const std::string& ds = std::string ()) |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
63 : octave_function (nm, ds), cmd_list (0), file_name (fnm) { } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
64 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
65 ~octave_user_script (void); |
5744 | 66 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
67 octave_function *function_value (bool = false) { return this; } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
68 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
69 octave_user_script *user_script_value (bool = false) { return this; } |
5744 | 70 |
71 // Scripts and user functions are both considered "scripts" because | |
72 // they are written in Octave's scripting language. | |
73 | |
74 bool is_user_script (void) const { return true; } | |
75 | |
76 void stash_fcn_file_name (const std::string& nm) { file_name = nm; } | |
77 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
78 void mark_fcn_file_up_to_date (const octave_time& t) { t_checked = t; } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
79 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
80 void stash_fcn_file_time (const octave_time& t) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
81 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
82 t_parsed = t; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
83 mark_fcn_file_up_to_date (t); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
84 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
85 |
5744 | 86 std::string fcn_file_name (void) const { return file_name; } |
87 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
88 octave_time time_parsed (void) const { return t_parsed; } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
89 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
90 octave_time time_checked (void) const { return t_checked; } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
91 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
92 octave_value subsref (const std::string& type, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
93 const std::list<octave_value_list>& idx) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
94 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
95 octave_value_list tmp = subsref (type, idx, 1); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
96 return tmp.length () > 0 ? tmp(0) : octave_value (); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
97 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
98 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
99 octave_value_list subsref (const std::string& type, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
100 const std::list<octave_value_list>& idx, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
101 int nargout); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
102 |
7336 | 103 octave_value_list |
104 do_multi_index_op (int nargout, const octave_value_list& args); | |
105 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
106 tree_statement_list *body (void) { return cmd_list; } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
107 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
108 void traceback_error (void) const; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
109 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
110 void accept (tree_walker& tw); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
111 |
5744 | 112 private: |
113 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
114 // The list of commands that make up the body of this function. |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
115 tree_statement_list *cmd_list; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
116 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
117 // The name of the file we parsed. |
5744 | 118 std::string file_name; |
119 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
120 // The time the file was parsed. |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
121 octave_time t_parsed; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
122 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
123 // The time the file was last checked to see if it needs to be |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
124 // parsed again. |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
125 octave_time t_checked; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
126 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
127 // Used to keep track of recursion depth. |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
128 int call_depth; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
129 |
5744 | 130 // No copying! |
131 | |
132 octave_user_script (const octave_user_script& f); | |
133 | |
134 octave_user_script& operator = (const octave_user_script& f); | |
135 | |
136 DECLARE_OCTAVE_ALLOCATOR | |
137 | |
138 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA | |
139 }; | |
140 | |
141 // User-defined functions. | |
2974 | 142 |
143 class | |
144 octave_user_function : public octave_function | |
145 { | |
146 public: | |
147 | |
7336 | 148 octave_user_function (symbol_table::scope_id sid = -1, |
149 tree_parameter_list *pl = 0, | |
2974 | 150 tree_parameter_list *rl = 0, |
7336 | 151 tree_statement_list *cl = 0); |
2974 | 152 |
153 ~octave_user_function (void); | |
154 | |
4654 | 155 octave_function *function_value (bool = false) { return this; } |
2974 | 156 |
4700 | 157 octave_user_function *user_function_value (bool = false) { return this; } |
158 | |
2974 | 159 octave_user_function *define_param_list (tree_parameter_list *t); |
160 | |
161 octave_user_function *define_ret_list (tree_parameter_list *t); | |
162 | |
4343 | 163 void stash_fcn_file_name (const std::string& nm); |
2974 | 164 |
6323 | 165 void stash_parent_fcn_name (const std::string& p) { parent_name = p; } |
166 | |
3665 | 167 void stash_leading_comment (octave_comment_list *lc) { lead_comm = lc; } |
168 | |
169 void stash_trailing_comment (octave_comment_list *tc) { trail_comm = tc; } | |
170 | |
3325 | 171 void mark_fcn_file_up_to_date (const octave_time& t) { t_checked = t; } |
3165 | 172 |
3255 | 173 void stash_fcn_file_time (const octave_time& t) |
3165 | 174 { |
175 t_parsed = t; | |
176 mark_fcn_file_up_to_date (t); | |
177 } | |
2974 | 178 |
4346 | 179 std::string fcn_file_name (void) const { return file_name; } |
2974 | 180 |
6323 | 181 std::string parent_fcn_name (void) const { return parent_name; } |
182 | |
7336 | 183 symbol_table::scope_id scope (void) { return local_scope; } |
184 | |
4346 | 185 octave_time time_parsed (void) const { return t_parsed; } |
2974 | 186 |
4346 | 187 octave_time time_checked (void) const { return t_checked; } |
3165 | 188 |
2974 | 189 void mark_as_system_fcn_file (void); |
190 | |
4346 | 191 bool is_system_fcn_file (void) const { return system_fcn_file; } |
2974 | 192 |
4748 | 193 bool is_user_function (void) const { return true; } |
194 | |
2974 | 195 bool takes_varargs (void) const; |
196 | |
5848 | 197 bool takes_var_return (void) const; |
2974 | 198 |
199 octave_value_list octave_all_va_args (void); | |
200 | |
4748 | 201 void stash_function_name (const std::string& s) { my_name = s; } |
2974 | 202 |
4238 | 203 void mark_as_nested_function (void) { nested_function = true; } |
204 | |
205 bool is_nested_function (void) const { return nested_function; } | |
206 | |
6149 | 207 void mark_as_inline_function (void) { inline_function = true; } |
208 | |
209 bool is_inline_function (void) const { return inline_function; } | |
210 | |
7336 | 211 void mark_as_class_constructor (void) { class_constructor = true; } |
212 | |
213 bool is_class_constructor (void) const { return class_constructor; } | |
214 | |
215 void mark_as_class_method (void) { class_method = true; } | |
216 | |
217 bool is_class_method (void) const { return class_method; } | |
218 | |
219 void stash_dispatch_class (const std::string& nm) { xdispatch_class = nm; } | |
220 | |
221 std::string dispatch_class (void) const { return xdispatch_class; } | |
222 | |
3875 | 223 void save_args_passed (const octave_value_list& args) |
224 { | |
225 if (call_depth > 1) | |
226 saved_args.push (args_passed); | |
227 | |
228 args_passed = args; | |
229 } | |
230 | |
231 void restore_args_passed (void) | |
232 { | |
3933 | 233 if (saved_args.empty ()) |
234 args_passed = octave_value_list (); | |
235 else | |
4214 | 236 { |
237 args_passed = saved_args.top (); | |
238 saved_args.pop (); | |
239 } | |
3875 | 240 } |
3239 | 241 |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
242 octave_value subsref (const std::string& type, |
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
243 const std::list<octave_value_list>& idx) |
4271 | 244 { |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
245 octave_value_list tmp = subsref (type, idx, 1); |
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
246 return tmp.length () > 0 ? tmp(0) : octave_value (); |
4271 | 247 } |
248 | |
4247 | 249 octave_value_list subsref (const std::string& type, |
4219 | 250 const std::list<octave_value_list>& idx, |
3933 | 251 int nargout); |
252 | |
3544 | 253 octave_value_list |
254 do_multi_index_op (int nargout, const octave_value_list& args); | |
2974 | 255 |
4346 | 256 void traceback_error (void) const; |
2974 | 257 |
258 tree_parameter_list *parameter_list (void) { return param_list; } | |
259 | |
260 tree_parameter_list *return_list (void) { return ret_list; } | |
261 | |
262 tree_statement_list *body (void) { return cmd_list; } | |
263 | |
3665 | 264 octave_comment_list *leading_comment (void) { return lead_comm; } |
265 | |
266 octave_comment_list *trailing_comment (void) { return trail_comm; } | |
267 | |
2974 | 268 void accept (tree_walker& tw); |
269 | |
7336 | 270 #if 0 |
3933 | 271 void print_symtab_info (std::ostream& os) const; |
7336 | 272 #endif |
3933 | 273 |
2974 | 274 private: |
275 | |
276 // List of arguments for this function. These are local variables. | |
277 tree_parameter_list *param_list; | |
278 | |
279 // List of parameters we return. These are also local variables in | |
280 // this function. | |
281 tree_parameter_list *ret_list; | |
282 | |
283 // The list of commands that make up the body of this function. | |
284 tree_statement_list *cmd_list; | |
285 | |
3665 | 286 // The comments preceding the FUNCTION token. |
287 octave_comment_list *lead_comm; | |
288 | |
289 // The comments preceding the ENDFUNCTION token. | |
290 octave_comment_list *trail_comm; | |
291 | |
6323 | 292 // The name of the file we parsed. |
3523 | 293 std::string file_name; |
2974 | 294 |
6323 | 295 // The name of the parent function, if any. |
296 std::string parent_name; | |
297 | |
2974 | 298 // The time the file was parsed. |
3255 | 299 octave_time t_parsed; |
2974 | 300 |
3165 | 301 // The time the file was last checked to see if it needs to be |
302 // parsed again. | |
3255 | 303 octave_time t_checked; |
3165 | 304 |
2974 | 305 // True if this function came from a file that is considered to be a |
306 // system function. This affects whether we check the time stamp | |
307 // on the file to see if it has changed. | |
308 bool system_fcn_file; | |
309 | |
310 // Used to keep track of recursion depth. | |
311 int call_depth; | |
312 | |
313 // The number of arguments that have names. | |
314 int num_named_args; | |
315 | |
4238 | 316 // TRUE means this is a nested function. |
317 bool nested_function; | |
318 | |
6149 | 319 // TRUE means this is an inline function. |
320 bool inline_function; | |
321 | |
7336 | 322 // TRUE means this function is the constructor for class object. |
323 bool class_constructor; | |
324 | |
325 // TRUE means this function is a method for a class. | |
326 bool class_method; | |
327 | |
328 // If this object is a class method or constructor, this is the name | |
329 // of the class to which the method belongs. | |
330 std::string xdispatch_class; | |
331 | |
2974 | 332 // The values that were passed as arguments. |
333 octave_value_list args_passed; | |
334 | |
3875 | 335 // A place to store the passed args for recursive calls. |
4214 | 336 std::stack<octave_value_list> saved_args; |
3875 | 337 |
2974 | 338 // The number of arguments passed in. |
339 int num_args_passed; | |
340 | |
7336 | 341 symbol_table::scope_id local_scope; |
2974 | 342 |
7336 | 343 #if 0 |
2974 | 344 // The symbol record for argn in the local symbol table. |
7336 | 345 octave_value& argn_varref; |
2974 | 346 |
347 // The symbol record for nargin in the local symbol table. | |
7336 | 348 octave_value& nargin_varref; |
2974 | 349 |
350 // The symbol record for nargout in the local symbol table. | |
7336 | 351 octave_value& nargout_varref; |
2974 | 352 |
3974 | 353 // The symbol record for varargin in the local symbol table. |
7336 | 354 octave_value& varargin_varref; |
355 #endif | |
3974 | 356 |
2974 | 357 void print_code_function_header (void); |
358 | |
359 void print_code_function_trailer (void); | |
360 | |
361 void bind_automatic_vars (const string_vector& arg_names, int nargin, | |
3974 | 362 int nargout, const octave_value_list& va_args); |
3219 | 363 |
4645 | 364 // No copying! |
365 | |
366 octave_user_function (const octave_user_function& fn); | |
367 | |
368 octave_user_function& operator = (const octave_user_function& fn); | |
369 | |
4612 | 370 DECLARE_OCTAVE_ALLOCATOR |
3219 | 371 |
4612 | 372 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2974 | 373 }; |
374 | |
375 #endif | |
376 | |
377 /* | |
378 ;;; Local Variables: *** | |
379 ;;; mode: C++ *** | |
380 ;;; End: *** | |
381 */ |