PageRenderTime 39ms CodeModel.GetById 30ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2-build190607/src/edu/vub/at/objects/natives/grammar/AGApplication.java

http://ambienttalk.googlecode.com/
Java | 98 lines | 42 code | 10 blank | 46 comment | 0 complexity | 8718b6372a78278396d9784a5c1c510e 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
42/**
43 * The native implementation of an application AG element.
44 * 
45 * @author tvc
46 */
47public final class AGApplication extends AGExpression implements ATApplication {
48
49	private final ATExpression funExp_;
50	private final ATTable arguments_;
51	
52	public AGApplication(ATExpression fun, ATTable arg) {
53		funExp_ = fun;
54		arguments_ = arg;
55	}
56	
57	public ATExpression base_getFunction() { return funExp_; }
58
59	public ATTable base_getArguments() { return arguments_; }
60
61	/**
62	 * To evaluate a function application, evaluate the receiver expression to a function, then evaluate the arguments
63	 * to the function application eagerly and apply the function.
64	 * 
65	 * AGAPL(fun,arg).eval(ctx) = fun.eval(ctx).apply(map eval(ctx) over arg)
66	 * 
67	 * @return the return value of the applied function.
68	 */
69	public ATObject meta_eval(ATContext ctx) throws InterpreterException {
70		ATClosure clo = funExp_.meta_eval(ctx).asClosure();
71		NATTable args = Evaluator.evaluateArguments(arguments_.asNativeTable(), ctx);
72		ATObject result = null;
73		InvocationStack stack = InvocationStack.getInvocationStack();
74		try {
75			stack.functionCalled(this, clo, args);
76			result = clo.base_apply(args);
77		} finally {
78			stack.funcallReturned(result);
79		}
80		return result;
81		//return clo.base_apply(Evaluator.evaluateArguments(arguments_.asNativeTable(), ctx));
82	}
83
84	/**
85	 * Quoting an application results in a new quoted application.
86	 * 
87	 * AGAPL(sel,arg).quote(ctx) = AGAPL(sel.quote(ctx), arg.quote(ctx))
88	 */
89	public ATObject meta_quote(ATContext ctx) throws InterpreterException {
90		return new AGApplication(funExp_.meta_quote(ctx).asExpression(),
91				                arguments_.meta_quote(ctx).asTable());
92	}
93	
94	public NATText meta_print() throws InterpreterException {
95		return NATText.atValue(funExp_.meta_print().javaValue + Evaluator.printAsList(arguments_).javaValue);
96	}
97
98}