PageRenderTime 40ms CodeModel.GetById 13ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist170907/src/edu/vub/at/objects/natives/NATMethod.java

http://ambienttalk.googlecode.com/
Java | 160 lines | 74 code | 18 blank | 68 comment | 0 complexity | 1155dfe52875bc5a44fb2622a90f70da MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATMethod.java created on Jul 24, 2006 at 11:30:35 PM
  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;
 29
 30import edu.vub.at.eval.PartialBinder;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XTypeMismatch;
 33import edu.vub.at.objects.ATClosure;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATMethod;
 36import edu.vub.at.objects.ATObject;
 37import edu.vub.at.objects.ATTable;
 38import edu.vub.at.objects.coercion.NativeTypeTags;
 39import edu.vub.at.objects.grammar.ATBegin;
 40import edu.vub.at.objects.grammar.ATSymbol;
 41import edu.vub.at.objects.mirrors.PrimitiveMethod;
 42import edu.vub.at.util.logging.Logging;
 43
 44/**
 45 * NATMethod implements methods as named functions which are in fact simply containers
 46 * for a name, a table of arguments and a body.
 47 * 
 48 * @author smostinc
 49 * @author tvcutsem
 50 */
 51public class NATMethod extends NATByCopy implements ATMethod {
 52
 53	private final ATSymbol 	name_;
 54	private final ATTable 	parameters_;
 55	private final ATBegin	body_;
 56	
 57	// partial function denoting a parameter binding algorithm specialized for this method's parameter list
 58	private final PartialBinder parameterBindingFunction_;
 59	
 60	/** construct a new method. This method may raise an exception if the parameter list is illegal. */
 61	public NATMethod(ATSymbol name, ATTable parameters, ATBegin body) throws InterpreterException {
 62		name_ 		= name;
 63		parameters_ = parameters;
 64		body_ 		= body;
 65		
 66		// calculate the parameter binding strategy to use using partial evaluation
 67		parameterBindingFunction_ =
 68			PartialBinder.calculateResidual(name_.base_text().asNativeText().javaValue, parameters);
 69	}
 70	
 71	/**
 72	 * Constructor to be used by primitive methods only.
 73	 */
 74	protected NATMethod(ATSymbol name, ATTable parameters, PrimitiveMethod.PrimitiveBody body) {
 75		name_ 		= name;
 76		parameters_ = parameters;
 77		body_ 		= body;
 78		PartialBinder parameterBindingFunction;
 79		try {
 80			// calculate the parameter binding strategy to use using partial evaluation
 81			parameterBindingFunction = PartialBinder.calculateResidual(name_.base_text().asNativeText().javaValue, parameters);
 82		} catch (InterpreterException e) {
 83			parameterBindingFunction = null;
 84			// this indicates a bug, primitive methods should not contain erroneous parameter lists
 85			Logging.VirtualMachine_LOG.fatal("error creating primitive method: ",e);
 86		}
 87		parameterBindingFunction_ = parameterBindingFunction;
 88	}
 89
 90	public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
 91		return new NATClosure(this, lexicalScope, dynamicReceiver);
 92	}
 93	
 94	public ATSymbol base_name() {
 95		return name_;
 96	}
 97
 98	public ATTable base_parameters() {
 99		return parameters_;
100	}
101
102	public ATBegin base_bodyExpression() {
103		return body_;
104	}
105	
106	/**
107	 * To apply a function, first bind its parameters to the evaluated arguments within a new call frame.
108	 * This call frame is lexically nested within the current lexical scope.
109	 * 
110	 * This method is invoked via the following paths:
111	 *  - either by directly 'calling a function', in which case this method is applied via NATClosure.base_apply.
112	 *    The closure ensures that the context used is the lexical scope, not the dynamic scope of invocation.
113	 *  - or by 'invoking a method' through an object, in which case this method is applied via NATObject.meta_invoke.
114	 *    The enclosing object ensures that the context is properly initialized with the implementor, the dynamic receiver
115	 *    and the implementor's parent.
116	 * 
117	 * @param arguments the evaluated actual arguments
118	 * @param ctx the context in which to evaluate the method body, where a call frame will be inserted first
119	 * @return the value of evaluating the function body
120	 */
121	public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
122		NATCallframe cf = new NATCallframe(ctx.base_lexicalScope());
123		ATContext evalCtx = ctx.base_withLexicalEnvironment(cf);
124		PartialBinder.defineParamsForArgs(parameterBindingFunction_, evalCtx, arguments);
125		return body_.meta_eval(evalCtx);
126	}
127	
128	/**
129	 * Applies the method in the context given, without first inserting a call frame to bind parameters.
130	 * Arguments are bound directly in the given lexical scope.
131	 * 
132	 * This method is often invoked via its enclosing closure when used to implement various language
133	 * constructs such as object:, mirror:, extend:with: etc.
134	 * 
135	 * @param arguments the evaluated actual arguments
136	 * @param ctx the context in which to evaluate the method body, to be used as-is
137	 * @return the value of evaluating the function body
138	 */
139	public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
140		PartialBinder.defineParamsForArgs(parameterBindingFunction_, ctx, arguments);
141		return body_.meta_eval(ctx);
142	}
143
144	public NATText meta_print() throws InterpreterException {
145		return NATText.atValue("<method:"+name_.meta_print().javaValue+">");
146	}
147	
148	public ATObject meta_clone() throws InterpreterException {
149		return this;
150	}
151
152	public ATMethod asMethod() throws XTypeMismatch {
153		return this;
154	}
155	
156    public ATTable meta_typeTags() throws InterpreterException {
157    	return NATTable.of(NativeTypeTags._METHOD_, NativeTypeTags._ISOLATE_);
158    }
159
160}