-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.java
More file actions
349 lines (308 loc) · 15.6 KB
/
SymbolTable.java
File metadata and controls
349 lines (308 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*******************************************************************
* SymbolTable Class *
* *
* PROGRAMMER: Emily Culp *
* COURSE: CS340 - Programming Language Design *
* DATE: 12/10/2024 *
* REQUIREMENT: Final - Compiler *
* *
* DESCRIPTION: *
* The SymbolTable class manages variables in the interpreter, *
* including their names, values, and unique IDs. It allows adding, *
* retrieving, updating, and checking for variables. Each variable *
* receives a unique ID starting from 600. The class also provides *
* a method to print the current state of the table for debugging. *
* *
* COPYRIGHT: This code is copyright (C) 2024 Emily Culp and Dean *
* Zeller. *
* *
* CREDITS: This code was written with the help of ChatGPT. *
*******************************************************************/
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class SymbolTable {
final Map<Integer, Entry> table; // Map to store variable names and their details
private int nextId; // To keep track of the next available ID
private Map<String, String> conditionRegisters;
private Map<String, Token> tokens;
/**********************************************************
* CONSTRUCTOR: SymbolTable() *
* DESCRIPTION: *
* Initializes the symbol table with an empty map and sets *
* the next ID to 600. *
**********************************************************/
public static class Entry{
private final int id;
String name;
String type;
Object value;
String scope;
String register;
public Entry(int id, String name, String type, Object value, String scope, String register){
this.id = id;
this.name = name;
this.type = type;
this.value = value;
this.scope = scope;
this.register = register;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getType(){
return type;
}
public void setType(String value){
this.type = value;
}
public Object getValue(){
return value;
}
public void setValue(Object value){
this.value = value;
}
public String getScope(){
return scope;
}
public String getRegister(){
return register;
}
public void setRegister(String reg){
this.register = reg;
}
@Override
public String toString(){
return String.format("Name: %s, Type: %s, Value: %s, Scope: %s", name, type, value, scope);
}
}
public SymbolTable() {
this.table = new HashMap<>();
this.nextId = 600; // Start IDs from 600
conditionRegisters = new HashMap<>();
tokens = new HashMap<>();
}
/**********************************************************
* METHOD: addEntry(String name, String type, Object value, *
* String scope, String register) *
* DESCRIPTION: *
* Adds a new entry to the symbol table. Each variable is *
* assigned a unique ID starting from 600. *
* PARAMETERS: *
* String name - the name of the variable *
* String type - the type of the variable (e.g., integer) *
* Object value - the value of the variable *
* String scope - the scope of the variable (e.g., global)*
* String register - the register assigned to the variable*
**********************************************************/
public void addEntry(String name, String type, Object value, String scope, String register){
table.put(nextId, new Entry(nextId, name, type, value, scope, register));
nextId++;
}
/**********************************************************
* METHOD: getRegister(String variableName) *
* DESCRIPTION: *
* Retrieves the register associated with a variable name. *
* PARAMETERS: *
* String variableName - the name of the variable *
* RETURN VALUE: *
* String - the register associated with the variable *
* or null if the variable is not found *
**********************************************************/
public String getRegister(String variableName){
// Iterate through the map and find the entry with the matching variable name
for(Map.Entry<Integer, Entry> entry : table.entrySet()){
if(entry.getValue().getName().equals(variableName)){
return entry.getValue().getRegister(); // Return the register of the matching variable
}
}
return null; // Return null if the variable is not found
}
/**********************************************************
* METHOD: addRegisterToVariable(String variableName, String register) *
* DESCRIPTION: *
* Adds a register to an existing variable. *
* PARAMETERS: *
* String variableName - the name of the variable *
* String register - the register to assign to the variable *
**********************************************************/
public void addRegisterToVariable(String variableName, String register) {
// Iterate through the map to find the entry with the matching variable name
for (Map.Entry<Integer, Entry> entry : table.entrySet()) {
if (entry.getValue().getName().equals(variableName)) {
// Update the register of the variable
entry.getValue().setRegister(register);
System.out.println("Register " + register + " has been assigned to variable " + variableName);
return;
}
}
// If the variable is not found, throw an exception
throw new IllegalArgumentException("Variable '" + variableName + "' not found in the Symbol Table.");
}
/**********************************************************
* METHOD: addRegisterToVariable(String variableName) *
* DESCRIPTION: *
* Searches for a variable by its name and returns the *
* associated register. If the variable is not found, a
* debug manage is printed, and null is returned.
* PARAMETERS: *
* String variableName - the name of the variable *
* RETURN VALUE: *
* String - the register associated with the variable, or
* null if the variable is not found
**********************************************************/
public String getRegisterForVariable(String variableName) {
System.out.println("Looking up register for variable name: " + variableName); // Debug print
for (Map.Entry<Integer, Entry> entry : table.entrySet()) {
System.out.println("Checking variable: " + entry.getValue().getName()); // Debug print
if (entry.getValue().getName().equals(variableName)) {
return entry.getValue().getRegister();
}
}
System.out.println("No register found for variable: " + variableName); // Debug print
return null; // Return null if the variable is not found
}
/**********************************************************
* METHOD: getValueById(int id) *
* DESCRIPTION: *
* Retrieves the value associated with a variable by ID. *
* PARAMETERS: *
* int id - the ID of the variable *
* RETURN VALUE: *
* Object - the value of the variable, or null if not found *
**********************************************************/
// Retrieves the value associated with a variable name
public Object getValueById(int id){
Entry entry = table.get(id);
return (entry != null) ? entry.value : null;
}
/**********************************************************
* METHOD: getIdByName(String name) *
* DESCRIPTION: *
* Retrieves the ID associated with a variable by its name.*
* PARAMETERS: *
* String name - the name of the variable *
* RETURN VALUE: *
* Integer - the ID of the variable, or null if not found *
**********************************************************/
public Integer getIdByName(String name){
for(Map.Entry<Integer, Entry> entry : table.entrySet()){
if(entry.getValue().getName().equals(name)){
return entry.getValue().getId();
}
}
return null; //not found
}
/**********************************************************
* METHOD: updateValue(String name, int value) *
* DESCRIPTION: *
* Updates the value of a variable. If the variable is not *
* found, an exception is thrown to indicate that it has *
* not been declared. *
* PARAMETERS: *
* String name - the name of the variable *
* int value - the new value to assign *
**********************************************************/
// Updates the value of a variable
public void updateValue(String name, Object newValue) {
boolean variableUpdated = false;
for(Map.Entry<Integer, Entry> entry : table.entrySet()){
System.out.println("Checking Variable: " +entry.getValue().name + ", Current Value: " +entry.getValue().value);
if(entry.getValue().name.trim().equalsIgnoreCase(name.trim())){
System.out.println("Updating variable '" +name+ "' to new value: " +newValue);
entry.getValue().value = newValue;
variableUpdated = true;
break;
}
}
if(!variableUpdated){
throw new IllegalArgumentException("Variable '" +name+ "' not found in the SymbolTable. Ensure it's declared");
}
}
/**********************************************************
* METHOD: containsVariable(String name) *
* DESCRIPTION: *
* Checks if a variable exists in the symbol table. *
* PARAMETERS: *
* String name - the name of the variable to check *
* RETURN VALUE: *
* boolean - true if the variable exists, false otherwise *
**********************************************************/
// Checks if a variable exists in the symbol table
public boolean containsVariable(String name) {
for(Entry entry : table.values()){
if(entry.getName().equals(name)){
return true;
}
}
return false;
}
/**********************************************************
* METHOD: getTypeByName(String variableName) *
* DESCRIPTION: *
* Searches for a variable by its name and returns its type. *
* If the variable is not found, null is returned
* PARAMETERS: *
* String variableName - the name of the variable *
* RETURN VALUE: *
* String - the type of the variable, or null if not found
**********************************************************/
public String getTypeByName(String variableName){
for(Map.Entry<Integer, Entry> entry : table.entrySet()){
if(entry.getValue().getName().equals(variableName)){
return entry.getValue().getType();
}
}
return null;
}
/**********************************************************
* METHOD: get(String name) *
* DESCRIPTION: *
* Retrieves the value of a variable by its name. If the *
* variable is not found, null is returned
* PARAMETERS: *
* String name - the name of the variable *
* RETURN VALUE:
* Object - the value of the variable, or null if not found *
**********************************************************/
public Object get(String name) {
for (Map.Entry<Integer, Entry> entry : table.entrySet()) {
if (entry.getValue().name.equals(name)) {
return entry.getValue().value;
}
}
return null; // Return null if the variable name is not found
}
/**********************************************************
* METHOD: getAllEntries() *
* DESCRIPTION: *
* Returns a map containing all entries in the symbol table.*
* The map associates variable IDs with their corresponding
* entries.
* RETURN VALUE:
* Map<Integer, Entry> - a map of all entries in the symbol table*
**********************************************************/
public Map<Integer, Entry> getAllEntries(){
return table;
}
/**********************************************************
* METHOD: display() *
* DESCRIPTION: *
* Prints the contents of the symbol table to the console, *
* displaying each variable's name, value, and ID. *
**********************************************************/
// Method to display all entries in the symbol table
public void display() {
System.out.println();
System.out.println("Symbol Table:");
System.out.println("ID | Name | Type | Value | Scope | Register");
System.out.println("---------------------------------------------------------------------");
for (Map.Entry<Integer, Entry> entry : table.entrySet()) {
System.out.printf("%-6d | %-10s | %-10s | %-10s | %-6s | %-10s\n",
entry.getKey(), entry.getValue().name, entry.getValue().type, entry.getValue().value, entry.getValue().scope, entry.getValue().getRegister());
}
}
}