PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist041108/src/edu/vub/at/objects/natives/grammar/AGDefFunction.java

http://ambienttalk.googlecode.com/
Java | 196 lines | 106 code | 25 blank | 65 comment | 7 complexity | c0cf5dbc2c5966a6dd308a3557dd17e4 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGDefFunction.java created on 26-jul-2006 at 15:44:08
  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.objects.ATContext;
 33import edu.vub.at.objects.ATMethod;
 34import edu.vub.at.objects.ATObject;
 35import edu.vub.at.objects.ATTable;
 36import edu.vub.at.objects.ATTypeTag;
 37import edu.vub.at.objects.coercion.NativeTypeTags;
 38import edu.vub.at.objects.grammar.ATBegin;
 39import edu.vub.at.objects.grammar.ATDefMethod;
 40import edu.vub.at.objects.grammar.ATDefinition;
 41import edu.vub.at.objects.grammar.ATExpression;
 42import edu.vub.at.objects.grammar.ATSymbol;
 43import edu.vub.at.objects.natives.NATClosure;
 44import edu.vub.at.objects.natives.NATMethod;
 45import edu.vub.at.objects.natives.NATTable;
 46import edu.vub.at.objects.natives.NATText;
 47
 48import java.util.HashSet;
 49import java.util.Set;
 50
 51/**
 52 * The native implementation of a function definition abstract grammar element.
 53 * This AG element covers both method and closure definition.
 54 * 
 55 * @author tvc
 56 */
 57public final class AGDefFunction extends AGDefinition implements ATDefMethod {
 58
 59	private final ATSymbol selectorExp_;
 60	private final ATTable argumentExps_;
 61	private final ATBegin bodyStmts_;
 62	private final ATExpression	annotationExps_;
 63
 64	private ATMethod preprocessedMethod_;
 65	
 66	public AGDefFunction(ATSymbol sel, ATTable args, ATBegin bdy, ATExpression ann) 
 67			throws InterpreterException {
 68		selectorExp_ = sel;
 69		argumentExps_ = args;
 70		bodyStmts_ = bdy;
 71		annotationExps_ = ann;
 72	}
 73	
 74	public ATSymbol base_selector() {
 75		return selectorExp_;
 76	}
 77
 78	public ATTable base_arguments() {
 79		return argumentExps_;
 80	}
 81
 82	public ATBegin base_bodyExpression() {
 83		return bodyStmts_;
 84	}
 85	
 86	public ATExpression base_annotationExpression() {
 87		return annotationExps_;
 88	}
 89	
 90	/**
 91	 * Defines a new function (method or closure) in the current scope.
 92	 * If a function definition is executed in the context of an object,
 93	 * a new method is created. If, however, the function definition
 94	 * is executed in the context of a call frame (i.e. its definition
 95	 * is nested within a method/function) then a closure is created instead.
 96	 * 
 97	 * The return value of a function definition is always a closure encapsulating the function.
 98	 * 
 99	 * AGDEFFUN(nam,par,bdy).eval(ctx) =
100	 *   if ctx.scope.isCallFrame
101	 *      ctx.scope.defineField(nam, AGCLO(AGMTH(nam,par,bdy), ctx))
102	 *   else
103	 *      ctx.scope.addMethod(nam, AGMTH(nam,par,bdy))
104	 * 
105	 */
106	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
107		// the method is not yet created in the constructor because this gives problems
108		// with quoted parameters: a quoted parameter would result in an illegal parameter
109		// exception while actually the function was defined in the context of a quotation,
110		// so at runtime the function definition would have never been evaluated (but quoted instead)
111		if (preprocessedMethod_ == null) {
112			ATObject oneOrMoreAnnotation = annotationExps_.meta_eval(ctx);
113			ATTable  annotationTable;
114			
115			if(oneOrMoreAnnotation.isTable()) {
116				annotationTable = oneOrMoreAnnotation.asTable();
117			} else {
118				annotationTable = NATTable.of(oneOrMoreAnnotation);
119			}
120			
121			preprocessedMethod_ = new NATMethod(selectorExp_, argumentExps_, bodyStmts_, annotationTable);
122			
123			ATObject[] annotations = annotationTable.asNativeTable().elements_;
124			
125			for (int i = 0; i < annotations.length; i++) {
126				ATTypeTag theAnnotation = annotations[i].asTypeTag();
127				
128				preprocessedMethod_ = theAnnotation.base_annotateMethod(preprocessedMethod_);
129			}
130		}
131		
132		ATObject current = ctx.base_lexicalScope();
133		if (current.isCallFrame()) {
134			NATClosure clo = new NATClosure(preprocessedMethod_, ctx);
135			current.meta_defineField(selectorExp_, clo);
136			return clo;
137		} else {
138			current.meta_addMethod(preprocessedMethod_);
139			return current.meta_select(current, selectorExp_);
140		}
141	}
142
143	/**
144	 * Quoting a function definition results in a new quoted function definition.
145	 * 
146	 * AGDEFFUN(nam,par,bdy).quote(ctx) = AGDEFFUN(nam.quote(ctx), par.quote(ctx), bdy.quote(ctx))
147	 */
148	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
149		return new AGDefFunction(selectorExp_.meta_quote(ctx).asSymbol(),
150				                 argumentExps_.meta_quote(ctx).asTable(),
151				                 bodyStmts_.meta_quote(ctx).asBegin(),
152				                 annotationExps_.meta_quote(ctx).asExpression());
153	}
154	
155	public NATText meta_print() throws InterpreterException {
156		return NATText.atValue("def " +
157				selectorExp_.meta_print().javaValue +
158				Evaluator.printAsList(argumentExps_).javaValue +
159				" { " + bodyStmts_.meta_print().javaValue + " }");
160	}
161	
162    public ATTable meta_typeTags() throws InterpreterException {
163    	return NATTable.of(NativeTypeTags._METHOD_DEFINITION_, NativeTypeTags._ISOLATE_);
164    }
165    
166	/**
167	 * IV(def f(args) @ annotations { body }) = { f }
168	 */
169	public Set impl_introducedVariables() throws InterpreterException {
170		Set singleton = new HashSet();
171		singleton.add(selectorExp_);
172		return singleton;
173	}
174
175	/**
176	 * FV(def f(args) @ annotations { body })
177	 *   = FV(annotations) U FV(optionalArgExps) U (FV(body) \ { args } \ { f })
178	 */
179	public Set impl_freeVariables() throws InterpreterException {
180		Set fvBody = bodyStmts_.impl_freeVariables();
181		fvBody.remove(selectorExp_);
182		Evaluator.processFreeVariables(fvBody, argumentExps_);
183		fvBody.addAll(annotationExps_.impl_freeVariables());
184		return fvBody;
185	}
186	
187	
188	public Set impl_quotedFreeVariables() throws InterpreterException {
189		Set qfv = selectorExp_.impl_quotedFreeVariables();
190		qfv.addAll(argumentExps_.impl_quotedFreeVariables());
191		qfv.addAll(bodyStmts_.impl_quotedFreeVariables());
192		qfv.addAll(annotationExps_.impl_quotedFreeVariables());
193		return qfv;
194	}
195
196}