From 765337c53286db39ffc3c64eede602afe3899647 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Thu, 25 Apr 2024 00:17:56 -0600 Subject: Rewrote SymbolTable to add uniqueness to stored tokens --- st/TokenKey.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 st/TokenKey.java (limited to 'st/TokenKey.java') diff --git a/st/TokenKey.java b/st/TokenKey.java new file mode 100644 index 0000000..2319917 --- /dev/null +++ b/st/TokenKey.java @@ -0,0 +1,45 @@ +package st; + +/** + * This class is a data structure used to distinguish tokens in + * a given program. Tokens are considered unique if their "beginLine" + * and name are different. + */ +public class TokenKey { + + private String name; + private int beginLine; + + public TokenKey(String name, int beginLine) { + // classes CANNOT collide, so CALLEES ARE EXPECTED TO USE ZERO! + this.name = name; + this.beginLine = beginLine; + } + + @Override public String toString() { + return String.format("%s (%d)", + this.name, + this.beginLine); + } + + + @Override public boolean equals(Object other) { + boolean ret = false; + TokenKey o; + if (other instanceof TokenKey && + (o = (TokenKey) other).name == this.name && + o.beginLine == this.beginLine) { + ret = true; + } + return ret; + } + + @Override public int hashCode() { + return this.name.hashCode(); + } + + public String getName() { + return this.name; + } + +} -- cgit v1.2.3