/interpreter/tags/at_build150307/src/edu/vub/at/objects/natives/grammar/AGClosureLiteral.java

http://ambienttalk.googlecode.com/ · Java · 98 lines · 43 code · 10 blank · 45 comment · 0 complexity · c5170c16084c1af7d69c34008c4efb8a MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * AGClosureLiteral.java created on 26-jul-2006 at 16:59:04
  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.ATObject;
  33. import edu.vub.at.objects.ATTable;
  34. import edu.vub.at.objects.grammar.ATBegin;
  35. import edu.vub.at.objects.grammar.ATClosureLiteral;
  36. import edu.vub.at.objects.mirrors.NativeClosure;
  37. import edu.vub.at.objects.natives.NATClosure;
  38. import edu.vub.at.objects.natives.NATMethod;
  39. import edu.vub.at.objects.natives.NATText;
  40. /**
  41. * The native implementation of a literal closure AG element.
  42. *
  43. * @author tvc
  44. */
  45. public final class AGClosureLiteral extends AGExpression implements ATClosureLiteral {
  46. protected final ATTable arguments_;
  47. protected final ATBegin body_;
  48. public AGClosureLiteral(ATTable args, ATBegin body) {
  49. arguments_ = args;
  50. body_ = body;
  51. }
  52. public ATTable base_getArguments() { return arguments_; }
  53. public ATBegin base_getBodyExpression() { return body_; }
  54. /**
  55. * To evaluate a closure literal, construct a closure pairing the literal's arguments and body with the
  56. * current evaluation context.
  57. *
  58. * AGCLOLIT(arg,bdy).eval(ctx) = AGCLO(AGMTH("lambda",arg,bdy), ctx)
  59. *
  60. * @return a native closure closing over the current evaluation context.
  61. */
  62. public ATObject meta_eval(ATContext ctx) throws InterpreterException {
  63. return new NATClosure(new NATMethod(Evaluator._LAMBDA_, arguments_, body_), ctx);
  64. }
  65. /**
  66. * Quoting a closure literal results in a new quoted closure literal.
  67. *
  68. * AGCLOLIT(arg,bdy).quote(ctx) = AGCLOLIT(arg.quote(ctx), bdy.quote(ctx))
  69. */
  70. public ATObject meta_quote(ATContext ctx) throws InterpreterException {
  71. return new AGClosureLiteral(arguments_.meta_quote(ctx).asTable(),
  72. body_.meta_quote(ctx).asBegin());
  73. }
  74. public NATText meta_print() throws InterpreterException {
  75. return arguments_.base_isEmpty().base_ifTrue_ifFalse_(
  76. new NativeClosure(this) {
  77. public ATObject base_apply(ATTable args) throws InterpreterException {
  78. return NATText.atValue("{ "+ body_.meta_print().javaValue + " }");
  79. }
  80. },
  81. new NativeClosure(this) {
  82. public ATObject base_apply(ATTable args) throws InterpreterException {
  83. return NATText.atValue(Evaluator.printElements(arguments_.asNativeTable(), "{ |", ", ", " | ").javaValue +
  84. body_.meta_print().javaValue+"}");
  85. }
  86. }).asNativeText();
  87. }
  88. }