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