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

http://ambienttalk.googlecode.com/ · Java · 111 lines · 10 code · 10 blank · 91 comment · 0 complexity · 5eed3be0dc058d72b6360f472e55816b 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. /**
  31. * The public interface to a native AmbientTalk closure (a method + enclosing environment).
  32. *
  33. * Since ATMethods are always wrapped either at creation time (blocks) or during
  34. * lookup (methods), ATClosures are by definition the only way methods and blocks
  35. * can be encountered at the ambienttalk base level. Closures should respond to the
  36. * base_apply method, which should trigger the invocation of their encapsulating method in the
  37. * enclosed closure context.
  38. *
  39. * Closures are sometimes also 'abused' to simply represent blocks of source code whose
  40. * body has to be evaluated not in the enclosed lexical context, but within the context
  41. * of another object. To facilitate such use, a closure provides the method 'base_applyInScope'
  42. * which will execute the enclosed method in the scope of the given object, rather than
  43. * in the enclosed lexical context.
  44. *
  45. * @author smostinc
  46. * @author tvcutsem
  47. */
  48. public interface ATClosure extends ATObject {
  49. /**
  50. * Structural access to the encapsulated method.
  51. */
  52. public ATMethod base_getMethod() throws InterpreterException;
  53. /**
  54. * Structural access to the scope of the closure.
  55. */
  56. public ATContext base_getContext() throws InterpreterException;
  57. /**
  58. * Applies the closure to the given arguments, already wrapped in a table.
  59. * The enclosed method is executed in the context provided by the closure.
  60. *
  61. * @param args the already evaluated arguments, wrapped in a table
  62. * @return the value of evaluating the method body in the context of the closure
  63. */
  64. public ATObject base_apply(ATTable args) throws InterpreterException;
  65. /**
  66. * Applies the closure to the given arguments, already wrapped in a table.
  67. * The enclodes method is executed in the context of the given object. The
  68. * enclosed closure context is disregarded.
  69. *
  70. * The context provided by an object is always equal to:
  71. * <tt>ctx(cur=object,self=object,super=object.dynamicParent)</tt>
  72. *
  73. * @param args the already evaluated arguments, wrapped in a table
  74. * @param scope the object that will act as self and as lexically enclosing scope.
  75. * @return the value of evaluating the method body in the context of the given object scope
  76. */
  77. public ATObject base_applyInScope(ATTable args, ATObject scope) throws InterpreterException;
  78. /**
  79. * Allows AmbientTalk programmers to write
  80. * { booleanCondition }.whileTrue: { body }
  81. * which will execute body as long as the boolean condition evaluates to true.
  82. */
  83. public ATObject base_whileTrue_(ATClosure body) throws InterpreterException;
  84. /**
  85. * { |quit| ... quit(val) ... }.escape()
  86. *
  87. * The escape control construct passes to its receiver block a function which
  88. * when invoked, immediately transfers control back to the caller of escape,
  89. * returning the value passed to quit.
  90. *
  91. * If no value is passed to quit, nil is returned instead.
  92. *
  93. * If quit is not invoked during the execution of the receiver block,
  94. * the block terminates normally, with its normal return value.
  95. *
  96. * If quit is invoked at the point where the call to escape has already returned,
  97. * either normally or via an exception or another escape call,
  98. * invoking quit will raise an XIllegalOperation exception.
  99. */
  100. public ATObject base_escape() throws InterpreterException;
  101. }