PageRenderTime 31ms CodeModel.GetById 6ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/natives/grammar/AGDefExternalMethod.java

http://ambienttalk.googlecode.com/
Java | 164 lines | 89 code | 22 blank | 53 comment | 7 complexity | 9d363e244c80e20ddac44b92407d1d7e MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGDefExternalMethod.java created on 15-nov-2006 at 19:33:18
  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.XIllegalOperation;
 33import edu.vub.at.objects.ATContext;
 34import edu.vub.at.objects.ATMethod;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.ATTable;
 37import edu.vub.at.objects.ATTypeTag;
 38import edu.vub.at.objects.coercion.NativeTypeTags;
 39import edu.vub.at.objects.grammar.ATBegin;
 40import edu.vub.at.objects.grammar.ATDefExternalMethod;
 41import edu.vub.at.objects.grammar.ATExpression;
 42import edu.vub.at.objects.grammar.ATSymbol;
 43import edu.vub.at.objects.natives.NATClosureMethod;
 44import edu.vub.at.objects.natives.NATMethod;
 45import edu.vub.at.objects.natives.NATTable;
 46import edu.vub.at.objects.natives.NATText;
 47
 48/**
 49 * The native implementation of an external method definition abstract grammar element.
 50 * 
 51 * @author tvcutsem
 52 */
 53public final class AGDefExternalMethod extends NATAbstractGrammar implements ATDefExternalMethod {
 54
 55	private final ATSymbol		rcvNam_;
 56	private final ATSymbol		selectorExp_;
 57	private final ATTable		argumentExps_;
 58	private final ATBegin		bodyStmts_;
 59	private final ATExpression	annotationExps_;
 60
 61	private ATMethod preprocessedMethod_;
 62	
 63	public AGDefExternalMethod(ATSymbol rcv, ATSymbol sel, ATTable args, ATBegin bdy, ATExpression ann)
 64	       throws InterpreterException {
 65		rcvNam_ = rcv;
 66		selectorExp_ = sel;
 67		argumentExps_ = args;
 68		bodyStmts_ = bdy;
 69		annotationExps_ = ann;
 70	}
 71
 72	public ATSymbol base_receiver() {
 73		return rcvNam_;
 74	}
 75	
 76	public ATSymbol base_selector() {
 77		return selectorExp_;
 78	}
 79
 80	public ATTable base_arguments() {
 81		return argumentExps_;
 82	}
 83
 84	public ATBegin base_bodyExpression() {
 85		return bodyStmts_;
 86	}
 87	
 88	public ATExpression base_annotationExpression() {
 89		return annotationExps_;
 90	}
 91	
 92	/**
 93	 * Evaluates the receiver symbol to an object to which a new 'closure method' will be added.
 94	 * Such a closure captures the current lexical scope, but not the values for 'self' and 'super'.
 95	 * 
 96	 * The return value of an external method definition is always the external method itself.
 97	 * 
 98	 * AGDEFEXTMTH(rcv,nam,par,bdy).eval(ctx) =
 99	 *   rcv.eval(ctx).defineField(nam, NATCLOMTH(ctx.cur,nam,par,bdy))
100	 *   
101	 * @throws XIllegalOperation if the receiver is an instance of a native 
102	 * type (whose method dictionaries are sealed) or if the receiver is an isolate. 
103	 */
104	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
105		// the method is not yet created in the constructor because this gives problems
106		// with quoted parameters: a quoted parameter would result in an illegal parameter
107		// exception while actually the external method was defined in the context of a quotation,
108		// so at runtime the external definition would have never been evaluated (but quoted instead)
109		if (preprocessedMethod_ == null) {
110			ATObject oneOrMoreAnnotation = annotationExps_.meta_eval(ctx);
111			ATTable  annotationTable;
112			
113			if(oneOrMoreAnnotation.isTable()) {
114				annotationTable = oneOrMoreAnnotation.asTable();
115			} else {
116				annotationTable = NATTable.of(oneOrMoreAnnotation);
117			}
118			
119			preprocessedMethod_ = new NATMethod(selectorExp_, argumentExps_, bodyStmts_, annotationTable);
120
121			ATObject[] annotations = annotationTable.asNativeTable().elements_;
122			
123			for (int i = 0; i < annotations.length; i++) {
124				ATTypeTag theAnnotation = annotations[i].asTypeTag();
125				
126				preprocessedMethod_ = theAnnotation.base_annotateMethod(preprocessedMethod_);
127			}
128		}
129		
130		ATObject receiver = rcvNam_.meta_eval(ctx);
131		if (receiver.meta_isTaggedAs(NativeTypeTags._ISOLATE_).asNativeBoolean().javaValue) {
132			throw new XIllegalOperation("Cannot define external methods on isolates");
133			
134		} else {
135			NATClosureMethod extMethod = new NATClosureMethod(ctx.base_lexicalScope(),
136                    preprocessedMethod_);
137
138			receiver.meta_addMethod(extMethod);
139			return extMethod;
140		}
141	}
142
143	/**
144	 * Quoting an external method definition results in a new quoted external method definition.
145	 * 
146	 * AGDEFEXTMTH(rcv,nam,par,bdy).quote(ctx) = AGDEFEXTMTH(rcv.quote(ctx),nam.quote(ctx), par.quote(ctx), bdy.quote(ctx))
147	 */
148	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
149		return new AGDefExternalMethod(rcvNam_.meta_quote(ctx).asSymbol(),
150				               selectorExp_.meta_quote(ctx).asSymbol(),
151				               argumentExps_.meta_quote(ctx).asTable(),
152				               bodyStmts_.meta_quote(ctx).asBegin(),
153				               annotationExps_.meta_quote(ctx).asExpression());
154	}
155	
156	public NATText meta_print() throws InterpreterException {
157		return NATText.atValue("def " +
158				rcvNam_.meta_print().javaValue + "." +
159				selectorExp_.meta_print().javaValue +
160				Evaluator.printAsList(argumentExps_).javaValue +
161				" { " + bodyStmts_.meta_print().javaValue + " }");
162	}
163
164}