PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/at/objects/natives/NATNil.java

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