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

/hxBF/BrainFuck.hx

http://github.com/andyli/hxBF
Haxe | 190 lines | 156 code | 20 blank | 14 comment | 29 complexity | ff21c5ca7f1e519209aeba62857bc2ce MD5 | raw file
  1. /*
  2. hxBF, BrainFuck interpreter written in haXe
  3. Based on the as3 version I've written some time ago: http://blog.onthewings.net/2009/10/08/brainflash-the-as3-brainfuck-interpreter
  4. Copyright (c) 2010, Andy Li
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  7. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  9. Neither the name of onthewings.net nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11. */
  12. package hxBF;
  13. import haxe.io.Bytes;
  14. import haxe.io.BytesOutput;
  15. import haxe.io.Input;
  16. import haxe.io.Output;
  17. import haxe.io.StringInput;
  18. import haxe.io.Eof;
  19. class BrainFuck {
  20. public var memory:Bytes;
  21. public var pointer:Int;
  22. public var input:Input;
  23. public var output:Output;
  24. public var program:String;
  25. public var programPosition:Int;
  26. public var showInput:Bool;
  27. public function new(?program:String = "", ?input:Input, ?output:Output, ?memory:Bytes):Void {
  28. this.program = program;
  29. this.input = input == null ? new StringInput("") : input;
  30. this.output = output == null ? new BytesOutput() : output;
  31. this.memory = memory == null ? Bytes.alloc(30000) : memory;
  32. showInput = false;
  33. programPosition = 0;
  34. pointer = 0;
  35. }
  36. public function run():BrainFuck {
  37. while (programPosition < this.program.length){
  38. runCommand(program.charAt(programPosition));
  39. }
  40. return this;
  41. }
  42. private function runCommand(command:String):Void {
  43. switch (command) {
  44. case '>': //Increment the pointer.
  45. ++pointer;
  46. moveToNextCommand();
  47. case '<': //Decrement the pointer.
  48. --pointer;
  49. moveToNextCommand();
  50. case '+': //Increment the byte at the pointer.
  51. try {
  52. memory.set(pointer,Std.int(Math.min(memory.get(pointer)+1,255)));
  53. } catch (e:Dynamic) {
  54. throw new InvalidMemoryAccessError();
  55. }
  56. moveToNextCommand();
  57. case '-': //Decrement the byte at the pointer.
  58. try {
  59. memory.set(pointer,Std.int(Math.max(memory.get(pointer)-1,0)));
  60. } catch (e:Dynamic) {
  61. throw new InvalidMemoryAccessError();
  62. }
  63. moveToNextCommand();
  64. case '.': //Output the byte at the pointer.
  65. try {
  66. output.writeByte(memory.get(pointer));
  67. } catch (e:Dynamic) {
  68. throw new InvalidMemoryAccessError();
  69. }
  70. moveToNextCommand();
  71. case ',': //Input a byte and store it in the byte at the pointer.
  72. var val;
  73. try {
  74. val = input.readByte();
  75. } catch (e:Eof) {
  76. throw new EndOfInputError();
  77. }
  78. if (showInput){
  79. output.writeByte(val);
  80. }
  81. try {
  82. memory.set(pointer,val);
  83. } catch (e:Dynamic) {
  84. throw new InvalidMemoryAccessError();
  85. }
  86. moveToNextCommand();
  87. case '[': //Jump forward past the matching ] if the byte at the pointer is zero.
  88. var val;
  89. try {
  90. val = memory.get(pointer);
  91. } catch (e:Dynamic) {
  92. throw new InvalidMemoryAccessError();
  93. }
  94. if (val == 0){
  95. var count = 1;
  96. var c = moveToNextCommand();
  97. while (true){
  98. if (c == '[') ++count;
  99. if (c == ']') --count;
  100. if (count == 0){
  101. moveToNextCommand();
  102. break;
  103. }
  104. c = moveToNextCommand();
  105. if (c == null) {
  106. throw new EndOfProgramError();
  107. }
  108. }
  109. } else {
  110. moveToNextCommand();
  111. }
  112. case ']': //Jump backward to the matching [ unless the byte at the pointer is zero.
  113. var val;
  114. try {
  115. val = memory.get(pointer);
  116. } catch (e:Dynamic) {
  117. throw new InvalidMemoryAccessError();
  118. }
  119. if (val > 0){
  120. var count = 1;
  121. var c = moveToPrevCommand();
  122. while (true){
  123. if (c == '[') --count;
  124. if (c == ']') ++count;
  125. if (count == 0){
  126. break;
  127. }
  128. c = moveToPrevCommand();
  129. if (c == null) {
  130. throw new EndOfProgramError();
  131. }
  132. }
  133. } else {
  134. moveToNextCommand();
  135. }
  136. default:
  137. moveToNextCommand();
  138. }
  139. }
  140. private function moveToNextCommand():String {
  141. return (++programPosition) >= program.length ? null : program.charAt(programPosition);
  142. }
  143. private function moveToPrevCommand():String {
  144. return (--programPosition < 0) ? null : program.charAt(programPosition);
  145. }
  146. }
  147. class EndOfProgramError {
  148. public function new():Void {
  149. }
  150. public function toString():String {
  151. return "End of program reached";
  152. }
  153. }
  154. class EndOfInputError {
  155. public function new():Void {
  156. }
  157. public function toString():String {
  158. return "End of input reached";
  159. }
  160. }
  161. class InvalidMemoryAccessError {
  162. public function new():Void {
  163. }
  164. public function toString():String {
  165. return "Invalid memory access";
  166. }
  167. }