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

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