PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/grammar/AGMessageSend.java

http://ambienttalk.googlecode.com/
Java | 123 lines | 59 code | 15 blank | 49 comment | 0 complexity | 3a09db856ec64875243199b370aa6955 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGMessageSend.java created on 26-jul-2006 at 16:03:06
  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.natives.grammar;
 29
 30import edu.vub.at.eval.InvocationStack;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.objects.ATContext;
 33import edu.vub.at.objects.ATMessage;
 34import edu.vub.at.objects.ATObject;
 35import edu.vub.at.objects.ATTable;
 36import edu.vub.at.objects.grammar.ATExpression;
 37import edu.vub.at.objects.grammar.ATMessageSend;
 38import edu.vub.at.objects.natives.NATText;
 39import edu.vub.at.parser.SourceLocation;
 40import edu.vub.util.TempFieldGenerator;
 41
 42import java.util.Set;
 43
 44/**
 45 * @author tvc
 46 *
 47 * The native implementation of a synchronous message send AG element.
 48 */
 49public final class AGMessageSend extends AGExpression implements ATMessageSend {
 50
 51	private final ATExpression rcvExp_;
 52	private final ATExpression message_;
 53	
 54	public AGMessageSend(ATExpression rcv, ATExpression msg) {
 55		rcvExp_ = rcv;
 56		message_ = msg;
 57	}
 58	
 59	public ATExpression base_receiverExpression() { return rcvExp_; }
 60
 61	public ATExpression base_messageExpression() { return message_; }
 62
 63	/**
 64	 * To evaluate a message send, evaluate the receiver expression into an object.
 65	 * Next, evaluate the message expression into a first-class message (asynchronous message or method invocation).
 66	 * The evaluation of the message send is delegated to the first-class message itself.
 67	 * 
 68	 * AGMSGSEND(rcv,msg).eval(ctx) = msg.eval(ctx).meta_sendTo(rcv.eval(ctx), ctx.self)
 69	 * 
 70	 * @return the value of the invoked method or NIL in the case of an asynchronous message send.
 71	 */
 72	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 73		ATMessage msg = message_.meta_eval(ctx).asMessage();
 74		ATObject rcvr = rcvExp_.meta_eval(ctx);
 75		ATObject result = null;
 76		InvocationStack stack = InvocationStack.getInvocationStack();
 77		ATTable args = msg.base_arguments();
 78		try {
 79			stack.methodInvoked(this, rcvr, args);
 80			result = msg.base_sendTo(rcvr, ctx.base_receiver());
 81		} finally {
 82			stack.methodReturned(result);
 83		}
 84		return result;
 85	}
 86
 87	/**
 88	 * Quoting a message send returns a new quoted message send.
 89	 * 
 90	 * AGMSGSEND(rcv,msg).quote(ctx) = AGMSGSEND(rcv.quote(ctx), msg.quote(ctx))
 91	 */
 92	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 93		return new AGMessageSend(rcvExp_.meta_quote(ctx).asExpression(),
 94				                 message_.meta_quote(ctx).asExpression());
 95	}
 96	
 97	public NATText meta_print() throws InterpreterException {
 98		return NATText.atValue(rcvExp_.meta_print().javaValue +
 99				               ((message_.isMessageCreation()) ? "" : "<+") + message_.meta_print().javaValue);
100	}
101	
102	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
103		return NATText.atValue(rcvExp_.impl_asUnquotedCode(objectMap).javaValue +
104				               ((message_.isMessageCreation()) ? "" : "<+") + message_.impl_asUnquotedCode(objectMap).javaValue);
105	}
106	
107	/**
108	 * FV(objExp <+ msgExp) = FV(objExp) U FV(msgExp)
109	 */
110	public Set impl_freeVariables() throws InterpreterException {
111        Set fvObjExp = rcvExp_.impl_freeVariables();
112        fvObjExp.addAll(message_.impl_freeVariables());
113        return fvObjExp;
114	}
115	
116	
117	public Set impl_quotedFreeVariables() throws InterpreterException {
118		Set qfv = rcvExp_.impl_quotedFreeVariables();
119		qfv.addAll(message_.impl_quotedFreeVariables());
120		return qfv;
121	}
122
123}