/interpreter/tags/at2dist041108/src/edu/vub/at/objects/natives/grammar/AGDefFunction.java

http://ambienttalk.googlecode.com/ · Java · 196 lines · 106 code · 25 blank · 65 comment · 7 complexity · c0cf5dbc2c5966a6dd308a3557dd17e4 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGDefFunction.java created on 26-jul-2006 at 15:44:08
  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.objects.ATContext;
  32. import edu.vub.at.objects.ATMethod;
  33. import edu.vub.at.objects.ATObject;
  34. import edu.vub.at.objects.ATTable;
  35. import edu.vub.at.objects.ATTypeTag;
  36. import edu.vub.at.objects.coercion.NativeTypeTags;
  37. import edu.vub.at.objects.grammar.ATBegin;
  38. import edu.vub.at.objects.grammar.ATDefMethod;
  39. import edu.vub.at.objects.grammar.ATDefinition;
  40. import edu.vub.at.objects.grammar.ATExpression;
  41. import edu.vub.at.objects.grammar.ATSymbol;
  42. import edu.vub.at.objects.natives.NATClosure;
  43. import edu.vub.at.objects.natives.NATMethod;
  44. import edu.vub.at.objects.natives.NATTable;
  45. import edu.vub.at.objects.natives.NATText;
  46. import java.util.HashSet;
  47. import java.util.Set;
  48. /**
  49. * The native implementation of a function definition abstract grammar element.
  50. * This AG element covers both method and closure definition.
  51. *
  52. * @author tvc
  53. */
  54. public final class AGDefFunction extends AGDefinition implements ATDefMethod {
  55. private final ATSymbol selectorExp_;
  56. private final ATTable argumentExps_;
  57. private final ATBegin bodyStmts_;
  58. private final ATExpression annotationExps_;
  59. private ATMethod preprocessedMethod_;
  60. public AGDefFunction(ATSymbol sel, ATTable args, ATBegin bdy, ATExpression ann)
  61. throws InterpreterException {
  62. selectorExp_ = sel;
  63. argumentExps_ = args;
  64. bodyStmts_ = bdy;
  65. annotationExps_ = ann;
  66. }
  67. public ATSymbol base_selector() {
  68. return selectorExp_;
  69. }
  70. public ATTable base_arguments() {
  71. return argumentExps_;
  72. }
  73. public ATBegin base_bodyExpression() {
  74. return bodyStmts_;
  75. }
  76. public ATExpression base_annotationExpression() {
  77. return annotationExps_;
  78. }
  79. /**
  80. * Defines a new function (method or closure) in the current scope.
  81. * If a function definition is executed in the context of an object,
  82. * a new method is created. If, however, the function definition
  83. * is executed in the context of a call frame (i.e. its definition
  84. * is nested within a method/function) then a closure is created instead.
  85. *
  86. * The return value of a function definition is always a closure encapsulating the function.
  87. *
  88. * AGDEFFUN(nam,par,bdy).eval(ctx) =
  89. * if ctx.scope.isCallFrame
  90. * ctx.scope.defineField(nam, AGCLO(AGMTH(nam,par,bdy), ctx))
  91. * else
  92. * ctx.scope.addMethod(nam, AGMTH(nam,par,bdy))
  93. *
  94. */
  95. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  96. // the method is not yet created in the constructor because this gives problems
  97. // with quoted parameters: a quoted parameter would result in an illegal parameter
  98. // exception while actually the function was defined in the context of a quotation,
  99. // so at runtime the function definition would have never been evaluated (but quoted instead)
  100. if (preprocessedMethod_ == null) {
  101. ATObject oneOrMoreAnnotation = annotationExps_.meta_eval(ctx);
  102. ATTable annotationTable;
  103. if(oneOrMoreAnnotation.isTable()) {
  104. annotationTable = oneOrMoreAnnotation.asTable();
  105. } else {
  106. annotationTable = NATTable.of(oneOrMoreAnnotation);
  107. }
  108. preprocessedMethod_ = new NATMethod(selectorExp_, argumentExps_, bodyStmts_, annotationTable);
  109. ATObject[] annotations = annotationTable.asNativeTable().elements_;
  110. for (int i = 0; i < annotations.length; i++) {
  111. ATTypeTag theAnnotation = annotations[i].asTypeTag();
  112. preprocessedMethod_ = theAnnotation.base_annotateMethod(preprocessedMethod_);
  113. }
  114. }
  115. ATObject current = ctx.base_lexicalScope();
  116. if (current.isCallFrame()) {
  117. NATClosure clo = new NATClosure(preprocessedMethod_, ctx);
  118. current.meta_defineField(selectorExp_, clo);
  119. return clo;
  120. } else {
  121. current.meta_addMethod(preprocessedMethod_);
  122. return current.meta_select(current, selectorExp_);
  123. }
  124. }
  125. /**
  126. * Quoting a function definition results in a new quoted function definition.
  127. *
  128. * AGDEFFUN(nam,par,bdy).quote(ctx) = AGDEFFUN(nam.quote(ctx), par.quote(ctx), bdy.quote(ctx))
  129. */
  130. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  131. return new AGDefFunction(selectorExp_.meta_quote(ctx).asSymbol(),
  132. argumentExps_.meta_quote(ctx).asTable(),
  133. bodyStmts_.meta_quote(ctx).asBegin(),
  134. annotationExps_.meta_quote(ctx).asExpression());
  135. }
  136. public NATText meta_print() throws InterpreterException {
  137. return NATText.atValue("def " +
  138. selectorExp_.meta_print().javaValue +
  139. Evaluator.printAsList(argumentExps_).javaValue +
  140. " { " + bodyStmts_.meta_print().javaValue + " }");
  141. }
  142. public ATTable meta_typeTags() throws InterpreterException {
  143. return NATTable.of(NativeTypeTags._METHOD_DEFINITION_, NativeTypeTags._ISOLATE_);
  144. }
  145. /**
  146. * IV(def f(args) @ annotations { body }) = { f }
  147. */
  148. public Set impl_introducedVariables() throws InterpreterException {
  149. Set singleton = new HashSet();
  150. singleton.add(selectorExp_);
  151. return singleton;
  152. }
  153. /**
  154. * FV(def f(args) @ annotations { body })
  155. * = FV(annotations) U FV(optionalArgExps) U (FV(body) \ { args } \ { f })
  156. */
  157. public Set impl_freeVariables() throws InterpreterException {
  158. Set fvBody = bodyStmts_.impl_freeVariables();
  159. fvBody.remove(selectorExp_);
  160. Evaluator.processFreeVariables(fvBody, argumentExps_);
  161. fvBody.addAll(annotationExps_.impl_freeVariables());
  162. return fvBody;
  163. }
  164. public Set impl_quotedFreeVariables() throws InterpreterException {
  165. Set qfv = selectorExp_.impl_quotedFreeVariables();
  166. qfv.addAll(argumentExps_.impl_quotedFreeVariables());
  167. qfv.addAll(bodyStmts_.impl_quotedFreeVariables());
  168. qfv.addAll(annotationExps_.impl_quotedFreeVariables());
  169. return qfv;
  170. }
  171. }