KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > Symbols


1 package gnu.expr;
2 import gnu.mapping.*;
3 import gnu.lists.Consumer;
4
5 /** Utility class containing various routines to manipulate Scheme symbols.
6   * Note Scheme symbols are represented using java.lang.String objects,
7   * and there are no Symbol objects. */

8
9 public class Symbols
10 {
11   /** There are no instances of this class. */
12   private Symbols ()
13   {
14   }
15
16   private static int gensym_counter;
17
18   static synchronized int generateInt()
19   {
20     return ++gensym_counter;
21   }
22
23   /* DEBUGGING:
24   static java.util.Vector gensyms = new java.util.Vector();
25
26   public static String show (String str)
27   {
28     StringBuffer buf = new StringBuffer(str);
29     if (str.intern() == str)
30       buf.append("<I>");
31     else
32       {
33     for (int i = gensyms.size(); ; )
34       {
35         if (--i < 0)
36           {
37         buf.append("<?>");
38         break;
39           }
40         else if (gensyms.elementAt(i) == str)
41           {
42         buf.append('<');
43         buf.append(i);
44         buf.append('>');
45         break;
46           }
47       }
48       }
49     return buf.toString();
50   }
51   */

52
53   /**
54    * Generate a new (interned) symbol with a unique name.
55    * @return the new symbol
56    */

57   public static final String JavaDoc gentemp ()
58   {
59     return Symbols.make("GS." + Integer.toString(generateInt()));
60   }
61
62   /**
63    * Create or find a Symbol with a given name.
64    * @param name the print-name of the desired Symbol
65    * @return a Symbol with the given name, newly created iff none such exist
66    */

67   static public String JavaDoc make (String JavaDoc name)
68   {
69     return name.intern();
70   }
71
72   static public final String JavaDoc intern (String JavaDoc name)
73   {
74     return make (name);
75   }
76
77   public static void print(String JavaDoc name, Consumer out)
78   {
79     boolean readable = (out instanceof OutPort)
80       && ((OutPort)out).printReadable;
81     if (readable)
82       {
83     int len = name.length ();
84     for (int i = 0; i < len; i++)
85       {
86         char ch = name.charAt (i);
87         if (!(Character.isLowerCase (ch)
88           || ch == '!' || ch == '$' || ch == '%' || ch == '&'
89           || ch == '*' || ch == '/' || ch == ':' || ch == '<'
90           || ch == '=' || ch == '>' || ch == '?' || ch == '~'
91           || ch == '_' || ch == '^'
92           || ((ch == '+' || ch == '-') && (i > 0 || len == 1))
93           || (Character.isDigit (ch) && i > 0)
94           || (ch == '.' && (i == 0 || name.charAt (i - 1) == '.'))))
95           out.write('\\');
96         out.write(ch);
97       }
98       }
99     else
100       out.write(name);
101   }
102
103 }
104
Popular Tags