/interpreter/branches/bytecode/bytecode/src/edu/vub/at/objects/ATNumber.java

http://ambienttalk.googlecode.com/ · Java · 172 lines · 15 code · 15 blank · 142 comment · 0 complexity · 2794c5e549d59946bcb87a274a9a29e1 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * ATNumber.java created on 26-jul-2006 at 15:15:59
  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;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. /**
  31. * ATNumber is the public interface to an AmbientTalk native number (an integer value).
  32. *
  33. * @author tvc
  34. */
  35. public interface ATNumber extends ATNumeric {
  36. // base-level interface
  37. // Arithmetic operations
  38. /**
  39. * Returns the number plus 1.
  40. *
  41. * @return an ATNumber resulting of adding 1 to the receiver.
  42. */
  43. public ATNumber base_inc() throws InterpreterException;
  44. /**
  45. * Returns the number minus 1.
  46. *
  47. * @return an ATNumber resulting of subtracting 1 to the receiver.
  48. */
  49. public ATNumber base_dec() throws InterpreterException;
  50. /**
  51. * Returns the absolute value of a number.
  52. * <p>
  53. * More specifically:
  54. * <ul>
  55. * <li>If the receiver >= 0, the receiver is returned.
  56. * <li>If the receiver < 0, the negation of the receiver is returned.
  57. * </ul>
  58. *
  59. * @return the absolute value of the receiver.
  60. */
  61. public ATNumber base_abs() throws InterpreterException;
  62. /**
  63. * Iterates as many times as the value of the number applying a closure passed as argument.
  64. * <p>
  65. * More specifically, the equivalent pseudo-code for this construct is:
  66. * <code>for i = 1 to receiver do code.eval(i); nil </code>
  67. * <p>
  68. * Here comes an example about how the doTimes: construct can be used to calculate the factorial of a number.
  69. * def fact(n) {
  70. * def res := 1;
  71. * n.doTimes: { |i| res := res * i};
  72. * res
  73. * }
  74. *
  75. * @param code a closure expected to take one argument to be applied in each iteration
  76. * @return nil
  77. * @throws InterpreterException if raised inside the iterator block.
  78. */
  79. public ATNil base_doTimes_(ATClosure code) throws InterpreterException;
  80. /**
  81. * Iterates from the value of the receiver to the one passed as argument applying a closure.
  82. * <p>
  83. * More specifically, this method calls:
  84. * <code>receiver.to: end step: 1 do: { |i| code } </code>
  85. * <p>
  86. * Note that if end is bigger than the receiver, the construct behaves as a down-to operation.
  87. * If receiver is equals to end, the code is not executed.
  88. *
  89. * @param end a number representing the stop value of the loop.
  90. * @param code a closure expected to take one argument to be applied in each iteration.
  91. * @return nil
  92. * @throws InterpreterException if raised inside the iterator block.
  93. */
  94. public ATNil base_to_do_(ATNumber end, ATClosure code) throws InterpreterException;
  95. /**
  96. * Iterates from the value of the receiver to the one passed as argument applying a closure.
  97. * <p>
  98. * More specifically, the equivalent pseudo-code for this method is:
  99. * <p>
  100. * <code>for (i = receiver; i < end ; i := i + inc) do code.eval(i); nil</code>
  101. * <p>
  102. * Note that if end is bigger than the receiver, the construct behaves as a down-to operation.
  103. * If receiver is equals to end, the code is not executed.
  104. *
  105. * @param end a number representing the stop value of the loop.
  106. * @param inc a number representing the step value of the loop.
  107. * @param code a closure expected to take one argument to be applied in each iteration.
  108. * @return nil
  109. * @throws InterpreterException if raised inside the iterator block.
  110. */
  111. public ATNil base_to_step_do_(ATNumber end, ATNumber inc, ATClosure code) throws InterpreterException;
  112. /**
  113. * Returns a table containing the exclusive range from the number to a number passed as argument.
  114. * <p>
  115. * Usage example:
  116. * <p>
  117. * <code> 2 ** 5 => [ 2, 3, 4 ]</code>
  118. * <code> 5 ** 2 => [ 5, 4, 3 ]</code>
  119. *
  120. * @param end a number representing the stop value of the range.
  121. * @return a {@link ATTable} representing [ receiver, ..., end [
  122. */
  123. public ATTable base__optms__optms_(ATNumber end) throws InterpreterException;
  124. /**
  125. * Returns a table containing the inclusive range from the number to a number passed as argument.
  126. * <p>
  127. * Usage example:
  128. * <p>
  129. * <code> 2 *** 5 => [ 2, 3, 4, 5 ]</code>
  130. * <code> 5 *** 2 => [ 5, 4, 3, 2 ]</code>
  131. *
  132. * @param end a number representing the stop value of the range.
  133. * @return a {@link ATTable} representing [ receiver, ..., end ]
  134. */
  135. public ATTable base__optms__optms__optms_(ATNumber end) throws InterpreterException;
  136. /**
  137. * Returns a random fraction in the exclusive range from the number to a number passed as argument.
  138. *
  139. * @param nbr a number representing the stop value of the range.
  140. * @return the {@link ATFraction} chosen randomly in [ receiver, ..., nbr [
  141. */
  142. public ATFraction base__opque__opque_(ATNumber nbr) throws InterpreterException;
  143. /**
  144. * Returns the modular arithmetic between the number and another number passed as argument.
  145. *
  146. * @param nbr a number.
  147. * @return an {@link ATNumber} resulting of the remainder of the division (of receiver by nbr) that truncates towards zero.
  148. */
  149. public ATNumber base__oprem_(ATNumber nbr) throws InterpreterException;
  150. /**
  151. * Returns the floor division between the number and another number passed as argument.
  152. *
  153. * @param nbr a number.
  154. * @return a {@link ATNumber} resulting of the floor division of receiver by nbr.
  155. */
  156. public ATNumber base__opdiv__opmns_(ATNumber nbr) throws InterpreterException;
  157. }