PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

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