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 |
1297
|
26 #if defined (__GNUG__) |
|
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, |
|
65 TEXT_FUNCTION = 16, |
|
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 (), |
|
82 definition (val), next_elem (0), count (1) { } |
|
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 |
|
95 bool is_function (void) const |
|
96 { |
|
97 return (symbol_type & symbol_record::USER_FUNCTION |
3325
|
98 || symbol_type & symbol_record::DLD_FUNCTION |
3011
|
99 || symbol_type & symbol_record::BUILTIN_FUNCTION); |
|
100 } |
|
101 |
|
102 bool is_user_variable (void) const |
|
103 { return (symbol_type & symbol_record::USER_VARIABLE); } |
195
|
104 |
3011
|
105 bool is_text_function (void) const |
|
106 { return (symbol_type & symbol_record::TEXT_FUNCTION); } |
|
107 |
|
108 bool is_mapper_function (void) const |
|
109 { return (symbol_type & symbol_record::MAPPER_FUNCTION); } |
|
110 |
|
111 bool is_user_function (void) const |
|
112 { return (symbol_type & symbol_record::USER_FUNCTION); } |
|
113 |
3258
|
114 bool is_builtin_constant (void) const |
|
115 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
116 |
3011
|
117 bool is_builtin_variable (void) const |
|
118 { return (symbol_type & symbol_record::BUILTIN_VARIABLE); } |
195
|
119 |
3011
|
120 bool is_builtin_function (void) const |
|
121 { return (symbol_type & symbol_record::BUILTIN_FUNCTION); } |
|
122 |
3325
|
123 bool is_dld_function (void) const |
|
124 { return (symbol_type & symbol_record::DLD_FUNCTION); } |
|
125 |
3011
|
126 // XXX FIXME XXX |
3523
|
127 bool is_map_element (const std::string& /* elts */) const |
3011
|
128 { return false; } |
|
129 |
|
130 bool is_defined (void) const |
|
131 { return definition.is_defined (); } |
|
132 |
|
133 bool is_read_only (void) const |
|
134 { return read_only; } |
|
135 |
|
136 bool is_eternal (void) const |
|
137 { return eternal; } |
8
|
138 |
3013
|
139 int rows (void) const { return definition.rows (); } |
|
140 int columns (void) const { return definition.columns (); } |
|
141 |
3523
|
142 std::string type_name (void) const { return definition.type_name (); } |
3013
|
143 |
3523
|
144 std::string type_as_string (void) const; |
3355
|
145 |
3523
|
146 void type (std::ostream& os, const std::string& name, bool pr_type_info, |
3356
|
147 bool quiet, bool pr_orig_txt); |
|
148 |
3523
|
149 std::string which (const std::string& name); |
3355
|
150 |
3523
|
151 void which (std::ostream& os, const std::string& name); |
3355
|
152 |
3011
|
153 void define (const octave_value& val, unsigned int sym_type) |
|
154 { |
|
155 definition = val; |
|
156 symbol_type = sym_type; |
|
157 } |
|
158 |
|
159 void protect (void) { read_only = 1; } |
|
160 |
|
161 void unprotect (void) { read_only = 0; } |
|
162 |
|
163 void make_eternal (void) { eternal = 1; } |
|
164 |
|
165 octave_value& def (void) { return definition; } |
|
166 |
3523
|
167 std::string help (void) const { return help_string; } |
3011
|
168 |
3523
|
169 void document (const std::string& h) { help_string = h; } |
3011
|
170 |
|
171 unsigned int type (void) { return symbol_type; } |
|
172 |
|
173 void *operator new (size_t size) |
|
174 { return allocator.alloc (size); } |
|
175 |
|
176 void operator delete (void *p, size_t size) |
|
177 { allocator.free (p, size); } |
|
178 |
|
179 static octave_allocator allocator; |
8
|
180 |
3011
|
181 // The type of this symbol (see the enum above). |
3325
|
182 unsigned int symbol_type : 8; |
3011
|
183 |
|
184 // Nonzero means this variable cannot be cleared. |
|
185 unsigned int eternal : 1; |
|
186 |
|
187 // Nonzero means this variable cannot be given a new value. |
|
188 unsigned int read_only : 1; |
|
189 |
|
190 // The doc string associated with this variable. |
3523
|
191 std::string help_string; |
3011
|
192 |
|
193 // The value of this definition. See ov.h and related files. |
|
194 octave_value definition; |
767
|
195 |
3011
|
196 // Pointer to next definition in chain. This is used so that |
|
197 // variables can hide function definitions, and so that the function |
|
198 // definitions can reappear if the variable is cleared. |
|
199 symbol_def *next_elem; |
|
200 |
|
201 // Reference count. |
|
202 int count; |
|
203 |
3239
|
204 void dump_symbol_info (void); |
|
205 |
3011
|
206 // No copying! |
|
207 |
|
208 symbol_def (const symbol_def& sd); |
|
209 |
|
210 symbol_def& operator = (const symbol_def& sd); |
|
211 }; |
8
|
212 |
|
213 public: |
2953
|
214 |
3011
|
215 typedef int (*change_function) (void); |
2953
|
216 |
3011
|
217 symbol_record (void) |
|
218 : formal_param (0), linked_to_global (0), tagged_static (0), |
|
219 nm (), chg_fcn (0), definition (new symbol_def ()), |
|
220 next_elem (0) { } |
|
221 |
3523
|
222 symbol_record (const std::string& n, symbol_record *nxt) |
3011
|
223 : formal_param (0), linked_to_global (0), tagged_static (0), |
|
224 nm (n), chg_fcn (0), definition (new symbol_def ()), |
|
225 next_elem (nxt) { } |
8
|
226 |
1755
|
227 ~symbol_record (void) { } |
8
|
228 |
3523
|
229 std::string name (void) const { return nm; } |
2975
|
230 |
3523
|
231 std::string help (void) const { return definition->help (); } |
3011
|
232 |
|
233 octave_value& def (void) { return definition->def (); } |
8
|
234 |
3523
|
235 void rename (const std::string& new_name); |
195
|
236 |
3011
|
237 bool is_function (void) const |
|
238 { return definition->is_function (); } |
|
239 |
|
240 bool is_text_function (void) const |
|
241 { return definition->is_text_function (); } |
|
242 |
|
243 bool is_mapper_function (void) const |
|
244 { return definition->is_mapper_function (); } |
|
245 |
|
246 bool is_user_function (void) const |
|
247 { return definition->is_user_function (); } |
195
|
248 |
3011
|
249 bool is_builtin_function (void) const |
|
250 { return definition->is_builtin_function (); } |
|
251 |
3325
|
252 bool is_dld_function (void) const |
|
253 { return definition->is_dld_function (); } |
|
254 |
3258
|
255 bool is_constant (void) const |
|
256 { return definition->is_constant (); } |
|
257 |
|
258 bool is_builtin_constant (void) const |
|
259 { return definition->is_builtin_constant (); } |
|
260 |
3011
|
261 bool is_variable (void) const |
|
262 { return definition->is_variable (); } |
8
|
263 |
3011
|
264 bool is_user_variable (void) const |
|
265 { return definition->is_user_variable (); } |
|
266 |
|
267 bool is_builtin_variable (void) const |
|
268 { return definition->is_builtin_variable (); } |
|
269 |
3523
|
270 bool is_map_element (const std::string& elts) const |
3011
|
271 { return definition->is_map_element (elts); } |
8
|
272 |
3011
|
273 unsigned int type (void) const { return definition->type (); } |
|
274 |
|
275 bool is_defined (void) const { return definition->is_defined (); } |
|
276 |
|
277 bool is_read_only (void) const { return definition->is_read_only (); } |
8
|
278 |
3011
|
279 bool is_eternal (void) const { return definition->is_eternal (); } |
|
280 |
|
281 void protect (void) { definition->protect (); } |
195
|
282 |
3011
|
283 void unprotect (void) { definition->unprotect (); } |
|
284 |
|
285 void make_eternal (void) { definition->make_eternal (); } |
2893
|
286 |
3011
|
287 void set_change_function (change_function f) { chg_fcn = f; } |
2893
|
288 |
3011
|
289 void define (const octave_value& v, unsigned int sym_type = USER_VARIABLE); |
|
290 |
|
291 void define_builtin_var (const octave_value& v); |
195
|
292 |
3258
|
293 bool define_builtin_const (const octave_value& v); |
3011
|
294 |
|
295 bool define (octave_function *f, unsigned int sym_type); |
2893
|
296 |
3523
|
297 void document (const std::string& h) { definition->document (h); } |
195
|
298 |
3011
|
299 void clear (void); |
195
|
300 |
2856
|
301 void alias (symbol_record *s, bool force = false); |
8
|
302 |
|
303 void mark_as_formal_parameter (void); |
3011
|
304 bool is_formal_parameter (void) const { return formal_param; } |
8
|
305 |
195
|
306 void mark_as_linked_to_global (void); |
3011
|
307 bool is_linked_to_global (void) const { return linked_to_global; } |
8
|
308 |
2846
|
309 void mark_as_static (void); |
3011
|
310 bool is_static (void) const { return tagged_static; } |
2846
|
311 |
3013
|
312 bool hides_fcn (void) const; |
|
313 bool hides_builtin (void) const; |
|
314 |
|
315 int rows (void) const { return definition->rows (); } |
|
316 int columns (void) const { return definition->columns (); } |
|
317 |
3523
|
318 std::string type_name (void) const { return definition->type_name (); } |
3013
|
319 |
3523
|
320 std::string type_as_string (void) const |
3355
|
321 { return definition->type_as_string (); } |
|
322 |
3523
|
323 void type (std::ostream& os, bool pr_type_info, bool quiet, bool pr_orig_txt) |
3356
|
324 { definition->type (os, name (), pr_type_info, quiet, pr_orig_txt); } |
|
325 |
3523
|
326 std::string which (void) { return definition->which (name ()); } |
3355
|
327 |
3523
|
328 void which (std::ostream& os) { definition->which (os, name ()); } |
3355
|
329 |
2975
|
330 octave_value& variable_value (void); |
2979
|
331 octave_lvalue variable_reference (void); |
2390
|
332 |
3011
|
333 symbol_record *next (void) const { return next_elem; } |
8
|
334 |
3011
|
335 void chain (symbol_record *s) { next_elem = s; } |
195
|
336 |
220
|
337 void push_context (void); |
3011
|
338 |
220
|
339 void pop_context (void); |
|
340 |
3523
|
341 void print_symbol_info_line (std::ostream& os); |
3013
|
342 |
3239
|
343 void dump_symbol_info (void); |
|
344 |
8
|
345 private: |
|
346 |
2893
|
347 unsigned int formal_param : 1; |
|
348 unsigned int linked_to_global : 1; |
|
349 unsigned int tagged_static : 1; |
195
|
350 |
3523
|
351 std::string nm; |
3011
|
352 change_function chg_fcn; |
195
|
353 symbol_def *definition; |
8
|
354 symbol_record *next_elem; |
395
|
355 |
3011
|
356 // This should maybe be one stack with a structure containing all the |
|
357 // items we need to save for recursive calls... |
220
|
358 SLStack <symbol_def *> context; |
2893
|
359 SLStack <unsigned int> global_link_context; |
8
|
360 |
3011
|
361 bool read_only_error (const char *action); |
195
|
362 |
|
363 void push_def (symbol_def *sd); |
3011
|
364 |
|
365 void remove_top_def (void); |
|
366 |
|
367 void replace_all_defs (symbol_def *sd); |
|
368 |
3258
|
369 void link_to_builtin_variable (void); |
|
370 |
3011
|
371 // No copying! |
|
372 |
|
373 symbol_record (const symbol_record& s); |
195
|
374 |
8
|
375 symbol_record& operator = (const symbol_record& s); |
|
376 }; |
|
377 |
767
|
378 // A symbol table. |
200
|
379 |
|
380 #define SYMTAB_LOCAL_SCOPE 1 |
|
381 #define SYMTAB_GLOBAL_SCOPE 2 |
|
382 |
|
383 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
|
384 |
3011
|
385 #define SYMTAB_ALL_TYPES (symbol_record::USER_FUNCTION \ |
|
386 | symbol_record::USER_VARIABLE \ |
3325
|
387 | symbol_record::DLD_FUNCTION \ |
3011
|
388 | symbol_record::BUILTIN_FUNCTION \ |
|
389 | symbol_record::TEXT_FUNCTION \ |
|
390 | symbol_record::MAPPER_FUNCTION \ |
3258
|
391 | symbol_record::BUILTIN_VARIABLE \ |
|
392 | symbol_record::BUILTIN_CONSTANT) |
200
|
393 |
3011
|
394 #define SYMTAB_VARIABLES (symbol_record::USER_VARIABLE \ |
|
395 | symbol_record::BUILTIN_VARIABLE) |
1412
|
396 |
8
|
397 class |
|
398 symbol_table |
|
399 { |
|
400 public: |
|
401 |
3011
|
402 symbol_table (unsigned int tab_size = 128) |
|
403 : table_size (tab_size), table (new symbol_record [table_size]) |
|
404 { |
|
405 assert ((tab_size % 2) == 0); |
|
406 } |
|
407 |
|
408 ~symbol_table (void) |
|
409 { |
|
410 delete [] table; |
|
411 } |
8
|
412 |
3523
|
413 symbol_record *lookup (const std::string& nm, bool insert = false, |
2856
|
414 bool warn = false); |
8
|
415 |
3523
|
416 void rename (const std::string& old_name, const std::string& new_name); |
572
|
417 |
2856
|
418 void clear (bool clear_user_functions = true); |
3523
|
419 bool clear (const std::string& nm, bool clear_user_functions = true); |
8
|
420 |
164
|
421 int size (void) const; |
8
|
422 |
3013
|
423 Array<symbol_record *> |
3355
|
424 symbol_list (const string_vector& pats = string_vector (), |
3013
|
425 unsigned int type = SYMTAB_ALL_TYPES, |
|
426 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
|
427 |
195
|
428 |
1755
|
429 string_vector |
3355
|
430 name_list (const string_vector& pats = string_vector (), |
3013
|
431 bool sort = false, unsigned int type = SYMTAB_ALL_TYPES, |
|
432 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
605
|
433 |
3011
|
434 |
|
435 int maybe_list (const char *header, const string_vector& argv, |
3523
|
436 std::ostream& os, bool show_verbose, |
3011
|
437 unsigned type, unsigned scope); |
|
438 |
3523
|
439 Array<symbol_record *> glob (const std::string& pat = std::string ("*"), |
3355
|
440 unsigned int type = SYMTAB_ALL_TYPES, |
|
441 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
8
|
442 |
220
|
443 void push_context (void); |
3011
|
444 |
220
|
445 void pop_context (void); |
|
446 |
3011
|
447 void print_stats (void); |
|
448 |
8
|
449 private: |
|
450 |
3011
|
451 unsigned int table_size; |
|
452 |
|
453 symbol_record *table; |
|
454 |
3523
|
455 unsigned int hash (const std::string& s); |
8
|
456 |
3011
|
457 // No copying! |
8
|
458 |
3011
|
459 symbol_table (const symbol_table&); |
1962
|
460 |
3011
|
461 symbol_table& operator = (const symbol_table&); |
|
462 }; |
2790
|
463 |
8
|
464 #endif |
|
465 |
|
466 /* |
|
467 ;;; Local Variables: *** |
|
468 ;;; mode: C++ *** |
|
469 ;;; End: *** |
|
470 */ |