530
|
1 // symtab.h -*- C++ -*- |
8
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
8
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
383
|
24 #if !defined (octave_symtab_h) |
|
25 #define octave_symtab_h 1 |
8
|
26 |
1297
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
220
|
31 #include "SLStack.h" |
|
32 |
530
|
33 #include "variables.h" |
|
34 |
|
35 class ostream; |
8
|
36 |
767
|
37 // Must be multiple of 2. |
|
38 #define HASH_TABLE_SIZE 1024 |
8
|
39 #define HASH_MASK (HASH_TABLE_SIZE - 1) |
|
40 |
|
41 class tree; |
489
|
42 class tree_fvc; |
8
|
43 class tree_builtin; |
|
44 class tree_constant; |
|
45 class tree_function; |
|
46 |
|
47 class symbol_def; |
|
48 class symbol_record; |
195
|
49 class symbol_record_info; |
8
|
50 class symbol_table; |
|
51 |
767
|
52 // Variables or functions. |
|
53 |
8
|
54 class symbol_def |
|
55 { |
|
56 friend class symbol_record; |
195
|
57 friend class symbol_record_info; |
8
|
58 |
|
59 public: |
|
60 |
|
61 symbol_def (void); |
|
62 symbol_def (tree_constant *t); |
530
|
63 symbol_def (tree_builtin *t, unsigned fcn_type = 0); |
|
64 symbol_def (tree_function *t, unsigned fcn_type = 0); |
8
|
65 |
|
66 ~symbol_def (void); |
|
67 |
195
|
68 int is_variable (void) const; |
|
69 int is_function (void) const; |
530
|
70 int is_text_function (void) const; |
|
71 int is_mapper_function (void) const; |
195
|
72 int is_user_variable (void) const; |
|
73 int is_user_function (void) const; |
|
74 int is_builtin_variable (void) const; |
|
75 int is_builtin_function (void) const; |
|
76 |
8
|
77 void define (tree_constant *t); |
530
|
78 void define (tree_builtin *t, unsigned fcn_type = 0); |
|
79 void define (tree_function *t, unsigned fcn_type = 0); |
8
|
80 |
195
|
81 void protect (void); |
|
82 void unprotect (void); |
|
83 void make_eternal (void); |
|
84 |
489
|
85 tree_fvc *def (void) const; |
164
|
86 char *help (void) const; |
|
87 void document (const char *h); |
8
|
88 |
195
|
89 enum TYPE |
|
90 { |
|
91 UNKNOWN = 0, |
|
92 USER_FUNCTION = 1, |
|
93 USER_VARIABLE = 2, |
|
94 BUILTIN_FUNCTION = 4, |
530
|
95 TEXT_FUNCTION = 8, |
|
96 MAPPER_FUNCTION = 16, |
|
97 BUILTIN_VARIABLE = 32 |
195
|
98 }; |
|
99 |
|
100 friend maybe_delete (symbol_def *def); |
|
101 |
8
|
102 private: |
|
103 |
530
|
104 unsigned type : 6; |
195
|
105 unsigned eternal : 1; |
|
106 unsigned read_only : 1; |
|
107 |
8
|
108 char *help_string; |
489
|
109 tree_fvc *definition; |
195
|
110 symbol_def *next_elem; |
8
|
111 int count; |
195
|
112 |
|
113 void init_state (void); |
8
|
114 |
|
115 symbol_def (const symbol_def& sd); |
|
116 symbol_def& operator = (const symbol_def& sd); |
|
117 }; |
|
118 |
767
|
119 // Individual records in a symbol table. |
|
120 |
8
|
121 class |
|
122 symbol_record |
|
123 { |
195
|
124 friend class symbol_record_info; |
8
|
125 |
|
126 public: |
|
127 symbol_record (void); |
530
|
128 symbol_record (const char *n, symbol_record *nxt = 0); |
8
|
129 |
|
130 ~symbol_record (void); |
|
131 |
164
|
132 char *name (void) const; |
|
133 char *help (void) const; |
489
|
134 tree_fvc *def (void) const; |
8
|
135 |
572
|
136 void rename (const char *new_name); |
195
|
137 |
164
|
138 int is_function (void) const; |
195
|
139 int is_user_function (void) const; |
530
|
140 int is_text_function (void) const; |
|
141 int is_mapper_function (void) const; |
195
|
142 int is_builtin_function (void) const; |
164
|
143 int is_variable (void) const; |
195
|
144 int is_user_variable (void) const; |
|
145 int is_builtin_variable (void) const; |
|
146 |
|
147 unsigned type (void) const; |
8
|
148 |
164
|
149 int is_defined (void) const; |
195
|
150 int is_read_only (void) const; |
|
151 int is_eternal (void) const; |
8
|
152 |
|
153 void protect (void); |
|
154 void unprotect (void); |
|
155 void make_eternal (void); |
|
156 |
195
|
157 void set_sv_function (sv_Function f); |
|
158 |
|
159 int define (tree_constant *t); |
530
|
160 int define (tree_builtin *t, int text_fcn = 0); |
|
161 int define (tree_function *t, int text_fcn = 0); |
195
|
162 int define_as_fcn (tree_constant *t); |
|
163 int define_builtin_var (tree_constant *t); |
|
164 |
|
165 void document (const char *h); |
|
166 |
|
167 int clear (void); |
|
168 |
|
169 void alias (symbol_record *s, int force = 0); |
8
|
170 |
|
171 void mark_as_formal_parameter (void); |
164
|
172 int is_formal_parameter (void) const; |
8
|
173 |
195
|
174 void mark_as_linked_to_global (void); |
|
175 int is_linked_to_global (void) const; |
8
|
176 |
164
|
177 symbol_record *next (void) const; |
8
|
178 |
195
|
179 void chain (symbol_record *s); |
|
180 |
220
|
181 void push_context (void); |
|
182 void pop_context (void); |
|
183 |
8
|
184 private: |
|
185 |
195
|
186 unsigned formal_param : 1; |
|
187 unsigned linked_to_global : 1; |
|
188 |
8
|
189 char *nm; |
|
190 sv_Function sv_fcn; |
195
|
191 symbol_def *definition; |
8
|
192 symbol_record *next_elem; |
395
|
193 |
|
194 // This should maybe be one stack with a structure containing all the |
|
195 // items we need to save for recursive calls... |
220
|
196 SLStack <symbol_def *> context; |
395
|
197 SLStack <unsigned> global_link_context; |
8
|
198 |
195
|
199 void init_state (void); |
|
200 |
|
201 int read_only_error (void); |
|
202 |
|
203 void push_def (symbol_def *sd); |
|
204 symbol_def *pop_def (void); |
|
205 |
8
|
206 symbol_record& operator = (const symbol_record& s); |
|
207 }; |
|
208 |
767
|
209 // A structure for handling verbose information about a symbol_record. |
195
|
210 |
|
211 class |
|
212 symbol_record_info |
|
213 { |
|
214 public: |
|
215 |
|
216 symbol_record_info (void); |
|
217 symbol_record_info (const symbol_record& s); |
|
218 |
|
219 symbol_record_info (const symbol_record_info& s); |
|
220 |
|
221 ~symbol_record_info (void); |
|
222 |
|
223 symbol_record_info& operator = (const symbol_record_info& s); |
|
224 |
|
225 int is_defined (void) const; |
|
226 int is_read_only (void) const; |
|
227 int is_eternal (void) const; |
|
228 int hides_fcn (void) const; |
|
229 int hides_builtin (void) const; |
|
230 char *type_as_string (void) const; |
|
231 int is_function (void) const; |
|
232 int rows (void) const; |
|
233 int columns (void) const; |
|
234 char *name (void) const; |
|
235 |
|
236 enum HIDES |
|
237 { |
|
238 SR_INFO_NONE = 0, |
|
239 SR_INFO_USER_FUNCTION = 1, |
|
240 SR_INFO_BUILTIN_FUNCTION = 2 |
|
241 }; |
|
242 |
|
243 enum CONST_TYPE |
|
244 { |
|
245 SR_INFO_UNKNOWN = 0, |
|
246 SR_INFO_SCALAR = 1, |
|
247 SR_INFO_COMPLEX_SCALAR = 2, |
|
248 SR_INFO_MATRIX = 4, |
|
249 SR_INFO_COMPLEX_MATRIX = 8, |
|
250 SR_INFO_RANGE = 16, |
|
251 SR_INFO_STRING = 32 |
|
252 }; |
|
253 |
|
254 private: |
|
255 |
|
256 void init_state (void); |
|
257 |
|
258 unsigned type : 4; |
|
259 unsigned const_type : 6; |
|
260 unsigned hides : 2; |
|
261 unsigned eternal : 1; |
|
262 unsigned read_only : 1; |
|
263 int nr; |
|
264 int nc; |
|
265 char *nm; |
|
266 |
|
267 int initialized; |
|
268 }; |
|
269 |
767
|
270 // A symbol table. |
200
|
271 |
|
272 #define SYMTAB_LOCAL_SCOPE 1 |
|
273 #define SYMTAB_GLOBAL_SCOPE 2 |
|
274 |
|
275 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
|
276 |
|
277 #define SYMTAB_ALL_TYPES (symbol_def::USER_FUNCTION \ |
|
278 | symbol_def::USER_VARIABLE \ |
|
279 | symbol_def::BUILTIN_FUNCTION \ |
530
|
280 | symbol_def::TEXT_FUNCTION \ |
541
|
281 | symbol_def::MAPPER_FUNCTION \ |
200
|
282 | symbol_def::BUILTIN_VARIABLE) |
|
283 |
8
|
284 class |
|
285 symbol_table |
|
286 { |
|
287 public: |
|
288 |
|
289 symbol_table (void); |
|
290 |
164
|
291 symbol_record *lookup (const char *nm, int insert = 0, int warn = 0); |
8
|
292 |
572
|
293 void rename (const char *old_name, const char *new_name); |
|
294 |
195
|
295 void clear (int clear_user_functions = 1); |
|
296 int clear (const char *nm, int clear_user_functions = 1); |
8
|
297 |
164
|
298 int size (void) const; |
8
|
299 |
867
|
300 symbol_record_info *long_list (int& count, char **pats = 0, |
|
301 int npats = 0, int sort = 0, |
195
|
302 unsigned type = SYMTAB_ALL_TYPES, |
|
303 unsigned scope = SYMTAB_ALL_SCOPES) const; |
|
304 |
867
|
305 char **list (int& count, char **pats = 0, int npats = 0, |
|
306 int sort = 0, unsigned type = SYMTAB_ALL_TYPES, |
605
|
307 unsigned scope = SYMTAB_ALL_SCOPES) const; |
|
308 |
|
309 symbol_record **glob (int& count, char *pat = "*", |
|
310 unsigned type = SYMTAB_ALL_TYPES, |
|
311 unsigned scope = SYMTAB_ALL_SCOPES) const; |
8
|
312 |
220
|
313 void push_context (void); |
|
314 void pop_context (void); |
|
315 |
8
|
316 private: |
|
317 |
|
318 unsigned int hash (const char *s); |
|
319 |
|
320 symbol_record table[HASH_TABLE_SIZE]; |
|
321 }; |
|
322 |
|
323 #endif |
|
324 |
|
325 /* |
|
326 ;;; Local Variables: *** |
|
327 ;;; mode: C++ *** |
|
328 ;;; page-delimiter: "^/\\*" *** |
|
329 ;;; End: *** |
|
330 */ |