/interpreter/tags/at2-build270707/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
- /**
- * AmbientTalk/2 Project
- * NATMethod.java created on Jul 24, 2006 at 11:30:35 PM
- * (c) Programming Technology Lab, 2006 - 2007
- * Authors: Tom Van Cutsem & Stijn Mostinckx
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
- package edu.vub.at.objects.natives;
- import edu.vub.at.eval.PartialBinder;
- import edu.vub.at.exceptions.InterpreterException;
- import edu.vub.at.exceptions.XTypeMismatch;
- import edu.vub.at.objects.ATClosure;
- import edu.vub.at.objects.ATContext;
- import edu.vub.at.objects.ATMethod;
- import edu.vub.at.objects.ATObject;
- import edu.vub.at.objects.ATTable;
- import edu.vub.at.objects.coercion.NativeTypeTags;
- import edu.vub.at.objects.grammar.ATBegin;
- import edu.vub.at.objects.grammar.ATSymbol;
- import edu.vub.at.objects.mirrors.PrimitiveMethod;
- import edu.vub.at.util.logging.Logging;
- /**
- * NATMethod implements methods as named functions which are in fact simply containers
- * for a name, a table of arguments and a body.
- *
- * @author smostinc
- * @author tvcutsem
- */
- public class NATMethod extends NATByCopy implements ATMethod {
- private final ATSymbol name_;
- private final ATTable parameters_;
- private final ATBegin body_;
-
- // partial function denoting a parameter binding algorithm specialized for this method's parameter list
- private final PartialBinder parameterBindingFunction_;
-
- /** construct a new method. This method may raise an exception if the parameter list is illegal. */
- public NATMethod(ATSymbol name, ATTable parameters, ATBegin body) throws InterpreterException {
- name_ = name;
- parameters_ = parameters;
- body_ = body;
-
- // calculate the parameter binding strategy to use using partial evaluation
- parameterBindingFunction_ =
- PartialBinder.calculateResidual(name_.base_text().asNativeText().javaValue, parameters);
- }
-
- /**
- * Constructor to be used by primitive methods only.
- */
- protected NATMethod(ATSymbol name, ATTable parameters, PrimitiveMethod.PrimitiveBody body) {
- name_ = name;
- parameters_ = parameters;
- body_ = body;
- PartialBinder parameterBindingFunction;
- try {
- // calculate the parameter binding strategy to use using partial evaluation
- parameterBindingFunction = PartialBinder.calculateResidual(name_.base_text().asNativeText().javaValue, parameters);
- } catch (InterpreterException e) {
- parameterBindingFunction = null;
- // this indicates a bug, primitive methods should not contain erroneous parameter lists
- Logging.VirtualMachine_LOG.fatal("error creating primitive method: ",e);
- }
- parameterBindingFunction_ = parameterBindingFunction;
- }
- public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
- return new NATClosure(this, lexicalScope, dynamicReceiver);
- }
-
- public ATSymbol base_name() {
- return name_;
- }
- public ATTable base_parameters() {
- return parameters_;
- }
- public ATBegin base_bodyExpression() {
- return body_;
- }
-
- /**
- * To apply a function, first bind its parameters to the evaluated arguments within a new call frame.
- * This call frame is lexically nested within the current lexical scope.
- *
- * This method is invoked via the following paths:
- * - either by directly 'calling a function', in which case this method is applied via NATClosure.base_apply.
- * The closure ensures that the context used is the lexical scope, not the dynamic scope of invocation.
- * - or by 'invoking a method' through an object, in which case this method is applied via NATObject.meta_invoke.
- * The enclosing object ensures that the context is properly initialized with the implementor, the dynamic receiver
- * and the implementor's parent.
- *
- * @param arguments the evaluated actual arguments
- * @param ctx the context in which to evaluate the method body, where a call frame will be inserted first
- * @return the value of evaluating the function body
- */
- public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
- NATCallframe cf = new NATCallframe(ctx.base_lexicalScope());
- ATContext evalCtx = ctx.base_withLexicalEnvironment(cf);
- PartialBinder.defineParamsForArgs(parameterBindingFunction_, evalCtx, arguments);
- return body_.meta_eval(evalCtx);
- }
-
- /**
- * Applies the method in the context given, without first inserting a call frame to bind parameters.
- * Arguments are bound directly in the given lexical scope.
- *
- * This method is often invoked via its enclosing closure when used to implement various language
- * constructs such as object:, mirror:, extend:with: etc.
- *
- * @param arguments the evaluated actual arguments
- * @param ctx the context in which to evaluate the method body, to be used as-is
- * @return the value of evaluating the function body
- */
- public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
- PartialBinder.defineParamsForArgs(parameterBindingFunction_, ctx, arguments);
- return body_.meta_eval(ctx);
- }
- public NATText meta_print() throws InterpreterException {
- return NATText.atValue("<method:"+name_.meta_print().javaValue+">");
- }
-
- public ATObject meta_clone() throws InterpreterException {
- return this;
- }
- public ATMethod asMethod() throws XTypeMismatch {
- return this;
- }
-
- public ATTable meta_typeTags() throws InterpreterException {
- return NATTable.of(NativeTypeTags._METHOD_, NativeTypeTags._ISOLATE_);
- }
- }