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