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