]>
Commit | Line | Data |
---|---|---|
c90f71dd RD |
1 | /******************************************************************************* |
2 | * Simplified Wrapper and Interface Generator (SWIG) | |
3 | * | |
4 | * Author : David Beazley | |
5 | * | |
6 | * Department of Computer Science | |
7 | * University of Chicago | |
8 | * 1100 E 58th Street | |
9 | * Chicago, IL 60637 | |
10 | * beazley@cs.uchicago.edu | |
11 | * | |
12 | * Please read the file LICENSE for the copyright and terms by which SWIG | |
13 | * can be used and distributed. | |
14 | *******************************************************************************/ | |
15 | ||
16 | #include "internal.h" | |
17 | ||
18 | /******************************************************************************* | |
19 | * $Header$ | |
20 | * | |
21 | * File : symbol.cxx | |
22 | * | |
23 | * Symbol table management. | |
24 | * | |
25 | *******************************************************************************/ | |
26 | ||
27 | // ----------------------------------------------------------------------------- | |
28 | // Symbol object | |
29 | // ----------------------------------------------------------------------------- | |
30 | ||
31 | struct Symbol { | |
32 | ~Symbol() { | |
33 | if (name) delete name; | |
34 | if (type) delete type; | |
35 | if (value) delete value; | |
36 | } | |
37 | char *name; | |
38 | DataType *type; // Optional datatype | |
39 | char *value; // Optional value (for constant expressions) | |
40 | }; | |
41 | ||
42 | static Hash SymHash; // SWIG Symbol table | |
43 | ||
44 | // ----------------------------------------------------------------------------- | |
45 | // int add_symbol(char *name, DataType *type, char *value) | |
46 | // | |
47 | // Adds a symbol to the symbol table. Returns -1 if symbol is already in the | |
48 | // table. | |
49 | // | |
50 | // Inputs : | |
51 | // name = Symbol name | |
52 | // type = Datatype (for constants). Optional. | |
53 | // value = Value string. Optional. | |
54 | // | |
55 | // Output : 0 on success, -1 if symbol already exists. | |
56 | // | |
57 | // Side Effects : None | |
58 | // ----------------------------------------------------------------------------- | |
59 | ||
60 | int add_symbol(char *name, DataType *type, char *value) { | |
61 | ||
62 | Symbol *s; | |
63 | int ret; | |
64 | ||
65 | s = new Symbol; | |
66 | s->name = copy_string(name); | |
67 | if (type) | |
68 | s->type = new DataType(type); | |
69 | else s->type = (DataType *) 0; | |
70 | if (value) | |
71 | s->value = copy_string(value); | |
72 | else s->value = (char *) 0; | |
73 | ||
74 | // Add this to the symbol table | |
75 | ||
76 | ret = SymHash.add(s->name, s); | |
77 | if (ret == -1) { | |
78 | delete s; | |
79 | } | |
80 | return ret; | |
81 | } | |
82 | ||
83 | // ----------------------------------------------------------------------------- | |
84 | // int lookup_symbol(char *name) | |
85 | // | |
86 | // Checks to see if a symbol is in the symbol table. | |
87 | // | |
88 | // Inputs : name = Symbol name | |
89 | // | |
90 | // Output : 0 if not found, 1 if found. | |
91 | // | |
92 | // Side Effects : None | |
93 | // ----------------------------------------------------------------------------- | |
94 | ||
95 | int lookup_symbol(char *name) { | |
96 | Symbol *s; | |
97 | ||
98 | s = (Symbol *) SymHash.lookup(name); | |
99 | if (s) return 1; | |
100 | else return 0; | |
101 | } | |
102 | ||
103 | // ----------------------------------------------------------------------------- | |
104 | // DataType *lookup_symtype(char *name) | |
105 | // | |
106 | // Returns the datatype of a symbol or NULL if not found. | |
107 | // | |
108 | // Inputs : name = Symbol name | |
109 | // | |
110 | // Output : Datatype of symbol, NULL if not found. | |
111 | // | |
112 | // Side Effects : None | |
113 | // ----------------------------------------------------------------------------- | |
114 | ||
115 | DataType *lookup_symtype(char *name) { | |
116 | ||
117 | Symbol *s; | |
118 | ||
119 | s = (Symbol *) SymHash.lookup(name); | |
120 | if (s) return s->type; | |
121 | else return (DataType *) 0; | |
122 | } | |
123 | ||
124 | // ----------------------------------------------------------------------------- | |
125 | // char *lookup_symvalue(char *name) | |
126 | // | |
127 | // Returns the value associate with a symbol. | |
128 | // | |
129 | // Inputs : name = Symbol name | |
130 | // | |
131 | // Output : Symbol value (or NULL if not present). | |
132 | // | |
133 | // Side Effects : None | |
134 | // ----------------------------------------------------------------------------- | |
135 | ||
136 | char *lookup_symvalue(char *name) { | |
137 | ||
138 | Symbol *s; | |
139 | ||
140 | s = (Symbol *) SymHash.lookup(name); | |
141 | if (s) return s->value; | |
142 | else return (char *) 0; | |
143 | } | |
144 | ||
145 | // ----------------------------------------------------------------------------- | |
146 | // int update_symbol(char *name, DataType *type, char *value) | |
147 | // | |
148 | // Updates a symbol (or create it) in the hash table. | |
149 | // | |
150 | // Inputs : | |
151 | // name = Name of symbol | |
152 | // type = Datatype of symbol (optional) | |
153 | // value = Symbol value (optional) | |
154 | // | |
155 | // Output : 0 | |
156 | // | |
157 | // Side Effects : None | |
158 | // ----------------------------------------------------------------------------- | |
159 | ||
160 | int update_symbol(char *name, DataType *type, char *value) { | |
161 | ||
162 | Symbol *s; | |
163 | ||
164 | s = (Symbol *) SymHash.lookup(name); | |
165 | if (s) { | |
166 | if (s->type) delete s->type; | |
167 | if (s->value) delete s->value; | |
168 | if (type) | |
169 | s->type = new DataType(type); | |
170 | else | |
171 | s->type = (DataType *) 0; | |
172 | if (value) | |
173 | s->value = copy_string(value); | |
174 | else | |
175 | s->value = (char *) 0; | |
176 | return 0; | |
177 | } else { | |
178 | return add_symbol(name, type, value); | |
179 | } | |
180 | } | |
181 | ||
182 | // ----------------------------------------------------------------------------- | |
183 | // void remove_symbol(char *name) | |
184 | // | |
185 | // Removes a symbol from the symbol table. | |
186 | // | |
187 | // Inputs : name = Symbol name. | |
188 | // | |
189 | // Output : None | |
190 | // | |
191 | // Side Effects : None | |
192 | // ----------------------------------------------------------------------------- | |
193 | ||
194 | void remove_symbol(char *name) { | |
195 | SymHash.remove(name); | |
196 | } |