/interpreter/tags/at2-build270707/src/edu/vub/at/objects/natives/grammar/AGMessageSend.java
Java | 103 lines | 46 code | 11 blank | 46 comment | 0 complexity | 183dc7a8d40a0b76984d1ff623a9c96e 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.ATNil; 35import edu.vub.at.objects.ATObject; 36import edu.vub.at.objects.grammar.ATExpression; 37import edu.vub.at.objects.grammar.ATMessageSend; 38import edu.vub.at.objects.natives.OBJNil; 39import edu.vub.at.objects.natives.NATText; 40 41/** 42 * @author tvc 43 * 44 * The native implementation of a synchronous message send AG element. 45 */ 46public final class AGMessageSend extends AGExpression implements ATMessageSend { 47 48 private ATExpression rcvExp_; 49 private final ATExpression message_; 50 51 public AGMessageSend(ATExpression rcv, ATExpression msg) { 52 rcvExp_ = rcv; 53 message_ = msg; 54 } 55 56 public ATExpression base_receiverExpression() { return rcvExp_; } 57 58 public ATNil base_receiverExpression__opeql_(ATExpression rcv) { 59 rcvExp_ = rcv; 60 return OBJNil._INSTANCE_; 61 } 62 63 public ATExpression base_messageExpression() { return message_; } 64 65 /** 66 * To evaluate a message send, evaluate the receiver expression into an object. 67 * Next, evaluate the message expression into a first-class message (asynchronous message or method invocation). 68 * The evaluation of the message send is delegated to the first-class message itself. 69 * 70 * AGMSGSEND(rcv,msg).eval(ctx) = msg.eval(ctx).meta_sendTo(rcv.eval(ctx), ctx.self) 71 * 72 * @return the value of the invoked method or NIL in the case of an asynchronous message send. 73 */ 74 public ATObject meta_eval(ATContext ctx) throws InterpreterException { 75 ATMessage msg = message_.meta_eval(ctx).asMessage(); 76 ATObject rcvr = rcvExp_.meta_eval(ctx); 77 ATObject result = null; 78 InvocationStack stack = InvocationStack.getInvocationStack(); 79 try { 80 stack.methodInvoked(this, rcvr, msg.base_arguments()); 81 result = msg.base_sendTo(rcvr, ctx.base_self()); 82 } finally { 83 stack.methodReturned(result); 84 } 85 return result; 86 } 87 88 /** 89 * Quoting a message send returns a new quoted message send. 90 * 91 * AGMSGSEND(rcv,msg).quote(ctx) = AGMSGSEND(rcv.quote(ctx), msg.quote(ctx)) 92 */ 93 public ATObject meta_quote(ATContext ctx) throws InterpreterException { 94 return new AGMessageSend(rcvExp_.meta_quote(ctx).asExpression(), 95 message_.meta_quote(ctx).asMessageCreation()); 96 } 97 98 public NATText meta_print() throws InterpreterException { 99 return NATText.atValue(rcvExp_.meta_print().javaValue + 100 ((message_.isMessageCreation()) ? "" : "<+") + message_.meta_print().javaValue); 101 } 102 103}