Files
MY_COMPILER/midend/symbol/SymbolManager.java
2025-12-12 20:14:00 +08:00

137 lines
3.8 KiB
Java
Executable File

package midend.symbol;
import java.util.ArrayList;
import error.Errors;
public class SymbolManager {
public static ArrayList<SymbolTable> symbolTableList;
public static ArrayList<Integer> sequence;
public static SymbolTable currentTable;
public static int currentSequence;
public static void init() {
symbolTableList = new ArrayList<>();
sequence = new ArrayList<>();
currentTable = null;
currentSequence = -1;
}
public static void addSymbolTable() {
SymbolTable symbolTable = new SymbolTable(symbolTableList.size() + 1);
symbolTableList.add(symbolTable);
sequence.add(symbolTable.getTableId());
currentSequence++;
if (currentTable != null) {
symbolTable.setParentTable(currentTable);
}
currentTable = symbolTable;
}
public static ArrayList<SymbolTable> getSymbolTableList() {
return symbolTableList;
}
public static void releaseSymbolTable(int tableId) {
symbolTableList.get(tableId - 1).release();
currentTable = null;
for (int i = symbolTableList.size() - 1; i >= 0; i--) {
if (!symbolTableList.get(i).isReleased()) {
currentTable = symbolTableList.get(i);
currentSequence++;
sequence.add(currentTable.getTableId());
break;
}
}
}
public static void releaseSymbolTable() {
releaseSymbolTable(currentTable.getTableId());
}
public static int getSymbolTableSize() {
return symbolTableList.size();
}
public static void addSymbol(Symbol symbol, Errors errors) {
currentTable.addSymbol(symbol, errors);
}
public static String getSymbolTableInfo() {
String info = "";
for (SymbolTable symbolTable : symbolTableList) {
info += symbolTable.toString();
}
return info;
}
public static Symbol getSymbol(String name) {
SymbolTable stable = currentTable;
Symbol symbol = null;
while (stable != null) {
symbol = stable.getSymbol(name);
if (symbol != null) {
break;
}
stable = stable.getParentTable();
}
return symbol;
}
public static Symbol getSymbol(String name, boolean defined) {
SymbolTable stable = currentTable;
Symbol symbol = null;
while (stable != null) {
symbol = stable.getSymbol(name);
if (symbol != null && symbol.getIrValue() != null) {
break;
}
stable = stable.getParentTable();
}
return symbol;
}
public static void nextTable() {
currentSequence++;
if (currentSequence >= sequence.size()) {
return;
}
currentTable = symbolTableList.get(sequence.get(currentSequence) - 1);
}
public static void lastTable() {
currentSequence--;
if (currentSequence < 0) {
currentSequence = 0;
currentTable = symbolTableList.get(0);
return;
}
currentTable = symbolTableList.get(sequence.get(currentSequence) - 1);
}
public static void reset() {
currentSequence = -1;
currentTable = null;
}
public static boolean IsGlobal() {
return currentTable.getTableId() == 1;
}
public static ArrayList<Integer> getSequence() {
return sequence;
}
public static int getCurrentSequence() {
return currentSequence;
}
public static int getCurrentTableId() {
return sequence.get(currentSequence);
}
public static SymbolTable getCurrentTable() {
return currentTable;
}
}