PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

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