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

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