PageRenderTime 38ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 134 lines | 70 code | 14 blank | 50 comment | 2 complexity | 298bd9094bbd77c21b957aaca6a0fb2a MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * AGApplication.java created on 26-jul-2006 at 16:13:31
  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.eval.InvocationStack;
 32import edu.vub.at.exceptions.InterpreterException;
 33import edu.vub.at.objects.ATClosure;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.ATTable;
 37import edu.vub.at.objects.grammar.ATApplication;
 38import edu.vub.at.objects.grammar.ATExpression;
 39import edu.vub.at.objects.natives.NATTable;
 40import edu.vub.at.objects.natives.NATText;
 41import edu.vub.util.TempFieldGenerator;
 42
 43import java.util.Set;
 44
 45/**
 46 * The native implementation of an application AG element.
 47 * 
 48 * @author tvcutsem
 49 */
 50public final class AGApplication extends AGExpression implements ATApplication {
 51
 52	private final ATExpression funExp_;
 53	private final ATTable arguments_;
 54	
 55	public AGApplication(ATExpression fun, ATTable arg) {
 56		funExp_ = fun;
 57		arguments_ = arg;
 58	}
 59	
 60	public ATExpression base_function() { return funExp_; }
 61
 62	public ATTable base_arguments() { return arguments_; }
 63
 64	/**
 65	 * To evaluate a function application, evaluate the receiver expression to a function, then evaluate the arguments
 66	 * to the function application eagerly and apply the function.
 67	 * 
 68	 * AGAPL(fun,arg).eval(ctx) = fun.eval(ctx).apply(map eval(ctx) over arg)
 69	 * 
 70	 * @return the return value of the applied function.
 71	 */
 72	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
 73		// calls of the form f(x) are transformed into ctx.lex.call(`f, [x])
 74		if(funExp_.isSymbol()) {
 75			NATTable args = Evaluator.evaluateArguments(arguments_.asNativeTable(), ctx);
 76			ATObject result = null;
 77			InvocationStack stack = InvocationStack.getInvocationStack();
 78			try {
 79				stack.functionCalled(this, null, args);
 80				result = ctx.base_lexicalScope().impl_callAccessor(funExp_.asSymbol(), args);
 81			} finally {
 82				stack.funcallReturned(result);
 83			}
 84			return result;
 85		// calls of the form (expr)(x) are transformed into expr.eval(ctx).apply([x])
 86		} else {
 87			ATClosure clo = funExp_.meta_eval(ctx).asClosure();
 88			NATTable args = Evaluator.evaluateArguments(arguments_.asNativeTable(), ctx);
 89			ATObject result = null;
 90			InvocationStack stack = InvocationStack.getInvocationStack();
 91			try {
 92				stack.functionCalled(this, clo, args);
 93				result = clo.base_apply(args);
 94			} finally {
 95				stack.funcallReturned(result);
 96			}
 97			return result;
 98		}
 99	}
100
101	/**
102	 * Quoting an application results in a new quoted application.
103	 * 
104	 * AGAPL(sel,arg).quote(ctx) = AGAPL(sel.quote(ctx), arg.quote(ctx))
105	 */
106	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
107		return new AGApplication(funExp_.meta_quote(ctx).asExpression(),
108				                arguments_.meta_quote(ctx).asTable());
109	}
110	
111	public NATText meta_print() throws InterpreterException {
112		return NATText.atValue(funExp_.meta_print().javaValue + Evaluator.printAsList(arguments_).javaValue);
113	}
114	
115	public NATText impl_asUnquotedCode(TempFieldGenerator objectMap) throws InterpreterException {
116		return NATText.atValue(funExp_.impl_asUnquotedCode(objectMap).javaValue + Evaluator.codeAsList(objectMap, arguments_).javaValue);
117	}
118	
119	/**
120	 * FV(fExp(args)) = FV(fExp) U FV(args)
121	 */
122	public Set impl_freeVariables() throws InterpreterException {
123		Set fvFunExp = funExp_.impl_freeVariables();
124		fvFunExp.addAll(arguments_.impl_freeVariables());
125		return fvFunExp;
126	}
127
128	public Set impl_quotedFreeVariables() throws InterpreterException {
129		Set fvFunExp = funExp_.impl_quotedFreeVariables();
130		fvFunExp.addAll(arguments_.impl_quotedFreeVariables());
131		return fvFunExp;
132	}
133
134}