import java.util.*; /** * Each Symbol object represents one entry in a SymbolTable. * * @author Hyung-Joon Kim */ public class Symbol { //----------------------------------------------------------------- private int type; private String label; private HashMap attributes; public static final int UNKNOWN_TYPE= 0; public static final int KEYWORD = 1; public static final int CLASSNAME = 2; public static final int VARIABLE = 3; public static final int METHOD = 4; //----------------------------------------------------------------- /** * Construct a new Symbol with the given name and type. * @param name the label by which the program being compiled * refers to this Symbol * @param t the symbol type, an int value. Can be one of the * type values defined by the Symbol class or some other class * like Token. */ public Symbol(String name,int t) { label = name; type = t; attributes = new HashMap(); } /** * Get the label attribute of this object. * @return the Symbol label */ public String getLabel() { return label; } /** * Set the label attribute of this object. * @param name the Symbol label */ public void setLabel(String name) { label = name; } /** * Get the type of this Symbol. * @return the Symbol type */ public int getType() { return type; } /** * Set the type of this Symbol. * @param t the Symbol type */ public void setType(int t) { type = t; } /** * Put an attribute in the HashMap of a Symbol. * @param key the name of the attribute * @param att the Object that holds the attribute's value */ public void putAttribute(String key,Object att) { attributes.put(key,att); } /** * Get an attribute from a Symbol. The result is whatever kind of * object was stored for this attribute, so make sure your putters * and getters agree. * @param key the name of the attribute to get * @return the Object that holds the attribute's value */ public Object getAttribute(String key) { return attributes.get(key); } /** * Provide the String that describes this Symbol. */ public String toString() { StringBuffer sb = new StringBuffer(label+", type: "+Integer.toString(type)); Set s = attributes.entrySet(); Iterator iter = s.iterator(); while (iter.hasNext()) { Map.Entry e = (Map.Entry)iter.next(); sb.append(", "+e.getKey()+" "+e.getValue()); } return sb.toString(); } }