1
|
1 // variables.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include <sys/types.h> |
|
29 #ifdef HAVE_UNISTD_H |
|
30 #include <unistd.h> |
|
31 #endif |
|
32 #include <ctype.h> |
|
33 #include <iostream.h> |
|
34 |
|
35 #include "statdefs.h" |
|
36 #include "tree-const.h" |
|
37 #include "variables.h" |
|
38 #include "symtab.h" |
|
39 #include "error.h" |
|
40 #include "utils.h" |
|
41 #include "tree.h" |
164
|
42 #include "help.h" |
1
|
43 |
|
44 // Symbol table for symbols at the top level. |
|
45 symbol_table *top_level_sym_tab; |
|
46 |
|
47 // Symbol table for the current scope. |
|
48 symbol_table *curr_sym_tab; |
|
49 |
|
50 // Symbol table for global symbols. |
|
51 symbol_table *global_sym_tab; |
|
52 |
|
53 /* |
|
54 * Is there a corresponding M-file that is newer than the symbol |
|
55 * definition? |
|
56 */ |
|
57 int |
|
58 symbol_out_of_date (symbol_record *sr) |
|
59 { |
|
60 int status = 0; |
|
61 if (sr != (symbol_record *) NULL) |
|
62 { |
|
63 tree *ans = sr->def (); |
|
64 if (ans != NULL_TREE) |
|
65 { |
|
66 char *mf = ans->m_file_name (); |
|
67 if (mf != (char *) NULL) |
|
68 { |
|
69 time_t tp = ans->time_parsed (); |
|
70 status = is_newer (mf, tp); |
|
71 } |
|
72 } |
|
73 } |
|
74 return status; |
|
75 } |
|
76 |
|
77 /* |
|
78 * Force a symbol into the global symbol table. |
|
79 */ |
|
80 symbol_record * |
|
81 force_global (char *name) |
|
82 { |
|
83 symbol_record *retval = (symbol_record *) NULL; |
|
84 |
|
85 if (valid_identifier (name)) |
|
86 { |
|
87 symbol_record *sr; |
|
88 sr = curr_sym_tab->lookup (name, 0, 0); |
|
89 if (sr == (symbol_record *) NULL) |
|
90 { |
|
91 retval = global_sym_tab->lookup (name, 1, 0); |
123
|
92 retval->mark_as_forced_global (); |
1
|
93 } |
|
94 else if (sr->is_formal_parameter ()) |
|
95 { |
|
96 error ("formal parameter `%s' can't be made global", name); |
|
97 } |
|
98 else |
|
99 { |
|
100 retval = global_sym_tab->lookup (name, 1, 0); |
123
|
101 retval->mark_as_forced_global (); |
1
|
102 retval->alias (sr); |
|
103 curr_sym_tab->clear (name); |
|
104 } |
|
105 } |
|
106 else |
|
107 warning ("`%s' is invalid as an identifier", name); |
|
108 |
|
109 return retval; |
|
110 } |
|
111 |
|
112 int |
|
113 bind_variable (char *varname, tree_constant *val) |
|
114 { |
|
115 // Look for the symbol in the current symbol table. If it's there, |
|
116 // great. If not, don't insert it, but look for it in the global |
|
117 // symbol table. If it's there, great. If not, insert it in the |
|
118 // original current symbol table. |
|
119 |
|
120 symbol_record *sr; |
|
121 sr = curr_sym_tab->lookup (varname, 0, 0); |
|
122 if (sr == (symbol_record *) NULL) |
|
123 { |
|
124 sr = global_sym_tab->lookup (varname, 0, 0); |
|
125 if (sr == (symbol_record *) NULL) |
|
126 { |
|
127 sr = curr_sym_tab->lookup (varname, 1); |
|
128 } |
|
129 } |
|
130 |
|
131 if (sr != (symbol_record *) NULL) |
|
132 { |
|
133 sr->define (val); |
|
134 return 0; |
|
135 } |
|
136 else |
|
137 return 1; |
|
138 } |
|
139 |
|
140 int |
|
141 bind_protected_variable (char *varname, tree_constant *val) |
|
142 { |
|
143 // Look for the symbol in the current symbol table. If it's there, |
|
144 // great. If not, don't insert it, but look for it in the global |
|
145 // symbol table. If it's there, great. If not, insert it in the |
|
146 // original current symbol table. |
|
147 |
|
148 symbol_record *sr; |
|
149 sr = curr_sym_tab->lookup (varname, 0, 0); |
|
150 if (sr == (symbol_record *) NULL) |
|
151 { |
|
152 sr = global_sym_tab->lookup (varname, 0, 0); |
|
153 if (sr == (symbol_record *) NULL) |
|
154 { |
|
155 sr = curr_sym_tab->lookup (varname, 1); |
|
156 } |
|
157 } |
|
158 |
|
159 if (sr != (symbol_record *) NULL) |
|
160 { |
|
161 sr->unprotect (); |
|
162 sr->define (val); |
|
163 sr->protect (); |
|
164 return 0; |
|
165 } |
|
166 else |
|
167 return 1; |
|
168 } |
|
169 |
|
170 /* |
|
171 * Look for name first in current then in global symbol tables. If |
|
172 * name is found and it refers to a string, return a new string |
|
173 * containing its value. Otherwise, return NULL. |
|
174 */ |
|
175 char * |
|
176 octave_string_variable (char *name) |
|
177 { |
|
178 char *retval = (char *) NULL; |
|
179 symbol_record *sr; |
|
180 sr = curr_sym_tab->lookup (name, 0, 0); |
|
181 if (sr == (symbol_record *) NULL) |
|
182 { |
|
183 sr = global_sym_tab->lookup (name, 0, 0); |
|
184 if (sr == (symbol_record *) NULL) |
|
185 return retval; |
|
186 } |
|
187 |
|
188 tree *defn = sr->def (); |
|
189 if (defn != NULL_TREE) |
|
190 { |
|
191 tree_constant val = defn->eval (0); |
|
192 if (val.is_string_type ()) |
|
193 { |
|
194 char *s = val.string_value (); |
|
195 if (s != (char *) NULL) |
|
196 retval = strsave (s); |
|
197 } |
|
198 } |
|
199 |
|
200 return retval; |
|
201 } |
|
202 |
|
203 /* |
|
204 * Look for name first in current then in global symbol tables. If |
|
205 * name is found and it refers to a real scalar, place the value in d |
|
206 * and return 0. Otherwise, return -1. |
|
207 */ |
|
208 int |
|
209 octave_real_scalar_variable (char *name, double& d) |
|
210 { |
|
211 int status = -1; |
|
212 symbol_record *sr; |
|
213 sr = curr_sym_tab->lookup (name, 0, 0); |
|
214 if (sr == (symbol_record *) NULL) |
|
215 { |
|
216 sr = global_sym_tab->lookup (name, 0, 0); |
|
217 if (sr == (symbol_record *) NULL) |
|
218 return status; |
|
219 } |
|
220 |
|
221 tree *defn = sr->def (); |
|
222 if (defn != NULL_TREE) |
|
223 { |
|
224 tree_constant val = defn->eval (0); |
|
225 if (val.const_type () == tree_constant_rep::scalar_constant) |
|
226 { |
|
227 d = val.double_value (); |
|
228 status = 0; |
|
229 } |
|
230 } |
|
231 |
|
232 return status; |
|
233 } |
|
234 |
|
235 /* |
|
236 * Extract a keyword and its value from a file. Input should look |
|
237 * something like: |
|
238 * |
|
239 * #[ \t]*keyword[ \t]*:[ \t]*string-value\n |
|
240 */ |
|
241 int |
|
242 extract_keyword (istream& is, char *keyword, char *value) |
|
243 { |
|
244 char *ptr = value; |
|
245 |
|
246 int status = 0; |
|
247 |
|
248 char c; |
|
249 while (is.get (c)) |
|
250 { |
|
251 if (c == '#') |
|
252 { |
|
253 while (is.get (c) && (c == ' ' || c == '\t' || c == '#')) |
|
254 ; // Skip whitespace and comment characters. |
|
255 |
|
256 if (isalpha (c)) |
|
257 *ptr++ = c; |
|
258 |
|
259 while (is.get (c) && isalpha (c)) |
|
260 *ptr++ = c; |
|
261 |
|
262 if (strncmp (value, keyword, strlen (keyword)) == 0) |
|
263 { |
|
264 ptr = value; |
|
265 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
266 ; // Skip whitespace and the colon. |
|
267 |
|
268 if (c != '\n') |
|
269 { |
|
270 *ptr++ = c; |
|
271 while (is.get (c) && c != '\n') |
|
272 *ptr++ = c; |
|
273 } |
|
274 *ptr = '\0'; |
|
275 status = 1; |
|
276 break; |
|
277 } |
|
278 } |
|
279 } |
|
280 return status; |
|
281 } |
|
282 |
|
283 int |
|
284 extract_keyword (istream& is, char *keyword, int& value) |
|
285 { |
|
286 char buf [128]; |
|
287 char *ptr = buf; |
|
288 |
|
289 int status = 0; |
|
290 value = 0; |
|
291 |
|
292 char c; |
|
293 while (is.get (c)) |
|
294 { |
|
295 if (c == '#') |
|
296 { |
|
297 while (is.get (c) && (c == ' ' || c == '\t' || c == '#')) |
|
298 ; // Skip whitespace and comment characters. |
|
299 |
|
300 if (isalpha (c)) |
|
301 *ptr++ = c; |
|
302 |
|
303 while (is.get (c) && isalpha (c)) |
|
304 *ptr++ = c; |
|
305 |
|
306 if (strncmp (buf, keyword, strlen (keyword)) == 0) |
|
307 { |
|
308 ptr = buf; |
|
309 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
310 ; // Skip whitespace and the colon. |
|
311 |
|
312 is.putback (c); |
|
313 if (c != '\n') |
|
314 is >> value; |
|
315 if (is) |
|
316 status = 1; |
|
317 while (is.get (c) && c != '\n') |
|
318 ; // Skip to beginning of next line; |
|
319 break; |
|
320 } |
|
321 } |
|
322 } |
|
323 return status; |
|
324 } |
|
325 |
|
326 /* |
|
327 * Skip trailing white space and |
|
328 */ |
|
329 void |
|
330 skip_comments (istream& is) |
|
331 { |
|
332 char c = '\0'; |
|
333 while (is.get (c)) |
|
334 { |
|
335 if (c == ' ' || c == '\t' || c == '\n') |
|
336 ; // Skip whitespace on way to beginning of next line. |
|
337 else |
|
338 break; |
|
339 } |
|
340 |
|
341 for (;;) |
|
342 { |
|
343 if (is && c == '#') |
|
344 while (is.get (c) && c != '\n') |
|
345 ; // Skip to beginning of next line, ignoring everything. |
|
346 else |
|
347 break; |
|
348 } |
|
349 } |
|
350 |
|
351 /* |
|
352 * Is `s' a valid identifier? |
|
353 */ |
|
354 int |
|
355 valid_identifier (char *s) |
|
356 { |
|
357 if (s == (char *) NULL || ! (isalnum (*s) || *s == '_')) |
|
358 return 0; |
|
359 |
|
360 while (*++s != '\0') |
|
361 if (! (isalnum (*s) || *s == '_')) |
|
362 return 0; |
|
363 |
|
364 return 1; |
|
365 } |
|
366 |
|
367 /* |
|
368 * See if the identifier is in scope. |
|
369 */ |
|
370 int |
|
371 identifier_exists (char *name) |
|
372 { |
159
|
373 symbol_record *sr = curr_sym_tab->lookup (name, 0, 0); |
|
374 if (sr == (symbol_record *) NULL) |
|
375 sr = global_sym_tab->lookup (name, 0, 0); |
1
|
376 |
159
|
377 if (sr != (symbol_record *) NULL && sr->is_variable ()) |
|
378 return 1; |
|
379 else if (sr != (symbol_record *) NULL && sr->is_function ()) |
|
380 return 2; |
1
|
381 else |
|
382 { |
|
383 char *path = m_file_in_path (name); |
|
384 if (path != (char *) NULL) |
|
385 { |
|
386 delete [] path; |
159
|
387 return 2; |
1
|
388 } |
|
389 else |
|
390 { |
|
391 struct stat buf; |
|
392 if (stat (name, &buf) == 0 && S_ISREG (buf.st_mode)) |
159
|
393 return 2; |
1
|
394 } |
|
395 |
|
396 } |
159
|
397 return 0; |
1
|
398 } |
|
399 |
|
400 /* |
|
401 * Is this tree_constant a valid function? |
|
402 */ |
|
403 tree * |
|
404 is_valid_function (tree_constant& arg, char *warn_for, int warn = 0) |
|
405 { |
|
406 tree *ans = NULL_TREE; |
|
407 |
|
408 if (! arg.is_string_type ()) |
|
409 { |
|
410 if (warn) |
|
411 message (warn_for, "expecting function name as argument"); |
|
412 return ans; |
|
413 } |
|
414 |
|
415 char *fcn_name = arg.string_value (); |
|
416 symbol_record *sr = global_sym_tab->lookup (fcn_name, 0, 0); |
|
417 |
|
418 if (sr == (symbol_record *) NULL) |
|
419 { |
|
420 sr = global_sym_tab->lookup (fcn_name, 1, 0); |
|
421 tree_identifier tmp (sr); |
|
422 tmp.parse_m_file (0); |
|
423 } |
|
424 else if (symbol_out_of_date (sr)) |
|
425 { |
|
426 tree_identifier tmp (sr); |
|
427 tmp.parse_m_file (0); |
|
428 } |
|
429 |
|
430 ans = sr->def (); |
|
431 if (ans == NULL_TREE || ! sr->is_function ()) |
|
432 { |
|
433 if (warn) |
|
434 message (warn_for, "the symbol `%s' is not valid as a function", |
|
435 fcn_name); |
|
436 ans = NULL_TREE; |
|
437 } |
|
438 |
|
439 return ans; |
|
440 } |
|
441 |
|
442 /* |
|
443 * Does this function take the right number of arguments? |
|
444 */ |
|
445 int |
|
446 takes_correct_nargs (tree *fcn, int expected_nargin, char *warn_for, |
|
447 int warn = 0) |
|
448 { |
|
449 int nargs = fcn->max_expected_args () - 1; |
|
450 int e_nargs = expected_nargin - 1; |
|
451 if (nargs != e_nargs) |
|
452 { |
|
453 if (warn) |
|
454 message (warn_for, "expecting function to take %d argument%c", |
|
455 e_nargs, s_plural (e_nargs)); |
|
456 return 0; |
|
457 } |
|
458 return 1; |
|
459 } |
|
460 |
|
461 char ** |
|
462 make_name_list (void) |
|
463 { |
|
464 int key_len = 0; |
|
465 int glb_len = 0; |
|
466 int top_len = 0; |
|
467 int lcl_len = 0; |
|
468 int mfl_len = 0; |
|
469 |
|
470 char **key = (char **) NULL; |
|
471 char **glb = (char **) NULL; |
|
472 char **top = (char **) NULL; |
|
473 char **lcl = (char **) NULL; |
|
474 char **mfl = (char **) NULL; |
|
475 |
|
476 key = names (keyword_help (), key_len); |
|
477 glb = global_sym_tab->list (glb_len); |
|
478 top = top_level_sym_tab->list (top_len); |
|
479 if (top_level_sym_tab != curr_sym_tab) |
|
480 lcl = curr_sym_tab->list (lcl_len); |
|
481 mfl = get_m_file_names (mfl_len, 1); |
|
482 |
|
483 int total_len = key_len + glb_len + top_len + lcl_len + mfl_len; |
|
484 |
|
485 char **list = new char * [total_len+1]; |
|
486 |
|
487 int j = 0; |
|
488 int i = 0; |
|
489 for (i = 0; i < key_len; i++) |
|
490 list[j++] = key[i]; |
|
491 |
|
492 for (i = 0; i < glb_len; i++) |
|
493 list[j++] = glb[i]; |
|
494 |
|
495 for (i = 0; i < top_len; i++) |
|
496 list[j++] = top[i]; |
|
497 |
|
498 for (i = 0; i < lcl_len; i++) |
|
499 list[j++] = lcl[i]; |
|
500 |
|
501 for (i = 0; i < mfl_len; i++) |
|
502 list[j++] = mfl[i]; |
|
503 |
|
504 list[j] = (char *) NULL; |
|
505 |
|
506 delete [] key; |
|
507 delete [] glb; |
|
508 delete [] top; |
|
509 delete [] lcl; |
|
510 delete [] mfl; |
|
511 |
|
512 return list; |
|
513 } |
|
514 |
|
515 /* |
|
516 ;;; Local Variables: *** |
|
517 ;;; mode: C++ *** |
|
518 ;;; page-delimiter: "^/\\*" *** |
|
519 ;;; End: *** |
|
520 */ |