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