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