/interpreter/tags/at2dist110511/src/edu/vub/at/objects/natives/grammar/AGDefExternalMethod.java

http://ambienttalk.googlecode.com/ · Java · 207 lines · 120 code · 27 blank · 60 comment · 7 complexity · bb17fb6c77cc75d718bc1784fcea641e MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGDefExternalMethod.java created on 15-nov-2006 at 19:33:18
  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.eval.Evaluator;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.exceptions.XIllegalOperation;
  32. import edu.vub.at.objects.ATContext;
  33. import edu.vub.at.objects.ATMethod;
  34. import edu.vub.at.objects.ATObject;
  35. import edu.vub.at.objects.ATTable;
  36. import edu.vub.at.objects.ATTypeTag;
  37. import edu.vub.at.objects.coercion.NativeTypeTags;
  38. import edu.vub.at.objects.grammar.ATBegin;
  39. import edu.vub.at.objects.grammar.ATDefExternalMethod;
  40. import edu.vub.at.objects.grammar.ATDefinition;
  41. import edu.vub.at.objects.grammar.ATExpression;
  42. import edu.vub.at.objects.grammar.ATSymbol;
  43. import edu.vub.at.objects.natives.NATClosureMethod;
  44. import edu.vub.at.objects.natives.NATMethod;
  45. import edu.vub.at.objects.natives.NATTable;
  46. import edu.vub.at.objects.natives.NATText;
  47. import edu.vub.util.TempFieldGenerator;
  48. import java.util.HashSet;
  49. import java.util.Set;
  50. /**
  51. * The native implementation of an external method definition abstract grammar element.
  52. *
  53. * @author tvcutsem
  54. */
  55. public final class AGDefExternalMethod extends AGDefinition implements ATDefExternalMethod {
  56. private final ATSymbol rcvNam_;
  57. private final ATSymbol selectorExp_;
  58. private final ATTable argumentExps_;
  59. private final ATBegin bodyStmts_;
  60. private final ATExpression annotationExps_;
  61. private ATMethod preprocessedMethod_;
  62. public AGDefExternalMethod(ATSymbol rcv, ATSymbol sel, ATTable args, ATBegin bdy, ATExpression ann)
  63. throws InterpreterException {
  64. rcvNam_ = rcv;
  65. selectorExp_ = sel;
  66. argumentExps_ = args;
  67. bodyStmts_ = bdy;
  68. annotationExps_ = ann;
  69. }
  70. public ATSymbol base_receiver() {
  71. return rcvNam_;
  72. }
  73. public ATSymbol base_selector() {
  74. return selectorExp_;
  75. }
  76. public ATTable base_arguments() {
  77. return argumentExps_;
  78. }
  79. public ATBegin base_bodyExpression() {
  80. return bodyStmts_;
  81. }
  82. public ATExpression base_annotationExpression() {
  83. return annotationExps_;
  84. }
  85. /**
  86. * Evaluates the receiver symbol to an object to which a new 'closure method' will be added.
  87. * Such a closure captures the current lexical scope, but not the values for 'self' and 'super'.
  88. *
  89. * The return value of an external method definition is always the external method itself.
  90. *
  91. * AGDEFEXTMTH(rcv,nam,par,bdy).eval(ctx) =
  92. * rcv.eval(ctx).defineField(nam, NATCLOMTH(ctx.cur,nam,par,bdy))
  93. *
  94. * @throws XIllegalOperation if the receiver is an instance of a native
  95. * type (whose method dictionaries are sealed) or if the receiver is an isolate.
  96. */
  97. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  98. // the method is not yet created in the constructor because this gives problems
  99. // with quoted parameters: a quoted parameter would result in an illegal parameter
  100. // exception while actually the external method was defined in the context of a quotation,
  101. // so at runtime the external definition would have never been evaluated (but quoted instead)
  102. if (preprocessedMethod_ == null) {
  103. ATObject oneOrMoreAnnotation = annotationExps_.meta_eval(ctx);
  104. ATTable annotationTable;
  105. if(oneOrMoreAnnotation.isTable()) {
  106. annotationTable = oneOrMoreAnnotation.asTable();
  107. } else {
  108. annotationTable = NATTable.of(oneOrMoreAnnotation);
  109. }
  110. NATMethod preprocessedMethod = new NATMethod(selectorExp_, argumentExps_, bodyStmts_, annotationTable);
  111. preprocessedMethod.impl_setLocation(this.impl_getLocation());
  112. preprocessedMethod_ = preprocessedMethod;
  113. ATObject[] annotations = annotationTable.asNativeTable().elements_;
  114. for (int i = 0; i < annotations.length; i++) {
  115. ATTypeTag theAnnotation = annotations[i].asTypeTag();
  116. preprocessedMethod_ = theAnnotation.base_annotateMethod(preprocessedMethod_);
  117. }
  118. }
  119. ATObject receiver = rcvNam_.meta_eval(ctx);
  120. if (receiver.meta_isTaggedAs(NativeTypeTags._ISOLATE_).asNativeBoolean().javaValue) {
  121. throw new XIllegalOperation("Cannot define external methods on isolates");
  122. } else {
  123. NATClosureMethod extMethod = new NATClosureMethod(ctx.base_lexicalScope(),
  124. preprocessedMethod_);
  125. receiver.meta_addMethod(extMethod);
  126. return extMethod;
  127. }
  128. }
  129. /**
  130. * Quoting an external method definition results in a new quoted external method definition.
  131. *
  132. * AGDEFEXTMTH(rcv,nam,par,bdy).quote(ctx) = AGDEFEXTMTH(rcv.quote(ctx),nam.quote(ctx), par.quote(ctx), bdy.quote(ctx))
  133. */
  134. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  135. return new AGDefExternalMethod(rcvNam_.meta_quote(ctx).asSymbol(),
  136. selectorExp_.meta_quote(ctx).asSymbol(),
  137. argumentExps_.meta_quote(ctx).asTable(),
  138. bodyStmts_.meta_quote(ctx).asBegin(),
  139. annotationExps_.meta_quote(ctx).asExpression());
  140. }
  141. public NATText meta_print() throws InterpreterException {
  142. return NATText.atValue("def " +
  143. rcvNam_.meta_print().javaValue + "." +
  144. selectorExp_.meta_print().javaValue +
  145. Evaluator.printAsList(argumentExps_).javaValue +
  146. " { " + bodyStmts_.meta_print().javaValue + " }");
  147. }
  148. public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
  149. return NATText.atValue("def " +
  150. rcvNam_.impl_asUnquotedCode(objectMap).javaValue + "." +
  151. selectorExp_.impl_asUnquotedCode(objectMap).javaValue +
  152. Evaluator.codeAsList(objectMap, argumentExps_).javaValue +
  153. " { " + bodyStmts_.impl_asUnquotedCode(objectMap).javaValue + " }");
  154. }
  155. /**
  156. * IV(def o.m(args) @ anns { body }) = { }
  157. */
  158. public Set impl_introducedVariables() throws InterpreterException {
  159. return new HashSet();
  160. }
  161. /**
  162. * FV(def o.m(args) @ anns { body }) =
  163. * { o } U FV(anns) U FV(optionalArgExps) U (FV(body) \ { args })
  164. */
  165. public Set impl_freeVariables() throws InterpreterException {
  166. Set fvBody = bodyStmts_.impl_freeVariables();
  167. Evaluator.processFreeVariables(fvBody, argumentExps_);
  168. fvBody.add(rcvNam_);
  169. fvBody.addAll(annotationExps_.impl_freeVariables());
  170. return fvBody;
  171. }
  172. public Set impl_quotedFreeVariables() throws InterpreterException {
  173. Set qfv = argumentExps_.impl_quotedFreeVariables();
  174. qfv.addAll(bodyStmts_.impl_quotedFreeVariables());
  175. qfv.addAll(annotationExps_.impl_quotedFreeVariables());
  176. qfv.addAll(rcvNam_.impl_quotedFreeVariables());
  177. qfv.addAll(selectorExp_.impl_quotedFreeVariables());
  178. return qfv;
  179. }
  180. }