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