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

http://ambienttalk.googlecode.com/ · Java · 112 lines · 55 code · 10 blank · 47 comment · 2 complexity · c9c9e726c9bb14c300ae4f607785330b MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGApplication.java created on 26-jul-2006 at 16:13:31
  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.eval.InvocationStack;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import edu.vub.at.objects.ATClosure;
  33. import edu.vub.at.objects.ATContext;
  34. import edu.vub.at.objects.ATObject;
  35. import edu.vub.at.objects.ATTable;
  36. import edu.vub.at.objects.grammar.ATApplication;
  37. import edu.vub.at.objects.grammar.ATExpression;
  38. import edu.vub.at.objects.natives.NATTable;
  39. import edu.vub.at.objects.natives.NATText;
  40. /**
  41. * The native implementation of an application AG element.
  42. *
  43. * @author tvc
  44. */
  45. public final class AGApplication extends AGExpression implements ATApplication {
  46. private final ATExpression funExp_;
  47. private final ATTable arguments_;
  48. public AGApplication(ATExpression fun, ATTable arg) {
  49. funExp_ = fun;
  50. arguments_ = arg;
  51. }
  52. public ATExpression base_function() { return funExp_; }
  53. public ATTable base_arguments() { return arguments_; }
  54. /**
  55. * To evaluate a function application, evaluate the receiver expression to a function, then evaluate the arguments
  56. * to the function application eagerly and apply the function.
  57. *
  58. * AGAPL(fun,arg).eval(ctx) = fun.eval(ctx).apply(map eval(ctx) over arg)
  59. *
  60. * @return the return value of the applied function.
  61. */
  62. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  63. // calls of the form f(x) are transformed into ctx.lex.call(`f, [x])
  64. if(funExp_.isSymbol()) {
  65. NATTable args = Evaluator.evaluateArguments(arguments_.asNativeTable(), ctx);
  66. ATObject result = null;
  67. InvocationStack stack = InvocationStack.getInvocationStack();
  68. try {
  69. stack.functionCalled(this, null, args);
  70. result = ctx.base_lexicalScope().impl_callAccessor(funExp_.asSymbol(), args);
  71. } finally {
  72. stack.funcallReturned(result);
  73. }
  74. return result;
  75. // calls of the form (expr)(x) are transformed into expr.eval(ctx).apply([x])
  76. } else {
  77. ATClosure clo = funExp_.meta_eval(ctx).asClosure();
  78. NATTable args = Evaluator.evaluateArguments(arguments_.asNativeTable(), ctx);
  79. ATObject result = null;
  80. InvocationStack stack = InvocationStack.getInvocationStack();
  81. try {
  82. stack.functionCalled(this, clo, args);
  83. result = clo.base_apply(args);
  84. } finally {
  85. stack.funcallReturned(result);
  86. }
  87. return result;
  88. }
  89. }
  90. /**
  91. * Quoting an application results in a new quoted application.
  92. *
  93. * AGAPL(sel,arg).quote(ctx) = AGAPL(sel.quote(ctx), arg.quote(ctx))
  94. */
  95. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  96. return new AGApplication(funExp_.meta_quote(ctx).asExpression(),
  97. arguments_.meta_quote(ctx).asTable());
  98. }
  99. public NATText meta_print() throws InterpreterException {
  100. return NATText.atValue(funExp_.meta_print().javaValue + Evaluator.printAsList(arguments_).javaValue);
  101. }
  102. }