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 |
1755
|
30 #include <string> |
|
31 |
220
|
32 #include "SLStack.h" |
|
33 |
1755
|
34 #include "str-vec.h" |
|
35 |
767
|
36 // Must be multiple of 2. |
|
37 #define HASH_TABLE_SIZE 1024 |
8
|
38 #define HASH_MASK (HASH_TABLE_SIZE - 1) |
|
39 |
2893
|
40 class octave_symbol; |
|
41 class octave_function; |
2956
|
42 class octave_value; |
|
43 class octave_variable_reference; |
8
|
44 |
1755
|
45 class string_vector; |
|
46 |
8
|
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 |
2893
|
61 symbol_def (octave_symbol *sym = 0, unsigned int sym_type = 0); |
8
|
62 |
|
63 ~symbol_def (void); |
|
64 |
2856
|
65 bool is_variable (void) const; |
|
66 bool is_function (void) const; |
|
67 bool is_text_function (void) const; |
|
68 bool is_mapper_function (void) const; |
|
69 bool is_user_variable (void) const; |
|
70 bool is_user_function (void) const; |
|
71 bool is_builtin_variable (void) const; |
|
72 bool is_builtin_function (void) const; |
|
73 bool is_map_element (const string& elts) const; |
195
|
74 |
2893
|
75 void define (octave_symbol *sym, unsigned int sym_type); |
8
|
76 |
195
|
77 void protect (void); |
|
78 void unprotect (void); |
|
79 void make_eternal (void); |
|
80 |
2893
|
81 octave_symbol *def (void) const; |
1755
|
82 string help (void) const; |
|
83 void document (const string& h); |
8
|
84 |
195
|
85 enum TYPE |
|
86 { |
|
87 UNKNOWN = 0, |
|
88 USER_FUNCTION = 1, |
|
89 USER_VARIABLE = 2, |
|
90 BUILTIN_FUNCTION = 4, |
530
|
91 TEXT_FUNCTION = 8, |
|
92 MAPPER_FUNCTION = 16, |
|
93 BUILTIN_VARIABLE = 32 |
195
|
94 }; |
|
95 |
2893
|
96 unsigned int symbol_type (void) { return type; } |
|
97 |
195
|
98 friend maybe_delete (symbol_def *def); |
|
99 |
8
|
100 private: |
|
101 |
2893
|
102 unsigned int type : 6; |
|
103 unsigned int eternal : 1; |
|
104 unsigned int read_only : 1; |
195
|
105 |
1755
|
106 string help_string; |
2893
|
107 octave_symbol *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: |
2953
|
125 |
|
126 typedef int (*sv_function) (void); |
|
127 |
8
|
128 symbol_record (void); |
1755
|
129 symbol_record (const string& n, symbol_record *nxt = 0); |
8
|
130 |
1755
|
131 ~symbol_record (void) { } |
8
|
132 |
1755
|
133 string name (void) const; |
|
134 string help (void) const; |
2893
|
135 octave_symbol *def (void) const; |
8
|
136 |
1755
|
137 void rename (const string& new_name); |
195
|
138 |
2856
|
139 bool is_function (void) const; |
|
140 bool is_user_function (void) const; |
|
141 bool is_text_function (void) const; |
|
142 bool is_mapper_function (void) const; |
|
143 bool is_builtin_function (void) const; |
|
144 bool is_variable (void) const; |
|
145 bool is_user_variable (void) const; |
|
146 bool is_builtin_variable (void) const; |
|
147 bool is_map_element (const string& elts) const; |
195
|
148 |
2893
|
149 unsigned int type (void) const; |
8
|
150 |
2856
|
151 bool is_defined (void) const; |
|
152 bool is_read_only (void) const; |
|
153 bool is_eternal (void) const; |
8
|
154 |
|
155 void protect (void); |
|
156 void unprotect (void); |
|
157 void make_eternal (void); |
|
158 |
2953
|
159 void set_sv_function (sv_function f); |
195
|
160 |
2893
|
161 int define (const octave_value& v, |
|
162 unsigned int sym_type = symbol_def::USER_VARIABLE); |
|
163 |
2390
|
164 int define_as_fcn (const octave_value& v); |
2893
|
165 |
2390
|
166 int define_builtin_var (const octave_value& v); |
195
|
167 |
2893
|
168 int define (octave_function *f, unsigned int sym_type); |
|
169 |
1755
|
170 void document (const string& h); |
195
|
171 |
|
172 int clear (void); |
|
173 |
2856
|
174 void alias (symbol_record *s, bool force = false); |
8
|
175 |
|
176 void mark_as_formal_parameter (void); |
2856
|
177 bool is_formal_parameter (void) const; |
8
|
178 |
195
|
179 void mark_as_linked_to_global (void); |
2856
|
180 bool is_linked_to_global (void) const; |
8
|
181 |
2846
|
182 void mark_as_static (void); |
2856
|
183 bool is_static (void) const; |
2846
|
184 |
2390
|
185 octave_value variable_value (void) const; |
2948
|
186 octave_variable_reference variable_reference (void); |
2390
|
187 |
164
|
188 symbol_record *next (void) const; |
8
|
189 |
195
|
190 void chain (symbol_record *s); |
|
191 |
220
|
192 void push_context (void); |
|
193 void pop_context (void); |
|
194 |
8
|
195 private: |
|
196 |
2893
|
197 unsigned int formal_param : 1; |
|
198 unsigned int linked_to_global : 1; |
|
199 unsigned int tagged_static : 1; |
195
|
200 |
1755
|
201 string nm; |
2953
|
202 sv_function sv_fcn; |
195
|
203 symbol_def *definition; |
8
|
204 symbol_record *next_elem; |
395
|
205 |
|
206 // This should maybe be one stack with a structure containing all the |
|
207 // items we need to save for recursive calls... |
220
|
208 SLStack <symbol_def *> context; |
2893
|
209 SLStack <unsigned int> global_link_context; |
8
|
210 |
195
|
211 void init_state (void); |
|
212 |
2791
|
213 int read_only_error (const char *action); |
195
|
214 |
|
215 void push_def (symbol_def *sd); |
|
216 symbol_def *pop_def (void); |
|
217 |
8
|
218 symbol_record& operator = (const symbol_record& s); |
|
219 }; |
|
220 |
767
|
221 // A structure for handling verbose information about a symbol_record. |
195
|
222 |
|
223 class |
|
224 symbol_record_info |
|
225 { |
|
226 public: |
|
227 |
|
228 symbol_record_info (void); |
|
229 symbol_record_info (const symbol_record& s); |
|
230 |
|
231 symbol_record_info (const symbol_record_info& s); |
|
232 |
1755
|
233 ~symbol_record_info (void) { } |
195
|
234 |
|
235 symbol_record_info& operator = (const symbol_record_info& s); |
|
236 |
2856
|
237 bool is_defined (void) const; |
|
238 bool is_read_only (void) const; |
|
239 bool is_eternal (void) const; |
|
240 bool hides_fcn (void) const; |
|
241 bool hides_builtin (void) const; |
2390
|
242 string type_name (void) const; |
2856
|
243 bool is_function (void) const; |
195
|
244 int rows (void) const; |
|
245 int columns (void) const; |
1755
|
246 string name (void) const; |
195
|
247 |
|
248 enum HIDES |
|
249 { |
|
250 SR_INFO_NONE = 0, |
|
251 SR_INFO_USER_FUNCTION = 1, |
|
252 SR_INFO_BUILTIN_FUNCTION = 2 |
|
253 }; |
|
254 |
|
255 private: |
|
256 |
2856
|
257 bool initialized; |
2404
|
258 int nr; |
|
259 int nc; |
2893
|
260 unsigned int type : 6; |
|
261 unsigned int hides : 2; |
|
262 unsigned int eternal : 1; |
|
263 unsigned int read_only : 1; |
1755
|
264 string nm; |
2404
|
265 string const_type; |
195
|
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 |
2856
|
292 symbol_record *lookup (const string& nm, bool insert = false, |
|
293 bool warn = false); |
8
|
294 |
1755
|
295 void rename (const string& old_name, const string& new_name); |
572
|
296 |
2856
|
297 void clear (bool clear_user_functions = true); |
|
298 int clear (const string& nm, bool clear_user_functions = true); |
8
|
299 |
164
|
300 int size (void) const; |
8
|
301 |
1755
|
302 symbol_record_info * |
|
303 long_list (int& count, const string_vector& pats = string_vector (), |
2856
|
304 int npats = 0, bool sort = false, |
2893
|
305 unsigned int type = SYMTAB_ALL_TYPES, |
|
306 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
195
|
307 |
1755
|
308 string_vector |
|
309 list (int& count, const string_vector& pats = string_vector (), |
2893
|
310 int npats = 0, bool sort = false, |
|
311 unsigned int type = SYMTAB_ALL_TYPES, |
|
312 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
605
|
313 |
1755
|
314 symbol_record **glob (int& count, const string& pat = string ("*"), |
2893
|
315 unsigned int type = SYMTAB_ALL_TYPES, |
|
316 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
8
|
317 |
220
|
318 void push_context (void); |
|
319 void pop_context (void); |
|
320 |
8
|
321 private: |
|
322 |
1755
|
323 unsigned int hash (const string& s); |
8
|
324 |
|
325 symbol_record table[HASH_TABLE_SIZE]; |
|
326 }; |
|
327 |
2856
|
328 extern bool valid_identifier (const char *s); |
1962
|
329 |
2856
|
330 extern bool valid_identifier (const string& s); |
2790
|
331 |
8
|
332 #endif |
|
333 |
|
334 /* |
|
335 ;;; Local Variables: *** |
|
336 ;;; mode: C++ *** |
|
337 ;;; End: *** |
|
338 */ |