/interpreter/tags/at2-build270707/src/edu/vub/at/objects/natives/grammar/AGSymbol.java

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