PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist041108/src/edu/vub/at/objects/natives/grammar/AGSymbol.java

http://ambienttalk.googlecode.com/
Java | 170 lines | 77 code | 26 blank | 67 comment | 4 complexity | 3de4f3bd600e92dd8b24ddab56014b8b MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGSymbol.java created on 26-jul-2006 at 16:21:55
  4 * (c) Programming Technology Lab, 2006 - 2007
  5 * Authors: Tom Van Cutsem & Stijn Mostinckx
  6 * 
  7 * Permission is hereby granted, free of charge, to any person
  8 * obtaining a copy of this software and associated documentation
  9 * files (the "Software"), to deal in the Software without
 10 * restriction, including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense, and/or
 12 * sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following
 14 * conditions:
 15 *
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26 * OTHER DEALINGS IN THE SOFTWARE.
 27 */
 28package edu.vub.at.objects.natives.grammar;
 29
 30import edu.vub.at.exceptions.InterpreterException;
 31import edu.vub.at.objects.ATBoolean;
 32import edu.vub.at.objects.ATContext;
 33import edu.vub.at.objects.ATObject;
 34import edu.vub.at.objects.ATText;
 35import edu.vub.at.objects.grammar.ATSymbol;
 36import edu.vub.at.objects.natives.NATBoolean;
 37import edu.vub.at.objects.natives.NATText;
 38
 39import java.util.HashMap;
 40import java.util.HashSet;
 41import java.util.Set;
 42
 43/**
 44 * @author tvcutsem
 45 *
 46 * The native implementation of a symbol AG element.
 47 * Symbols should only be created via a call to AGSymbol.alloc
 48 * This ensures that symbols remain unique within one AmbientTalk VM.
 49 */
 50public class AGSymbol extends AGExpression implements ATSymbol {
 51
 52	/** hashmap shared by ALL actors/VMs, hence, thread access should be explicitly synchronized */
 53	protected static final HashMap _STRINGPOOL_ = new HashMap();
 54
 55	private final String txt_;
 56	
 57	protected AGSymbol(String txt) {
 58		txt_ = txt;
 59	}
 60	
 61	/**
 62	 * Allocate and return a unique symbol object denoting the given
 63	 * text string.
 64	 */
 65	public static AGSymbol jAlloc(String name) {
 66		synchronized (_STRINGPOOL_) {
 67			AGSymbol existing = (AGSymbol) _STRINGPOOL_.get(name);
 68			if (existing == null) {
 69				existing = new AGSymbol(name);
 70				_STRINGPOOL_.put(name, existing);
 71			}
 72			return existing;	
 73		}
 74	}
 75	
 76	/**
 77	 * Allocate and return a unique symbol denoting the given native text.
 78	 */
 79	public static final AGSymbol alloc(NATText txt) {
 80		return jAlloc(txt.javaValue);
 81	}
 82	
 83	public ATText base_text() { return NATText.atValue(txt_); }
 84	
 85	/**
 86	 * To evaluate a symbol reference, look up the symbol in the lexical scope.
 87	 * 
 88	 * AGSYM(txt).eval(ctx) = ctx.scope.lookup(AGSYM(txt))
 89	 * 
 90	 * @return the value bound to this symbol in the lexical environment
 91	 */
 92	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 93		return ctx.base_lexicalScope().impl_callField(this);
 94	}
 95
 96	/**
 97	 * Quoting a symbol results in the same symbol.
 98	 * 
 99	 * sym.quote(ctx) = sym
100	 */
101	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
102		return this;
103	}
104	
105	public NATText meta_print() throws InterpreterException {
106		return NATText.atValue(txt_);
107	}
108	
109	public boolean isSymbol() {
110		return true;
111	}
112	
113	public ATSymbol asSymbol() {
114		return this;
115	}
116	
117	public boolean isAssignmentSymbol() {
118		return false;
119	}
120	
121	public AGAssignmentSymbol asAssignmentSymbol() {
122		return (AGAssignmentSymbol) AGAssignmentSymbol.jAlloc(txt_+":=");
123	}
124	
125	// comparison and identity operations
126	
127	public boolean equals(Object other) {
128		// pointer equality is valid for symbols as they are pooled
129		return this == other;
130	}
131	
132    public ATBoolean base__opeql__opeql_(ATObject comparand) throws InterpreterException {
133        return NATBoolean.atValue(this == comparand);
134    }
135	
136	// important as AGSymbols are often used as keys in hash tables
137	
138	public int hashCode() {
139		return this.txt_.hashCode();
140	}
141	
142	public String toString() {
143		return txt_;
144	}
145	
146	/**
147	 * After deserialization, ensure that the symbol remains unique.
148	 */
149	public ATObject meta_resolve() throws InterpreterException {
150		return jAlloc(txt_);
151	}
152	
153	/**
154	 * FV(nam) = { nam }
155	 */
156	public Set impl_freeVariables() throws InterpreterException {
157        HashSet singleton = new HashSet();
158        singleton.add(this);
159        return singleton;
160	}
161	
162	/**
163	 * Within a quoted expression, a variable reference is not considered
164	 * a free variable, e.g. `(x)
165	 */
166	public Set impl_quotedFreeVariables() throws InterpreterException {
167		return new HashSet();
168	}
169
170}