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