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

http://ambienttalk.googlecode.com/ · Java · 136 lines · 67 code · 16 blank · 53 comment · 4 complexity · 86c61a4d2d9245f6535c02d3c856cec5 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.ATObject;
  34. import edu.vub.at.objects.ATTable;
  35. import edu.vub.at.objects.coercion.NativeTypeTags;
  36. import edu.vub.at.objects.grammar.ATBegin;
  37. import edu.vub.at.objects.grammar.ATDefExternalMethod;
  38. import edu.vub.at.objects.grammar.ATSymbol;
  39. import edu.vub.at.objects.natives.NATClosureMethod;
  40. import edu.vub.at.objects.natives.NATMethod;
  41. import edu.vub.at.objects.natives.NATText;
  42. /**
  43. * The native implementation of an external method definition abstract grammar element.
  44. *
  45. * @author tvcutsem
  46. */
  47. public final class AGDefExternalMethod extends NATAbstractGrammar implements ATDefExternalMethod {
  48. private final ATSymbol rcvNam_;
  49. private final ATSymbol selectorExp_;
  50. private final ATTable argumentExps_;
  51. private final ATBegin bodyStmts_;
  52. private NATMethod preprocessedMethod_;
  53. public AGDefExternalMethod(ATSymbol rcv, ATSymbol sel, ATTable args, ATBegin bdy)
  54. throws InterpreterException {
  55. rcvNam_ = rcv;
  56. selectorExp_ = sel;
  57. argumentExps_ = args;
  58. bodyStmts_ = bdy;
  59. }
  60. public ATSymbol base_receiver() {
  61. return rcvNam_;
  62. }
  63. public ATSymbol base_selector() {
  64. return selectorExp_;
  65. }
  66. public ATTable base_arguments() {
  67. return argumentExps_;
  68. }
  69. public ATBegin base_bodyExpression() {
  70. return bodyStmts_;
  71. }
  72. /**
  73. * Evaluates the receiver symbol to an object to which a new 'closure method' will be added.
  74. * Such a closure captures the current lexical scope, but not the values for 'self' and 'super'.
  75. *
  76. * The return value of an external method definition is always the external method itself.
  77. *
  78. * AGDEFEXTMTH(rcv,nam,par,bdy).eval(ctx) =
  79. * rcv.eval(ctx).defineField(nam, NATCLOMTH(ctx.cur,nam,par,bdy))
  80. *
  81. * @throws XIllegalOperation if the receiver is an instance of a native
  82. * type (whose method dictionaries are sealed) or if the receiver is an isolate.
  83. */
  84. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  85. // the method is not yet created in the constructor because this gives problems
  86. // with quoted parameters: a quoted parameter would result in an illegal parameter
  87. // exception while actually the external method was defined in the context of a quotation,
  88. // so at runtime the external definition would have never been evaluated (but quoted instead)
  89. if (preprocessedMethod_ == null) {
  90. preprocessedMethod_ = new NATMethod(selectorExp_, argumentExps_, bodyStmts_);
  91. }
  92. ATObject receiver = rcvNam_.meta_eval(ctx);
  93. if (receiver.meta_isTaggedAs(NativeTypeTags._ISOLATE_).asNativeBoolean().javaValue) {
  94. throw new XIllegalOperation("Cannot define external methods on isolates");
  95. } else {
  96. NATClosureMethod extMethod = new NATClosureMethod(ctx.base_lexicalScope(),
  97. preprocessedMethod_);
  98. receiver.meta_addMethod(extMethod);
  99. return extMethod;
  100. }
  101. }
  102. /**
  103. * Quoting an external method definition results in a new quoted external method definition.
  104. *
  105. * AGDEFEXTMTH(rcv,nam,par,bdy).quote(ctx) = AGDEFEXTMTH(rcv.quote(ctx),nam.quote(ctx), par.quote(ctx), bdy.quote(ctx))
  106. */
  107. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  108. return new AGDefExternalMethod(rcvNam_.meta_quote(ctx).asSymbol(),
  109. selectorExp_.meta_quote(ctx).asSymbol(),
  110. argumentExps_.meta_quote(ctx).asTable(),
  111. bodyStmts_.meta_quote(ctx).asBegin());
  112. }
  113. public NATText meta_print() throws InterpreterException {
  114. return NATText.atValue("def " +
  115. rcvNam_.meta_print().javaValue + "." +
  116. selectorExp_.meta_print().javaValue +
  117. Evaluator.printAsList(argumentExps_).javaValue +
  118. " { " + bodyStmts_.meta_print().javaValue + " }");
  119. }
  120. }