8
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
8
|
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. |
8
|
20 |
|
21 */ |
|
22 |
383
|
23 #if !defined (octave_symtab_h) |
|
24 #define octave_symtab_h 1 |
8
|
25 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
27 #pragma interface |
|
28 #endif |
|
29 |
3011
|
30 #include <cassert> |
|
31 |
1755
|
32 #include <string> |
4214
|
33 #include <stack> |
220
|
34 |
3011
|
35 #include "oct-alloc.h" |
1755
|
36 #include "str-vec.h" |
|
37 |
2975
|
38 #include "ov.h" |
|
39 |
2979
|
40 class octave_lvalue; |
8
|
41 |
1755
|
42 class string_vector; |
|
43 |
8
|
44 class symbol_record; |
|
45 class symbol_table; |
|
46 |
3011
|
47 // Individual records in a symbol table. |
8
|
48 |
3011
|
49 class |
|
50 symbol_record |
|
51 { |
|
52 public: |
8
|
53 |
3258
|
54 // If you add or delete an entry here, you'll also need to change |
3325
|
55 // the width parameter in the declaration for symbol_type below... |
3258
|
56 |
195
|
57 enum TYPE |
|
58 { |
|
59 UNKNOWN = 0, |
|
60 USER_FUNCTION = 1, |
|
61 USER_VARIABLE = 2, |
3325
|
62 DLD_FUNCTION = 4, |
|
63 BUILTIN_FUNCTION = 8, |
4208
|
64 COMMAND = 16, |
3325
|
65 MAPPER_FUNCTION = 32, |
|
66 BUILTIN_VARIABLE = 64, |
|
67 BUILTIN_CONSTANT = 128 |
195
|
68 }; |
|
69 |
8
|
70 private: |
|
71 |
3011
|
72 // Variables or functions. |
|
73 |
|
74 class symbol_def |
|
75 { |
|
76 public: |
|
77 |
|
78 symbol_def (const octave_value& val = octave_value (), |
|
79 unsigned int sym_type = 0) |
|
80 : symbol_type (sym_type), eternal (0), read_only (0), help_string (), |
4009
|
81 definition (val), count (1) { } |
3011
|
82 |
|
83 ~symbol_def (void) { } |
|
84 |
3258
|
85 bool is_constant (void) const |
|
86 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
87 |
3011
|
88 bool is_variable (void) const |
|
89 { |
|
90 return (symbol_type & symbol_record::USER_VARIABLE |
|
91 || symbol_type & symbol_record::BUILTIN_VARIABLE); |
|
92 } |
|
93 |
4208
|
94 // It's not necessary to check for COMMAND and MAPPER_FUNCTION |
3968
|
95 // here. Those tags are just used as additional qualifiers for |
|
96 // the other types of functions. |
|
97 |
3011
|
98 bool is_function (void) const |
|
99 { |
|
100 return (symbol_type & symbol_record::USER_FUNCTION |
3325
|
101 || symbol_type & symbol_record::DLD_FUNCTION |
3011
|
102 || symbol_type & symbol_record::BUILTIN_FUNCTION); |
|
103 } |
|
104 |
|
105 bool is_user_variable (void) const |
|
106 { return (symbol_type & symbol_record::USER_VARIABLE); } |
195
|
107 |
4208
|
108 void mark_as_command (void) |
|
109 { symbol_type |= symbol_record::COMMAND; } |
4207
|
110 |
4208
|
111 void unmark_command (void) |
|
112 { symbol_type &= ~symbol_record::COMMAND; } |
4207
|
113 |
4208
|
114 bool is_command (void) const |
|
115 { return (symbol_type & symbol_record::COMMAND); } |
3011
|
116 |
|
117 bool is_mapper_function (void) const |
|
118 { return (symbol_type & symbol_record::MAPPER_FUNCTION); } |
|
119 |
|
120 bool is_user_function (void) const |
|
121 { return (symbol_type & symbol_record::USER_FUNCTION); } |
|
122 |
3258
|
123 bool is_builtin_constant (void) const |
|
124 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
125 |
3011
|
126 bool is_builtin_variable (void) const |
|
127 { return (symbol_type & symbol_record::BUILTIN_VARIABLE); } |
195
|
128 |
3011
|
129 bool is_builtin_function (void) const |
|
130 { return (symbol_type & symbol_record::BUILTIN_FUNCTION); } |
|
131 |
3325
|
132 bool is_dld_function (void) const |
|
133 { return (symbol_type & symbol_record::DLD_FUNCTION); } |
|
134 |
3011
|
135 // XXX FIXME XXX |
3523
|
136 bool is_map_element (const std::string& /* elts */) const |
3011
|
137 { return false; } |
|
138 |
|
139 bool is_defined (void) const |
|
140 { return definition.is_defined (); } |
|
141 |
|
142 bool is_read_only (void) const |
|
143 { return read_only; } |
|
144 |
|
145 bool is_eternal (void) const |
|
146 { return eternal; } |
8
|
147 |
3013
|
148 int rows (void) const { return definition.rows (); } |
|
149 int columns (void) const { return definition.columns (); } |
|
150 |
3523
|
151 std::string type_name (void) const { return definition.type_name (); } |
3013
|
152 |
3523
|
153 std::string type_as_string (void) const; |
3355
|
154 |
3523
|
155 void type (std::ostream& os, const std::string& name, bool pr_type_info, |
3356
|
156 bool quiet, bool pr_orig_txt); |
|
157 |
3523
|
158 std::string which (const std::string& name); |
3355
|
159 |
3523
|
160 void which (std::ostream& os, const std::string& name); |
3355
|
161 |
3011
|
162 void define (const octave_value& val, unsigned int sym_type) |
|
163 { |
|
164 definition = val; |
|
165 symbol_type = sym_type; |
|
166 } |
|
167 |
|
168 void protect (void) { read_only = 1; } |
|
169 |
|
170 void unprotect (void) { read_only = 0; } |
|
171 |
|
172 void make_eternal (void) { eternal = 1; } |
|
173 |
|
174 octave_value& def (void) { return definition; } |
|
175 |
3523
|
176 std::string help (void) const { return help_string; } |
3011
|
177 |
3523
|
178 void document (const std::string& h) { help_string = h; } |
3011
|
179 |
|
180 unsigned int type (void) { return symbol_type; } |
|
181 |
|
182 void *operator new (size_t size) |
|
183 { return allocator.alloc (size); } |
|
184 |
|
185 void operator delete (void *p, size_t size) |
|
186 { allocator.free (p, size); } |
|
187 |
|
188 static octave_allocator allocator; |
8
|
189 |
3011
|
190 // The type of this symbol (see the enum above). |
3325
|
191 unsigned int symbol_type : 8; |
3011
|
192 |
|
193 // Nonzero means this variable cannot be cleared. |
|
194 unsigned int eternal : 1; |
|
195 |
|
196 // Nonzero means this variable cannot be given a new value. |
|
197 unsigned int read_only : 1; |
|
198 |
|
199 // The doc string associated with this variable. |
3523
|
200 std::string help_string; |
3011
|
201 |
|
202 // The value of this definition. See ov.h and related files. |
|
203 octave_value definition; |
767
|
204 |
3011
|
205 // Reference count. |
|
206 int count; |
|
207 |
3933
|
208 void print_info (std::ostream& os, |
|
209 const std::string& prefix = std::string ()) const; |
3239
|
210 |
3011
|
211 // No copying! |
|
212 |
|
213 symbol_def (const symbol_def& sd); |
|
214 |
|
215 symbol_def& operator = (const symbol_def& sd); |
|
216 }; |
8
|
217 |
|
218 public: |
2953
|
219 |
3011
|
220 typedef int (*change_function) (void); |
2953
|
221 |
3011
|
222 symbol_record (void) |
|
223 : formal_param (0), linked_to_global (0), tagged_static (0), |
|
224 nm (), chg_fcn (0), definition (new symbol_def ()), |
|
225 next_elem (0) { } |
|
226 |
3523
|
227 symbol_record (const std::string& n, symbol_record *nxt) |
3011
|
228 : formal_param (0), linked_to_global (0), tagged_static (0), |
|
229 nm (n), chg_fcn (0), definition (new symbol_def ()), |
|
230 next_elem (nxt) { } |
8
|
231 |
1755
|
232 ~symbol_record (void) { } |
8
|
233 |
3523
|
234 std::string name (void) const { return nm; } |
2975
|
235 |
3523
|
236 std::string help (void) const { return definition->help (); } |
3011
|
237 |
|
238 octave_value& def (void) { return definition->def (); } |
8
|
239 |
3523
|
240 void rename (const std::string& new_name); |
195
|
241 |
3011
|
242 bool is_function (void) const |
|
243 { return definition->is_function (); } |
|
244 |
4208
|
245 void mark_as_command (void) |
|
246 { definition->mark_as_command (); } |
4207
|
247 |
4208
|
248 void unmark_command (void) |
|
249 { definition->unmark_command (); } |
4207
|
250 |
4208
|
251 bool is_command (void) const |
|
252 { return definition->is_command (); } |
3011
|
253 |
|
254 bool is_mapper_function (void) const |
|
255 { return definition->is_mapper_function (); } |
|
256 |
|
257 bool is_user_function (void) const |
|
258 { return definition->is_user_function (); } |
195
|
259 |
3011
|
260 bool is_builtin_function (void) const |
|
261 { return definition->is_builtin_function (); } |
|
262 |
3325
|
263 bool is_dld_function (void) const |
|
264 { return definition->is_dld_function (); } |
|
265 |
3258
|
266 bool is_constant (void) const |
|
267 { return definition->is_constant (); } |
|
268 |
|
269 bool is_builtin_constant (void) const |
|
270 { return definition->is_builtin_constant (); } |
|
271 |
3011
|
272 bool is_variable (void) const |
|
273 { return definition->is_variable (); } |
8
|
274 |
3011
|
275 bool is_user_variable (void) const |
|
276 { return definition->is_user_variable (); } |
|
277 |
|
278 bool is_builtin_variable (void) const |
|
279 { return definition->is_builtin_variable (); } |
|
280 |
3523
|
281 bool is_map_element (const std::string& elts) const |
3011
|
282 { return definition->is_map_element (elts); } |
8
|
283 |
3011
|
284 unsigned int type (void) const { return definition->type (); } |
|
285 |
|
286 bool is_defined (void) const { return definition->is_defined (); } |
|
287 |
|
288 bool is_read_only (void) const { return definition->is_read_only (); } |
8
|
289 |
3011
|
290 bool is_eternal (void) const { return definition->is_eternal (); } |
|
291 |
|
292 void protect (void) { definition->protect (); } |
195
|
293 |
3011
|
294 void unprotect (void) { definition->unprotect (); } |
|
295 |
|
296 void make_eternal (void) { definition->make_eternal (); } |
2893
|
297 |
3011
|
298 void set_change_function (change_function f) { chg_fcn = f; } |
2893
|
299 |
3011
|
300 void define (const octave_value& v, unsigned int sym_type = USER_VARIABLE); |
|
301 |
|
302 void define_builtin_var (const octave_value& v); |
195
|
303 |
3258
|
304 bool define_builtin_const (const octave_value& v); |
3011
|
305 |
|
306 bool define (octave_function *f, unsigned int sym_type); |
2893
|
307 |
3523
|
308 void document (const std::string& h) { definition->document (h); } |
195
|
309 |
3011
|
310 void clear (void); |
195
|
311 |
4009
|
312 void alias (symbol_record *s); |
8
|
313 |
|
314 void mark_as_formal_parameter (void); |
3011
|
315 bool is_formal_parameter (void) const { return formal_param; } |
8
|
316 |
195
|
317 void mark_as_linked_to_global (void); |
3011
|
318 bool is_linked_to_global (void) const { return linked_to_global; } |
8
|
319 |
2846
|
320 void mark_as_static (void); |
3011
|
321 bool is_static (void) const { return tagged_static; } |
2846
|
322 |
3013
|
323 int rows (void) const { return definition->rows (); } |
|
324 int columns (void) const { return definition->columns (); } |
|
325 |
3523
|
326 std::string type_name (void) const { return definition->type_name (); } |
3013
|
327 |
3523
|
328 std::string type_as_string (void) const |
3355
|
329 { return definition->type_as_string (); } |
|
330 |
3523
|
331 void type (std::ostream& os, bool pr_type_info, bool quiet, bool pr_orig_txt) |
3356
|
332 { definition->type (os, name (), pr_type_info, quiet, pr_orig_txt); } |
|
333 |
3523
|
334 std::string which (void) { return definition->which (name ()); } |
3355
|
335 |
3523
|
336 void which (std::ostream& os) { definition->which (os, name ()); } |
3355
|
337 |
2975
|
338 octave_value& variable_value (void); |
2979
|
339 octave_lvalue variable_reference (void); |
2390
|
340 |
3011
|
341 symbol_record *next (void) const { return next_elem; } |
8
|
342 |
3011
|
343 void chain (symbol_record *s) { next_elem = s; } |
195
|
344 |
220
|
345 void push_context (void); |
3011
|
346 |
220
|
347 void pop_context (void); |
|
348 |
3933
|
349 void print_symbol_info_line (std::ostream& os) const; |
3013
|
350 |
3933
|
351 void print_info (std::ostream& os, |
|
352 const std::string& prefix = std::string ()) const; |
3239
|
353 |
8
|
354 private: |
|
355 |
2893
|
356 unsigned int formal_param : 1; |
|
357 unsigned int linked_to_global : 1; |
|
358 unsigned int tagged_static : 1; |
195
|
359 |
3523
|
360 std::string nm; |
3011
|
361 change_function chg_fcn; |
195
|
362 symbol_def *definition; |
8
|
363 symbol_record *next_elem; |
395
|
364 |
3011
|
365 // This should maybe be one stack with a structure containing all the |
|
366 // items we need to save for recursive calls... |
4214
|
367 std::stack <symbol_def *> context; |
|
368 std::stack <unsigned int> global_link_context; |
8
|
369 |
3011
|
370 bool read_only_error (const char *action); |
195
|
371 |
3258
|
372 void link_to_builtin_variable (void); |
|
373 |
3011
|
374 // No copying! |
|
375 |
|
376 symbol_record (const symbol_record& s); |
195
|
377 |
8
|
378 symbol_record& operator = (const symbol_record& s); |
|
379 }; |
|
380 |
767
|
381 // A symbol table. |
200
|
382 |
|
383 #define SYMTAB_LOCAL_SCOPE 1 |
|
384 #define SYMTAB_GLOBAL_SCOPE 2 |
|
385 |
|
386 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
|
387 |
3011
|
388 #define SYMTAB_ALL_TYPES (symbol_record::USER_FUNCTION \ |
|
389 | symbol_record::USER_VARIABLE \ |
3325
|
390 | symbol_record::DLD_FUNCTION \ |
3011
|
391 | symbol_record::BUILTIN_FUNCTION \ |
4208
|
392 | symbol_record::COMMAND \ |
3011
|
393 | symbol_record::MAPPER_FUNCTION \ |
3258
|
394 | symbol_record::BUILTIN_VARIABLE \ |
|
395 | symbol_record::BUILTIN_CONSTANT) |
200
|
396 |
3011
|
397 #define SYMTAB_VARIABLES (symbol_record::USER_VARIABLE \ |
|
398 | symbol_record::BUILTIN_VARIABLE) |
1412
|
399 |
8
|
400 class |
|
401 symbol_table |
|
402 { |
|
403 public: |
|
404 |
3011
|
405 symbol_table (unsigned int tab_size = 128) |
|
406 : table_size (tab_size), table (new symbol_record [table_size]) |
4009
|
407 { |
|
408 assert ((tab_size % 2) == 0); |
|
409 } |
3011
|
410 |
|
411 ~symbol_table (void) |
4009
|
412 { |
|
413 clear (); |
|
414 delete [] table; |
|
415 } |
8
|
416 |
3523
|
417 symbol_record *lookup (const std::string& nm, bool insert = false, |
2856
|
418 bool warn = false); |
8
|
419 |
3523
|
420 void rename (const std::string& old_name, const std::string& new_name); |
572
|
421 |
4009
|
422 void clear (void); |
|
423 |
|
424 void clear_variables (void); |
|
425 void clear_functions (void); |
|
426 void clear_globals (void); |
|
427 |
|
428 bool clear (const std::string& nm); |
|
429 |
|
430 bool clear_variable (const std::string& nm); |
|
431 bool clear_function (const std::string& nm); |
|
432 bool clear_global (const std::string& nm); |
|
433 |
|
434 bool clear_variable_pattern (const std::string& pat); |
|
435 bool clear_function_pattern (const std::string& pat); |
|
436 bool clear_global_pattern (const std::string& pat); |
8
|
437 |
164
|
438 int size (void) const; |
8
|
439 |
3013
|
440 Array<symbol_record *> |
3355
|
441 symbol_list (const string_vector& pats = string_vector (), |
3013
|
442 unsigned int type = SYMTAB_ALL_TYPES, |
|
443 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
|
444 |
195
|
445 |
1755
|
446 string_vector |
3355
|
447 name_list (const string_vector& pats = string_vector (), |
3013
|
448 bool sort = false, unsigned int type = SYMTAB_ALL_TYPES, |
|
449 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
605
|
450 |
4009
|
451 string_vector |
|
452 user_function_name_list (void) const |
|
453 { |
|
454 return name_list |
|
455 (string_vector (), false, |
|
456 symbol_record::USER_FUNCTION|symbol_record::DLD_FUNCTION, |
|
457 SYMTAB_ALL_SCOPES); |
|
458 } |
|
459 |
|
460 string_vector |
|
461 global_variable_name_list (void) const |
|
462 { |
|
463 return name_list |
|
464 (string_vector (), false, SYMTAB_VARIABLES, SYMTAB_GLOBAL_SCOPE); |
|
465 } |
|
466 |
|
467 string_vector |
|
468 variable_name_list (void) const |
|
469 { |
|
470 return name_list |
|
471 (string_vector (), false, SYMTAB_VARIABLES, SYMTAB_LOCAL_SCOPE); |
|
472 } |
3011
|
473 |
|
474 int maybe_list (const char *header, const string_vector& argv, |
3523
|
475 std::ostream& os, bool show_verbose, |
3011
|
476 unsigned type, unsigned scope); |
|
477 |
3523
|
478 Array<symbol_record *> glob (const std::string& pat = std::string ("*"), |
3355
|
479 unsigned int type = SYMTAB_ALL_TYPES, |
|
480 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
8
|
481 |
220
|
482 void push_context (void); |
3011
|
483 |
220
|
484 void pop_context (void); |
|
485 |
3941
|
486 void print_info (std::ostream& os) const; |
3011
|
487 |
8
|
488 private: |
|
489 |
3011
|
490 unsigned int table_size; |
|
491 |
|
492 symbol_record *table; |
|
493 |
3523
|
494 unsigned int hash (const std::string& s); |
8
|
495 |
3011
|
496 // No copying! |
8
|
497 |
3011
|
498 symbol_table (const symbol_table&); |
1962
|
499 |
3011
|
500 symbol_table& operator = (const symbol_table&); |
|
501 }; |
2790
|
502 |
8
|
503 #endif |
|
504 |
|
505 /* |
|
506 ;;; Local Variables: *** |
|
507 ;;; mode: C++ *** |
|
508 ;;; End: *** |
|
509 */ |