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