PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/objects/mirrors/PrimitiveMethod.java

http://ambienttalk.googlecode.com/
Java | 124 lines | 46 code | 15 blank | 63 comment | 0 complexity | 3df8c1cc417d5cd5ec25bd12ea651340 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * PrimitiveMethod.java created on 2-feb-2007 at 21:44:00
  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.mirrors;
 29
 30import edu.vub.at.eval.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XTypeMismatch;
 33import edu.vub.at.objects.ATContext;
 34import edu.vub.at.objects.ATObject;
 35import edu.vub.at.objects.ATTable;
 36import edu.vub.at.objects.grammar.ATBegin;
 37import edu.vub.at.objects.grammar.ATSymbol;
 38import edu.vub.at.objects.natives.NATMethod;
 39import edu.vub.at.objects.natives.NATNil;
 40import edu.vub.at.objects.natives.NATTable;
 41import edu.vub.at.objects.natives.NATText;
 42import edu.vub.at.objects.natives.grammar.NATAbstractGrammar;
 43
 44/**
 45 * A primitive method is the equivalent of a NativeClosure but for methods rather
 46 * than closures. The advantage of PrimtiveMethods is that their base_apply method
 47 * gives access to both arguments as well as to the runtime context that contains
 48 * the lexical environment in which they were defined.
 49 * 
 50 * Example primitive methods are '==', '!=', 'new' and 'init' implemented by {@link NATNil}.
 51 *
 52 * Primitive methods should implement this method's base_apply method or invoke the
 53 * constructor taking a PrimitiveBody parameter and implement that class's meta_eval
 54 * method, if they want to make use of AT/2's parameter binding semantics.
 55 * 
 56 * Primitive methods installed in native objects that can be extended should ensure
 57 * that they use the dynamic receiver stored in the application context (the AmbientTalk
 58 * 'self') rather than the Java 'this' variable to perform self-sends. The former will
 59 * properly invoke/select the overridden methods/fields of a child AT object, the latter
 60 * will simply refer to the native instance, disregarding any modifications by child objects.
 61 *
 62 * @author tvcutsem
 63 */
 64public class PrimitiveMethod extends NATMethod {
 65	
 66	/**
 67	 * Instances of this helper class represent primitive method bodies. To the
 68	 * AT programmer, they look like empty methods (i.e. { nil }). The native Java
 69	 * implementation is specified by overriding the meta_eval method.
 70	 */
 71	public static abstract class PrimitiveBody extends NATAbstractGrammar implements ATBegin {
 72		
 73		/**
 74		 * A primitive can override this method and has access to:
 75		 * - ctx.lexicalScope = the call frame for this method invocation
 76		 * - ctx.lexicalScope.lexicalParent = the object in which the method was found
 77		 * - ctx.dynamicReceiver = the object on which the method was invoked
 78		 */
 79		public abstract ATObject meta_eval(ATContext ctx) throws InterpreterException;
 80
 81		public ATObject meta_quote(ATContext ctx) throws InterpreterException {
 82			return this;
 83		}
 84		
 85		public ATTable base_statements() { return NATTable.of(Evaluator.getNil()); }
 86		
 87		public NATText meta_print() throws InterpreterException {
 88			return NATText.atValue("<primitive body>");
 89		}
 90		
 91		public ATBegin asBegin() throws XTypeMismatch {
 92			return this;
 93		}
 94		
 95	}
 96
 97	public PrimitiveMethod(ATSymbol name, ATTable formals, PrimitiveBody body, ATTable annotations) {
 98		super(name, formals, body, annotations);
 99	}
100	
101	public PrimitiveMethod(ATSymbol name, ATTable formals, PrimitiveBody body) {
102		super(name, formals, body, NATTable.EMPTY);
103	}
104
105	/**
106	 * Constructor for the creation of primitive methods where the body is left empty.
107	 * The idea is that the creator of such primitive methods overrides the base_apply
108	 * method because the primitive method has no need for a call frame or parameter binding.
109	 */
110	public PrimitiveMethod(ATSymbol name, ATTable formals) {
111		super(name, formals, new PrimitiveBody() {
112            private static final long serialVersionUID = -2177230227968386983L;
113
114			public ATObject meta_eval(ATContext ctx) throws InterpreterException {
115				return Evaluator.getNil();
116			}
117		}, NATTable.EMPTY);
118	}
119	
120	public NATText meta_print() throws InterpreterException {
121		return NATText.atValue("<primitive method:"+base_name()+">");
122	}
123	
124}