/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/natives/NATNil.java

http://ambienttalk.googlecode.com/ · Java · 223 lines · 103 code · 29 blank · 91 comment · 3 complexity · b06329098e8aff8d35f1a4e078c681a5 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATNil.java created on 15 jul 2007 at 18:33:28
  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;
  29. import edu.vub.at.eval.Evaluator;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XArityMismatch;
  32. import edu.vub.at.objects.ATBoolean;
  33. import edu.vub.at.objects.ATClosure;
  34. import edu.vub.at.objects.ATContext;
  35. import edu.vub.at.objects.ATNil;
  36. import edu.vub.at.objects.ATObject;
  37. import edu.vub.at.objects.ATTable;
  38. import edu.vub.at.objects.grammar.ATAssignmentSymbol;
  39. import edu.vub.at.objects.grammar.ATSymbol;
  40. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  41. import edu.vub.at.objects.natives.grammar.AGSplice;
  42. import edu.vub.at.objects.natives.grammar.AGSymbol;
  43. /**
  44. * This class encapsulates the behaviour of the native
  45. * <tt>nil</tt> AmbientTalk object.
  46. *
  47. * def nil := object: {
  48. * super := dynamicSentinel;
  49. * def ==(other); // native implementation
  50. * def !=(other) { self.==(other).not }
  51. * def new(@args) { def c := (reflect: self).clone(); c.init(@args); c }
  52. * def init(@args) { }
  53. * }
  54. *
  55. * @author tvcutsem
  56. */
  57. public class NATNil extends NATObject implements ATNil {
  58. /**
  59. * Nil has a special parent object which ends the recursion
  60. * along the dynamic delegation chain. These methods cannot be implemented
  61. * directly in this class because this class still implements useful
  62. * <tt>base_</tt> Java methods which have to be invoked by means of the
  63. * implementations defined in {@link NativeATObject}.
  64. *
  65. * This object is shared by all actors, but it is constant so introduces
  66. * no concurrency issues.
  67. */
  68. private static final NativeATObject dynamicSentinel_ = new NATByCopy() {
  69. private static final long serialVersionUID = -1307795172754062220L;
  70. // METHODS THAT END THE DYNAMIC DELEGATION CHAIN
  71. public ATBoolean meta_respondsTo(ATSymbol selector) throws InterpreterException {
  72. // no more delegation
  73. return NATBoolean._FALSE_;
  74. }
  75. /**
  76. * When performing <tt>o.m()</tt> and <tt>m</tt> is not found, invoke
  77. * <tt>doesNotUnderStand</tt> and apply the resulting closure to the given arguments.
  78. */
  79. public ATObject impl_invokeAccessor(ATObject receiver, ATSymbol selector, ATTable arguments) throws InterpreterException {
  80. return receiver.meta_doesNotUnderstand(selector).base_apply(arguments);
  81. }
  82. public ATObject impl_invokeMutator(ATObject receiver, ATAssignmentSymbol selector, ATTable arguments) throws InterpreterException {
  83. return receiver.meta_doesNotUnderstand(selector).base_apply(arguments);
  84. }
  85. /**
  86. * When performing <tt>o.x</tt> and <tt>x</tt> is not found, invoke
  87. * <tt>doesNotUnderStand</tt> and apply the corresponding closure with zero arguments.
  88. */
  89. public ATObject meta_invokeField(ATObject receiver, ATSymbol selector) throws InterpreterException {
  90. return receiver.meta_doesNotUnderstand(selector).base_apply(NATTable.EMPTY);
  91. }
  92. public ATClosure impl_selectAccessor(ATObject receiver, final ATSymbol selector) throws InterpreterException {
  93. return receiver.meta_doesNotUnderstand(selector);
  94. }
  95. public ATClosure impl_selectMutator(ATObject receiver, final ATAssignmentSymbol selector) throws InterpreterException {
  96. return receiver.meta_doesNotUnderstand(selector);
  97. }
  98. public NATText meta_print() throws InterpreterException {
  99. return NATText.atValue("dynamicsentinel");
  100. }
  101. };
  102. // The names of nil's primitive methods
  103. public static final AGSymbol _EQL_NAME_ = AGSymbol.jAlloc("==");
  104. public static final AGSymbol _NEW_NAME_ = AGSymbol.jAlloc("new");
  105. public static final AGSymbol _INI_NAME_ = AGSymbol.jAlloc("init");
  106. public static final ATSymbol _NEQ_NAME_ = AGSymbol.jAlloc("!=");
  107. // The primitive methods themselves
  108. /** def ==(comparand) { comparand.impl_identityEquals(self) } */
  109. private static final PrimitiveMethod _PRIM_EQL_ = new PrimitiveMethod(
  110. _EQL_NAME_, NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("comparand")})) {
  111. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  112. if (!arguments.base_length().equals(NATNumber.ONE)) {
  113. throw new XArityMismatch("==", 1, arguments.base_length().asNativeNumber().javaValue);
  114. }
  115. ATObject comparand = arguments.base_at(NATNumber.ONE);
  116. // make other object perform the actual pointer equality
  117. // if comparand is a proxy, it can delegate this request to its principal
  118. return comparand.impl_identityEquals(ctx.base_receiver());
  119. }
  120. };
  121. /** def new(@initargs) { (reflect: self).newInstance(initargs) } */
  122. private static final PrimitiveMethod _PRIM_NEW_ = new PrimitiveMethod(
  123. _NEW_NAME_, NATTable.atValue(new ATObject[] { new AGSplice(AGSymbol.jAlloc("initargs")) })) {
  124. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  125. return ctx.base_receiver().meta_newInstance(arguments);
  126. // return ctx.base_receiver().base_new(arguments.asNativeTable().elements_);
  127. }
  128. };
  129. /** def init(@initargs) { self } */
  130. private static final PrimitiveMethod _PRIM_INI_ = new PrimitiveMethod(
  131. _INI_NAME_, NATTable.atValue(new ATObject[] { new AGSplice(AGSymbol.jAlloc("initargs")) })) {
  132. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  133. return ctx.base_receiver();
  134. }
  135. };
  136. /** def !=(other) { self.==(other).not } */
  137. private static final PrimitiveMethod _PRIM_NEQ_ = new PrimitiveMethod(
  138. _NEQ_NAME_, NATTable.of(AGSymbol.jAlloc("other"))) {
  139. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  140. int arity = arguments.base_length().asNativeNumber().javaValue;
  141. if (arity != 1) {
  142. throw new XArityMismatch("!=", 1, arity);
  143. }
  144. ATObject other = arguments.base_at(NATNumber.ONE);
  145. return ctx.base_receiver().base__opeql__opeql_(other).base_not();
  146. }
  147. };
  148. /**
  149. * Construct and initialize a new instance of <tt>nil</tt>. Only one instance
  150. * of <tt>nil</tt> should be created per actor.
  151. */
  152. public NATNil() {
  153. // super(dynamicParent, lexicalParent, parentType)
  154. // super := dynamicSentinel
  155. // the lexical root of nil is set to dynamicSentinel_ because,
  156. // if we would make it refer to Evaluator.getGlobalLexicalRoot(),
  157. // we get an infinite recursion: nil requires root, root requires nil, ...
  158. super(dynamicSentinel_, dynamicSentinel_, NATObject._SHARES_A_);
  159. // add ==, new, init and != to the method dictionary directly
  160. // cannot use meta_addMethod because this method returns nil,
  161. // thus constructing nil requires already having a constructed nil...
  162. // super.meta_addMethod(_PRIM_NEQ_);
  163. methodDictionary_.put(_EQL_NAME_, _PRIM_EQL_);
  164. methodDictionary_.put(_NEW_NAME_, _PRIM_NEW_);
  165. methodDictionary_.put(_INI_NAME_, _PRIM_INI_);
  166. methodDictionary_.put(_NEQ_NAME_, _PRIM_NEQ_);
  167. }
  168. public ATBoolean base__opnot__opeql_(ATObject other) throws InterpreterException {
  169. // immediately hard-wired implementation, because we know 'self' is really
  170. // bound to the Java 'this'.
  171. // return this.meta_invoke(this, _NEQ_NAME_, NATTable.of(other));
  172. return this.base__opeql__opeql_(other).base_not();
  173. }
  174. public ATObject base_super() {
  175. return dynamicSentinel_;
  176. }
  177. public ATObject meta_pass() throws InterpreterException {
  178. return this;
  179. }
  180. /**
  181. * After deserialization, ensure that nil becomes rebound to the nil
  182. * object of the new actor.
  183. */
  184. public ATObject meta_resolve() throws InterpreterException {
  185. return Evaluator.getNil();
  186. }
  187. /** nil is a singleton */
  188. public ATObject meta_clone() throws InterpreterException {
  189. return this;
  190. }
  191. public NATText meta_print() throws InterpreterException {
  192. return NATText.atValue("nil");
  193. }
  194. }