/interpreter/tags/at2dist130208/src/edu/vub/at/objects/ATClosure.java

http://ambienttalk.googlecode.com/ · Java · 134 lines · 11 code · 10 blank · 113 comment · 0 complexity · 466d5ebf9c39ecb6e4e6f20911a87591 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * ATClosure.java created on Jul 23, 2006 at 11:52:10 AM
  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;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.exceptions.XIllegalOperation;
  31. /**
  32. * ATClosure is the public interface to a native AmbientTalk closure (a method + enclosing environment).
  33. * <p>
  34. * Since {@link ATMethod}s are always wrapped either at creation time (blocks) or during
  35. * lookup (methods), ATClosures are by definition the only way methods and blocks
  36. * can be encountered at the AmbientTalk base level.
  37. * <p>
  38. * Closures should respond to the base_apply method, which should trigger the invocation of their encapsulating method in the
  39. * enclosed closure context.
  40. * <p>
  41. * Closures are sometimes also 'abused' to simply represent blocks of source code whose
  42. * body has to be evaluated not in the enclosed lexical context, but within the context
  43. * of another object. To facilitate such use, a closure provides the method 'base_applyInScope'
  44. * which will execute the enclosed method in the scope of the given object, rather than
  45. * in the enclosed lexical context.
  46. *
  47. * @author smostinc
  48. * @author tvcutsem
  49. */
  50. public interface ATClosure extends ATObject {
  51. /**
  52. * Returns the encapsulated method.
  53. *
  54. * @return an {@link ATMethod} that returns the encapsulated method.
  55. */
  56. public ATMethod base_method() throws InterpreterException;
  57. /**
  58. * Returns the scope of the closure.
  59. *
  60. * @return an {@link ATMethod} that returns the scope of the closure.
  61. */
  62. public ATContext base_context() throws InterpreterException;
  63. /**
  64. * Applies the closure to the given arguments, already wrapped in a table.
  65. * The enclosed method is executed in the context provided by the closure.
  66. *
  67. * @param args the already evaluated arguments, wrapped in a table.
  68. * @return the value of evaluating the method body in the context of the closure.
  69. */
  70. public ATObject base_apply(ATTable args) throws InterpreterException;
  71. /**
  72. * Applies the closure to the given arguments, already wrapped in a table.
  73. * The enclosed method is executed in the context of the given object. The
  74. * enclosed closure context is disregarded.
  75. * <p>
  76. * The context provided by an object is always equal to:
  77. * <tt>ctx(cur=object,self=object,super=object.dynamicParent)</tt>
  78. *
  79. * @param args the already evaluated arguments, wrapped in a table.
  80. * @param scope the object that will act as self and as lexically enclosing scope.
  81. * @return the value of evaluating the method body in the context of the given object scope.
  82. */
  83. public ATObject base_applyInScope(ATTable args, ATObject scope) throws InterpreterException;
  84. /**
  85. * Allows AmbientTalk programmers to write
  86. * <code>{ booleanCondition }.whileTrue: { body }</code>
  87. * which will execute body as long as the boolean condition evaluates to true.
  88. * <p>
  89. * More specifically, what the native implementation (expressed in AmbientTalk syntax) does is:
  90. * <p>
  91. * <code>
  92. * def whileTrue: body {
  93. * self.apply().ifTrue: {
  94. * body();
  95. * self.whileTrue: body
  96. * }
  97. * }
  98. * </code>
  99. *
  100. * @param body the block of code that will be executed as long as the boolean condition evaluates to true.
  101. * @return the value of the last closure applied.
  102. * @throws InterpreterException if raised inside the code closure.
  103. */
  104. public ATObject base_whileTrue_(ATClosure body) throws InterpreterException;
  105. /**
  106. * Escape control construct <code>{ |quit| ... quit(val) ... }.escape()</code>
  107. * <p>
  108. * The escape control construct passes to its receiver block a function which
  109. * when invoked, immediately transfers control back to the caller of escape,
  110. * returning the value passed to quit.
  111. * <p>
  112. * If no value is passed to quit, nil is returned instead.
  113. * <p>
  114. * If quit is not invoked during the execution of the receiver block,
  115. * the block terminates normally, with its normal return value.
  116. * <p>
  117. * If quit is invoked at the point where the call to escape has already returned,
  118. * either normally or via an exception or another escape call,
  119. * invoking quit will raise a {@link XIllegalOperation} exception.
  120. *
  121. * @return the value passed to quit.
  122. */
  123. public ATObject base_escape() throws InterpreterException;
  124. }