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