69 lines
1.6 KiB
Java
Executable File
69 lines
1.6 KiB
Java
Executable File
package midend.symbol;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
|
|
import error.Error;
|
|
import error.ErrorType;
|
|
import error.Errors;
|
|
|
|
public class SymbolTable {
|
|
private ArrayList<Symbol> symbolList;
|
|
private HashMap<String, Symbol> symbolMap;
|
|
private int tableId;
|
|
private boolean isReleased;
|
|
private SymbolTable parentTable;
|
|
|
|
public SymbolTable(int tableId) {
|
|
this.tableId = tableId;
|
|
isReleased = false;
|
|
symbolList = new ArrayList<>();
|
|
symbolMap = new HashMap<>();
|
|
parentTable = null;
|
|
}
|
|
|
|
public int getTableId() {
|
|
return tableId;
|
|
}
|
|
|
|
public boolean isReleased() {
|
|
return isReleased;
|
|
}
|
|
|
|
public void release() {
|
|
isReleased = true;
|
|
}
|
|
|
|
public void addSymbol(Symbol symbol, Errors errors) {
|
|
if (symbolMap.containsKey(symbol.getName())) {
|
|
errors.addError(new Error(symbol.getLine(), ErrorType.b));
|
|
return;
|
|
}
|
|
symbolList.add(symbol);
|
|
symbolMap.put(symbol.getName(), symbol);
|
|
}
|
|
|
|
public void setParentTable(SymbolTable parentTable) {
|
|
this.parentTable = parentTable;
|
|
}
|
|
|
|
public SymbolTable getParentTable() {
|
|
return parentTable;
|
|
}
|
|
|
|
public Symbol getSymbol(String name) {
|
|
if (symbolMap.containsKey(name)) {
|
|
return symbolMap.get(name);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String toString() {
|
|
String info = "";
|
|
for (Symbol symbol : symbolList) {
|
|
info += tableId + " " + symbol.toString();
|
|
}
|
|
return info;
|
|
}
|
|
}
|