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 |
|
180 // No copying! |
|
181 |
|
182 symbol_def (const symbol_def& sd); |
|
183 |
|
184 symbol_def& operator = (const symbol_def& sd); |
|
185 }; |
8
|
186 |
|
187 public: |
2953
|
188 |
3011
|
189 typedef int (*change_function) (void); |
2953
|
190 |
3011
|
191 symbol_record (void) |
|
192 : formal_param (0), linked_to_global (0), tagged_static (0), |
|
193 nm (), chg_fcn (0), definition (new symbol_def ()), |
|
194 next_elem (0) { } |
|
195 |
|
196 symbol_record (const string& n, symbol_record *nxt) |
|
197 : formal_param (0), linked_to_global (0), tagged_static (0), |
|
198 nm (n), chg_fcn (0), definition (new symbol_def ()), |
|
199 next_elem (nxt) { } |
8
|
200 |
1755
|
201 ~symbol_record (void) { } |
8
|
202 |
3011
|
203 string name (void) const { return nm; } |
2975
|
204 |
3011
|
205 string help (void) const { return definition->help (); } |
|
206 |
|
207 octave_value& def (void) { return definition->def (); } |
8
|
208 |
1755
|
209 void rename (const string& new_name); |
195
|
210 |
3011
|
211 bool is_function (void) const |
|
212 { return definition->is_function (); } |
|
213 |
|
214 bool is_text_function (void) const |
|
215 { return definition->is_text_function (); } |
|
216 |
|
217 bool is_mapper_function (void) const |
|
218 { return definition->is_mapper_function (); } |
|
219 |
|
220 bool is_user_function (void) const |
|
221 { return definition->is_user_function (); } |
195
|
222 |
3011
|
223 bool is_builtin_function (void) const |
|
224 { return definition->is_builtin_function (); } |
|
225 |
|
226 bool is_variable (void) const |
|
227 { return definition->is_variable (); } |
8
|
228 |
3011
|
229 bool is_user_variable (void) const |
|
230 { return definition->is_user_variable (); } |
|
231 |
|
232 bool is_builtin_variable (void) const |
|
233 { return definition->is_builtin_variable (); } |
|
234 |
|
235 bool is_map_element (const string& elts) const |
|
236 { return definition->is_map_element (elts); } |
8
|
237 |
3011
|
238 unsigned int type (void) const { return definition->type (); } |
|
239 |
|
240 bool is_defined (void) const { return definition->is_defined (); } |
|
241 |
|
242 bool is_read_only (void) const { return definition->is_read_only (); } |
8
|
243 |
3011
|
244 bool is_eternal (void) const { return definition->is_eternal (); } |
|
245 |
|
246 void protect (void) { definition->protect (); } |
195
|
247 |
3011
|
248 void unprotect (void) { definition->unprotect (); } |
|
249 |
|
250 void make_eternal (void) { definition->make_eternal (); } |
2893
|
251 |
3011
|
252 void set_change_function (change_function f) { chg_fcn = f; } |
2893
|
253 |
3011
|
254 void define (const octave_value& v, unsigned int sym_type = USER_VARIABLE); |
|
255 |
|
256 void define_builtin_var (const octave_value& v); |
195
|
257 |
3011
|
258 bool define_as_fcn (const octave_value& v); |
|
259 |
|
260 bool define (octave_function *f, unsigned int sym_type); |
2893
|
261 |
3011
|
262 void document (const string& h) { definition->document (h); } |
195
|
263 |
3011
|
264 void clear (void); |
195
|
265 |
2856
|
266 void alias (symbol_record *s, bool force = false); |
8
|
267 |
|
268 void mark_as_formal_parameter (void); |
3011
|
269 bool is_formal_parameter (void) const { return formal_param; } |
8
|
270 |
195
|
271 void mark_as_linked_to_global (void); |
3011
|
272 bool is_linked_to_global (void) const { return linked_to_global; } |
8
|
273 |
2846
|
274 void mark_as_static (void); |
3011
|
275 bool is_static (void) const { return tagged_static; } |
2846
|
276 |
3013
|
277 bool hides_fcn (void) const; |
|
278 bool hides_builtin (void) const; |
|
279 |
|
280 int rows (void) const { return definition->rows (); } |
|
281 int columns (void) const { return definition->columns (); } |
|
282 |
|
283 string type_name (void) const { return definition->type_name (); } |
|
284 |
2975
|
285 octave_value& variable_value (void); |
2979
|
286 octave_lvalue variable_reference (void); |
2390
|
287 |
3011
|
288 symbol_record *next (void) const { return next_elem; } |
8
|
289 |
3011
|
290 void chain (symbol_record *s) { next_elem = s; } |
195
|
291 |
220
|
292 void push_context (void); |
3011
|
293 |
220
|
294 void pop_context (void); |
|
295 |
3013
|
296 void print_symbol_info_line (ostream& os); |
|
297 |
8
|
298 private: |
|
299 |
2893
|
300 unsigned int formal_param : 1; |
|
301 unsigned int linked_to_global : 1; |
|
302 unsigned int tagged_static : 1; |
195
|
303 |
1755
|
304 string nm; |
3011
|
305 change_function chg_fcn; |
195
|
306 symbol_def *definition; |
8
|
307 symbol_record *next_elem; |
395
|
308 |
3011
|
309 // This should maybe be one stack with a structure containing all the |
|
310 // items we need to save for recursive calls... |
220
|
311 SLStack <symbol_def *> context; |
2893
|
312 SLStack <unsigned int> global_link_context; |
8
|
313 |
3011
|
314 bool read_only_error (const char *action); |
195
|
315 |
|
316 void push_def (symbol_def *sd); |
3011
|
317 |
|
318 void remove_top_def (void); |
|
319 |
|
320 void replace_all_defs (symbol_def *sd); |
|
321 |
|
322 // No copying! |
|
323 |
|
324 symbol_record (const symbol_record& s); |
195
|
325 |
8
|
326 symbol_record& operator = (const symbol_record& s); |
|
327 }; |
|
328 |
767
|
329 // A symbol table. |
200
|
330 |
|
331 #define SYMTAB_LOCAL_SCOPE 1 |
|
332 #define SYMTAB_GLOBAL_SCOPE 2 |
|
333 |
|
334 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
|
335 |
3011
|
336 #define SYMTAB_ALL_TYPES (symbol_record::USER_FUNCTION \ |
|
337 | symbol_record::USER_VARIABLE \ |
|
338 | symbol_record::BUILTIN_FUNCTION \ |
|
339 | symbol_record::TEXT_FUNCTION \ |
|
340 | symbol_record::MAPPER_FUNCTION \ |
|
341 | symbol_record::BUILTIN_VARIABLE) |
200
|
342 |
3011
|
343 #define SYMTAB_VARIABLES (symbol_record::USER_VARIABLE \ |
|
344 | symbol_record::BUILTIN_VARIABLE) |
1412
|
345 |
8
|
346 class |
|
347 symbol_table |
|
348 { |
|
349 public: |
|
350 |
3011
|
351 symbol_table (unsigned int tab_size = 128) |
|
352 : table_size (tab_size), table (new symbol_record [table_size]) |
|
353 { |
|
354 assert ((tab_size % 2) == 0); |
|
355 } |
|
356 |
|
357 ~symbol_table (void) |
|
358 { |
|
359 delete [] table; |
|
360 } |
8
|
361 |
2856
|
362 symbol_record *lookup (const string& nm, bool insert = false, |
|
363 bool warn = false); |
8
|
364 |
1755
|
365 void rename (const string& old_name, const string& new_name); |
572
|
366 |
2856
|
367 void clear (bool clear_user_functions = true); |
3011
|
368 bool clear (const string& nm, bool clear_user_functions = true); |
8
|
369 |
164
|
370 int size (void) const; |
8
|
371 |
3013
|
372 Array<symbol_record *> |
|
373 symbol_list (int& count, const string_vector& pats = string_vector (), |
|
374 unsigned int type = SYMTAB_ALL_TYPES, |
|
375 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
|
376 |
195
|
377 |
1755
|
378 string_vector |
3013
|
379 name_list (int& count, const string_vector& pats = string_vector (), |
|
380 bool sort = false, unsigned int type = SYMTAB_ALL_TYPES, |
|
381 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
605
|
382 |
3011
|
383 |
|
384 int maybe_list (const char *header, const string_vector& argv, |
3013
|
385 ostream& os, bool show_verbose, |
3011
|
386 unsigned type, unsigned scope); |
|
387 |
1755
|
388 symbol_record **glob (int& count, const string& pat = string ("*"), |
2893
|
389 unsigned int type = SYMTAB_ALL_TYPES, |
|
390 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
8
|
391 |
220
|
392 void push_context (void); |
3011
|
393 |
220
|
394 void pop_context (void); |
|
395 |
3011
|
396 void print_stats (void); |
|
397 |
8
|
398 private: |
|
399 |
3011
|
400 unsigned int table_size; |
|
401 |
|
402 symbol_record *table; |
|
403 |
1755
|
404 unsigned int hash (const string& s); |
8
|
405 |
3011
|
406 // No copying! |
8
|
407 |
3011
|
408 symbol_table (const symbol_table&); |
1962
|
409 |
3011
|
410 symbol_table& operator = (const symbol_table&); |
|
411 }; |
2790
|
412 |
8
|
413 #endif |
|
414 |
|
415 /* |
|
416 ;;; Local Variables: *** |
|
417 ;;; mode: C++ *** |
|
418 ;;; End: *** |
|
419 */ |