PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/nars_util/src/main/java/objenome/op/compute/BrainfuckMachine.java

https://gitlab.com/opennars/opennars
Java | 141 lines | 96 code | 19 blank | 26 comment | 15 complexity | 41b525aa35b3fff5f345dfffcf4a7894 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0, GPL-3.0, LGPL-3.0
  1. package objenome.op.compute;
  2. import java.util.Arrays;
  3. /**
  4. * Created by me on 7/11/15.
  5. */
  6. public class BrainfuckMachine {
  7. // The indexable memory available to programs.
  8. protected final byte[] memory;
  9. // Pointer to current memory address.
  10. protected int pointer;
  11. public BrainfuckMachine() {
  12. this(64);
  13. }
  14. public BrainfuckMachine(int memorySize) {
  15. memory = new byte[memorySize];
  16. pointer = 0;
  17. }
  18. /*
  19. * Resets the memory array to be filled with 0 bytes, and the pointer to
  20. * address element 0.
  21. */
  22. public void reset() {
  23. Arrays.fill(memory, (byte) 0);
  24. pointer = 0;
  25. }
  26. public void move(int p) {
  27. if (p < 0) {
  28. p = memory.length - 1;
  29. }
  30. this.pointer = p;
  31. }
  32. /*
  33. * Parses and executes the given source string as a Brainfuck program.
  34. */
  35. public void execute(final String source) {
  36. if (source == null) {
  37. return;
  38. }
  39. for (int i = 0; i < source.length(); i++) {
  40. final char c = source(source, i);
  41. switch (c) {
  42. case '>':
  43. move (1 + pointer % memory.length);
  44. break;
  45. case '<':
  46. move(pointer-1);
  47. break;
  48. case '+':
  49. add(pointer, 1); //memory[pointer]++;
  50. break;
  51. case '-':
  52. add(pointer, -1); //memory[pointer]--;
  53. break;
  54. case ',':
  55. // Not supported.
  56. break;
  57. case '.':
  58. // Not supported.
  59. get(pointer); //System.out...
  60. break;
  61. case '[':
  62. final int bracketIndex = closingBracket(source.substring(i + 1)) + (i + 1);
  63. final String loopSource = source.substring((i + 1), bracketIndex);
  64. while (isEmpty(pointer)) {
  65. execute(loopSource);
  66. }
  67. i = bracketIndex;
  68. break;
  69. case ']':
  70. // Implemented as part of '['.
  71. break;
  72. default:
  73. // Ignore all other characters.
  74. break;
  75. }
  76. getMemory(); //state of each memory per cycle
  77. }
  78. }
  79. public char source(String source, int i) {
  80. return source.charAt(i);
  81. }
  82. public boolean isEmpty(int pointer) {
  83. return get(pointer) != 0;
  84. }
  85. public int get(int pointer) {
  86. return memory[pointer];
  87. }
  88. public int set(int pointer, int newValue) {
  89. return memory[pointer] = (byte)newValue;
  90. }
  91. public int add(int pointer, int delta) {
  92. return memory[pointer] += delta;
  93. }
  94. /*
  95. * Locate the matching bracket in the given source.
  96. */
  97. public int closingBracket(final String source) {
  98. int open = 1;
  99. for (int i = 0; i < source.length(); i++) {
  100. final char c = source(source, i);
  101. if (c == '[') {
  102. open++;
  103. } else if (c == ']') {
  104. open--;
  105. if (open == 0) {
  106. return i;
  107. }
  108. }
  109. }
  110. // There is no closing bracket.
  111. return -1;
  112. }
  113. /**
  114. * Returns the byte array which is providing indexed memory for the
  115. * programs. The array will be cleared for each execution.
  116. *
  117. * @return the program's indexed memory.
  118. */
  119. public byte[] getMemory() {
  120. return memory;
  121. }
  122. }