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