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