PageRenderTime 25ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist090708/src/edu/vub/at/objects/natives/grammar/AGMessageCreation.java

http://ambienttalk.googlecode.com/
Java | 138 lines | 60 code | 16 blank | 62 comment | 3 complexity | 3a278ffa524d64fe5e3e4650c77d73e7 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGMessageCreation.java created on 10-aug-2006 at 14:10:17
  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.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XTypeMismatch;
 33import edu.vub.at.objects.ATContext;
 34import edu.vub.at.objects.ATMessage;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.ATTable;
 37import edu.vub.at.objects.grammar.ATExpression;
 38import edu.vub.at.objects.grammar.ATMessageCreation;
 39import edu.vub.at.objects.grammar.ATSymbol;
 40import edu.vub.at.objects.natives.NATTable;
 41import edu.vub.at.objects.natives.NATText;
 42
 43/**
 44 * The common superclass of AGAsyncMessageCreation and AGMethodInvocationCreation.
 45 * This class serves as a common repository for both kinds of first-class message AG elements.
 46 * 
 47 * @author tvcutsem
 48 */
 49public abstract class AGMessageCreation extends AGExpression implements ATMessageCreation {
 50
 51	private final ATSymbol selector_;
 52	private final ATTable arguments_;
 53	private final ATExpression annotations_;
 54	
 55	public AGMessageCreation(ATSymbol sel, ATTable args, ATExpression annotations) {
 56		selector_ = sel;
 57		arguments_ = args;
 58		annotations_ = annotations;
 59	}
 60	
 61	public ATSymbol base_selector() { return selector_; }
 62
 63	public ATTable base_arguments() { return arguments_; }
 64
 65	public ATExpression base_annotations() { return annotations_; }
 66	
 67	public boolean isMessageCreation() {
 68   	    return true;
 69    }
 70	
 71	public ATMessageCreation asMessageCreation() throws XTypeMismatch {
 72		return this;
 73	}
 74	
 75	/**
 76	 * To evaluate a message send, transform the selector
 77	 * and evaluated arguments into a first-class Message object.
 78	 * It is important to note that the arguments are all eagerly evaluated.
 79	 * 
 80	 * The annotation to the message send is either included in the generated message
 81	 * as a table of type tags, if it evaluates to a table, or as a one-sized table of the
 82	 * evaluated annotation. This means that <code>o.m(x)@y</code> is
 83	 * evaluated as <code>o.m(x)@[y]</code>, for example.
 84	 * 
 85	 * AGMSG(sel,arg,ann).eval(ctx) = NATMSG(sel, map eval(ctx) over arg, (a:ann.eval(ctx).isTable? ? a : [a]))
 86	 * 
 87	 * @return a first-class method invocation
 88	 */
 89	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 90		NATTable evaluatedArgs = Evaluator.evaluateArguments(arguments_.asNativeTable(), ctx);
 91		ATObject annotations = annotations_.meta_eval(ctx);
 92		return this.createMessage(ctx,
 93				                  selector_,
 94				                  evaluatedArgs,
 95				                  (annotations.isTable()) ? annotations.asTable() : NATTable.of(annotations));
 96	}
 97	
 98	/**
 99	 * Quoting a message creation element returns a new quoted message creation element.
100	 */
101	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
102		return this.newQuoted(selector_.meta_quote(ctx).asSymbol(),
103				              arguments_.meta_quote(ctx).asTable(),
104				              annotations_.meta_quote(ctx).asExpression());
105	}
106	
107	public NATText meta_print() throws InterpreterException {
108		if (annotations_ == NATTable.EMPTY) {
109			return NATText.atValue(this.getMessageToken() +
110		               selector_.meta_print().javaValue +
111		               Evaluator.printAsList(arguments_).javaValue);
112		} else {
113			return NATText.atValue(this.getMessageToken() +
114		               selector_.meta_print().javaValue +
115		               Evaluator.printAsList(arguments_).javaValue +
116		               "@" + annotations_.meta_print().javaValue);
117		}
118	}
119	
120	/**
121	 * Subclasses must implement this method in order to return a new instance of themselves
122	 * parameterized with their quoted arguments.
123	 */
124	protected abstract ATObject newQuoted(ATSymbol quotedSel, ATTable quotedArgs, ATExpression quotedAnnotations);
125	
126	/**
127	 * Subclasses must implement this method such that the correct messaging token
128	 * can be displayed when printing an AGMessageCreation element.
129	 */
130	protected abstract String getMessageToken();
131	
132	/**
133	 * Subclasses must implement this method such that the correct kind of message is created
134	 * for a given selector, evaluated arguments and annotation
135	 */
136	protected abstract ATMessage createMessage(ATContext ctx, ATSymbol selector, ATTable evaluatedArgs, ATTable annotations) throws InterpreterException;
137	
138}