PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/objects/natives/grammar/AGDefExternalMethod.java

http://ambienttalk.googlecode.com/
Java | 207 lines | 120 code | 27 blank | 60 comment | 7 complexity | bb17fb6c77cc75d718bc1784fcea641e 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.ATDefinition;
 42import edu.vub.at.objects.grammar.ATExpression;
 43import edu.vub.at.objects.grammar.ATSymbol;
 44import edu.vub.at.objects.natives.NATClosureMethod;
 45import edu.vub.at.objects.natives.NATMethod;
 46import edu.vub.at.objects.natives.NATTable;
 47import edu.vub.at.objects.natives.NATText;
 48import edu.vub.util.TempFieldGenerator;
 49
 50import java.util.HashSet;
 51import java.util.Set;
 52
 53/**
 54 * The native implementation of an external method definition abstract grammar element.
 55 * 
 56 * @author tvcutsem
 57 */
 58public final class AGDefExternalMethod extends AGDefinition implements ATDefExternalMethod {
 59
 60	private final ATSymbol		rcvNam_;
 61	private final ATSymbol		selectorExp_;
 62	private final ATTable		argumentExps_;
 63	private final ATBegin		bodyStmts_;
 64	private final ATExpression	annotationExps_;
 65
 66	private ATMethod preprocessedMethod_;
 67	
 68	public AGDefExternalMethod(ATSymbol rcv, ATSymbol sel, ATTable args, ATBegin bdy, ATExpression ann)
 69	       throws InterpreterException {
 70		rcvNam_ = rcv;
 71		selectorExp_ = sel;
 72		argumentExps_ = args;
 73		bodyStmts_ = bdy;
 74		annotationExps_ = ann;
 75	}
 76
 77	public ATSymbol base_receiver() {
 78		return rcvNam_;
 79	}
 80	
 81	public ATSymbol base_selector() {
 82		return selectorExp_;
 83	}
 84
 85	public ATTable base_arguments() {
 86		return argumentExps_;
 87	}
 88
 89	public ATBegin base_bodyExpression() {
 90		return bodyStmts_;
 91	}
 92	
 93	public ATExpression base_annotationExpression() {
 94		return annotationExps_;
 95	}
 96	
 97	/**
 98	 * Evaluates the receiver symbol to an object to which a new 'closure method' will be added.
 99	 * Such a closure captures the current lexical scope, but not the values for 'self' and 'super'.
100	 * 
101	 * The return value of an external method definition is always the external method itself.
102	 * 
103	 * AGDEFEXTMTH(rcv,nam,par,bdy).eval(ctx) =
104	 *   rcv.eval(ctx).defineField(nam, NATCLOMTH(ctx.cur,nam,par,bdy))
105	 *   
106	 * @throws XIllegalOperation if the receiver is an instance of a native 
107	 * type (whose method dictionaries are sealed) or if the receiver is an isolate. 
108	 */
109	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
110		// the method is not yet created in the constructor because this gives problems
111		// with quoted parameters: a quoted parameter would result in an illegal parameter
112		// exception while actually the external method was defined in the context of a quotation,
113		// so at runtime the external definition would have never been evaluated (but quoted instead)
114		if (preprocessedMethod_ == null) {
115			ATObject oneOrMoreAnnotation = annotationExps_.meta_eval(ctx);
116			ATTable  annotationTable;
117			
118			if(oneOrMoreAnnotation.isTable()) {
119				annotationTable = oneOrMoreAnnotation.asTable();
120			} else {
121				annotationTable = NATTable.of(oneOrMoreAnnotation);
122			}
123			
124			NATMethod preprocessedMethod = new NATMethod(selectorExp_, argumentExps_, bodyStmts_, annotationTable);
125			preprocessedMethod.impl_setLocation(this.impl_getLocation());
126			preprocessedMethod_ = preprocessedMethod;
127
128			ATObject[] annotations = annotationTable.asNativeTable().elements_;
129			
130			for (int i = 0; i < annotations.length; i++) {
131				ATTypeTag theAnnotation = annotations[i].asTypeTag();
132				
133				preprocessedMethod_ = theAnnotation.base_annotateMethod(preprocessedMethod_);
134			}
135		}
136		
137		ATObject receiver = rcvNam_.meta_eval(ctx);
138		if (receiver.meta_isTaggedAs(NativeTypeTags._ISOLATE_).asNativeBoolean().javaValue) {
139			throw new XIllegalOperation("Cannot define external methods on isolates");
140			
141		} else {
142			NATClosureMethod extMethod = new NATClosureMethod(ctx.base_lexicalScope(),
143                    preprocessedMethod_);
144
145			receiver.meta_addMethod(extMethod);
146			return extMethod;
147		}
148	}
149
150	/**
151	 * Quoting an external method definition results in a new quoted external method definition.
152	 * 
153	 * AGDEFEXTMTH(rcv,nam,par,bdy).quote(ctx) = AGDEFEXTMTH(rcv.quote(ctx),nam.quote(ctx), par.quote(ctx), bdy.quote(ctx))
154	 */
155	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
156		return new AGDefExternalMethod(rcvNam_.meta_quote(ctx).asSymbol(),
157				               selectorExp_.meta_quote(ctx).asSymbol(),
158				               argumentExps_.meta_quote(ctx).asTable(),
159				               bodyStmts_.meta_quote(ctx).asBegin(),
160				               annotationExps_.meta_quote(ctx).asExpression());
161	}
162	
163	public NATText meta_print() throws InterpreterException {
164		return NATText.atValue("def " +
165				rcvNam_.meta_print().javaValue + "." +
166				selectorExp_.meta_print().javaValue +
167				Evaluator.printAsList(argumentExps_).javaValue +
168				" { " + bodyStmts_.meta_print().javaValue + " }");
169	}
170	
171	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
172		return NATText.atValue("def " +
173				rcvNam_.impl_asUnquotedCode(objectMap).javaValue + "." +
174				selectorExp_.impl_asUnquotedCode(objectMap).javaValue +
175				Evaluator.codeAsList(objectMap, argumentExps_).javaValue +
176				" { " + bodyStmts_.impl_asUnquotedCode(objectMap).javaValue + " }");
177	}
178
179	/**
180	 * IV(def o.m(args) @ anns { body }) = { }
181	 */
182	public Set impl_introducedVariables() throws InterpreterException {
183		return new HashSet();
184	}
185
186	/**
187	 * FV(def o.m(args) @ anns { body }) =
188	 *   { o } U FV(anns) U FV(optionalArgExps) U (FV(body) \ { args }) 
189	 */
190	public Set impl_freeVariables() throws InterpreterException {
191		Set fvBody = bodyStmts_.impl_freeVariables();
192		Evaluator.processFreeVariables(fvBody, argumentExps_);
193		fvBody.add(rcvNam_);
194		fvBody.addAll(annotationExps_.impl_freeVariables());
195		return fvBody;
196	}
197	
198	public Set impl_quotedFreeVariables() throws InterpreterException {
199		Set qfv = argumentExps_.impl_quotedFreeVariables();
200		qfv.addAll(bodyStmts_.impl_quotedFreeVariables());
201		qfv.addAll(annotationExps_.impl_quotedFreeVariables());
202		qfv.addAll(rcvNam_.impl_quotedFreeVariables());
203		qfv.addAll(selectorExp_.impl_quotedFreeVariables());
204		return qfv;
205	}
206	
207}