/interpreter/tags/at2-build270707/src/edu/vub/at/objects/natives/NATBoolean.java

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