PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/faabtech/brainfuck/impl/OokEngine.java

http://github.com/clone1018/Shocky
Java | 210 lines | 110 code | 21 blank | 79 comment | 22 complexity | bd1fc69591b93269a0d461f5fa45fa30 MD5 | raw file
Possible License(s): 0BSD, Apache-2.0
  1. package org.faabtech.brainfuck.impl;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.io.PrintStream;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. * The {@link OokEngine} is an implementation for the
  9. * <code>brainfuck<code> dialect
  10. * <code>Ook!</code>.
  11. *
  12. * @author Fabian M.
  13. */
  14. public class OokEngine extends TrollScriptEngine {
  15. /**
  16. * The default length of a token.
  17. */
  18. protected int defaultTokenLength = 9;
  19. /**
  20. * The {@link Token} enum contains tokens of <code>Ook!</code>.
  21. *
  22. * @author Fabian M.
  23. */
  24. protected static enum Token {
  25. NEXT("Ook. Ook?"),
  26. PREVIOUS("Ook? Ook."),
  27. PLUS("Ook. Ook."),
  28. MINUS("Ook! Ook!"),
  29. OUTPUT("Ook! Ook."),
  30. INPUT("Ook. Ook!"),
  31. BRACKET_LEFT("Ook! Ook?"),
  32. BRACKET_RIGHT("Ook? Ook!");
  33. String value;
  34. /**
  35. * Constructs a new token.
  36. *
  37. * @param value The value of the token.
  38. */
  39. Token(String value) {
  40. this.value = value;
  41. }
  42. /**
  43. * Get the value of the token.
  44. *
  45. * @return the value.
  46. */
  47. public String getValue() {
  48. return value;
  49. }
  50. }
  51. /**
  52. * Constructs a new {@link OokEngine} instance.
  53. *
  54. * @param cells
  55. * The amount of memory cells.
  56. */
  57. public OokEngine(int cells) {
  58. this(cells, new PrintStream(System.out), System.in);
  59. }
  60. /**
  61. * Constructs a new {@link OokEngine} instance.
  62. *
  63. * @param cells
  64. * The amount of memory cells.
  65. * @param out
  66. * The outputstream of this program.
  67. */
  68. public OokEngine(int cells, OutputStream out) {
  69. this(cells, out, System.in);
  70. }
  71. /**
  72. * Constructs a new {@link OokEngine} instance.
  73. *
  74. * @param cells
  75. * The amount of memory cells.
  76. * @param out
  77. * The printstream of this program.
  78. * @param in
  79. * The outputstream of this program.
  80. */
  81. public OokEngine(int cells, OutputStream out, InputStream in) {
  82. super(cells, out, in);
  83. }
  84. /**
  85. * Interprets the given string.
  86. *
  87. * @param str
  88. * The string to interpret.
  89. * @throws Exception
  90. */
  91. @Override
  92. public void interpret(String str) throws Exception {
  93. // List with tokens.defaultTokenLenght
  94. List<Token> tokens = new ArrayList<Token>();
  95. // It fine that all Ook! tokens are 9 characters long :)
  96. // So we aren't going to loop through all characters..
  97. for (; charPointer < str.length(); ) {
  98. String token = "";
  99. if (charPointer + defaultTokenLength <= str.length())
  100. // The string we found.
  101. token = str.substring(charPointer, charPointer + defaultTokenLength);
  102. else
  103. token = str.substring(charPointer, charPointer
  104. + (str.length() - charPointer));
  105. boolean b = false;
  106. for (Token tokenCheck : Token.values()) {
  107. if (tokenCheck.getValue().equals(token)) {
  108. tokens.add(tokenCheck);
  109. charPointer += defaultTokenLength;
  110. b = true;
  111. break;
  112. }
  113. }
  114. // If the token was invalid, b is false.
  115. if (!b)
  116. if (charPointer + defaultTokenLength > str.length())
  117. charPointer += (str.length() - charPointer);
  118. else
  119. charPointer++;
  120. }
  121. // Loop through all tokens.
  122. for (int tokenPointer = 0; tokenPointer < tokens.size(); ) {
  123. Token token = tokens.get(tokenPointer);
  124. System.out.println(token);
  125. switch(token) {
  126. case NEXT:
  127. // increment the data pointer (to point to the next cell
  128. // to the
  129. // right).
  130. dataPointer = (dataPointer == data.length - 1 ? 0 : dataPointer + 1);
  131. break;
  132. case PREVIOUS:
  133. // decrement the data pointer (to point to the next cell
  134. // to the
  135. // left).
  136. dataPointer = (dataPointer == 0 ? data.length - 1 : dataPointer - 1);
  137. break;
  138. case PLUS:
  139. // increment (increase by one) the byte at the data
  140. // pointer.
  141. data[dataPointer]++;
  142. break;
  143. case MINUS:
  144. // decrement (decrease by one) the byte at the data
  145. // pointer.
  146. data[dataPointer]--;
  147. break;
  148. case OUTPUT:
  149. // Output the byte at the current index in a character.
  150. //outWriter.write((char) data[dataPointer]);
  151. // Flush the outputstream.
  152. //outWriter.flush();
  153. break;
  154. case INPUT:
  155. // accept one byte of input, storing its value in the
  156. // byte at the data pointer.
  157. data[dataPointer] = (byte) consoleReader.read();
  158. break;
  159. case BRACKET_LEFT:
  160. if (data[dataPointer] == 0) {
  161. int level = 1;
  162. while (level > 0) {
  163. tokenPointer++;
  164. if (tokens.get(tokenPointer).equals(Token.BRACKET_LEFT))
  165. level++;
  166. else if (tokens.get(tokenPointer).equals(Token.BRACKET_RIGHT))
  167. level--;
  168. }
  169. }
  170. break;
  171. case BRACKET_RIGHT:
  172. if (data[dataPointer] != 0) {
  173. int level = 1;
  174. while (level > 0) {
  175. tokenPointer--;
  176. if (tokens.get(tokenPointer).equals(Token.BRACKET_LEFT))
  177. level--;
  178. else if (tokens.get(tokenPointer).equals(Token.BRACKET_RIGHT))
  179. level++;
  180. }
  181. }
  182. break;
  183. }
  184. tokenPointer++;
  185. }
  186. // Clear all data.
  187. initate(data.length);
  188. }
  189. }