summaryrefslogtreecommitdiff
path: root/Homework/cs5300/p4-formatter/submit/SymbolTable.java
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
commit6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch)
treeed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs5300/p4-formatter/submit/SymbolTable.java
downloadmisc-undergrad-main.tar.gz
misc-undergrad-main.zip
Diffstat (limited to 'Homework/cs5300/p4-formatter/submit/SymbolTable.java')
-rw-r--r--Homework/cs5300/p4-formatter/submit/SymbolTable.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/Homework/cs5300/p4-formatter/submit/SymbolTable.java b/Homework/cs5300/p4-formatter/submit/SymbolTable.java
new file mode 100644
index 0000000..bfa531e
--- /dev/null
+++ b/Homework/cs5300/p4-formatter/submit/SymbolTable.java
@@ -0,0 +1,55 @@
+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; }
+}