cminus/submit/SymbolTable.java

64 lines
1.1 KiB
Java
Raw Normal View History

2023-04-10 11:17:11 -04:00
package submit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/*
* Code formatter project
* CS 4481
*/
/**
*
*/
public class SymbolTable {
private final HashMap<String, SymbolInfo> table;
private SymbolTable parent;
private final List<SymbolTable> children;
public SymbolTable() {
table = new HashMap<>();
parent = null;
children = new ArrayList<>();
}
public void addSymbol(String id, SymbolInfo symbol) {
table.put(id, symbol);
}
/**
* Returns null if no symbol with that id is in this symbol table or an
* ancestor table.
*
* @param id
* @return
*/
public SymbolInfo find(String id) {
if (table.containsKey(id)) {
return table.get(id);
}
if (parent != null) {
return parent.find(id);
}
return null;
}
/**
* Returns the new child.
*
* @return
*/
public SymbolTable createChild() {
SymbolTable child = new SymbolTable();
children.add(child);
child.parent = this;
return child;
}
public SymbolTable getParent() {
return parent;
}
}