/* Author: Jeff Dalton * Updated: Sun Dec 2 03:26:18 2001 by Jeff Dalton */ package ix.util.lisp; import java.io.Serializable; import java.util.*; import ix.util.*; /** * The root class for Lisp symbols.

* * The correct way to create a new symbol is to call * Symbol.intern(String name). * It will return an instance of Symbol or of an appropriate subclass, * depending on the first character of the name. * */ // At least that's how things are for now. It's not yet clear what's best. public class Symbol implements LispObject, Comparable, Serializable { protected static Hashtable obTable = new Hashtable(); public String name; protected Symbol(String name) { this.name = name; Debug.assert(obTable.get(name) == null); obTable.put(name, this); } public static Symbol intern(String s) { Symbol sym = (Symbol)obTable.get(s); if (sym != null) return sym; else if (s.length() == 0) return new Symbol(s); else { switch (s.charAt(0)) { case ':' : return new Keyword(s); case '?' : return new ItemVar(s); default : return new Symbol(s); } } } public int compareTo(Object o) { if (o instanceof Symbol) return name.compareTo(((Symbol)o).name); else throw new ClassCastException ("Cannot compare symbol " + this + " to " + o); } protected Object readResolve() throws java.io.ObjectStreamException { // Deserialization magic to avoid multiple (visible) // instances of Symbols that should be eq. The instances // are created (it seems) but then this method is called ... return intern(this.name); } public String toString() { return name; } }