01: package jsint;
02:
03: import java.util.Hashtable;
04:
05: /** In Jscheme 1.0 to 1.4, symbols were implemented as Strings. In 1.5, we
06: * add a Symbol class, with a field for the global value. This makes
07: * global lookup faster, but limits us to only one global environment.
08: * @author Peter Norvig, Copyright 1998, peter@norvig.com, <a href="license.txt">license</a>
09: * subsequently modified by Jscheme project members
10: * licensed under zlib licence (see license.txt)
11: **/
12:
13: public class Symbol implements java.io.Serializable,
14: jscheme.SchemeSymbol {
15:
16: private String name;
17:
18: /** The symbolTable keeps a unique mapping from String name to Symbol. **/
19: public static final Hashtable symbolTable = new Hashtable(500);
20:
21: public static final Symbol BEGIN = intern("begin"),
22: CAR = intern("car"), CDR = intern("cdr"),
23: DEFINE = intern("define"), IF = intern("if"),
24: LAMBDA = intern("lambda"), MACRO = intern("macro"),
25: NEWLINE = intern("newline"), NULL = intern("null"),
26: OR = intern("or"), QUASIQUOTE = intern("quasiquote"),
27: QUOTE = intern("quote"), SET = intern("set!"),
28: SPACE = intern("space"), UNQUOTE = intern("unquote"),
29: UNQUOTE_SPLICING = intern("unquote-splicing"),
30: PACKAGE = intern("package");
31:
32: /** Constructor. From outside the class, call Symbol.intern(name). **/
33: private Symbol(String name) {
34: this .name = name;
35: }
36:
37: private Object readResolve() throws java.io.ObjectStreamException {
38: return intern(name);
39: }
40:
41: /** Intern a string: look up or make a symbol. **/
42: public static synchronized Symbol intern(String name) {
43: Symbol result = (Symbol) symbolTable.get(name);
44: if (result == null)
45: symbolTable.put(name, result = new Symbol(name));
46: return result;
47: }
48:
49: /** A symbol can be printed using its name.
50: * We don't do escape processing. **/
51: public String toString() {
52: return name;
53: }
54:
55: public Object getGlobalValue() {
56: return Scheme.getInteractionEnvironment().getValue(this );
57: }
58:
59: public synchronized Object setGlobalValue(Object newval) {
60: return Scheme.getInteractionEnvironment()
61: .setValue(this , newval);
62: }
63:
64: public boolean isDefined() {
65: return Scheme.getInteractionEnvironment().isDefined(this);
66: }
67:
68: }
|