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 |
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 |
2893
|
42 class octave_symbol; |
|
43 class octave_function; |
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: |
|
125 symbol_record (void); |
1755
|
126 symbol_record (const string& n, symbol_record *nxt = 0); |
8
|
127 |
1755
|
128 ~symbol_record (void) { } |
8
|
129 |
1755
|
130 string name (void) const; |
|
131 string help (void) const; |
2893
|
132 octave_symbol *def (void) const; |
8
|
133 |
1755
|
134 void rename (const string& new_name); |
195
|
135 |
2856
|
136 bool is_function (void) const; |
|
137 bool is_user_function (void) const; |
|
138 bool is_text_function (void) const; |
|
139 bool is_mapper_function (void) const; |
|
140 bool is_builtin_function (void) const; |
|
141 bool is_variable (void) const; |
|
142 bool is_user_variable (void) const; |
|
143 bool is_builtin_variable (void) const; |
|
144 bool is_map_element (const string& elts) const; |
195
|
145 |
2893
|
146 unsigned int type (void) const; |
8
|
147 |
2856
|
148 bool is_defined (void) const; |
|
149 bool is_read_only (void) const; |
|
150 bool is_eternal (void) const; |
8
|
151 |
|
152 void protect (void); |
|
153 void unprotect (void); |
|
154 void make_eternal (void); |
|
155 |
195
|
156 void set_sv_function (sv_Function f); |
|
157 |
2893
|
158 int define (const octave_value& v, |
|
159 unsigned int sym_type = symbol_def::USER_VARIABLE); |
|
160 |
2390
|
161 int define_as_fcn (const octave_value& v); |
2893
|
162 |
2390
|
163 int define_builtin_var (const octave_value& v); |
195
|
164 |
2893
|
165 int define (octave_function *f, unsigned int sym_type); |
|
166 |
1755
|
167 void document (const string& h); |
195
|
168 |
|
169 int clear (void); |
|
170 |
2856
|
171 void alias (symbol_record *s, bool force = false); |
8
|
172 |
|
173 void mark_as_formal_parameter (void); |
2856
|
174 bool is_formal_parameter (void) const; |
8
|
175 |
195
|
176 void mark_as_linked_to_global (void); |
2856
|
177 bool is_linked_to_global (void) const; |
8
|
178 |
2846
|
179 void mark_as_static (void); |
2856
|
180 bool is_static (void) const; |
2846
|
181 |
2390
|
182 octave_value variable_value (void) const; |
2948
|
183 octave_variable_reference variable_reference (void); |
2390
|
184 |
164
|
185 symbol_record *next (void) const; |
8
|
186 |
195
|
187 void chain (symbol_record *s); |
|
188 |
220
|
189 void push_context (void); |
|
190 void pop_context (void); |
|
191 |
8
|
192 private: |
|
193 |
2893
|
194 unsigned int formal_param : 1; |
|
195 unsigned int linked_to_global : 1; |
|
196 unsigned int tagged_static : 1; |
195
|
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; |
2893
|
206 SLStack <unsigned int> 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 |
2856
|
234 bool is_defined (void) const; |
|
235 bool is_read_only (void) const; |
|
236 bool is_eternal (void) const; |
|
237 bool hides_fcn (void) const; |
|
238 bool hides_builtin (void) const; |
2390
|
239 string type_name (void) const; |
2856
|
240 bool is_function (void) const; |
195
|
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 |
2856
|
254 bool initialized; |
2404
|
255 int nr; |
|
256 int nc; |
2893
|
257 unsigned int type : 6; |
|
258 unsigned int hides : 2; |
|
259 unsigned int eternal : 1; |
|
260 unsigned int 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 |
2856
|
289 symbol_record *lookup (const string& nm, bool insert = false, |
|
290 bool warn = false); |
8
|
291 |
1755
|
292 void rename (const string& old_name, const string& new_name); |
572
|
293 |
2856
|
294 void clear (bool clear_user_functions = true); |
|
295 int clear (const string& nm, bool clear_user_functions = true); |
8
|
296 |
164
|
297 int size (void) const; |
8
|
298 |
1755
|
299 symbol_record_info * |
|
300 long_list (int& count, const string_vector& pats = string_vector (), |
2856
|
301 int npats = 0, bool sort = false, |
2893
|
302 unsigned int type = SYMTAB_ALL_TYPES, |
|
303 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
195
|
304 |
1755
|
305 string_vector |
|
306 list (int& count, const string_vector& pats = string_vector (), |
2893
|
307 int npats = 0, bool sort = false, |
|
308 unsigned int type = SYMTAB_ALL_TYPES, |
|
309 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
605
|
310 |
1755
|
311 symbol_record **glob (int& count, const string& pat = string ("*"), |
2893
|
312 unsigned int type = SYMTAB_ALL_TYPES, |
|
313 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
8
|
314 |
220
|
315 void push_context (void); |
|
316 void pop_context (void); |
|
317 |
8
|
318 private: |
|
319 |
1755
|
320 unsigned int hash (const string& s); |
8
|
321 |
|
322 symbol_record table[HASH_TABLE_SIZE]; |
|
323 }; |
|
324 |
2856
|
325 extern bool valid_identifier (const char *s); |
1962
|
326 |
2856
|
327 extern bool valid_identifier (const string& s); |
2790
|
328 |
8
|
329 #endif |
|
330 |
|
331 /* |
|
332 ;;; Local Variables: *** |
|
333 ;;; mode: C++ *** |
|
334 ;;; End: *** |
|
335 */ |