1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1468
|
27 #include <cstdio> |
1343
|
28 #include <cstring> |
605
|
29 |
1728
|
30 #include <string> |
|
31 |
2926
|
32 #include "file-stat.h" |
|
33 #include "oct-env.h" |
|
34 #include "glob-match.h" |
1755
|
35 #include "str-vec.h" |
|
36 |
2492
|
37 #include <defaults.h> |
1352
|
38 #include "defun.h" |
|
39 #include "dirfns.h" |
|
40 #include "error.h" |
2205
|
41 #include "gripes.h" |
1352
|
42 #include "help.h" |
|
43 #include "lex.h" |
2926
|
44 #include "oct-map.h" |
|
45 #include "oct-obj.h" |
|
46 #include "ov.h" |
605
|
47 #include "pager.h" |
1352
|
48 #include "parse.h" |
2926
|
49 #include "symtab.h" |
2205
|
50 #include "toplev.h" |
1352
|
51 #include "unwind-prot.h" |
1
|
52 #include "utils.h" |
1352
|
53 #include "variables.h" |
2205
|
54 |
|
55 // Should Octave always check to see if function files have changed |
|
56 // since they were last compiled? |
2806
|
57 static int Vignore_function_time_stamp; |
2205
|
58 |
1
|
59 // Symbol table for symbols at the top level. |
581
|
60 symbol_table *top_level_sym_tab = 0; |
1
|
61 |
|
62 // Symbol table for the current scope. |
581
|
63 symbol_table *curr_sym_tab = 0; |
1
|
64 |
|
65 // Symbol table for global symbols. |
581
|
66 symbol_table *global_sym_tab = 0; |
1
|
67 |
593
|
68 // Initialization. |
|
69 |
|
70 // Create the initial symbol tables and set the current scope at the |
|
71 // top level. |
|
72 |
195
|
73 void |
|
74 initialize_symbol_tables (void) |
|
75 { |
581
|
76 if (! global_sym_tab) |
3005
|
77 global_sym_tab = new symbol_table (2048); |
195
|
78 |
581
|
79 if (! top_level_sym_tab) |
3005
|
80 top_level_sym_tab = new symbol_table (4096); |
195
|
81 |
|
82 curr_sym_tab = top_level_sym_tab; |
|
83 } |
|
84 |
593
|
85 // Attributes of variables and functions. |
|
86 |
|
87 // Is this variable a builtin? |
|
88 |
1827
|
89 bool |
1755
|
90 is_builtin_variable (const string& name) |
593
|
91 { |
2856
|
92 symbol_record *sr = global_sym_tab->lookup (name); |
593
|
93 return (sr && sr->is_builtin_variable ()); |
|
94 } |
|
95 |
|
96 // Is this a text-style function? |
|
97 |
1827
|
98 bool |
1755
|
99 is_text_function_name (const string& s) |
593
|
100 { |
|
101 symbol_record *sr = global_sym_tab->lookup (s); |
|
102 return (sr && sr->is_text_function ()); |
|
103 } |
|
104 |
2294
|
105 // Is this a mapper function? |
|
106 |
|
107 bool |
|
108 is_builtin_function_name (const string& s) |
|
109 { |
|
110 symbol_record *sr = global_sym_tab->lookup (s); |
|
111 return (sr && sr->is_builtin_function ()); |
|
112 } |
|
113 |
|
114 // Is this a mapper function? |
|
115 |
|
116 bool |
|
117 is_mapper_function_name (const string& s) |
|
118 { |
|
119 symbol_record *sr = global_sym_tab->lookup (s); |
|
120 return (sr && sr->is_mapper_function ()); |
|
121 } |
|
122 |
593
|
123 // Is this function globally in this scope? |
|
124 |
1827
|
125 bool |
1755
|
126 is_globally_visible (const string& name) |
593
|
127 { |
2856
|
128 symbol_record *sr = curr_sym_tab->lookup (name); |
593
|
129 return (sr && sr->is_linked_to_global ()); |
|
130 } |
|
131 |
2086
|
132 // Is this octave_value a valid function? |
593
|
133 |
2975
|
134 octave_function * |
2856
|
135 is_valid_function (const octave_value& arg, const string& warn_for, bool warn) |
593
|
136 { |
2975
|
137 octave_function *ans = 0; |
593
|
138 |
1755
|
139 string fcn_name; |
1728
|
140 |
1517
|
141 if (arg.is_string ()) |
1755
|
142 fcn_name = arg.string_value (); |
1517
|
143 |
1755
|
144 if (fcn_name.empty () || error_state) |
593
|
145 { |
|
146 if (warn) |
1755
|
147 error ("%s: expecting function name as argument", |
|
148 warn_for.c_str ()); |
593
|
149 return ans; |
|
150 } |
|
151 |
|
152 symbol_record *sr = 0; |
1755
|
153 |
|
154 if (! fcn_name.empty ()) |
593
|
155 sr = lookup_by_name (fcn_name); |
|
156 |
|
157 if (sr) |
2975
|
158 { |
|
159 octave_value tmp = sr->def (); |
|
160 ans = tmp.function_value (true); |
|
161 } |
593
|
162 |
|
163 if (! sr || ! ans || ! sr->is_function ()) |
|
164 { |
|
165 if (warn) |
|
166 error ("%s: the symbol `%s' is not valid as a function", |
1755
|
167 warn_for.c_str (), fcn_name.c_str ()); |
593
|
168 ans = 0; |
|
169 } |
|
170 |
|
171 return ans; |
|
172 } |
|
173 |
2975
|
174 octave_function * |
2796
|
175 extract_function (const octave_value& arg, const string& warn_for, |
|
176 const string& fname, const string& header, |
|
177 const string& trailer) |
|
178 { |
2975
|
179 octave_function *retval = 0; |
2796
|
180 |
|
181 retval = is_valid_function (arg, warn_for, 0); |
|
182 |
|
183 if (! retval) |
|
184 { |
|
185 string s = arg.string_value (); |
|
186 |
|
187 string cmd = header; |
|
188 cmd.append (s); |
|
189 cmd.append (trailer); |
|
190 |
|
191 if (! error_state) |
|
192 { |
|
193 int parse_status; |
|
194 |
2898
|
195 eval_string (cmd, true, parse_status); |
2796
|
196 |
|
197 if (parse_status == 0) |
|
198 { |
|
199 retval = is_valid_function (fname, warn_for, 0); |
|
200 |
|
201 if (! retval) |
|
202 { |
|
203 error ("%s: `%s' is not valid as a function", |
|
204 warn_for.c_str (), fname.c_str ()); |
|
205 return retval; |
|
206 } |
|
207 } |
|
208 else |
|
209 error ("%s: `%s' is not valid as a function", |
|
210 warn_for.c_str (), fname.c_str ()); |
|
211 } |
|
212 else |
|
213 error ("%s: expecting first argument to be a string", |
|
214 warn_for.c_str ()); |
|
215 } |
|
216 |
|
217 return retval; |
|
218 } |
|
219 |
2921
|
220 string_vector |
|
221 get_struct_elts (const string& text) |
|
222 { |
|
223 int n = 1; |
|
224 |
|
225 size_t pos = 0; |
|
226 |
|
227 size_t len = text.length (); |
|
228 |
|
229 while ((pos = text.find ('.', pos)) != NPOS) |
|
230 { |
|
231 if (++pos == len) |
|
232 break; |
|
233 |
|
234 n++; |
|
235 } |
|
236 |
|
237 string_vector retval (n); |
|
238 |
|
239 pos = 0; |
|
240 |
|
241 for (int i = 0; i < n; i++) |
|
242 { |
|
243 size_t len = text.find ('.', pos); |
|
244 |
|
245 if (len != NPOS) |
|
246 len -= pos; |
|
247 |
|
248 retval[i] = text.substr (pos, len); |
|
249 |
|
250 if (len != NPOS) |
|
251 pos += len + 1; |
|
252 } |
|
253 |
|
254 return retval; |
|
255 } |
|
256 |
|
257 string_vector |
|
258 generate_struct_completions (const string& text, string& prefix, string& hint) |
|
259 { |
|
260 string_vector names; |
|
261 |
|
262 size_t pos = text.rfind ('.'); |
|
263 |
|
264 string id; |
|
265 string_vector elts; |
|
266 |
|
267 if (pos == NPOS) |
|
268 { |
|
269 hint = text; |
|
270 prefix = text; |
|
271 elts.resize (1, text); |
|
272 } |
|
273 else |
|
274 { |
|
275 if (pos == text.length ()) |
|
276 hint = ""; |
|
277 else |
|
278 hint = text.substr (pos+1); |
|
279 |
|
280 prefix = text.substr (0, pos); |
|
281 |
|
282 elts = get_struct_elts (prefix); |
|
283 } |
|
284 |
|
285 id = elts[0]; |
|
286 |
|
287 symbol_record *sr = curr_sym_tab->lookup (id); |
|
288 |
|
289 if (! sr) |
|
290 sr = global_sym_tab->lookup (id); |
|
291 |
|
292 if (sr && sr->is_defined ()) |
|
293 { |
2975
|
294 octave_value tmp = sr->def (); |
2921
|
295 |
2963
|
296 // XXX FIXME XXX -- make this work for all types that can do |
|
297 // structure reference operations. |
2975
|
298 if (tmp.is_map ()) |
2921
|
299 { |
|
300 for (int i = 1; i < elts.length (); i++) |
|
301 { |
2975
|
302 tmp = tmp.do_struct_elt_index_op (elts[i], true); |
2921
|
303 |
2975
|
304 if (! tmp.is_map ()) |
2921
|
305 break; |
|
306 } |
|
307 |
2975
|
308 if (tmp.is_map ()) |
2921
|
309 { |
2975
|
310 Octave_map m = tmp.map_value (); |
2921
|
311 |
|
312 names = m.make_name_list (); |
|
313 } |
|
314 } |
|
315 } |
|
316 |
|
317 return names; |
|
318 } |
|
319 |
|
320 bool |
|
321 looks_like_struct (const string& text) |
|
322 { |
|
323 bool retval = true; |
|
324 |
|
325 string_vector elts = get_struct_elts (text); |
|
326 |
|
327 string id = elts[0]; |
|
328 |
|
329 symbol_record *sr = curr_sym_tab->lookup (id); |
|
330 |
|
331 if (! sr) |
|
332 sr = global_sym_tab->lookup (id); |
|
333 |
|
334 if (sr && sr->is_defined ()) |
|
335 { |
2975
|
336 octave_value tmp = sr->def (); |
2921
|
337 |
2963
|
338 // XXX FIXME XXX -- should this work for all types that can do |
|
339 // structure reference operations? |
|
340 |
2975
|
341 if (tmp.is_map ()) |
2921
|
342 { |
|
343 for (int i = 1; i < elts.length (); i++) |
|
344 { |
2975
|
345 tmp = tmp.do_struct_elt_index_op (elts[i], true); |
2921
|
346 |
2975
|
347 if (! tmp.is_map ()) |
2921
|
348 { |
|
349 retval = false; |
|
350 break; |
|
351 } |
|
352 } |
|
353 } |
|
354 else |
|
355 retval = false; |
|
356 } |
|
357 else |
|
358 retval = false; |
|
359 |
|
360 return retval; |
|
361 } |
2796
|
362 |
1957
|
363 DEFUN (is_global, args, , |
593
|
364 "is_global (X): return 1 if the string X names a global variable\n\ |
|
365 otherwise, return 0.") |
|
366 { |
2086
|
367 octave_value_list retval = 0.0; |
593
|
368 |
712
|
369 int nargin = args.length (); |
|
370 |
|
371 if (nargin != 1) |
593
|
372 { |
|
373 print_usage ("is_global"); |
|
374 return retval; |
|
375 } |
|
376 |
1755
|
377 string name = args(0).string_value (); |
593
|
378 |
636
|
379 if (error_state) |
|
380 { |
|
381 error ("is_global: expecting string argument"); |
|
382 return retval; |
|
383 } |
|
384 |
2856
|
385 symbol_record *sr = curr_sym_tab->lookup (name); |
593
|
386 |
2800
|
387 retval = static_cast<double> (sr && sr->is_linked_to_global ()); |
593
|
388 |
|
389 return retval; |
|
390 } |
|
391 |
1957
|
392 DEFUN (exist, args, , |
593
|
393 "exist (NAME): check if variable or file exists\n\ |
|
394 \n\ |
1564
|
395 returns:\n\ |
|
396 \n\ |
|
397 0 : NAME is undefined\n\ |
|
398 1 : NAME is a variable\n\ |
|
399 2 : NAME is a function\n\ |
|
400 3 : NAME is a .oct file in the current LOADPATH\n\ |
|
401 5 : NAME is a built-in function") |
593
|
402 { |
2086
|
403 octave_value_list retval; |
593
|
404 |
712
|
405 int nargin = args.length (); |
|
406 |
|
407 if (nargin != 1) |
593
|
408 { |
|
409 print_usage ("exist"); |
|
410 return retval; |
|
411 } |
|
412 |
1755
|
413 string name = args(0).string_value (); |
593
|
414 |
636
|
415 if (error_state) |
|
416 { |
|
417 error ("exist: expecting string argument"); |
|
418 return retval; |
|
419 } |
|
420 |
1755
|
421 string struct_elts; |
2790
|
422 string symbol_name = name; |
1755
|
423 |
|
424 size_t pos = name.find ('.'); |
|
425 |
2790
|
426 if (pos != NPOS && pos > 0) |
1277
|
427 { |
1755
|
428 struct_elts = name.substr (pos+1); |
2790
|
429 symbol_name = name.substr (0, pos); |
1277
|
430 } |
|
431 |
2856
|
432 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
593
|
433 if (! sr) |
2856
|
434 sr = global_sym_tab->lookup (symbol_name); |
593
|
435 |
|
436 retval = 0.0; |
|
437 |
|
438 if (sr && sr->is_variable () && sr->is_defined ()) |
1277
|
439 { |
2390
|
440 if (struct_elts.empty () || sr->is_map_element (struct_elts)) |
|
441 retval = 1.0; |
1277
|
442 } |
1421
|
443 else if (sr && sr->is_builtin_function ()) |
|
444 { |
|
445 retval = 5.0; |
|
446 } |
|
447 else if (sr && sr->is_user_function ()) |
|
448 { |
|
449 retval = 2.0; |
|
450 } |
593
|
451 else |
|
452 { |
1755
|
453 string path = fcn_file_in_path (name); |
|
454 |
|
455 if (path.length () > 0) |
593
|
456 { |
|
457 retval = 2.0; |
|
458 } |
|
459 else |
|
460 { |
1421
|
461 path = oct_file_in_path (name); |
1755
|
462 |
|
463 if (path.length () > 0) |
1421
|
464 { |
|
465 retval = 3.0; |
|
466 } |
|
467 else |
|
468 { |
1766
|
469 file_stat fs (name); |
|
470 |
|
471 if (fs && fs.is_reg ()) |
1421
|
472 retval = 2.0; |
|
473 } |
593
|
474 } |
|
475 } |
|
476 |
|
477 return retval; |
|
478 } |
|
479 |
581
|
480 // Is there a corresponding function file that is newer than the |
|
481 // symbol definition? |
|
482 |
2926
|
483 static bool |
1
|
484 symbol_out_of_date (symbol_record *sr) |
|
485 { |
2926
|
486 bool retval = false; |
195
|
487 |
2926
|
488 if (Vignore_function_time_stamp != 2 && sr) |
1
|
489 { |
2975
|
490 octave_value ans = sr->def (); |
|
491 |
3022
|
492 octave_function *tmp = ans.function_value (true); |
2975
|
493 |
3022
|
494 if (tmp) |
|
495 { |
|
496 string ff = tmp->fcn_file_name (); |
1755
|
497 |
3022
|
498 if (! (ff.empty () |
|
499 || (Vignore_function_time_stamp |
|
500 && tmp->is_system_fcn_file ()))) |
|
501 { |
|
502 time_t tp = tmp->time_parsed (); |
2975
|
503 |
3022
|
504 string fname = fcn_file_in_path (ff); |
1755
|
505 |
3022
|
506 int status = file_stat::is_newer (fname, tp); |
|
507 |
|
508 if (status > 0) |
|
509 retval = true; |
1
|
510 } |
|
511 } |
|
512 } |
2926
|
513 |
|
514 return retval; |
1
|
515 } |
|
516 |
1827
|
517 bool |
2856
|
518 lookup (symbol_record *sym_rec, bool exec_script) |
581
|
519 { |
1827
|
520 bool script_executed = false; |
581
|
521 |
|
522 if (! sym_rec->is_linked_to_global ()) |
|
523 { |
|
524 if (sym_rec->is_defined ()) |
|
525 { |
|
526 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
527 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
528 } |
|
529 else if (! sym_rec->is_formal_parameter ()) |
|
530 { |
|
531 link_to_builtin_or_function (sym_rec); |
1271
|
532 |
581
|
533 if (! sym_rec->is_defined ()) |
1271
|
534 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
535 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
536 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
537 } |
|
538 } |
|
539 |
1271
|
540 return script_executed; |
581
|
541 } |
|
542 |
|
543 // Get the symbol record for the given name that is visible in the |
|
544 // current scope. Reread any function definitions that appear to be |
|
545 // out of date. If a function is available in a file but is not |
|
546 // currently loaded, this will load it and insert the name in the |
|
547 // current symbol table. |
|
548 |
|
549 symbol_record * |
2856
|
550 lookup_by_name (const string& nm, bool exec_script) |
581
|
551 { |
2856
|
552 symbol_record *sym_rec = curr_sym_tab->lookup (nm, true); |
581
|
553 |
|
554 lookup (sym_rec, exec_script); |
|
555 |
|
556 return sym_rec; |
|
557 } |
|
558 |
2849
|
559 octave_value |
|
560 get_global_value (const string& nm) |
|
561 { |
|
562 octave_value retval; |
|
563 |
|
564 symbol_record *sr = global_sym_tab->lookup (nm); |
|
565 |
|
566 if (sr) |
|
567 { |
2975
|
568 octave_value sr_def = sr->def (); |
2849
|
569 |
2975
|
570 if (sr_def.is_undefined ()) |
2849
|
571 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); |
|
572 } |
|
573 else |
|
574 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ()); |
|
575 |
|
576 return retval; |
|
577 } |
|
578 |
|
579 void |
|
580 set_global_value (const string& nm, const octave_value& val) |
|
581 { |
2856
|
582 symbol_record *sr = global_sym_tab->lookup (nm, true); |
2849
|
583 |
|
584 if (sr) |
|
585 sr->define (val); |
|
586 else |
|
587 panic_impossible (); |
|
588 } |
|
589 |
593
|
590 // Variable values. |
195
|
591 |
581
|
592 // Look for the given name in the global symbol table. If it refers |
|
593 // to a string, return a new copy. If not, return 0; |
|
594 |
1755
|
595 string |
|
596 builtin_string_variable (const string& name) |
1
|
597 { |
2856
|
598 symbol_record *sr = global_sym_tab->lookup (name); |
195
|
599 |
1271
|
600 // It is a prorgramming error to look for builtins that aren't. |
195
|
601 |
529
|
602 assert (sr); |
195
|
603 |
1755
|
604 string retval; |
1
|
605 |
2975
|
606 octave_value val = sr->def (); |
195
|
607 |
2975
|
608 if (! error_state && val.is_string ()) |
|
609 retval = val.string_value (); |
1
|
610 |
|
611 return retval; |
|
612 } |
|
613 |
581
|
614 // Look for the given name in the global symbol table. If it refers |
1504
|
615 // to a real scalar, place the value in d and return 1. Otherwise, |
|
616 // return 0. |
581
|
617 |
1
|
618 int |
1755
|
619 builtin_real_scalar_variable (const string& name, double& d) |
1
|
620 { |
1504
|
621 int status = 0; |
2856
|
622 symbol_record *sr = global_sym_tab->lookup (name); |
195
|
623 |
1271
|
624 // It is a prorgramming error to look for builtins that aren't. |
195
|
625 |
529
|
626 assert (sr); |
1
|
627 |
2975
|
628 octave_value val = sr->def (); |
195
|
629 |
2975
|
630 if (! error_state && val.is_scalar_type ()) |
|
631 { |
|
632 d = val.double_value (); |
|
633 status = 1; |
1
|
634 } |
|
635 |
|
636 return status; |
|
637 } |
|
638 |
1093
|
639 // Look for the given name in the global symbol table. |
|
640 |
2086
|
641 octave_value |
1755
|
642 builtin_any_variable (const string& name) |
1093
|
643 { |
2856
|
644 symbol_record *sr = global_sym_tab->lookup (name); |
1093
|
645 |
1271
|
646 // It is a prorgramming error to look for builtins that aren't. |
1093
|
647 |
|
648 assert (sr); |
|
649 |
2975
|
650 return sr->def (); |
1093
|
651 } |
|
652 |
593
|
653 // Global stuff and links to builtin variables and functions. |
|
654 |
581
|
655 // Make the definition of the symbol record sr be the same as the |
|
656 // definition of the global variable of the same name, creating it if |
1418
|
657 // it doesn't already exist. |
581
|
658 |
195
|
659 void |
|
660 link_to_global_variable (symbol_record *sr) |
|
661 { |
2975
|
662 if (! sr->is_linked_to_global ()) |
195
|
663 { |
2975
|
664 sr->mark_as_linked_to_global (); |
195
|
665 |
2975
|
666 if (! error_state) |
|
667 { |
|
668 string nm = sr->name (); |
2846
|
669 |
2975
|
670 symbol_record *gsr = global_sym_tab->lookup (nm, true); |
|
671 |
|
672 // There must be a better way to do this. XXX FIXME XXX |
195
|
673 |
2975
|
674 if (sr->is_variable ()) |
|
675 gsr->define (sr->def ()); |
|
676 else |
|
677 sr->clear (); |
1271
|
678 |
2975
|
679 // Make sure this symbol is a variable. |
2900
|
680 |
2975
|
681 if (! gsr->is_variable ()) |
|
682 gsr->define (octave_value ()); |
2900
|
683 |
2975
|
684 sr->alias (gsr, 1); |
|
685 } |
195
|
686 } |
|
687 } |
|
688 |
581
|
689 // Make the definition of the symbol record sr be the same as the |
|
690 // definition of the builtin variable of the same name. |
|
691 |
195
|
692 void |
|
693 link_to_builtin_variable (symbol_record *sr) |
|
694 { |
2856
|
695 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
195
|
696 |
529
|
697 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
698 sr->alias (tmp_sym); |
195
|
699 } |
|
700 |
581
|
701 // Make the definition of the symbol record sr be the same as the |
|
702 // definition of the builtin variable or function, or user function of |
|
703 // the same name, provided that the name has not been used as a formal |
|
704 // parameter. |
|
705 |
195
|
706 void |
|
707 link_to_builtin_or_function (symbol_record *sr) |
|
708 { |
2856
|
709 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
195
|
710 |
529
|
711 if (tmp_sym |
|
712 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
713 && ! tmp_sym->is_formal_parameter ()) |
|
714 sr->alias (tmp_sym); |
195
|
715 } |
|
716 |
581
|
717 // Force a link to a function in the current symbol table. This is |
|
718 // used just after defining a function to avoid different behavior |
|
719 // depending on whether or not the function has been evaluated after |
|
720 // being defined. |
|
721 // |
|
722 // Return without doing anything if there isn't a function with the |
|
723 // given name defined in the global symbol table. |
|
724 |
195
|
725 void |
1755
|
726 force_link_to_function (const string& id_name) |
195
|
727 { |
2856
|
728 symbol_record *gsr = global_sym_tab->lookup (id_name, true); |
195
|
729 if (gsr->is_function ()) |
|
730 { |
|
731 curr_sym_tab->clear (id_name); |
2856
|
732 symbol_record *csr = curr_sym_tab->lookup (id_name, true); |
195
|
733 csr->alias (gsr); |
|
734 } |
|
735 } |
|
736 |
2294
|
737 DEFUN (document, args, , |
|
738 "document (NAME, STRING)\n\ |
593
|
739 \n\ |
|
740 Associate a cryptic message with a variable name.") |
|
741 { |
2294
|
742 octave_value retval; |
1755
|
743 |
2294
|
744 int nargin = args.length (); |
1755
|
745 |
2294
|
746 if (nargin == 2) |
593
|
747 { |
2294
|
748 string name = args(0).string_value (); |
593
|
749 |
2294
|
750 if (! error_state) |
593
|
751 { |
2294
|
752 string help = args(1).string_value (); |
593
|
753 |
2294
|
754 if (! error_state) |
|
755 { |
|
756 if (is_builtin_variable (name) |
|
757 || is_text_function_name (name) |
|
758 || is_mapper_function_name (name) |
|
759 || is_builtin_function_name (name)) |
|
760 error ("document: can't redefine help for built-in variables and functions"); |
|
761 else |
|
762 { |
2856
|
763 symbol_record *sym_rec = curr_sym_tab->lookup (name); |
2294
|
764 |
|
765 if (sym_rec) |
|
766 sym_rec->document (help); |
|
767 else |
|
768 error ("document: no such symbol `%s'", name.c_str ()); |
|
769 } |
|
770 } |
593
|
771 } |
|
772 } |
|
773 else |
|
774 print_usage ("document"); |
|
775 |
|
776 return retval; |
|
777 } |
|
778 |
584
|
779 // XXX FIXME XXX -- this should take a list of regular expressions |
|
780 // naming the variables to look for. |
|
781 |
2086
|
782 static octave_value_list |
1755
|
783 do_who (int argc, const string_vector& argv) |
529
|
784 { |
2086
|
785 octave_value_list retval; |
529
|
786 |
2856
|
787 bool show_builtins = false; |
|
788 bool show_functions = (curr_sym_tab == top_level_sym_tab); |
|
789 bool show_variables = true; |
|
790 bool show_verbose = false; |
529
|
791 |
1755
|
792 string my_name = argv[0]; |
584
|
793 |
529
|
794 if (argc > 1) |
|
795 { |
2856
|
796 show_functions = false; |
|
797 show_variables = false; |
529
|
798 } |
|
799 |
1857
|
800 int i; |
|
801 for (i = 1; i < argc; i++) |
529
|
802 { |
1755
|
803 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
804 { |
2856
|
805 show_builtins = true; |
|
806 show_functions = true; |
|
807 show_variables = true; |
529
|
808 } |
1755
|
809 else if (argv[i] == "-builtins" || argv[i] == "-b") |
2856
|
810 show_builtins = true; |
1755
|
811 else if (argv[i] == "-functions" || argv[i] == "-f") |
2856
|
812 show_functions = true; |
1755
|
813 else if (argv[i] == "-long" || argv[i] == "-l") |
2856
|
814 show_verbose = true; |
1755
|
815 else if (argv[i] == "-variables" || argv[i] == "-v") |
2856
|
816 show_variables = true; |
1755
|
817 else if (argv[i][0] == '-') |
|
818 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
819 argv[i].c_str ()); |
529
|
820 else |
867
|
821 break; |
529
|
822 } |
|
823 |
1857
|
824 int npats = argc - i; |
|
825 string_vector pats (npats); |
|
826 for (int j = 0; j < npats; j++) |
|
827 pats[j] = argv[i+j]; |
|
828 |
1271
|
829 // If the user specified -l and nothing else, show variables. If |
|
830 // evaluating this at the top level, also show functions. |
529
|
831 |
|
832 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
833 { |
|
834 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
835 show_variables = 1; |
|
836 } |
|
837 |
|
838 int pad_after = 0; |
|
839 |
|
840 if (show_builtins) |
|
841 { |
3011
|
842 pad_after += global_sym_tab->maybe_list |
3013
|
843 ("*** built-in variables:", pats, octave_stdout, |
3011
|
844 show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
529
|
845 |
3011
|
846 pad_after += global_sym_tab->maybe_list |
3013
|
847 ("*** built-in functions:", pats, octave_stdout, |
3011
|
848 show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES); |
529
|
849 } |
|
850 |
|
851 if (show_functions) |
|
852 { |
3011
|
853 pad_after += global_sym_tab->maybe_list |
3013
|
854 ("*** currently compiled functions:", pats, |
3011
|
855 octave_stdout, show_verbose, symbol_record::USER_FUNCTION, |
|
856 SYMTAB_ALL_SCOPES); |
529
|
857 } |
|
858 |
|
859 if (show_variables) |
|
860 { |
3011
|
861 pad_after += curr_sym_tab->maybe_list |
3013
|
862 ("*** local user variables:", pats, octave_stdout, |
3011
|
863 show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE); |
529
|
864 |
3011
|
865 pad_after += curr_sym_tab->maybe_list |
3013
|
866 ("*** globally visible user variables:", pats, |
3011
|
867 octave_stdout, show_verbose, symbol_record::USER_VARIABLE, |
|
868 SYMTAB_GLOBAL_SCOPE); |
529
|
869 } |
|
870 |
|
871 if (pad_after) |
2095
|
872 octave_stdout << "\n"; |
529
|
873 |
581
|
874 return retval; |
|
875 } |
|
876 |
1957
|
877 DEFUN_TEXT (who, args, , |
581
|
878 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
879 \n\ |
|
880 List currently defined symbol(s). Options may be shortened to one\n\ |
|
881 character, but may not be combined.") |
|
882 { |
2086
|
883 octave_value_list retval; |
581
|
884 |
1755
|
885 int argc = args.length () + 1; |
|
886 |
1968
|
887 string_vector argv = args.make_argv ("who"); |
1755
|
888 |
|
889 if (error_state) |
|
890 return retval; |
581
|
891 |
1488
|
892 retval = do_who (argc, argv); |
581
|
893 |
529
|
894 return retval; |
|
895 } |
|
896 |
1957
|
897 DEFUN_TEXT (whos, args, , |
581
|
898 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
899 \n\ |
|
900 List currently defined symbol(s). Options may be shortened to one\n\ |
|
901 character, but may not be combined.") |
|
902 { |
2086
|
903 octave_value_list retval; |
581
|
904 |
712
|
905 int nargin = args.length (); |
|
906 |
2086
|
907 octave_value_list tmp_args; |
742
|
908 for (int i = nargin; i > 0; i--) |
|
909 tmp_args(i) = args(i-1); |
|
910 tmp_args(0) = "-long"; |
581
|
911 |
742
|
912 int argc = tmp_args.length () + 1; |
1755
|
913 |
1980
|
914 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
915 |
|
916 if (error_state) |
|
917 return retval; |
|
918 |
1488
|
919 retval = do_who (argc, argv); |
581
|
920 |
|
921 return retval; |
|
922 } |
|
923 |
593
|
924 // Defining variables. |
|
925 |
1162
|
926 void |
2856
|
927 bind_ans (const octave_value& val, bool print) |
1162
|
928 { |
2856
|
929 static symbol_record *sr = global_sym_tab->lookup ("ans", true); |
1162
|
930 |
2978
|
931 if (val.is_defined ()) |
|
932 { |
|
933 sr->define (val); |
1162
|
934 |
2978
|
935 if (print) |
|
936 val.print_with_name (octave_stdout, "ans"); |
|
937 } |
1162
|
938 } |
|
939 |
593
|
940 // Give a global variable a definition. This will insert the symbol |
|
941 // in the global table if necessary. |
|
942 |
|
943 // How is this different than install_builtin_variable? Are both |
|
944 // functions needed? |
|
945 |
|
946 void |
2390
|
947 bind_builtin_variable (const string& varname, const octave_value& val, |
2953
|
948 bool protect, bool eternal, |
3005
|
949 symbol_record::change_function chg_fcn, |
1755
|
950 const string& help) |
593
|
951 { |
2856
|
952 symbol_record *sr = global_sym_tab->lookup (varname, true); |
593
|
953 |
1271
|
954 // It is a programming error for a builtin symbol to be missing. |
|
955 // Besides, we just inserted it, so it must be there. |
593
|
956 |
|
957 assert (sr); |
|
958 |
|
959 sr->unprotect (); |
|
960 |
1271
|
961 // Must do this before define, since define will call the special |
|
962 // variable function only if it knows about it, and it needs to, so |
|
963 // that user prefs can be properly initialized. |
593
|
964 |
3005
|
965 if (chg_fcn) |
|
966 sr->set_change_function (chg_fcn); |
593
|
967 |
|
968 sr->define_builtin_var (val); |
|
969 |
|
970 if (protect) |
|
971 sr->protect (); |
|
972 |
|
973 if (eternal) |
|
974 sr->make_eternal (); |
|
975 |
1755
|
976 sr->document (help); |
529
|
977 } |
|
978 |
593
|
979 // Deleting names from the symbol tables. |
|
980 |
1957
|
981 DEFUN_TEXT (clear, args, , |
668
|
982 "clear [-x] [name ...]\n\ |
|
983 \n\ |
|
984 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
985 \n\ |
2802
|
986 If no arguments are given, clear all user-defined variables and\n\ |
668
|
987 functions.\n\ |
|
988 \n\ |
|
989 With -x, exclude the named variables") |
529
|
990 { |
2086
|
991 octave_value_list retval; |
593
|
992 |
1755
|
993 int argc = args.length () + 1; |
593
|
994 |
1968
|
995 string_vector argv = args.make_argv ("clear"); |
1755
|
996 |
|
997 if (error_state) |
|
998 return retval; |
668
|
999 |
1271
|
1000 // Always clear the local table, but don't clear currently compiled |
|
1001 // functions unless we are at the top level. (Allowing that to |
|
1002 // happen inside functions would result in pretty odd behavior...) |
593
|
1003 |
2856
|
1004 bool clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
593
|
1005 |
1755
|
1006 if (argc == 1) |
593
|
1007 { |
|
1008 curr_sym_tab->clear (); |
|
1009 global_sym_tab->clear (clear_user_functions); |
|
1010 } |
529
|
1011 else |
|
1012 { |
668
|
1013 int exclusive = 0; |
|
1014 |
1755
|
1015 int idx = 1; |
|
1016 |
|
1017 if (argc > 1) |
668
|
1018 { |
1755
|
1019 if (argv[idx] == "-x") |
|
1020 exclusive = 1; |
668
|
1021 } |
|
1022 |
|
1023 int lcount = 0; |
|
1024 int gcount = 0; |
|
1025 int fcount = 0; |
529
|
1026 |
1755
|
1027 string_vector lvars; |
|
1028 string_vector gvars; |
|
1029 string_vector fcns; |
668
|
1030 |
|
1031 if (argc > 0) |
593
|
1032 { |
3013
|
1033 string_vector tmp; |
|
1034 |
|
1035 lvars = curr_sym_tab->name_list (lcount, tmp, false, |
|
1036 SYMTAB_VARIABLES, |
|
1037 SYMTAB_LOCAL_SCOPE); |
668
|
1038 |
3013
|
1039 gvars = curr_sym_tab->name_list (gcount, tmp, false, |
|
1040 SYMTAB_VARIABLES, |
|
1041 SYMTAB_GLOBAL_SCOPE); |
668
|
1042 |
3013
|
1043 fcns = global_sym_tab->name_list (fcount, tmp, false, |
|
1044 symbol_record::USER_FUNCTION, |
|
1045 SYMTAB_ALL_SCOPES); |
668
|
1046 } |
|
1047 |
2438
|
1048 // XXX FIXME XXX -- this needs to be optimized to avoid the |
|
1049 // pattern matching code if the string doesn't contain any |
|
1050 // globbing patterns. |
|
1051 |
1970
|
1052 for (int k = idx; k < argc; k++) |
668
|
1053 { |
1755
|
1054 string patstr = argv[k]; |
668
|
1055 |
1755
|
1056 if (! patstr.empty ()) |
593
|
1057 { |
1792
|
1058 glob_match pattern (patstr); |
1755
|
1059 |
593
|
1060 int i; |
|
1061 for (i = 0; i < lcount; i++) |
|
1062 { |
1755
|
1063 string nm = lvars[i]; |
1792
|
1064 int match = pattern.match (nm); |
668
|
1065 if ((exclusive && ! match) || (! exclusive && match)) |
|
1066 curr_sym_tab->clear (nm); |
593
|
1067 } |
529
|
1068 |
593
|
1069 int count; |
|
1070 for (i = 0; i < gcount; i++) |
|
1071 { |
1755
|
1072 string nm = gvars[i]; |
1792
|
1073 int match = pattern.match (nm); |
668
|
1074 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1075 { |
668
|
1076 count = curr_sym_tab->clear (nm); |
593
|
1077 if (count > 0) |
668
|
1078 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1079 } |
|
1080 } |
529
|
1081 |
593
|
1082 for (i = 0; i < fcount; i++) |
|
1083 { |
1755
|
1084 string nm = fcns[i]; |
1792
|
1085 int match = pattern.match (nm); |
668
|
1086 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1087 { |
668
|
1088 count = curr_sym_tab->clear (nm); |
864
|
1089 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1090 } |
|
1091 } |
|
1092 } |
|
1093 } |
|
1094 } |
|
1095 |
|
1096 return retval; |
529
|
1097 } |
|
1098 |
3005
|
1099 DEFUN (__dump_symtab_info__, args, , |
|
1100 "__dump_symtab_info__ (): print raw symbol table statistices") |
|
1101 { |
|
1102 octave_value_list retval; |
|
1103 |
|
1104 int nargin = args.length (); |
|
1105 |
|
1106 if (nargin == 1) |
|
1107 { |
|
1108 string arg = args(0).string_value (); |
|
1109 |
|
1110 if (arg == "global") |
|
1111 global_sym_tab->print_stats (); |
|
1112 else |
|
1113 print_usage ("__dump_symtab_info__"); |
|
1114 } |
|
1115 else if (nargin == 0) |
|
1116 curr_sym_tab->print_stats (); |
|
1117 else |
|
1118 print_usage ("__dump_symtab_info__"); |
|
1119 |
|
1120 return retval; |
|
1121 } |
|
1122 |
3016
|
1123 // XXX FIXME XXX -- some of these should do their own checking to be |
|
1124 // able to provide more meaningful warning or error messages. |
|
1125 |
|
1126 static int |
|
1127 ignore_function_time_stamp (void) |
|
1128 { |
|
1129 int pref = 0; |
|
1130 |
|
1131 string val = builtin_string_variable ("ignore_function_time_stamp"); |
|
1132 |
|
1133 if (! val.empty ()) |
|
1134 { |
|
1135 if (val.compare ("all", 0, 3) == 0) |
|
1136 pref = 2; |
|
1137 if (val.compare ("system", 0, 6) == 0) |
|
1138 pref = 1; |
|
1139 } |
|
1140 |
|
1141 Vignore_function_time_stamp = pref; |
|
1142 |
|
1143 return 0; |
|
1144 } |
|
1145 |
|
1146 // XXX FIXME XXX -- there still may be better places for some of these |
|
1147 // to be defined. |
|
1148 |
|
1149 void |
|
1150 symbols_of_variables (void) |
|
1151 { |
|
1152 DEFVAR (ans, , 0, 0, |
|
1153 ""); |
|
1154 |
|
1155 DEFVAR (ignore_function_time_stamp, "system", 0, ignore_function_time_stamp, |
|
1156 "don't check to see if function files have changed since they were\n\ |
3020
|
1157 last compiled. Possible values are \"system\" and \"all\""); |
3016
|
1158 } |
|
1159 |
1
|
1160 /* |
|
1161 ;;; Local Variables: *** |
|
1162 ;;; mode: C++ *** |
|
1163 ;;; End: *** |
|
1164 */ |