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