/interpreter/tags/at2dist110511/src/edu/vub/at/objects/natives/grammar/AGSymbol.java
Java | 192 lines | 93 code | 30 blank | 69 comment | 4 complexity | 4e1f99638624fbbd549888d8da8ab287 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.ATTable; 35import edu.vub.at.objects.ATText; 36import edu.vub.at.objects.coercion.NativeTypeTags; 37import edu.vub.at.objects.grammar.ATSymbol; 38import edu.vub.at.objects.natives.NATBoolean; 39import edu.vub.at.objects.natives.NATTable; 40import edu.vub.at.objects.natives.NATText; 41import edu.vub.at.parser.SourceLocation; 42import edu.vub.util.TempFieldGenerator; 43 44import java.util.HashMap; 45import java.util.HashSet; 46import java.util.Set; 47 48/** 49 * @author tvcutsem 50 * 51 * The native implementation of a symbol AG element. 52 * Symbols should only be created via a call to AGSymbol.alloc 53 * This ensures that symbols remain unique within one AmbientTalk VM. 54 */ 55public class AGSymbol extends AGExpression implements ATSymbol { 56 57 /** hashmap shared by ALL actors/VMs, hence, thread access should be explicitly synchronized */ 58 protected static final HashMap _STRINGPOOL_ = new HashMap(); 59 60 private final String txt_; 61 62 protected AGSymbol(String txt) { 63 txt_ = txt; 64 } 65 66 /** 67 * Allocate and return a unique symbol object denoting the given 68 * text string. 69 */ 70 public static AGSymbol jAlloc(String name) { 71 synchronized (_STRINGPOOL_) { 72 AGSymbol existing = (AGSymbol) _STRINGPOOL_.get(name); 73 if (existing == null) { 74 existing = new AGSymbol(name); 75 _STRINGPOOL_.put(name, existing); 76 } 77 return existing; 78 } 79 } 80 81 /** 82 * Allocate and return a unique symbol denoting the given native text. 83 */ 84 public static final AGSymbol alloc(NATText txt) { 85 return jAlloc(txt.javaValue); 86 } 87 88 public ATText base_text() { return NATText.atValue(txt_); } 89 90 /** 91 * To evaluate a symbol reference, look up the symbol in the lexical scope. 92 * 93 * AGSYM(txt).eval(ctx) = ctx.scope.lookup(AGSYM(txt)) 94 * 95 * @return the value bound to this symbol in the lexical environment 96 */ 97 public ATObject meta_eval(ATContext ctx) throws InterpreterException { 98 return ctx.base_lexicalScope().impl_callField(this); 99 } 100 101 /** 102 * Quoting a symbol results in the same symbol. 103 * 104 * sym.quote(ctx) = sym 105 */ 106 public ATObject meta_quote(ATContext ctx) throws InterpreterException { 107 return this; 108 } 109 110 public NATText meta_print() throws InterpreterException { 111 return NATText.atValue(txt_); 112 } 113 114 public ATTable meta_typeTags() throws InterpreterException { 115 return NATTable.of(NativeTypeTags._SYMBOL_, NativeTypeTags._ISOLATE_); 116 } 117 118 public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException { 119 return NATText.atValue("`" + txt_); 120 } 121 122 public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException { 123 return NATText.atValue(txt_); 124 } 125 126 public boolean isSymbol() { 127 return true; 128 } 129 130 public ATSymbol asSymbol() { 131 return this; 132 } 133 134 public boolean isAssignmentSymbol() { 135 return false; 136 } 137 138 public AGAssignmentSymbol asAssignmentSymbol() { 139 return (AGAssignmentSymbol) AGAssignmentSymbol.jAlloc(txt_+":="); 140 } 141 142 // comparison and identity operations 143 144 public boolean equals(Object other) { 145 // pointer equality is valid for symbols as they are pooled 146 return this == other; 147 } 148 149 public ATBoolean base__opeql__opeql_(ATObject comparand) throws InterpreterException { 150 return NATBoolean.atValue(this == comparand); 151 } 152 153 // important as AGSymbols are often used as keys in hash tables 154 155 public int hashCode() { 156 return this.txt_.hashCode(); 157 } 158 159 public String toString() { 160 return txt_; 161 } 162 163 /** 164 * After deserialization, ensure that the symbol remains unique. 165 */ 166 public ATObject meta_resolve() throws InterpreterException { 167 return jAlloc(txt_); 168 } 169 170 /** 171 * FV(nam) = { nam } 172 */ 173 public Set impl_freeVariables() throws InterpreterException { 174 HashSet singleton = new HashSet(); 175 singleton.add(this); 176 return singleton; 177 } 178 179 /** 180 * Within a quoted expression, a variable reference is not considered 181 * a free variable, e.g. `(x) 182 */ 183 public Set impl_quotedFreeVariables() throws InterpreterException { 184 return new HashSet(); 185 } 186 187 // since symbols are interned, they are shared among parse trees 188 // (even among actors!) so their source location is meaningless 189 public SourceLocation impl_getLocation() { return null; } 190 public void impl_setLocation(SourceLocation loc) {} 191 192}