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