PageRenderTime 40ms CodeModel.GetById 10ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 185 lines | 102 code | 42 blank | 41 comment | 2 complexity | 0d0791e65bf7f59534159146cda909be MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATBoolean.java created on Jul 23, 2006 at 12:52:29 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.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.objects.ATBoolean;
 33import edu.vub.at.objects.ATClosure;
 34import edu.vub.at.objects.ATObject;
 35import edu.vub.at.objects.ATTable;
 36import edu.vub.at.objects.coercion.NativeTypeTags;
 37
 38/**
 39 * NATBoolean is simply a container class for ambienttalk booleans. The native 
 40 * implementations of true and false can be accessed using the class's atValue
 41 * method.
 42 * 
 43 * @author smostinc
 44 */
 45public abstract class NATBoolean extends NATByCopy implements ATBoolean {
 46	
 47	/**
 48	 * Returns the corresponding ATBoolean given a java truth value. This constructor
 49	 * function is to be used only when the result to be given out depends on a 
 50	 * dynamic test, else the static fields _TRUE_ and _FALSE_ should be used instead.
 51	 */
 52	public static ATBoolean atValue(boolean b) {
 53		if (b) {
 54			return _TRUE_;
 55		} else {
 56			return _FALSE_;
 57		}
 58	}
 59	
 60	public final boolean javaValue;
 61	
 62	private NATBoolean(boolean b) {
 63		javaValue = b;
 64	}
 65	
 66	public boolean isNativeBoolean() {
 67		return true;
 68	}
 69
 70	public ATBoolean asBoolean() {
 71		return this;
 72	}
 73	
 74	public NATBoolean asNativeBoolean() {
 75		return this;
 76	}
 77	
 78	public ATObject meta_clone() throws InterpreterException {
 79		return this;
 80	}
 81	
 82    public ATTable meta_typeTags() throws InterpreterException {
 83    	return NATTable.of(NativeTypeTags._BOOLEAN_, NativeTypeTags._ISOLATE_);
 84    }
 85	
 86	public static class NATTrue extends NATBoolean {
 87		
 88		public static final NATTrue _INSTANCE_ = new NATTrue();
 89		
 90		public NATTrue() { super(true); }
 91		
 92		public NATText meta_print() throws InterpreterException { return NATText.atValue("true"); }
 93		
 94		// base interface for true
 95		
 96		public ATObject base_ifTrue_(ATClosure clo) throws InterpreterException {
 97			return clo.base_apply(NATTable.EMPTY);
 98		}
 99
100		public ATObject base_ifFalse_(ATClosure clo) throws InterpreterException {
101			return Evaluator.getNil();
102		}
103		
104		public ATObject base_ifTrue_ifFalse_(ATClosure consequent, ATClosure alternative) throws InterpreterException {
105			return consequent.base_apply(NATTable.EMPTY);
106		}
107		
108		public ATBoolean base_and_(ATClosure other) throws InterpreterException {
109			return other.base_apply(NATTable.EMPTY).asBoolean();
110		}
111		
112		public ATBoolean base_or_(ATClosure other) throws InterpreterException {
113			return this;
114		}
115		
116		public ATBoolean base_and_and_(ATClosure b1, ATClosure b2) throws InterpreterException {
117			return b1.base_apply(NATTable.EMPTY).asBoolean().base_and_(b2);
118		}
119
120		public ATBoolean base_or_or_(ATClosure b1, ATClosure b2) throws InterpreterException {
121			return this;
122		}
123
124		public ATBoolean base_not() {
125			return NATFalse._INSTANCE_;
126		}
127		
128		public ATObject meta_resolve() throws InterpreterException {
129			return NATTrue._INSTANCE_;
130		}
131		
132	}
133
134	public static class NATFalse extends NATBoolean {
135		
136		public static final NATFalse _INSTANCE_ = new NATFalse();
137		
138		public NATFalse() { super(false); }
139		
140		public NATText meta_print() throws InterpreterException { return NATText.atValue("false"); }
141
142		// base interface for false
143		
144		public ATObject base_ifTrue_(ATClosure clo) throws InterpreterException {
145			return Evaluator.getNil();
146		}
147
148		public ATObject base_ifFalse_(ATClosure clo) throws InterpreterException {
149			return clo.base_apply(NATTable.EMPTY);
150		}
151		
152		public ATObject base_ifTrue_ifFalse_(ATClosure consequent, ATClosure alternative) throws InterpreterException {
153			return alternative.base_apply(NATTable.EMPTY);
154		}
155		
156		public ATBoolean base_and_(ATClosure other) throws InterpreterException {
157			return this;
158		}
159		
160		public ATBoolean base_or_(ATClosure other) throws InterpreterException {
161			return other.base_apply(NATTable.EMPTY).asBoolean();
162		}
163		
164		public ATBoolean base_not() {
165			return NATTrue._INSTANCE_;
166		}
167		
168		public ATBoolean base_and_and_(ATClosure b1, ATClosure b2) throws InterpreterException {
169			return this;
170		}
171
172		public ATBoolean base_or_or_(ATClosure b1, ATClosure b2) throws InterpreterException {
173			return b1.base_apply(NATTable.EMPTY).asBoolean().base_or_(b2);
174		}
175		
176		public ATObject meta_resolve() throws InterpreterException {
177			return NATFalse._INSTANCE_;
178		}
179		
180	}
181	
182	public static final NATTrue _TRUE_ = NATTrue._INSTANCE_;
183	public static final NATFalse _FALSE_ = NATFalse._INSTANCE_;
184
185}