/interpreter/tags/at_build150307/src/edu/vub/at/objects/natives/grammar/AGSymbol.java
Java | 130 lines | 56 code | 21 blank | 53 comment | 3 complexity | 6fc33b02debdd0449dcbc9f9c7eac924 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.ATContext; 32import edu.vub.at.objects.ATObject; 33import edu.vub.at.objects.ATText; 34import edu.vub.at.objects.grammar.ATSymbol; 35import edu.vub.at.objects.natives.NATText; 36 37import java.util.HashMap; 38 39/** 40 * @author tvcutsem 41 * 42 * The native implementation of a symbol AG element. 43 * Symbols should only be created via a call to AGSymbol.alloc 44 * This ensures that symbols remain unique within one AmbientTalk VM. 45 */ 46public class AGSymbol extends AGExpression implements ATSymbol { 47 48 /** hashmap shared by ALL actors/VMs, hence, thread access should be explicitly synchronized */ 49 private static final HashMap _STRINGPOOL_ = new HashMap(); 50 51 private final String txt_; 52 53 protected AGSymbol(String txt) { 54 txt_ = txt; 55 } 56 57 public static final AGSymbol jAlloc(String name) { 58 synchronized (_STRINGPOOL_) { 59 AGSymbol existing = (AGSymbol) _STRINGPOOL_.get(name); 60 if (existing == null) { 61 existing = new AGSymbol(name); 62 _STRINGPOOL_.put(name, existing); 63 } 64 return existing; 65 } 66 } 67 68 public static final AGSymbol alloc(NATText txt) { 69 return jAlloc(txt.javaValue); 70 } 71 72 public ATText base_getText() { return NATText.atValue(txt_); } 73 74 /** 75 * To evaluate a symbol reference, look up the symbol in the lexical scope. 76 * 77 * AGSYM(txt).eval(ctx) = ctx.scope.lookup(AGSYM(txt)) 78 * 79 * @return the value bound to this symbol in the lexical environment 80 */ 81 public ATObject meta_eval(ATContext ctx) throws InterpreterException { 82 return ctx.base_getLexicalScope().meta_lookup(this); 83 } 84 85 /** 86 * Quoting a symbol results in the same symbol. 87 * 88 * sym.quote(ctx) = sym 89 */ 90 public ATObject meta_quote(ATContext ctx) throws InterpreterException { 91 return this; 92 } 93 94 public NATText meta_print() throws InterpreterException { 95 return NATText.atValue(txt_); 96 } 97 98 public boolean isSymbol() { 99 return true; 100 } 101 102 public ATSymbol asSymbol() { 103 return this; 104 } 105 106 // comparison and identity operations 107 108 public boolean equals(Object other) { 109 // pointer equality is valid for symbols as they are pooled 110 return this == other; 111 } 112 113 // important as AGSymbols are often used as keys in hash tables 114 115 public int hashCode() { 116 return this.txt_.hashCode(); 117 } 118 119 public String toString() { 120 return txt_; 121 } 122 123 /** 124 * After deserialization, ensure that the symbol remains unique. 125 */ 126 public ATObject meta_resolve() throws InterpreterException { 127 return jAlloc(txt_); 128 } 129 130}