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 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
27 #pragma interface |
|
28 #endif |
|
29 |
3011
|
30 #include <cassert> |
|
31 |
1755
|
32 #include <string> |
4214
|
33 #include <stack> |
220
|
34 |
4238
|
35 #include "lo-sstream.h" |
3011
|
36 #include "oct-alloc.h" |
1755
|
37 #include "str-vec.h" |
|
38 |
2975
|
39 #include "ov.h" |
|
40 |
2979
|
41 class octave_lvalue; |
8
|
42 |
1755
|
43 class string_vector; |
|
44 |
8
|
45 class symbol_record; |
|
46 class symbol_table; |
|
47 |
4913
|
48 struct |
|
49 whos_parameter |
|
50 { |
|
51 char command, modifier; |
|
52 int parameter_length, first_parameter_length, dimensions, balance; |
|
53 std::string text; |
|
54 std::string line; |
|
55 }; |
|
56 |
3011
|
57 // Individual records in a symbol table. |
8
|
58 |
3011
|
59 class |
|
60 symbol_record |
|
61 { |
|
62 public: |
8
|
63 |
3258
|
64 // If you add or delete an entry here, you'll also need to change |
3325
|
65 // the width parameter in the declaration for symbol_type below... |
3258
|
66 |
195
|
67 enum TYPE |
|
68 { |
|
69 UNKNOWN = 0, |
|
70 USER_FUNCTION = 1, |
|
71 USER_VARIABLE = 2, |
3325
|
72 DLD_FUNCTION = 4, |
|
73 BUILTIN_FUNCTION = 8, |
4208
|
74 COMMAND = 16, |
3325
|
75 MAPPER_FUNCTION = 32, |
|
76 BUILTIN_VARIABLE = 64, |
|
77 BUILTIN_CONSTANT = 128 |
195
|
78 }; |
|
79 |
8
|
80 private: |
|
81 |
3011
|
82 // Variables or functions. |
|
83 |
|
84 class symbol_def |
|
85 { |
|
86 public: |
|
87 |
|
88 symbol_def (const octave_value& val = octave_value (), |
|
89 unsigned int sym_type = 0) |
|
90 : symbol_type (sym_type), eternal (0), read_only (0), help_string (), |
4009
|
91 definition (val), count (1) { } |
3011
|
92 |
|
93 ~symbol_def (void) { } |
|
94 |
3258
|
95 bool is_constant (void) const |
|
96 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
97 |
3011
|
98 bool is_variable (void) const |
|
99 { |
|
100 return (symbol_type & symbol_record::USER_VARIABLE |
|
101 || symbol_type & symbol_record::BUILTIN_VARIABLE); |
|
102 } |
|
103 |
4208
|
104 // It's not necessary to check for COMMAND and MAPPER_FUNCTION |
3968
|
105 // here. Those tags are just used as additional qualifiers for |
|
106 // the other types of functions. |
|
107 |
3011
|
108 bool is_function (void) const |
|
109 { |
|
110 return (symbol_type & symbol_record::USER_FUNCTION |
3325
|
111 || symbol_type & symbol_record::DLD_FUNCTION |
3011
|
112 || symbol_type & symbol_record::BUILTIN_FUNCTION); |
|
113 } |
|
114 |
|
115 bool is_user_variable (void) const |
|
116 { return (symbol_type & symbol_record::USER_VARIABLE); } |
195
|
117 |
4208
|
118 void mark_as_command (void) |
|
119 { symbol_type |= symbol_record::COMMAND; } |
4207
|
120 |
4208
|
121 void unmark_command (void) |
|
122 { symbol_type &= ~symbol_record::COMMAND; } |
4207
|
123 |
4208
|
124 bool is_command (void) const |
|
125 { return (symbol_type & symbol_record::COMMAND); } |
3011
|
126 |
|
127 bool is_mapper_function (void) const |
|
128 { return (symbol_type & symbol_record::MAPPER_FUNCTION); } |
|
129 |
|
130 bool is_user_function (void) const |
|
131 { return (symbol_type & symbol_record::USER_FUNCTION); } |
|
132 |
3258
|
133 bool is_builtin_constant (void) const |
|
134 { return (symbol_type & symbol_record::BUILTIN_CONSTANT); } |
|
135 |
3011
|
136 bool is_builtin_variable (void) const |
|
137 { return (symbol_type & symbol_record::BUILTIN_VARIABLE); } |
195
|
138 |
3011
|
139 bool is_builtin_function (void) const |
|
140 { return (symbol_type & symbol_record::BUILTIN_FUNCTION); } |
|
141 |
3325
|
142 bool is_dld_function (void) const |
|
143 { return (symbol_type & symbol_record::DLD_FUNCTION); } |
|
144 |
3011
|
145 // XXX FIXME XXX |
3523
|
146 bool is_map_element (const std::string& /* elts */) const |
3011
|
147 { return false; } |
|
148 |
|
149 bool is_defined (void) const |
|
150 { return definition.is_defined (); } |
|
151 |
|
152 bool is_read_only (void) const |
|
153 { return read_only; } |
|
154 |
|
155 bool is_eternal (void) const |
|
156 { return eternal; } |
8
|
157 |
4913
|
158 bool is_matrix_type (void) const |
|
159 { return definition.is_matrix_type (); } |
|
160 |
|
161 size_t byte_size (void) const |
|
162 { return definition.byte_size (); }; |
|
163 |
|
164 int numel (void) const |
|
165 { return definition.numel (); }; |
|
166 |
|
167 dim_vector dims (void) const |
|
168 { return definition.dims (); } |
|
169 |
3013
|
170 int rows (void) const { return definition.rows (); } |
|
171 int columns (void) const { return definition.columns (); } |
|
172 |
3523
|
173 std::string type_name (void) const { return definition.type_name (); } |
3013
|
174 |
3523
|
175 std::string type_as_string (void) const; |
3355
|
176 |
3523
|
177 void type (std::ostream& os, const std::string& name, bool pr_type_info, |
3356
|
178 bool quiet, bool pr_orig_txt); |
|
179 |
3523
|
180 std::string which (const std::string& name); |
3355
|
181 |
3523
|
182 void which (std::ostream& os, const std::string& name); |
3355
|
183 |
3011
|
184 void define (const octave_value& val, unsigned int sym_type) |
|
185 { |
|
186 definition = val; |
|
187 symbol_type = sym_type; |
|
188 } |
|
189 |
|
190 void protect (void) { read_only = 1; } |
|
191 |
|
192 void unprotect (void) { read_only = 0; } |
|
193 |
|
194 void make_eternal (void) { eternal = 1; } |
|
195 |
|
196 octave_value& def (void) { return definition; } |
|
197 |
3523
|
198 std::string help (void) const { return help_string; } |
3011
|
199 |
3523
|
200 void document (const std::string& h) { help_string = h; } |
3011
|
201 |
|
202 unsigned int type (void) { return symbol_type; } |
|
203 |
|
204 void *operator new (size_t size) |
|
205 { return allocator.alloc (size); } |
|
206 |
|
207 void operator delete (void *p, size_t size) |
|
208 { allocator.free (p, size); } |
|
209 |
|
210 static octave_allocator allocator; |
8
|
211 |
3011
|
212 // The type of this symbol (see the enum above). |
3325
|
213 unsigned int symbol_type : 8; |
3011
|
214 |
|
215 // Nonzero means this variable cannot be cleared. |
|
216 unsigned int eternal : 1; |
|
217 |
|
218 // Nonzero means this variable cannot be given a new value. |
|
219 unsigned int read_only : 1; |
|
220 |
|
221 // The doc string associated with this variable. |
3523
|
222 std::string help_string; |
3011
|
223 |
|
224 // The value of this definition. See ov.h and related files. |
|
225 octave_value definition; |
767
|
226 |
3011
|
227 // Reference count. |
|
228 int count; |
|
229 |
3933
|
230 void print_info (std::ostream& os, |
|
231 const std::string& prefix = std::string ()) const; |
3239
|
232 |
3011
|
233 // No copying! |
|
234 |
|
235 symbol_def (const symbol_def& sd); |
|
236 |
|
237 symbol_def& operator = (const symbol_def& sd); |
|
238 }; |
8
|
239 |
|
240 public: |
2953
|
241 |
3011
|
242 typedef int (*change_function) (void); |
2953
|
243 |
3011
|
244 symbol_record (void) |
|
245 : formal_param (0), linked_to_global (0), tagged_static (0), |
4234
|
246 can_hide_function (1), nm (), chg_fcn (0), |
|
247 definition (new symbol_def ()), next_elem (0) { } |
|
248 |
|
249 // XXX FIXME XXX -- kluge alert! We obviously need a better way of |
|
250 // handling allow_shadow! |
3011
|
251 |
3523
|
252 symbol_record (const std::string& n, symbol_record *nxt) |
3011
|
253 : formal_param (0), linked_to_global (0), tagged_static (0), |
4234
|
254 can_hide_function (n != "__end__"), nm (n), chg_fcn (0), |
|
255 definition (new symbol_def ()), next_elem (nxt) { } |
8
|
256 |
1755
|
257 ~symbol_record (void) { } |
8
|
258 |
3523
|
259 std::string name (void) const { return nm; } |
2975
|
260 |
3523
|
261 std::string help (void) const { return definition->help (); } |
3011
|
262 |
|
263 octave_value& def (void) { return definition->def (); } |
8
|
264 |
3523
|
265 void rename (const std::string& new_name); |
195
|
266 |
3011
|
267 bool is_function (void) const |
|
268 { return definition->is_function (); } |
|
269 |
4208
|
270 void mark_as_command (void) |
|
271 { definition->mark_as_command (); } |
4207
|
272 |
4208
|
273 void unmark_command (void) |
|
274 { definition->unmark_command (); } |
4207
|
275 |
4208
|
276 bool is_command (void) const |
|
277 { return definition->is_command (); } |
3011
|
278 |
|
279 bool is_mapper_function (void) const |
|
280 { return definition->is_mapper_function (); } |
|
281 |
|
282 bool is_user_function (void) const |
|
283 { return definition->is_user_function (); } |
195
|
284 |
3011
|
285 bool is_builtin_function (void) const |
|
286 { return definition->is_builtin_function (); } |
|
287 |
3325
|
288 bool is_dld_function (void) const |
|
289 { return definition->is_dld_function (); } |
|
290 |
3258
|
291 bool is_constant (void) const |
|
292 { return definition->is_constant (); } |
|
293 |
|
294 bool is_builtin_constant (void) const |
|
295 { return definition->is_builtin_constant (); } |
|
296 |
3011
|
297 bool is_variable (void) const |
|
298 { return definition->is_variable (); } |
8
|
299 |
3011
|
300 bool is_user_variable (void) const |
|
301 { return definition->is_user_variable (); } |
|
302 |
|
303 bool is_builtin_variable (void) const |
|
304 { return definition->is_builtin_variable (); } |
|
305 |
3523
|
306 bool is_map_element (const std::string& elts) const |
3011
|
307 { return definition->is_map_element (elts); } |
8
|
308 |
3011
|
309 unsigned int type (void) const { return definition->type (); } |
|
310 |
|
311 bool is_defined (void) const { return definition->is_defined (); } |
|
312 |
|
313 bool is_read_only (void) const { return definition->is_read_only (); } |
8
|
314 |
3011
|
315 bool is_eternal (void) const { return definition->is_eternal (); } |
|
316 |
|
317 void protect (void) { definition->protect (); } |
195
|
318 |
3011
|
319 void unprotect (void) { definition->unprotect (); } |
|
320 |
|
321 void make_eternal (void) { definition->make_eternal (); } |
2893
|
322 |
3011
|
323 void set_change_function (change_function f) { chg_fcn = f; } |
2893
|
324 |
3011
|
325 void define (const octave_value& v, unsigned int sym_type = USER_VARIABLE); |
|
326 |
|
327 void define_builtin_var (const octave_value& v); |
195
|
328 |
3258
|
329 bool define_builtin_const (const octave_value& v); |
3011
|
330 |
|
331 bool define (octave_function *f, unsigned int sym_type); |
2893
|
332 |
3523
|
333 void document (const std::string& h) { definition->document (h); } |
195
|
334 |
3011
|
335 void clear (void); |
195
|
336 |
4009
|
337 void alias (symbol_record *s); |
8
|
338 |
|
339 void mark_as_formal_parameter (void); |
3011
|
340 bool is_formal_parameter (void) const { return formal_param; } |
8
|
341 |
195
|
342 void mark_as_linked_to_global (void); |
3011
|
343 bool is_linked_to_global (void) const { return linked_to_global; } |
8
|
344 |
2846
|
345 void mark_as_static (void); |
3011
|
346 bool is_static (void) const { return tagged_static; } |
4319
|
347 void unmark_static (void) { tagged_static = false; } |
2846
|
348 |
4913
|
349 bool is_matrix_type (void) const |
|
350 { return definition->is_matrix_type (); } |
|
351 |
|
352 size_t byte_size (void) const |
|
353 { return definition->byte_size (); }; |
|
354 |
|
355 int numel (void) const |
|
356 { return definition->numel (); }; |
|
357 |
|
358 dim_vector dims (void) const { return definition->dims (); } |
|
359 |
|
360 int dimensions_string_req_first_space (int print_dims) const; |
|
361 |
|
362 int dimensions_string_req_total_space (int print_dims) const; |
|
363 |
|
364 std::string make_dimensions_string (int print_dims) const; |
|
365 |
3013
|
366 int rows (void) const { return definition->rows (); } |
|
367 int columns (void) const { return definition->columns (); } |
|
368 |
3523
|
369 std::string type_name (void) const { return definition->type_name (); } |
3013
|
370 |
3523
|
371 std::string type_as_string (void) const |
3355
|
372 { return definition->type_as_string (); } |
|
373 |
3523
|
374 void type (std::ostream& os, bool pr_type_info, bool quiet, bool pr_orig_txt) |
3356
|
375 { definition->type (os, name (), pr_type_info, quiet, pr_orig_txt); } |
|
376 |
3523
|
377 std::string which (void) { return definition->which (name ()); } |
3355
|
378 |
3523
|
379 void which (std::ostream& os) { definition->which (os, name ()); } |
3355
|
380 |
2975
|
381 octave_value& variable_value (void); |
2979
|
382 octave_lvalue variable_reference (void); |
2390
|
383 |
3011
|
384 symbol_record *next (void) const { return next_elem; } |
8
|
385 |
3011
|
386 void chain (symbol_record *s) { next_elem = s; } |
195
|
387 |
220
|
388 void push_context (void); |
3011
|
389 |
220
|
390 void pop_context (void); |
|
391 |
4913
|
392 void print_symbol_info_line (std::ostream& os, |
|
393 std::list<whos_parameter>& params) const; |
3013
|
394 |
3933
|
395 void print_info (std::ostream& os, |
|
396 const std::string& prefix = std::string ()) const; |
3239
|
397 |
8
|
398 private: |
|
399 |
2893
|
400 unsigned int formal_param : 1; |
|
401 unsigned int linked_to_global : 1; |
|
402 unsigned int tagged_static : 1; |
4234
|
403 unsigned int can_hide_function : 1; |
195
|
404 |
3523
|
405 std::string nm; |
3011
|
406 change_function chg_fcn; |
195
|
407 symbol_def *definition; |
8
|
408 symbol_record *next_elem; |
395
|
409 |
3011
|
410 // This should maybe be one stack with a structure containing all the |
|
411 // items we need to save for recursive calls... |
4214
|
412 std::stack <symbol_def *> context; |
|
413 std::stack <unsigned int> global_link_context; |
8
|
414 |
3011
|
415 bool read_only_error (const char *action); |
195
|
416 |
3258
|
417 void link_to_builtin_variable (void); |
|
418 |
3011
|
419 // No copying! |
|
420 |
|
421 symbol_record (const symbol_record& s); |
195
|
422 |
8
|
423 symbol_record& operator = (const symbol_record& s); |
|
424 }; |
|
425 |
767
|
426 // A symbol table. |
200
|
427 |
|
428 #define SYMTAB_LOCAL_SCOPE 1 |
|
429 #define SYMTAB_GLOBAL_SCOPE 2 |
|
430 |
|
431 #define SYMTAB_ALL_SCOPES (SYMTAB_LOCAL_SCOPE | SYMTAB_GLOBAL_SCOPE) |
|
432 |
3011
|
433 #define SYMTAB_ALL_TYPES (symbol_record::USER_FUNCTION \ |
|
434 | symbol_record::USER_VARIABLE \ |
3325
|
435 | symbol_record::DLD_FUNCTION \ |
3011
|
436 | symbol_record::BUILTIN_FUNCTION \ |
4208
|
437 | symbol_record::COMMAND \ |
3011
|
438 | symbol_record::MAPPER_FUNCTION \ |
3258
|
439 | symbol_record::BUILTIN_VARIABLE \ |
|
440 | symbol_record::BUILTIN_CONSTANT) |
200
|
441 |
3011
|
442 #define SYMTAB_VARIABLES (symbol_record::USER_VARIABLE \ |
|
443 | symbol_record::BUILTIN_VARIABLE) |
1412
|
444 |
8
|
445 class |
|
446 symbol_table |
|
447 { |
|
448 public: |
|
449 |
4238
|
450 symbol_table (unsigned int tab_size = 128, |
|
451 const std::string& nm = std::string ()) |
|
452 : table_size (tab_size), table (new symbol_record [table_size]), |
|
453 table_name (nm) |
4009
|
454 { |
|
455 assert ((tab_size % 2) == 0); |
4238
|
456 |
|
457 if (table_name.empty ()) |
|
458 { |
|
459 OSSTREAM buf; |
|
460 buf << symtab_count++ << OSSTREAM_ENDS; |
|
461 table_name = OSSTREAM_STR (buf); |
|
462 OSSTREAM_FREEZE (buf); |
|
463 } |
4009
|
464 } |
3011
|
465 |
|
466 ~symbol_table (void) |
4009
|
467 { |
|
468 clear (); |
|
469 delete [] table; |
|
470 } |
8
|
471 |
3523
|
472 symbol_record *lookup (const std::string& nm, bool insert = false, |
2856
|
473 bool warn = false); |
8
|
474 |
3523
|
475 void rename (const std::string& old_name, const std::string& new_name); |
572
|
476 |
4009
|
477 void clear (void); |
|
478 |
|
479 void clear_variables (void); |
|
480 void clear_functions (void); |
|
481 void clear_globals (void); |
|
482 |
|
483 bool clear (const std::string& nm); |
|
484 |
|
485 bool clear_variable (const std::string& nm); |
|
486 bool clear_function (const std::string& nm); |
|
487 bool clear_global (const std::string& nm); |
|
488 |
|
489 bool clear_variable_pattern (const std::string& pat); |
|
490 bool clear_function_pattern (const std::string& pat); |
|
491 bool clear_global_pattern (const std::string& pat); |
8
|
492 |
164
|
493 int size (void) const; |
8
|
494 |
3013
|
495 Array<symbol_record *> |
4913
|
496 subsymbol_list (const string_vector& pats = string_vector (), |
|
497 unsigned int type = SYMTAB_ALL_TYPES, |
|
498 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
|
499 |
|
500 Array<symbol_record *> |
3355
|
501 symbol_list (const string_vector& pats = string_vector (), |
3013
|
502 unsigned int type = SYMTAB_ALL_TYPES, |
|
503 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
|
504 |
195
|
505 |
1755
|
506 string_vector |
3355
|
507 name_list (const string_vector& pats = string_vector (), |
3013
|
508 bool sort = false, unsigned int type = SYMTAB_ALL_TYPES, |
|
509 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
605
|
510 |
4009
|
511 string_vector |
|
512 user_function_name_list (void) const |
|
513 { |
|
514 return name_list |
|
515 (string_vector (), false, |
|
516 symbol_record::USER_FUNCTION|symbol_record::DLD_FUNCTION, |
|
517 SYMTAB_ALL_SCOPES); |
|
518 } |
|
519 |
|
520 string_vector |
|
521 global_variable_name_list (void) const |
|
522 { |
|
523 return name_list |
|
524 (string_vector (), false, SYMTAB_VARIABLES, SYMTAB_GLOBAL_SCOPE); |
|
525 } |
|
526 |
|
527 string_vector |
|
528 variable_name_list (void) const |
|
529 { |
|
530 return name_list |
|
531 (string_vector (), false, SYMTAB_VARIABLES, SYMTAB_LOCAL_SCOPE); |
|
532 } |
3011
|
533 |
|
534 int maybe_list (const char *header, const string_vector& argv, |
3523
|
535 std::ostream& os, bool show_verbose, |
3011
|
536 unsigned type, unsigned scope); |
|
537 |
3523
|
538 Array<symbol_record *> glob (const std::string& pat = std::string ("*"), |
3355
|
539 unsigned int type = SYMTAB_ALL_TYPES, |
|
540 unsigned int scope = SYMTAB_ALL_SCOPES) const; |
8
|
541 |
220
|
542 void push_context (void); |
3011
|
543 |
220
|
544 void pop_context (void); |
|
545 |
3941
|
546 void print_info (std::ostream& os) const; |
3011
|
547 |
8
|
548 private: |
|
549 |
3011
|
550 unsigned int table_size; |
|
551 |
|
552 symbol_record *table; |
|
553 |
4238
|
554 std::string table_name; |
|
555 |
|
556 static unsigned long int symtab_count; |
|
557 |
4913
|
558 void |
|
559 print_descriptor (std::ostream& os, |
|
560 std::list<whos_parameter> params) const; |
|
561 |
|
562 std::list<whos_parameter> |
|
563 parse_whos_line_format (Array<symbol_record *>& symbols) const; |
|
564 |
3523
|
565 unsigned int hash (const std::string& s); |
8
|
566 |
3011
|
567 // No copying! |
8
|
568 |
3011
|
569 symbol_table (const symbol_table&); |
1962
|
570 |
3011
|
571 symbol_table& operator = (const symbol_table&); |
|
572 }; |
2790
|
573 |
4913
|
574 // Defines layout for the whos/who -long command. |
|
575 extern std::string Vwhos_line_format; |
|
576 |
8
|
577 #endif |
|
578 |
|
579 /* |
|
580 ;;; Local Variables: *** |
|
581 ;;; mode: C++ *** |
|
582 ;;; End: *** |
|
583 */ |