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\ |
3096
|
401 5 : NAME is a built-in function\n\ |
|
402 \n\ |
|
403 This function also returns 2 if a regular file called NAME exists in\n\ |
|
404 Octave's LOADPATH. If you want information about other types of\n\ |
|
405 files, you should use some combination of the functions file_in_path\n\ |
|
406 and stat instead.") |
593
|
407 { |
2086
|
408 octave_value_list retval; |
593
|
409 |
712
|
410 int nargin = args.length (); |
|
411 |
|
412 if (nargin != 1) |
593
|
413 { |
|
414 print_usage ("exist"); |
|
415 return retval; |
|
416 } |
|
417 |
1755
|
418 string name = args(0).string_value (); |
593
|
419 |
636
|
420 if (error_state) |
|
421 { |
|
422 error ("exist: expecting string argument"); |
|
423 return retval; |
|
424 } |
|
425 |
1755
|
426 string struct_elts; |
2790
|
427 string symbol_name = name; |
1755
|
428 |
|
429 size_t pos = name.find ('.'); |
|
430 |
2790
|
431 if (pos != NPOS && pos > 0) |
1277
|
432 { |
1755
|
433 struct_elts = name.substr (pos+1); |
2790
|
434 symbol_name = name.substr (0, pos); |
1277
|
435 } |
|
436 |
2856
|
437 symbol_record *sr = curr_sym_tab->lookup (symbol_name); |
593
|
438 if (! sr) |
2856
|
439 sr = global_sym_tab->lookup (symbol_name); |
593
|
440 |
|
441 retval = 0.0; |
|
442 |
|
443 if (sr && sr->is_variable () && sr->is_defined ()) |
1277
|
444 { |
2390
|
445 if (struct_elts.empty () || sr->is_map_element (struct_elts)) |
|
446 retval = 1.0; |
1277
|
447 } |
1421
|
448 else if (sr && sr->is_builtin_function ()) |
|
449 { |
|
450 retval = 5.0; |
|
451 } |
|
452 else if (sr && sr->is_user_function ()) |
|
453 { |
|
454 retval = 2.0; |
|
455 } |
593
|
456 else |
|
457 { |
1755
|
458 string path = fcn_file_in_path (name); |
|
459 |
|
460 if (path.length () > 0) |
593
|
461 { |
|
462 retval = 2.0; |
|
463 } |
|
464 else |
|
465 { |
1421
|
466 path = oct_file_in_path (name); |
1755
|
467 |
|
468 if (path.length () > 0) |
1421
|
469 { |
|
470 retval = 3.0; |
|
471 } |
|
472 else |
|
473 { |
3096
|
474 string file_name = file_in_path (name, ""); |
1766
|
475 |
3096
|
476 if (! file_name.empty ()) |
|
477 { |
|
478 file_stat fs (file_name); |
|
479 |
|
480 if (fs && fs.is_reg ()) |
|
481 retval = 2.0; |
|
482 } |
1421
|
483 } |
593
|
484 } |
|
485 } |
|
486 |
|
487 return retval; |
|
488 } |
|
489 |
581
|
490 // Is there a corresponding function file that is newer than the |
|
491 // symbol definition? |
|
492 |
2926
|
493 static bool |
1
|
494 symbol_out_of_date (symbol_record *sr) |
|
495 { |
2926
|
496 bool retval = false; |
195
|
497 |
2926
|
498 if (Vignore_function_time_stamp != 2 && sr) |
1
|
499 { |
2975
|
500 octave_value ans = sr->def (); |
|
501 |
3022
|
502 octave_function *tmp = ans.function_value (true); |
2975
|
503 |
3022
|
504 if (tmp) |
|
505 { |
|
506 string ff = tmp->fcn_file_name (); |
1755
|
507 |
3022
|
508 if (! (ff.empty () |
|
509 || (Vignore_function_time_stamp |
|
510 && tmp->is_system_fcn_file ()))) |
|
511 { |
|
512 time_t tp = tmp->time_parsed (); |
2975
|
513 |
3022
|
514 string fname = fcn_file_in_path (ff); |
1755
|
515 |
3022
|
516 int status = file_stat::is_newer (fname, tp); |
|
517 |
|
518 if (status > 0) |
|
519 retval = true; |
1
|
520 } |
|
521 } |
|
522 } |
2926
|
523 |
|
524 return retval; |
1
|
525 } |
|
526 |
1827
|
527 bool |
2856
|
528 lookup (symbol_record *sym_rec, bool exec_script) |
581
|
529 { |
1827
|
530 bool script_executed = false; |
581
|
531 |
|
532 if (! sym_rec->is_linked_to_global ()) |
|
533 { |
|
534 if (sym_rec->is_defined ()) |
|
535 { |
|
536 if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
537 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
538 } |
|
539 else if (! sym_rec->is_formal_parameter ()) |
|
540 { |
|
541 link_to_builtin_or_function (sym_rec); |
1271
|
542 |
581
|
543 if (! sym_rec->is_defined ()) |
1271
|
544 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
545 else if (sym_rec->is_function () && symbol_out_of_date (sym_rec)) |
1271
|
546 script_executed = load_fcn_from_file (sym_rec, exec_script); |
581
|
547 } |
|
548 } |
|
549 |
1271
|
550 return script_executed; |
581
|
551 } |
|
552 |
|
553 // Get the symbol record for the given name that is visible in the |
|
554 // current scope. Reread any function definitions that appear to be |
|
555 // out of date. If a function is available in a file but is not |
|
556 // currently loaded, this will load it and insert the name in the |
|
557 // current symbol table. |
|
558 |
|
559 symbol_record * |
2856
|
560 lookup_by_name (const string& nm, bool exec_script) |
581
|
561 { |
2856
|
562 symbol_record *sym_rec = curr_sym_tab->lookup (nm, true); |
581
|
563 |
|
564 lookup (sym_rec, exec_script); |
|
565 |
|
566 return sym_rec; |
|
567 } |
|
568 |
2849
|
569 octave_value |
|
570 get_global_value (const string& nm) |
|
571 { |
|
572 octave_value retval; |
|
573 |
|
574 symbol_record *sr = global_sym_tab->lookup (nm); |
|
575 |
|
576 if (sr) |
|
577 { |
2975
|
578 octave_value sr_def = sr->def (); |
2849
|
579 |
2975
|
580 if (sr_def.is_undefined ()) |
2849
|
581 error ("get_global_by_name: undefined symbol `%s'", nm.c_str ()); |
3088
|
582 else |
|
583 retval = sr_def; |
2849
|
584 } |
|
585 else |
|
586 error ("get_global_by_name: unknown symbol `%s'", nm.c_str ()); |
|
587 |
|
588 return retval; |
|
589 } |
|
590 |
|
591 void |
|
592 set_global_value (const string& nm, const octave_value& val) |
|
593 { |
2856
|
594 symbol_record *sr = global_sym_tab->lookup (nm, true); |
2849
|
595 |
|
596 if (sr) |
|
597 sr->define (val); |
|
598 else |
|
599 panic_impossible (); |
|
600 } |
|
601 |
593
|
602 // Variable values. |
195
|
603 |
581
|
604 // Look for the given name in the global symbol table. If it refers |
|
605 // to a string, return a new copy. If not, return 0; |
|
606 |
1755
|
607 string |
|
608 builtin_string_variable (const string& name) |
1
|
609 { |
2856
|
610 symbol_record *sr = global_sym_tab->lookup (name); |
195
|
611 |
1271
|
612 // It is a prorgramming error to look for builtins that aren't. |
195
|
613 |
529
|
614 assert (sr); |
195
|
615 |
1755
|
616 string retval; |
1
|
617 |
2975
|
618 octave_value val = sr->def (); |
195
|
619 |
2975
|
620 if (! error_state && val.is_string ()) |
|
621 retval = val.string_value (); |
1
|
622 |
|
623 return retval; |
|
624 } |
|
625 |
581
|
626 // Look for the given name in the global symbol table. If it refers |
1504
|
627 // to a real scalar, place the value in d and return 1. Otherwise, |
|
628 // return 0. |
581
|
629 |
1
|
630 int |
1755
|
631 builtin_real_scalar_variable (const string& name, double& d) |
1
|
632 { |
1504
|
633 int status = 0; |
2856
|
634 symbol_record *sr = global_sym_tab->lookup (name); |
195
|
635 |
1271
|
636 // It is a prorgramming error to look for builtins that aren't. |
195
|
637 |
529
|
638 assert (sr); |
1
|
639 |
2975
|
640 octave_value val = sr->def (); |
195
|
641 |
2975
|
642 if (! error_state && val.is_scalar_type ()) |
|
643 { |
|
644 d = val.double_value (); |
|
645 status = 1; |
1
|
646 } |
|
647 |
|
648 return status; |
|
649 } |
|
650 |
1093
|
651 // Look for the given name in the global symbol table. |
|
652 |
2086
|
653 octave_value |
1755
|
654 builtin_any_variable (const string& name) |
1093
|
655 { |
2856
|
656 symbol_record *sr = global_sym_tab->lookup (name); |
1093
|
657 |
1271
|
658 // It is a prorgramming error to look for builtins that aren't. |
1093
|
659 |
|
660 assert (sr); |
|
661 |
2975
|
662 return sr->def (); |
1093
|
663 } |
|
664 |
593
|
665 // Global stuff and links to builtin variables and functions. |
|
666 |
581
|
667 // Make the definition of the symbol record sr be the same as the |
|
668 // definition of the global variable of the same name, creating it if |
1418
|
669 // it doesn't already exist. |
581
|
670 |
195
|
671 void |
|
672 link_to_global_variable (symbol_record *sr) |
|
673 { |
2975
|
674 if (! sr->is_linked_to_global ()) |
195
|
675 { |
2975
|
676 sr->mark_as_linked_to_global (); |
195
|
677 |
2975
|
678 if (! error_state) |
|
679 { |
|
680 string nm = sr->name (); |
2846
|
681 |
2975
|
682 symbol_record *gsr = global_sym_tab->lookup (nm, true); |
|
683 |
|
684 // There must be a better way to do this. XXX FIXME XXX |
195
|
685 |
2975
|
686 if (sr->is_variable ()) |
|
687 gsr->define (sr->def ()); |
|
688 else |
|
689 sr->clear (); |
1271
|
690 |
2975
|
691 // Make sure this symbol is a variable. |
2900
|
692 |
2975
|
693 if (! gsr->is_variable ()) |
|
694 gsr->define (octave_value ()); |
2900
|
695 |
2975
|
696 sr->alias (gsr, 1); |
|
697 } |
195
|
698 } |
|
699 } |
|
700 |
581
|
701 // Make the definition of the symbol record sr be the same as the |
|
702 // definition of the builtin variable of the same name. |
|
703 |
195
|
704 void |
|
705 link_to_builtin_variable (symbol_record *sr) |
|
706 { |
2856
|
707 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
195
|
708 |
529
|
709 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
710 sr->alias (tmp_sym); |
195
|
711 } |
|
712 |
581
|
713 // Make the definition of the symbol record sr be the same as the |
|
714 // definition of the builtin variable or function, or user function of |
|
715 // the same name, provided that the name has not been used as a formal |
|
716 // parameter. |
|
717 |
195
|
718 void |
|
719 link_to_builtin_or_function (symbol_record *sr) |
|
720 { |
2856
|
721 symbol_record *tmp_sym = global_sym_tab->lookup (sr->name ()); |
195
|
722 |
529
|
723 if (tmp_sym |
|
724 && (tmp_sym->is_builtin_variable () || tmp_sym->is_function ()) |
|
725 && ! tmp_sym->is_formal_parameter ()) |
|
726 sr->alias (tmp_sym); |
195
|
727 } |
|
728 |
581
|
729 // Force a link to a function in the current symbol table. This is |
|
730 // used just after defining a function to avoid different behavior |
|
731 // depending on whether or not the function has been evaluated after |
|
732 // being defined. |
|
733 // |
|
734 // Return without doing anything if there isn't a function with the |
|
735 // given name defined in the global symbol table. |
|
736 |
195
|
737 void |
1755
|
738 force_link_to_function (const string& id_name) |
195
|
739 { |
2856
|
740 symbol_record *gsr = global_sym_tab->lookup (id_name, true); |
195
|
741 if (gsr->is_function ()) |
|
742 { |
|
743 curr_sym_tab->clear (id_name); |
2856
|
744 symbol_record *csr = curr_sym_tab->lookup (id_name, true); |
195
|
745 csr->alias (gsr); |
|
746 } |
|
747 } |
|
748 |
2294
|
749 DEFUN (document, args, , |
|
750 "document (NAME, STRING)\n\ |
593
|
751 \n\ |
|
752 Associate a cryptic message with a variable name.") |
|
753 { |
2294
|
754 octave_value retval; |
1755
|
755 |
2294
|
756 int nargin = args.length (); |
1755
|
757 |
2294
|
758 if (nargin == 2) |
593
|
759 { |
2294
|
760 string name = args(0).string_value (); |
593
|
761 |
2294
|
762 if (! error_state) |
593
|
763 { |
2294
|
764 string help = args(1).string_value (); |
593
|
765 |
2294
|
766 if (! error_state) |
|
767 { |
|
768 if (is_builtin_variable (name) |
|
769 || is_text_function_name (name) |
|
770 || is_mapper_function_name (name) |
|
771 || is_builtin_function_name (name)) |
|
772 error ("document: can't redefine help for built-in variables and functions"); |
|
773 else |
|
774 { |
2856
|
775 symbol_record *sym_rec = curr_sym_tab->lookup (name); |
2294
|
776 |
|
777 if (sym_rec) |
|
778 sym_rec->document (help); |
|
779 else |
|
780 error ("document: no such symbol `%s'", name.c_str ()); |
|
781 } |
|
782 } |
593
|
783 } |
|
784 } |
|
785 else |
|
786 print_usage ("document"); |
|
787 |
|
788 return retval; |
|
789 } |
|
790 |
584
|
791 // XXX FIXME XXX -- this should take a list of regular expressions |
|
792 // naming the variables to look for. |
|
793 |
2086
|
794 static octave_value_list |
1755
|
795 do_who (int argc, const string_vector& argv) |
529
|
796 { |
2086
|
797 octave_value_list retval; |
529
|
798 |
2856
|
799 bool show_builtins = false; |
|
800 bool show_functions = (curr_sym_tab == top_level_sym_tab); |
|
801 bool show_variables = true; |
|
802 bool show_verbose = false; |
529
|
803 |
1755
|
804 string my_name = argv[0]; |
584
|
805 |
529
|
806 if (argc > 1) |
|
807 { |
2856
|
808 show_functions = false; |
|
809 show_variables = false; |
529
|
810 } |
|
811 |
1857
|
812 int i; |
|
813 for (i = 1; i < argc; i++) |
529
|
814 { |
1755
|
815 if (argv[i] == "-all" || argv[i] == "-a") |
529
|
816 { |
2856
|
817 show_builtins = true; |
|
818 show_functions = true; |
|
819 show_variables = true; |
529
|
820 } |
1755
|
821 else if (argv[i] == "-builtins" || argv[i] == "-b") |
2856
|
822 show_builtins = true; |
1755
|
823 else if (argv[i] == "-functions" || argv[i] == "-f") |
2856
|
824 show_functions = true; |
1755
|
825 else if (argv[i] == "-long" || argv[i] == "-l") |
2856
|
826 show_verbose = true; |
1755
|
827 else if (argv[i] == "-variables" || argv[i] == "-v") |
2856
|
828 show_variables = true; |
1755
|
829 else if (argv[i][0] == '-') |
|
830 warning ("%s: unrecognized option `%s'", my_name.c_str (), |
|
831 argv[i].c_str ()); |
529
|
832 else |
867
|
833 break; |
529
|
834 } |
|
835 |
1857
|
836 int npats = argc - i; |
|
837 string_vector pats (npats); |
|
838 for (int j = 0; j < npats; j++) |
|
839 pats[j] = argv[i+j]; |
|
840 |
1271
|
841 // If the user specified -l and nothing else, show variables. If |
|
842 // evaluating this at the top level, also show functions. |
529
|
843 |
|
844 if (show_verbose && ! (show_builtins || show_functions || show_variables)) |
|
845 { |
|
846 show_functions = (curr_sym_tab == top_level_sym_tab); |
|
847 show_variables = 1; |
|
848 } |
|
849 |
|
850 int pad_after = 0; |
|
851 |
|
852 if (show_builtins) |
|
853 { |
3011
|
854 pad_after += global_sym_tab->maybe_list |
3013
|
855 ("*** built-in variables:", pats, octave_stdout, |
3011
|
856 show_verbose, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
529
|
857 |
3011
|
858 pad_after += global_sym_tab->maybe_list |
3013
|
859 ("*** built-in functions:", pats, octave_stdout, |
3011
|
860 show_verbose, symbol_record::BUILTIN_FUNCTION, SYMTAB_ALL_SCOPES); |
529
|
861 } |
|
862 |
|
863 if (show_functions) |
|
864 { |
3011
|
865 pad_after += global_sym_tab->maybe_list |
3013
|
866 ("*** currently compiled functions:", pats, |
3011
|
867 octave_stdout, show_verbose, symbol_record::USER_FUNCTION, |
|
868 SYMTAB_ALL_SCOPES); |
529
|
869 } |
|
870 |
|
871 if (show_variables) |
|
872 { |
3011
|
873 pad_after += curr_sym_tab->maybe_list |
3013
|
874 ("*** local user variables:", pats, octave_stdout, |
3011
|
875 show_verbose, symbol_record::USER_VARIABLE, SYMTAB_LOCAL_SCOPE); |
529
|
876 |
3011
|
877 pad_after += curr_sym_tab->maybe_list |
3013
|
878 ("*** globally visible user variables:", pats, |
3011
|
879 octave_stdout, show_verbose, symbol_record::USER_VARIABLE, |
|
880 SYMTAB_GLOBAL_SCOPE); |
529
|
881 } |
|
882 |
|
883 if (pad_after) |
2095
|
884 octave_stdout << "\n"; |
529
|
885 |
581
|
886 return retval; |
|
887 } |
|
888 |
1957
|
889 DEFUN_TEXT (who, args, , |
581
|
890 "who [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
891 \n\ |
|
892 List currently defined symbol(s). Options may be shortened to one\n\ |
|
893 character, but may not be combined.") |
|
894 { |
2086
|
895 octave_value_list retval; |
581
|
896 |
1755
|
897 int argc = args.length () + 1; |
|
898 |
1968
|
899 string_vector argv = args.make_argv ("who"); |
1755
|
900 |
|
901 if (error_state) |
|
902 return retval; |
581
|
903 |
1488
|
904 retval = do_who (argc, argv); |
581
|
905 |
529
|
906 return retval; |
|
907 } |
|
908 |
1957
|
909 DEFUN_TEXT (whos, args, , |
581
|
910 "whos [-all] [-builtins] [-functions] [-long] [-variables]\n\ |
|
911 \n\ |
|
912 List currently defined symbol(s). Options may be shortened to one\n\ |
|
913 character, but may not be combined.") |
|
914 { |
2086
|
915 octave_value_list retval; |
581
|
916 |
712
|
917 int nargin = args.length (); |
|
918 |
2086
|
919 octave_value_list tmp_args; |
742
|
920 for (int i = nargin; i > 0; i--) |
|
921 tmp_args(i) = args(i-1); |
|
922 tmp_args(0) = "-long"; |
581
|
923 |
742
|
924 int argc = tmp_args.length () + 1; |
1755
|
925 |
1980
|
926 string_vector argv = tmp_args.make_argv ("whos"); |
581
|
927 |
|
928 if (error_state) |
|
929 return retval; |
|
930 |
1488
|
931 retval = do_who (argc, argv); |
581
|
932 |
|
933 return retval; |
|
934 } |
|
935 |
593
|
936 // Defining variables. |
|
937 |
1162
|
938 void |
2856
|
939 bind_ans (const octave_value& val, bool print) |
1162
|
940 { |
2856
|
941 static symbol_record *sr = global_sym_tab->lookup ("ans", true); |
1162
|
942 |
2978
|
943 if (val.is_defined ()) |
|
944 { |
|
945 sr->define (val); |
1162
|
946 |
2978
|
947 if (print) |
|
948 val.print_with_name (octave_stdout, "ans"); |
|
949 } |
1162
|
950 } |
|
951 |
593
|
952 // Give a global variable a definition. This will insert the symbol |
|
953 // in the global table if necessary. |
|
954 |
|
955 // How is this different than install_builtin_variable? Are both |
|
956 // functions needed? |
|
957 |
|
958 void |
2390
|
959 bind_builtin_variable (const string& varname, const octave_value& val, |
2953
|
960 bool protect, bool eternal, |
3005
|
961 symbol_record::change_function chg_fcn, |
1755
|
962 const string& help) |
593
|
963 { |
2856
|
964 symbol_record *sr = global_sym_tab->lookup (varname, true); |
593
|
965 |
1271
|
966 // It is a programming error for a builtin symbol to be missing. |
|
967 // Besides, we just inserted it, so it must be there. |
593
|
968 |
|
969 assert (sr); |
|
970 |
|
971 sr->unprotect (); |
|
972 |
1271
|
973 // Must do this before define, since define will call the special |
|
974 // variable function only if it knows about it, and it needs to, so |
|
975 // that user prefs can be properly initialized. |
593
|
976 |
3005
|
977 if (chg_fcn) |
|
978 sr->set_change_function (chg_fcn); |
593
|
979 |
|
980 sr->define_builtin_var (val); |
|
981 |
|
982 if (protect) |
|
983 sr->protect (); |
|
984 |
|
985 if (eternal) |
|
986 sr->make_eternal (); |
|
987 |
1755
|
988 sr->document (help); |
529
|
989 } |
|
990 |
593
|
991 // Deleting names from the symbol tables. |
|
992 |
1957
|
993 DEFUN_TEXT (clear, args, , |
668
|
994 "clear [-x] [name ...]\n\ |
|
995 \n\ |
|
996 Clear symbol(s) matching a list of globbing patterns.\n\ |
593
|
997 \n\ |
2802
|
998 If no arguments are given, clear all user-defined variables and\n\ |
668
|
999 functions.\n\ |
|
1000 \n\ |
|
1001 With -x, exclude the named variables") |
529
|
1002 { |
2086
|
1003 octave_value_list retval; |
593
|
1004 |
1755
|
1005 int argc = args.length () + 1; |
593
|
1006 |
1968
|
1007 string_vector argv = args.make_argv ("clear"); |
1755
|
1008 |
|
1009 if (error_state) |
|
1010 return retval; |
668
|
1011 |
1271
|
1012 // Always clear the local table, but don't clear currently compiled |
|
1013 // functions unless we are at the top level. (Allowing that to |
|
1014 // happen inside functions would result in pretty odd behavior...) |
593
|
1015 |
2856
|
1016 bool clear_user_functions = (curr_sym_tab == top_level_sym_tab); |
593
|
1017 |
1755
|
1018 if (argc == 1) |
593
|
1019 { |
|
1020 curr_sym_tab->clear (); |
|
1021 global_sym_tab->clear (clear_user_functions); |
|
1022 } |
529
|
1023 else |
|
1024 { |
668
|
1025 int exclusive = 0; |
|
1026 |
1755
|
1027 int idx = 1; |
|
1028 |
|
1029 if (argc > 1) |
668
|
1030 { |
1755
|
1031 if (argv[idx] == "-x") |
|
1032 exclusive = 1; |
668
|
1033 } |
|
1034 |
|
1035 int lcount = 0; |
|
1036 int gcount = 0; |
|
1037 int fcount = 0; |
529
|
1038 |
1755
|
1039 string_vector lvars; |
|
1040 string_vector gvars; |
|
1041 string_vector fcns; |
668
|
1042 |
|
1043 if (argc > 0) |
593
|
1044 { |
3013
|
1045 string_vector tmp; |
|
1046 |
|
1047 lvars = curr_sym_tab->name_list (lcount, tmp, false, |
|
1048 SYMTAB_VARIABLES, |
|
1049 SYMTAB_LOCAL_SCOPE); |
668
|
1050 |
3013
|
1051 gvars = curr_sym_tab->name_list (gcount, tmp, false, |
|
1052 SYMTAB_VARIABLES, |
|
1053 SYMTAB_GLOBAL_SCOPE); |
668
|
1054 |
3013
|
1055 fcns = global_sym_tab->name_list (fcount, tmp, false, |
|
1056 symbol_record::USER_FUNCTION, |
|
1057 SYMTAB_ALL_SCOPES); |
668
|
1058 } |
|
1059 |
2438
|
1060 // XXX FIXME XXX -- this needs to be optimized to avoid the |
|
1061 // pattern matching code if the string doesn't contain any |
|
1062 // globbing patterns. |
|
1063 |
1970
|
1064 for (int k = idx; k < argc; k++) |
668
|
1065 { |
1755
|
1066 string patstr = argv[k]; |
668
|
1067 |
1755
|
1068 if (! patstr.empty ()) |
593
|
1069 { |
1792
|
1070 glob_match pattern (patstr); |
1755
|
1071 |
593
|
1072 int i; |
|
1073 for (i = 0; i < lcount; i++) |
|
1074 { |
1755
|
1075 string nm = lvars[i]; |
1792
|
1076 int match = pattern.match (nm); |
668
|
1077 if ((exclusive && ! match) || (! exclusive && match)) |
|
1078 curr_sym_tab->clear (nm); |
593
|
1079 } |
529
|
1080 |
593
|
1081 int count; |
|
1082 for (i = 0; i < gcount; i++) |
|
1083 { |
1755
|
1084 string nm = gvars[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); |
593
|
1089 if (count > 0) |
668
|
1090 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1091 } |
|
1092 } |
529
|
1093 |
593
|
1094 for (i = 0; i < fcount; i++) |
|
1095 { |
1755
|
1096 string nm = fcns[i]; |
1792
|
1097 int match = pattern.match (nm); |
668
|
1098 if ((exclusive && ! match) || (! exclusive && match)) |
593
|
1099 { |
668
|
1100 count = curr_sym_tab->clear (nm); |
864
|
1101 global_sym_tab->clear (nm, clear_user_functions); |
593
|
1102 } |
|
1103 } |
|
1104 } |
|
1105 } |
|
1106 } |
|
1107 |
|
1108 return retval; |
529
|
1109 } |
|
1110 |
3005
|
1111 DEFUN (__dump_symtab_info__, args, , |
|
1112 "__dump_symtab_info__ (): print raw symbol table statistices") |
|
1113 { |
|
1114 octave_value_list retval; |
|
1115 |
|
1116 int nargin = args.length (); |
|
1117 |
|
1118 if (nargin == 1) |
|
1119 { |
|
1120 string arg = args(0).string_value (); |
|
1121 |
|
1122 if (arg == "global") |
|
1123 global_sym_tab->print_stats (); |
|
1124 else |
|
1125 print_usage ("__dump_symtab_info__"); |
|
1126 } |
|
1127 else if (nargin == 0) |
|
1128 curr_sym_tab->print_stats (); |
|
1129 else |
|
1130 print_usage ("__dump_symtab_info__"); |
|
1131 |
|
1132 return retval; |
|
1133 } |
|
1134 |
3016
|
1135 // XXX FIXME XXX -- some of these should do their own checking to be |
|
1136 // able to provide more meaningful warning or error messages. |
|
1137 |
|
1138 static int |
|
1139 ignore_function_time_stamp (void) |
|
1140 { |
|
1141 int pref = 0; |
|
1142 |
|
1143 string val = builtin_string_variable ("ignore_function_time_stamp"); |
|
1144 |
|
1145 if (! val.empty ()) |
|
1146 { |
|
1147 if (val.compare ("all", 0, 3) == 0) |
|
1148 pref = 2; |
|
1149 if (val.compare ("system", 0, 6) == 0) |
|
1150 pref = 1; |
|
1151 } |
|
1152 |
|
1153 Vignore_function_time_stamp = pref; |
|
1154 |
|
1155 return 0; |
|
1156 } |
|
1157 |
|
1158 // XXX FIXME XXX -- there still may be better places for some of these |
|
1159 // to be defined. |
|
1160 |
|
1161 void |
|
1162 symbols_of_variables (void) |
|
1163 { |
|
1164 DEFVAR (ans, , 0, 0, |
|
1165 ""); |
|
1166 |
|
1167 DEFVAR (ignore_function_time_stamp, "system", 0, ignore_function_time_stamp, |
|
1168 "don't check to see if function files have changed since they were\n\ |
3020
|
1169 last compiled. Possible values are \"system\" and \"all\""); |
3016
|
1170 } |
|
1171 |
1
|
1172 /* |
|
1173 ;;; Local Variables: *** |
|
1174 ;;; mode: C++ *** |
|
1175 ;;; End: *** |
|
1176 */ |