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