diff options
author | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-10 09:17:11 -0600 |
---|---|---|
committer | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-10 09:17:11 -0600 |
commit | 5f28f80c4e25a56cd444914c2f0b3da5e7fdb088 (patch) | |
tree | 600ad8b1ec5aad5155baf8c0352281054a8e6366 /submit/SymbolTable.java | |
download | cminus-5f28f80c4e25a56cd444914c2f0b3da5e7fdb088.tar.gz cminus-5f28f80c4e25a56cd444914c2f0b3da5e7fdb088.zip |
Initial commit - building
Diffstat (limited to 'submit/SymbolTable.java')
-rw-r--r-- | submit/SymbolTable.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/submit/SymbolTable.java b/submit/SymbolTable.java new file mode 100644 index 0000000..7ca6b27 --- /dev/null +++ b/submit/SymbolTable.java @@ -0,0 +1,63 @@ +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; + } + +} |