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

http://ambienttalk.googlecode.com/ · 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. */
  28. package edu.vub.at.objects.natives;
  29. import java.util.HashMap;
  30. import edu.vub.at.eval.Evaluator;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import edu.vub.at.objects.ATBoolean;
  33. import edu.vub.at.objects.ATClosure;
  34. import edu.vub.at.objects.ATObject;
  35. import edu.vub.at.objects.ATTable;
  36. import edu.vub.at.objects.coercion.NativeTypeTags;
  37. import edu.vub.util.TempFieldGenerator;
  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. */
  45. public abstract class NATBoolean extends NATByCopy implements ATBoolean {
  46. /**
  47. * Returns the corresponding ATBoolean given a java truth value. This constructor
  48. * function is to be used only when the result to be given out depends on a
  49. * dynamic test, else the static fields _TRUE_ and _FALSE_ should be used instead.
  50. */
  51. public static ATBoolean atValue(boolean b) {
  52. if (b) {
  53. return _TRUE_;
  54. } else {
  55. return _FALSE_;
  56. }
  57. }
  58. public final boolean javaValue;
  59. private NATBoolean(boolean b) {
  60. javaValue = b;
  61. }
  62. public boolean isNativeBoolean() {
  63. return true;
  64. }
  65. public ATBoolean asBoolean() {
  66. return this;
  67. }
  68. public NATBoolean asNativeBoolean() {
  69. return this;
  70. }
  71. public ATObject meta_clone() throws InterpreterException {
  72. return this;
  73. }
  74. public ATTable meta_typeTags() throws InterpreterException {
  75. return NATTable.of(NativeTypeTags._BOOLEAN_, NativeTypeTags._ISOLATE_);
  76. }
  77. public static class NATTrue extends NATBoolean {
  78. public static final NATTrue _INSTANCE_ = new NATTrue();
  79. public NATTrue() { super(true); }
  80. public NATText meta_print() throws InterpreterException { return NATText.atValue("true"); }
  81. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  82. if (objectMap.contains(this)) {
  83. return objectMap.getName(this);
  84. }
  85. NATText name = objectMap.put(this, NATText.atValue("true"));
  86. return name;
  87. }
  88. // base interface for true
  89. public ATObject base_ifTrue_(ATClosure clo) throws InterpreterException {
  90. return clo.base_apply(NATTable.EMPTY);
  91. }
  92. public ATObject base_ifFalse_(ATClosure clo) throws InterpreterException {
  93. return Evaluator.getNil();
  94. }
  95. public ATObject base_ifTrue_ifFalse_(ATClosure consequent, ATClosure alternative) throws InterpreterException {
  96. return consequent.base_apply(NATTable.EMPTY);
  97. }
  98. public ATBoolean base_and_(ATClosure other) throws InterpreterException {
  99. return other.base_apply(NATTable.EMPTY).asBoolean();
  100. }
  101. public ATBoolean base_or_(ATClosure other) throws InterpreterException {
  102. return this;
  103. }
  104. public ATBoolean base_and_and_(ATClosure b1, ATClosure b2) throws InterpreterException {
  105. return b1.base_apply(NATTable.EMPTY).asBoolean().base_and_(b2);
  106. }
  107. public ATBoolean base_or_or_(ATClosure b1, ATClosure b2) throws InterpreterException {
  108. return this;
  109. }
  110. public ATBoolean base_not() {
  111. return NATFalse._INSTANCE_;
  112. }
  113. public ATObject meta_resolve() throws InterpreterException {
  114. return NATTrue._INSTANCE_;
  115. }
  116. }
  117. public static class NATFalse extends NATBoolean {
  118. public static final NATFalse _INSTANCE_ = new NATFalse();
  119. public NATFalse() { super(false); }
  120. public NATText meta_print() throws InterpreterException { return NATText.atValue("false"); }
  121. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  122. if (objectMap.contains(this)) {
  123. return objectMap.getName(this);
  124. }
  125. NATText name = objectMap.put(this, NATText.atValue("false"));
  126. return name;
  127. }
  128. // base interface for false
  129. public ATObject base_ifTrue_(ATClosure clo) throws InterpreterException {
  130. return Evaluator.getNil();
  131. }
  132. public ATObject base_ifFalse_(ATClosure clo) throws InterpreterException {
  133. return clo.base_apply(NATTable.EMPTY);
  134. }
  135. public ATObject base_ifTrue_ifFalse_(ATClosure consequent, ATClosure alternative) throws InterpreterException {
  136. return alternative.base_apply(NATTable.EMPTY);
  137. }
  138. public ATBoolean base_and_(ATClosure other) throws InterpreterException {
  139. return this;
  140. }
  141. public ATBoolean base_or_(ATClosure other) throws InterpreterException {
  142. return other.base_apply(NATTable.EMPTY).asBoolean();
  143. }
  144. public ATBoolean base_not() {
  145. return NATTrue._INSTANCE_;
  146. }
  147. public ATBoolean base_and_and_(ATClosure b1, ATClosure b2) throws InterpreterException {
  148. return this;
  149. }
  150. public ATBoolean base_or_or_(ATClosure b1, ATClosure b2) throws InterpreterException {
  151. return b1.base_apply(NATTable.EMPTY).asBoolean().base_or_(b2);
  152. }
  153. public ATObject meta_resolve() throws InterpreterException {
  154. return NATFalse._INSTANCE_;
  155. }
  156. }
  157. public static final NATTrue _TRUE_ = NATTrue._INSTANCE_;
  158. public static final NATFalse _FALSE_ = NATFalse._INSTANCE_;
  159. }