package midend.symbol; import java.util.ArrayList; import error.Errors; public class SymbolManager { public static ArrayList symbolTableList; public static ArrayList 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 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 getSequence() { return sequence; } public static int getCurrentSequence() { return currentSequence; } public static int getCurrentTableId() { return sequence.get(currentSequence); } public static SymbolTable getCurrentTable() { return currentTable; } }