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