PageRenderTime 50ms CodeModel.GetById 10ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 1ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/natives/NATNil.java

http://ambienttalk.googlecode.com/
Java | 223 lines | 103 code | 29 blank | 91 comment | 3 complexity | b06329098e8aff8d35f1a4e078c681a5 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATNil.java created on 15 jul 2007 at 18:33:28
  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.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XArityMismatch;
 33import edu.vub.at.objects.ATBoolean;
 34import edu.vub.at.objects.ATClosure;
 35import edu.vub.at.objects.ATContext;
 36import edu.vub.at.objects.ATNil;
 37import edu.vub.at.objects.ATObject;
 38import edu.vub.at.objects.ATTable;
 39import edu.vub.at.objects.grammar.ATAssignmentSymbol;
 40import edu.vub.at.objects.grammar.ATSymbol;
 41import edu.vub.at.objects.mirrors.PrimitiveMethod;
 42import edu.vub.at.objects.natives.grammar.AGSplice;
 43import edu.vub.at.objects.natives.grammar.AGSymbol;
 44
 45/**
 46 * This class encapsulates the behaviour of the native
 47 * <tt>nil</tt> AmbientTalk object.
 48 * 
 49 * def nil := object: {
 50 *   super := dynamicSentinel;
 51 *   def ==(other); // native implementation
 52 *   def !=(other) { self.==(other).not }
 53 *   def new(@args) { def c := (reflect: self).clone(); c.init(@args); c } 
 54 *   def init(@args) { }
 55 * }
 56 * 
 57 * @author tvcutsem
 58 */
 59public class NATNil extends NATObject implements ATNil {
 60	
 61	/**
 62	 * Nil has a special parent object which ends the recursion
 63	 * along the dynamic delegation chain. These methods cannot be implemented
 64	 * directly in this class because this class still implements useful
 65	 * <tt>base_</tt> Java methods which have to be invoked by means of the
 66	 * implementations defined in {@link NativeATObject}.
 67	 * 
 68	 * This object is shared by all actors, but it is constant so introduces
 69	 * no concurrency issues.
 70	 */
 71	private static final NativeATObject dynamicSentinel_ = new NATByCopy() {
 72
 73      private static final long serialVersionUID = -1307795172754062220L;
 74		// METHODS THAT END THE DYNAMIC DELEGATION CHAIN
 75		
 76		public ATBoolean meta_respondsTo(ATSymbol selector) throws InterpreterException {
 77			// no more delegation
 78			return NATBoolean._FALSE_;
 79		}
 80		
 81		/**
 82		 * When performing <tt>o.m()</tt> and <tt>m</tt> is not found, invoke
 83		 * <tt>doesNotUnderStand</tt> and apply the resulting closure to the given arguments.
 84		 */
 85		public ATObject impl_invokeAccessor(ATObject receiver, ATSymbol selector, ATTable arguments) throws InterpreterException {
 86			return receiver.meta_doesNotUnderstand(selector).base_apply(arguments);
 87		}
 88		
 89		public ATObject impl_invokeMutator(ATObject receiver, ATAssignmentSymbol selector, ATTable arguments) throws InterpreterException {
 90			return receiver.meta_doesNotUnderstand(selector).base_apply(arguments);
 91		}
 92
 93		/**
 94		 * When performing <tt>o.x</tt> and <tt>x</tt> is not found, invoke
 95		 * <tt>doesNotUnderStand</tt> and apply the corresponding closure with zero arguments.
 96		 */
 97		public ATObject meta_invokeField(ATObject receiver, ATSymbol selector) throws InterpreterException {
 98			return receiver.meta_doesNotUnderstand(selector).base_apply(NATTable.EMPTY);
 99		}
100	    
101	    public ATClosure impl_selectAccessor(ATObject receiver, final ATSymbol selector) throws InterpreterException {
102	    	return receiver.meta_doesNotUnderstand(selector);
103	    }
104
105		public ATClosure impl_selectMutator(ATObject receiver, final ATAssignmentSymbol selector) throws InterpreterException {
106			return receiver.meta_doesNotUnderstand(selector);
107		}
108
109		public NATText meta_print() throws InterpreterException {
110			return NATText.atValue("dynamicsentinel");
111		}
112	};
113	
114	// The names of nil's primitive methods
115	public static final AGSymbol _EQL_NAME_ = AGSymbol.jAlloc("==");
116	public static final AGSymbol _NEW_NAME_ = AGSymbol.jAlloc("new");
117	public static final AGSymbol _INI_NAME_ = AGSymbol.jAlloc("init");
118	public static final ATSymbol _NEQ_NAME_ = AGSymbol.jAlloc("!=");
119
120	// The primitive methods themselves
121	
122	/** def ==(comparand) { comparand.impl_identityEquals(self) } */
123	private static final PrimitiveMethod _PRIM_EQL_ = new PrimitiveMethod(
124			_EQL_NAME_, NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("comparand")})) {      
125		public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
126			if (!arguments.base_length().equals(NATNumber.ONE)) {
127				throw new XArityMismatch("==", 1, arguments.base_length().asNativeNumber().javaValue);
128			}
129			
130			ATObject comparand = arguments.base_at(NATNumber.ONE);
131			
132			// make other object perform the actual pointer equality
133			// if comparand is a proxy, it can delegate this request to its principal
134			return comparand.impl_identityEquals(ctx.base_receiver());
135		}
136	};
137	
138	/** def new(@initargs) { (reflect: self).newInstance(initargs) } */
139	private static final PrimitiveMethod _PRIM_NEW_ = new PrimitiveMethod(
140			_NEW_NAME_, NATTable.atValue(new ATObject[] { new AGSplice(AGSymbol.jAlloc("initargs")) })) {
141		public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
142			return ctx.base_receiver().meta_newInstance(arguments);
143			// return ctx.base_receiver().base_new(arguments.asNativeTable().elements_);
144		}
145	};
146	
147	/** def init(@initargs) { self } */
148	private static final PrimitiveMethod _PRIM_INI_ = new PrimitiveMethod(
149			_INI_NAME_, NATTable.atValue(new ATObject[] { new AGSplice(AGSymbol.jAlloc("initargs")) })) {
150		public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
151			return ctx.base_receiver();
152		}
153	};
154	
155	/** def !=(other) { self.==(other).not } */
156	private static final PrimitiveMethod _PRIM_NEQ_ = new PrimitiveMethod(        
157			_NEQ_NAME_, NATTable.of(AGSymbol.jAlloc("other"))) {
158      public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
159			int arity = arguments.base_length().asNativeNumber().javaValue;
160			if (arity != 1) {
161				throw new XArityMismatch("!=", 1, arity);
162			}
163			ATObject other = arguments.base_at(NATNumber.ONE);
164			return ctx.base_receiver().base__opeql__opeql_(other).base_not();
165		}
166	};
167	
168	/**
169	 * Construct and initialize a new instance of <tt>nil</tt>. Only one instance
170	 * of <tt>nil</tt> should be created per actor.
171	 */
172	public NATNil() {
173		// super(dynamicParent, lexicalParent, parentType)
174
175		// super := dynamicSentinel
176		// the lexical root of nil is set to dynamicSentinel_ because,
177		// if we would make it refer to Evaluator.getGlobalLexicalRoot(),
178		// we get an infinite recursion: nil requires root, root requires nil, ...
179		super(dynamicSentinel_, dynamicSentinel_, NATObject._SHARES_A_);
180
181		// add ==, new, init and != to the method dictionary directly		
182		// cannot use meta_addMethod because this method returns nil,
183		// thus constructing nil requires already having a constructed nil...
184		// super.meta_addMethod(_PRIM_NEQ_);		
185		methodDictionary_.put(_EQL_NAME_, _PRIM_EQL_);
186		methodDictionary_.put(_NEW_NAME_, _PRIM_NEW_);
187		methodDictionary_.put(_INI_NAME_, _PRIM_INI_);
188		methodDictionary_.put(_NEQ_NAME_, _PRIM_NEQ_);
189	}
190	
191	public ATBoolean base__opnot__opeql_(ATObject other) throws InterpreterException {
192		// immediately hard-wired implementation, because we know 'self' is really
193		// bound to the Java 'this'.
194		// return this.meta_invoke(this, _NEQ_NAME_, NATTable.of(other));
195		return this.base__opeql__opeql_(other).base_not();
196	}
197
198	public ATObject base_super() {
199		return dynamicSentinel_;
200	}
201	
202	public ATObject meta_pass() throws InterpreterException {
203	    return this;
204	}
205	
206    /**
207     * After deserialization, ensure that nil becomes rebound to the nil
208     * object of the new actor.
209     */
210    public ATObject meta_resolve() throws InterpreterException {
211    	return Evaluator.getNil();
212    }
213    
214    /** nil is a singleton */
215    public ATObject meta_clone() throws InterpreterException {
216    	return this;
217    }
218
219	public NATText meta_print() throws InterpreterException {
220		return NATText.atValue("nil");
221	}
222	
223}