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