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

http://ambienttalk.googlecode.com/ · Java · 160 lines · 74 code · 18 blank · 68 comment · 0 complexity · 1155dfe52875bc5a44fb2622a90f70da MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATMethod.java created on Jul 24, 2006 at 11:30:35 PM
  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.PartialBinder;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XTypeMismatch;
  32. import edu.vub.at.objects.ATClosure;
  33. import edu.vub.at.objects.ATContext;
  34. import edu.vub.at.objects.ATMethod;
  35. import edu.vub.at.objects.ATObject;
  36. import edu.vub.at.objects.ATTable;
  37. import edu.vub.at.objects.coercion.NativeTypeTags;
  38. import edu.vub.at.objects.grammar.ATBegin;
  39. import edu.vub.at.objects.grammar.ATSymbol;
  40. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  41. import edu.vub.at.util.logging.Logging;
  42. /**
  43. * NATMethod implements methods as named functions which are in fact simply containers
  44. * for a name, a table of arguments and a body.
  45. *
  46. * @author smostinc
  47. * @author tvcutsem
  48. */
  49. public class NATMethod extends NATByCopy implements ATMethod {
  50. private final ATSymbol name_;
  51. private final ATTable parameters_;
  52. private final ATBegin body_;
  53. // partial function denoting a parameter binding algorithm specialized for this method's parameter list
  54. private final PartialBinder parameterBindingFunction_;
  55. /** construct a new method. This method may raise an exception if the parameter list is illegal. */
  56. public NATMethod(ATSymbol name, ATTable parameters, ATBegin body) throws InterpreterException {
  57. name_ = name;
  58. parameters_ = parameters;
  59. body_ = body;
  60. // calculate the parameter binding strategy to use using partial evaluation
  61. parameterBindingFunction_ =
  62. PartialBinder.calculateResidual(name_.base_text().asNativeText().javaValue, parameters);
  63. }
  64. /**
  65. * Constructor to be used by primitive methods only.
  66. */
  67. protected NATMethod(ATSymbol name, ATTable parameters, PrimitiveMethod.PrimitiveBody body) {
  68. name_ = name;
  69. parameters_ = parameters;
  70. body_ = body;
  71. PartialBinder parameterBindingFunction;
  72. try {
  73. // calculate the parameter binding strategy to use using partial evaluation
  74. parameterBindingFunction = PartialBinder.calculateResidual(name_.base_text().asNativeText().javaValue, parameters);
  75. } catch (InterpreterException e) {
  76. parameterBindingFunction = null;
  77. // this indicates a bug, primitive methods should not contain erroneous parameter lists
  78. Logging.VirtualMachine_LOG.fatal("error creating primitive method: ",e);
  79. }
  80. parameterBindingFunction_ = parameterBindingFunction;
  81. }
  82. public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
  83. return new NATClosure(this, lexicalScope, dynamicReceiver);
  84. }
  85. public ATSymbol base_name() {
  86. return name_;
  87. }
  88. public ATTable base_parameters() {
  89. return parameters_;
  90. }
  91. public ATBegin base_bodyExpression() {
  92. return body_;
  93. }
  94. /**
  95. * To apply a function, first bind its parameters to the evaluated arguments within a new call frame.
  96. * This call frame is lexically nested within the current lexical scope.
  97. *
  98. * This method is invoked via the following paths:
  99. * - either by directly 'calling a function', in which case this method is applied via NATClosure.base_apply.
  100. * The closure ensures that the context used is the lexical scope, not the dynamic scope of invocation.
  101. * - or by 'invoking a method' through an object, in which case this method is applied via NATObject.meta_invoke.
  102. * The enclosing object ensures that the context is properly initialized with the implementor, the dynamic receiver
  103. * and the implementor's parent.
  104. *
  105. * @param arguments the evaluated actual arguments
  106. * @param ctx the context in which to evaluate the method body, where a call frame will be inserted first
  107. * @return the value of evaluating the function body
  108. */
  109. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  110. NATCallframe cf = new NATCallframe(ctx.base_lexicalScope());
  111. ATContext evalCtx = ctx.base_withLexicalEnvironment(cf);
  112. PartialBinder.defineParamsForArgs(parameterBindingFunction_, evalCtx, arguments);
  113. return body_.meta_eval(evalCtx);
  114. }
  115. /**
  116. * Applies the method in the context given, without first inserting a call frame to bind parameters.
  117. * Arguments are bound directly in the given lexical scope.
  118. *
  119. * This method is often invoked via its enclosing closure when used to implement various language
  120. * constructs such as object:, mirror:, extend:with: etc.
  121. *
  122. * @param arguments the evaluated actual arguments
  123. * @param ctx the context in which to evaluate the method body, to be used as-is
  124. * @return the value of evaluating the function body
  125. */
  126. public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
  127. PartialBinder.defineParamsForArgs(parameterBindingFunction_, ctx, arguments);
  128. return body_.meta_eval(ctx);
  129. }
  130. public NATText meta_print() throws InterpreterException {
  131. return NATText.atValue("<method:"+name_.meta_print().javaValue+">");
  132. }
  133. public ATObject meta_clone() throws InterpreterException {
  134. return this;
  135. }
  136. public ATMethod asMethod() throws XTypeMismatch {
  137. return this;
  138. }
  139. public ATTable meta_typeTags() throws InterpreterException {
  140. return NATTable.of(NativeTypeTags._METHOD_, NativeTypeTags._ISOLATE_);
  141. }
  142. }