001: package gnu.expr;
002:
003: import gnu.mapping.*;
004:
005: import java.io.PrintWriter;
006:
007: /** Utility class containing various routines to manipulate Scheme symbols.
008: * Note Scheme symbols are represented using java.lang.String objects,
009: * and there are no Symbol objects. */
010:
011: public class Symbol {
012: /** There are no instances of this class. */
013: private Symbol() {
014: }
015:
016: public static final String makeUninterned(String str) {
017: str = new String(str);
018: /* DEBUGGING:
019: gensyms.addElement(str);
020: */
021: return str;
022: }
023:
024: private static int gensym_counter;
025:
026: /* DEBUGGING:
027: static java.util.Vector gensyms = new java.util.Vector();
028:
029: public static String show (String str)
030: {
031: StringBuffer buf = new StringBuffer(str);
032: if (str.intern() == str)
033: buf.append("<I>");
034: else
035: {
036: for (int i = gensyms.size(); ; )
037: {
038: if (--i < 0)
039: {
040: buf.append("<?>");
041: break;
042: }
043: else if (gensyms.elementAt(i) == str)
044: {
045: buf.append('<');
046: buf.append(i);
047: buf.append('>');
048: break;
049: }
050: }
051: }
052: return buf.toString();
053: }
054: */
055:
056: /**
057: * Generate a new un-interned Symbol with a unique name.
058: * @return the new Symbol
059: */
060: public static final String generate() {
061: String str = new String("GS."
062: + Integer.toString(++gensym_counter));
063: /* DEBUGGING:
064: gensyms.addElement(str);
065: */
066: return str;
067: }
068:
069: /**
070: * Generate a new (interned) symbol with a unique name.
071: * @return the new symbol
072: */
073: public static final String gentemp() {
074: return Symbol.make("GS." + Integer.toString(++gensym_counter));
075: }
076:
077: /**
078: * Create or find a Symbol with a given name.
079: * @param name the print-name of the desired Symbol
080: * @return a Symbol with the given name, newly created iff none such exist
081: */
082: static public String make(String name) {
083: return name.intern();
084: }
085:
086: static public final String intern(String name) {
087: return make(name);
088: }
089:
090: public static void print(String name, java.io.PrintWriter ps) {
091: boolean readable = (ps instanceof OutPort)
092: && ((OutPort) ps).printReadable;
093: if (readable) {
094: int len = name.length();
095: for (int i = 0; i < len; i++) {
096: char ch = name.charAt(i);
097: if (!(Character.isLowerCase(ch)
098: || ch == '!'
099: || ch == '$'
100: || ch == '%'
101: || ch == '&'
102: || ch == '*'
103: || ch == '/'
104: || ch == ':'
105: || ch == '<'
106: || ch == '='
107: || ch == '>'
108: || ch == '?'
109: || ch == '~'
110: || ch == '_'
111: || ch == '^'
112: || ((ch == '+' || ch == '-') && (i > 0 || len == 1))
113: || (Character.isDigit(ch) && i > 0) || (ch == '.' && (i == 0 || name
114: .charAt(i - 1) == '.'))))
115: ps.print('\\');
116: ps.print(ch);
117: }
118: } else
119: ps.print(name);
120: }
121:
122: }
|