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