8
|
1 // Symbol table classes. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (_symtab_h) |
|
25 #define _symtab_h 1 |
|
26 |
|
27 #ifdef __GNUG__ |
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 #include <stdlib.h> |
|
32 #include <string.h> |
|
33 #include <iostream.h> |
|
34 #include <assert.h> |
|
35 |
|
36 #ifndef SV_FUNCTION_TYPEDEFS |
|
37 #define SV_FUNCTION_TYPEDEFS 1 |
|
38 |
|
39 typedef int (*sv_Function)(void); |
|
40 |
|
41 #endif |
|
42 |
|
43 #define HASH_TABLE_SIZE 1024 /* Must be multiple of 2 */ |
|
44 #define HASH_MASK (HASH_TABLE_SIZE - 1) |
|
45 |
|
46 class tree; |
|
47 class tree_builtin; |
|
48 class tree_constant; |
|
49 class tree_function; |
|
50 |
|
51 class symbol_def; |
|
52 class symbol_record; |
|
53 class symbol_table; |
|
54 |
|
55 /* |
|
56 * Variables or functions. |
|
57 */ |
|
58 class symbol_def |
|
59 { |
|
60 friend class symbol_record; |
|
61 |
|
62 public: |
|
63 |
|
64 enum symbol_type |
|
65 { |
|
66 unknown_type, |
|
67 variable, |
|
68 builtin_function, |
|
69 user_function, |
|
70 }; |
|
71 |
|
72 enum symbol_lifespan |
|
73 { |
|
74 temporary, |
|
75 eternal, |
|
76 }; |
|
77 |
|
78 enum symbol_class |
|
79 { |
|
80 read_write, |
|
81 read_only, |
|
82 }; |
|
83 |
|
84 symbol_def (void); |
|
85 symbol_def (tree_constant *t); |
|
86 symbol_def (tree_builtin *t); |
|
87 symbol_def (tree_function *t); |
|
88 |
|
89 ~symbol_def (void); |
|
90 |
|
91 void define (tree_constant *t); |
|
92 void define (tree_builtin *t); |
|
93 void define (tree_function *t); |
|
94 |
164
|
95 tree *def (void) const; |
|
96 char *help (void) const; |
|
97 void document (const char *h); |
8
|
98 |
|
99 int save (ostream& os, int mark_as_global); |
|
100 |
|
101 private: |
|
102 |
|
103 char *help_string; |
|
104 symbol_lifespan lifespan; |
|
105 symbol_class sym_class; |
|
106 symbol_type type; |
|
107 tree *definition; |
|
108 int count; |
|
109 int preserve; |
|
110 |
|
111 symbol_def (const symbol_def& sd); |
|
112 symbol_def& operator = (const symbol_def& sd); |
|
113 }; |
|
114 |
|
115 /* |
|
116 * Individual records in a symbol table. |
|
117 */ |
|
118 class |
|
119 symbol_record |
|
120 { |
|
121 friend class symbol_table; |
|
122 |
|
123 public: |
|
124 symbol_record (void); |
164
|
125 symbol_record (const char *n); |
|
126 symbol_record (const char *n, symbol_record *nxt); |
8
|
127 |
|
128 ~symbol_record (void); |
|
129 |
164
|
130 char *name (void) const; |
|
131 char *help (void) const; |
|
132 tree *def (void) const; |
8
|
133 |
164
|
134 int is_function (void) const; |
|
135 int is_variable (void) const; |
8
|
136 |
164
|
137 int is_defined (void) const; |
8
|
138 |
|
139 void set_sv_function (sv_Function f); |
|
140 |
|
141 int var_read_only (void); |
|
142 int read_only (void); |
|
143 |
|
144 int define (tree_constant *t); |
|
145 int define (tree_builtin *t); |
|
146 int define (tree_function *t); |
|
147 int define_as_fcn (tree_constant *t); |
|
148 |
164
|
149 void document (const char *h); |
8
|
150 |
|
151 void protect (void); |
|
152 void unprotect (void); |
|
153 void make_eternal (void); |
|
154 |
|
155 int save (ostream& os, int mark_as_global = 0); |
|
156 |
186
|
157 int clear_visible (void); |
8
|
158 void clear_all (void); |
|
159 |
|
160 void mark_as_formal_parameter (void); |
164
|
161 int is_formal_parameter (void) const; |
8
|
162 |
121
|
163 void mark_as_forced_global (void); |
164
|
164 int is_forced_global (void) const; |
121
|
165 |
8
|
166 void alias (symbol_record *s, int force = 0); |
|
167 |
164
|
168 symbol_record *next (void) const; |
8
|
169 |
|
170 private: |
|
171 |
|
172 char *nm; |
|
173 int formal_param; |
121
|
174 int forced_global; |
8
|
175 symbol_def *var; |
|
176 symbol_def *fcn; |
|
177 sv_Function sv_fcn; |
|
178 symbol_record *next_elem; |
|
179 |
186
|
180 symbol_record (const symbol_record& s) { assert (0); } |
8
|
181 symbol_record& operator = (const symbol_record& s); |
|
182 }; |
|
183 |
|
184 /* |
|
185 * A symbol table. |
|
186 */ |
|
187 class |
|
188 symbol_table |
|
189 { |
|
190 public: |
|
191 |
|
192 symbol_table (void); |
|
193 |
164
|
194 symbol_record *lookup (const char *nm, int insert = 0, int warn = 0); |
8
|
195 |
|
196 void clear (void); |
164
|
197 int clear (const char *nm); |
8
|
198 |
|
199 void bind_globals (void); |
|
200 |
|
201 int save (ostream& os, int mark_as_global = 0); |
164
|
202 int save (ostream& os, const char *name, int mark_as_global = 0); |
8
|
203 |
164
|
204 int size (void) const; |
8
|
205 |
164
|
206 char **list (void) const; |
|
207 char **var_list (void) const; |
|
208 char **fcn_list (void) const; |
8
|
209 |
164
|
210 char **list (int& count) const; |
|
211 char **var_list (int& count) const; |
|
212 char **fcn_list (int& count) const; |
8
|
213 |
164
|
214 char **sorted_list (void) const; |
|
215 char **sorted_var_list (void) const; |
|
216 char **sorted_fcn_list (void) const; |
8
|
217 |
164
|
218 char **sorted_list (int& count) const; |
|
219 char **sorted_var_list (int& count) const; |
|
220 char **sorted_fcn_list (int& count) const; |
8
|
221 |
|
222 private: |
|
223 |
|
224 unsigned int hash (const char *s); |
|
225 |
|
226 symbol_record table[HASH_TABLE_SIZE]; |
|
227 }; |
|
228 |
|
229 #endif |
|
230 |
|
231 /* |
|
232 ;;; Local Variables: *** |
|
233 ;;; mode: C++ *** |
|
234 ;;; page-delimiter: "^/\\*" *** |
|
235 ;;; End: *** |
|
236 */ |