/interpreter/tags/at2dist030708/src/edu/vub/at/objects/natives/grammar/AGMessageCreation.java

http://ambienttalk.googlecode.com/ · Java · 138 lines · 60 code · 16 blank · 62 comment · 3 complexity · 3a278ffa524d64fe5e3e4650c77d73e7 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGMessageCreation.java created on 10-aug-2006 at 14:10:17
  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.XTypeMismatch;
  32. import edu.vub.at.objects.ATContext;
  33. import edu.vub.at.objects.ATMessage;
  34. import edu.vub.at.objects.ATObject;
  35. import edu.vub.at.objects.ATTable;
  36. import edu.vub.at.objects.grammar.ATExpression;
  37. import edu.vub.at.objects.grammar.ATMessageCreation;
  38. import edu.vub.at.objects.grammar.ATSymbol;
  39. import edu.vub.at.objects.natives.NATTable;
  40. import edu.vub.at.objects.natives.NATText;
  41. /**
  42. * The common superclass of AGAsyncMessageCreation and AGMethodInvocationCreation.
  43. * This class serves as a common repository for both kinds of first-class message AG elements.
  44. *
  45. * @author tvcutsem
  46. */
  47. public abstract class AGMessageCreation extends AGExpression implements ATMessageCreation {
  48. private final ATSymbol selector_;
  49. private final ATTable arguments_;
  50. private final ATExpression annotations_;
  51. public AGMessageCreation(ATSymbol sel, ATTable args, ATExpression annotations) {
  52. selector_ = sel;
  53. arguments_ = args;
  54. annotations_ = annotations;
  55. }
  56. public ATSymbol base_selector() { return selector_; }
  57. public ATTable base_arguments() { return arguments_; }
  58. public ATExpression base_annotations() { return annotations_; }
  59. public boolean isMessageCreation() {
  60. return true;
  61. }
  62. public ATMessageCreation asMessageCreation() throws XTypeMismatch {
  63. return this;
  64. }
  65. /**
  66. * To evaluate a message send, transform the selector
  67. * and evaluated arguments into a first-class Message object.
  68. * It is important to note that the arguments are all eagerly evaluated.
  69. *
  70. * The annotation to the message send is either included in the generated message
  71. * as a table of type tags, if it evaluates to a table, or as a one-sized table of the
  72. * evaluated annotation. This means that <code>o.m(x)@y</code> is
  73. * evaluated as <code>o.m(x)@[y]</code>, for example.
  74. *
  75. * AGMSG(sel,arg,ann).eval(ctx) = NATMSG(sel, map eval(ctx) over arg, (a:ann.eval(ctx).isTable? ? a : [a]))
  76. *
  77. * @return a first-class method invocation
  78. */
  79. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  80. NATTable evaluatedArgs = Evaluator.evaluateArguments(arguments_.asNativeTable(), ctx);
  81. ATObject annotations = annotations_.meta_eval(ctx);
  82. return this.createMessage(ctx,
  83. selector_,
  84. evaluatedArgs,
  85. (annotations.isTable()) ? annotations.asTable() : NATTable.of(annotations));
  86. }
  87. /**
  88. * Quoting a message creation element returns a new quoted message creation element.
  89. */
  90. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  91. return this.newQuoted(selector_.meta_quote(ctx).asSymbol(),
  92. arguments_.meta_quote(ctx).asTable(),
  93. annotations_.meta_quote(ctx).asExpression());
  94. }
  95. public NATText meta_print() throws InterpreterException {
  96. if (annotations_ == NATTable.EMPTY) {
  97. return NATText.atValue(this.getMessageToken() +
  98. selector_.meta_print().javaValue +
  99. Evaluator.printAsList(arguments_).javaValue);
  100. } else {
  101. return NATText.atValue(this.getMessageToken() +
  102. selector_.meta_print().javaValue +
  103. Evaluator.printAsList(arguments_).javaValue +
  104. "@" + annotations_.meta_print().javaValue);
  105. }
  106. }
  107. /**
  108. * Subclasses must implement this method in order to return a new instance of themselves
  109. * parameterized with their quoted arguments.
  110. */
  111. protected abstract ATObject newQuoted(ATSymbol quotedSel, ATTable quotedArgs, ATExpression quotedAnnotations);
  112. /**
  113. * Subclasses must implement this method such that the correct messaging token
  114. * can be displayed when printing an AGMessageCreation element.
  115. */
  116. protected abstract String getMessageToken();
  117. /**
  118. * Subclasses must implement this method such that the correct kind of message is created
  119. * for a given selector, evaluated arguments and annotation
  120. */
  121. protected abstract ATMessage createMessage(ATContext ctx, ATSymbol selector, ATTable evaluatedArgs, ATTable annotations) throws InterpreterException;
  122. }