PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 162 lines | 84 code | 20 blank | 58 comment | 7 complexity | f898d354b8eee3622c66f339b27b1bcd 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.ATExpression;
 41import edu.vub.at.objects.grammar.ATSymbol;
 42import edu.vub.at.objects.natives.NATClosure;
 43import edu.vub.at.objects.natives.NATMethod;
 44import edu.vub.at.objects.natives.NATTable;
 45import edu.vub.at.objects.natives.NATText;
 46
 47/**
 48 * The native implementation of a function definition abstract grammar element.
 49 * This AG element covers both method and closure definition.
 50 * 
 51 * @author tvc
 52 */
 53public final class AGDefFunction extends NATAbstractGrammar implements ATDefMethod {
 54
 55	private final ATSymbol selectorExp_;
 56	private final ATTable argumentExps_;
 57	private final ATBegin bodyStmts_;
 58	private final ATExpression	annotationExps_;
 59
 60	private ATMethod preprocessedMethod_;
 61	
 62	public AGDefFunction(ATSymbol sel, ATTable args, ATBegin bdy, ATExpression ann) 
 63			throws InterpreterException {
 64		selectorExp_ = sel;
 65		argumentExps_ = args;
 66		bodyStmts_ = bdy;
 67		annotationExps_ = ann;
 68	}
 69	
 70	public ATSymbol base_selector() {
 71		return selectorExp_;
 72	}
 73
 74	public ATTable base_arguments() {
 75		return argumentExps_;
 76	}
 77
 78	public ATBegin base_bodyExpression() {
 79		return bodyStmts_;
 80	}
 81	
 82	public ATExpression base_annotationExpression() {
 83		return annotationExps_;
 84	}
 85	
 86	/**
 87	 * Defines a new function (method or closure) in the current scope.
 88	 * If a function definition is executed in the context of an object,
 89	 * a new method is created. If, however, the function definition
 90	 * is executed in the context of a call frame (i.e. its definition
 91	 * is nested within a method/function) then a closure is created instead.
 92	 * 
 93	 * The return value of a function definition is always a closure encapsulating the function.
 94	 * 
 95	 * AGDEFFUN(nam,par,bdy).eval(ctx) =
 96	 *   if ctx.scope.isCallFrame
 97	 *      ctx.scope.defineField(nam, AGCLO(AGMTH(nam,par,bdy), ctx))
 98	 *   else
 99	 *      ctx.scope.addMethod(nam, AGMTH(nam,par,bdy))
100	 * 
101	 */
102	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
103		// the method is not yet created in the constructor because this gives problems
104		// with quoted parameters: a quoted parameter would result in an illegal parameter
105		// exception while actually the function was defined in the context of a quotation,
106		// so at runtime the function definition would have never been evaluated (but quoted instead)
107		if (preprocessedMethod_ == null) {
108			ATObject oneOrMoreAnnotation = annotationExps_.meta_eval(ctx);
109			ATTable  annotationTable;
110			
111			if(oneOrMoreAnnotation.isTable()) {
112				annotationTable = oneOrMoreAnnotation.asTable();
113			} else {
114				annotationTable = NATTable.of(oneOrMoreAnnotation);
115			}
116			
117			preprocessedMethod_ = new NATMethod(selectorExp_, argumentExps_, bodyStmts_, annotationTable);
118			
119			ATObject[] annotations = annotationTable.asNativeTable().elements_;
120			
121			for (int i = 0; i < annotations.length; i++) {
122				ATTypeTag theAnnotation = annotations[i].asTypeTag();
123				
124				preprocessedMethod_ = theAnnotation.base_annotateMethod(preprocessedMethod_);
125			}
126		}
127		
128		ATObject current = ctx.base_lexicalScope();
129		if (current.isCallFrame()) {
130			NATClosure clo = new NATClosure(preprocessedMethod_, ctx);
131			current.meta_defineField(selectorExp_, clo);
132			return clo;
133		} else {
134			current.meta_addMethod(preprocessedMethod_);
135			return current.meta_select(current, selectorExp_);
136		}
137	}
138
139	/**
140	 * Quoting a function definition results in a new quoted function definition.
141	 * 
142	 * AGDEFFUN(nam,par,bdy).quote(ctx) = AGDEFFUN(nam.quote(ctx), par.quote(ctx), bdy.quote(ctx))
143	 */
144	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
145		return new AGDefFunction(selectorExp_.meta_quote(ctx).asSymbol(),
146				                 argumentExps_.meta_quote(ctx).asTable(),
147				                 bodyStmts_.meta_quote(ctx).asBegin(),
148				                 annotationExps_.meta_quote(ctx).asExpression());
149	}
150	
151	public NATText meta_print() throws InterpreterException {
152		return NATText.atValue("def " +
153				selectorExp_.meta_print().javaValue +
154				Evaluator.printAsList(argumentExps_).javaValue +
155				" { " + bodyStmts_.meta_print().javaValue + " }");
156	}
157	
158    public ATTable meta_typeTags() throws InterpreterException {
159    	return NATTable.of(NativeTypeTags._METHOD_DEFINITION_, NativeTypeTags._ISOLATE_);
160    }
161
162}