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