/examples/interpreter/cgbfi.bf

https://github.com/FabianM/brainfuck · Brainfuck · 199 lines · 192 code · 7 blank · 0 comment · 265 complexity · 55b9058d47ff6b2f720b416da38f4d18 MD5 · raw file

  1. Brainfuck Self Interpreter: by Clive Gifford
  2. Version 1: 1 December 2006
  3. The code to parse/load the input to the interpreter is almost exactly the same
  4. as that in the interpreter described in the Nov 2003 paper by Oleg Mazonka and
  5. Daniel Cristofani but the main interpreter loop is "new"
  6. The main change in the loader code is to leave every second cell free for
  7. working storage
  8. The goal for this interpreter was to be more efficient with the main change in
  9. approach being to only make one round trip between the emulated code and data
  10. memory location for each instruction executed instead of multiple round trips
  11. (according to the value of the internal coding scheme which for example is 8
  12. for the "plus" instruction)
  13. Other info:
  14. An exclamation mark MUST be used to delimit the end of the input (code) to be
  15. interpreted and any other "higher level" data ~ you can stack multiple copies
  16. if you wish as long as they are all separated by exclamation marks also
  17. Input is BF codes (with comments also if desired except for exclamation mark)
  18. The underlying interpreter determines the possible range of data cell values
  19. Does not use wrapping of data values
  20. >>>> leave a little extra space before the program code
  21. >+ start in right hand cell of first program code pair
  22. [
  23. ->>
  24. ++>+>+++++++ setup differences and read char as per dbfi
  25. [<++++>>++<-]++>>+>+>
  26. +++++[>++>++++++<<-]
  27. +>>>,<++
  28. [
  29. [>[->>]<[>>]<<-] section 3
  30. <[<]<+>>[>]>
  31. [ section 4
  32. <+>-
  33. [[<+>-]>] section 5
  34. < section 6
  35. [
  36. [[-]<] scan left and zero the differences
  37. ++<- section 7
  38. [
  39. <+++++++++>[<->-]>>
  40. ]
  41. >>
  42. ]
  43. ]
  44. <<
  45. ]
  46. *** switch to handle three possibilities and spacing of instruction codes ***
  47. >[-]+<< set "done" flag and position to value
  48. [
  49. --
  50. [
  51. [-]
  52. >>- >+< <<
  53. ]
  54. >> [- <<<<[>+<-]>>>>> >+<] <<
  55. ]
  56. >> [- 0] <<
  57. >>>[<<<+>>>-]<<<
  58. ]
  59. >>>+<<<<[<<]>> go to first instruction
  60. ********** MAIN INTERPRETER LOOPS STARTS HERER **********
  61. [ start on current instruction code
  62. below is a big switch statement to decode instructions
  63. [<+>>+<-] first need to move/copy instruction code and set flag so can
  64. +<- start with '(i less 1) (1) (i) where i is instruction code
  65. [
  66. -[
  67. -[
  68. -[
  69. -[
  70. -[
  71. -[
  72. - can't be anything but 1 so bracketing not needed
  73. >- >> [>>] >> [>>]< + <[<<] << [<<] < increment
  74. ]
  75. >[- >> [>>] >> [>>]< , <[<<] << [<<]] < input
  76. ]
  77. >[- >> [>>] >> [>>]< - <[<<] << [<<]]< decrement
  78. ]
  79. >[- >> [>>] >> [>>]< . <[<<] << [<<]]< output
  80. ]
  81. >[- >> [>>] >> [>>] <<-<< [<<] << [<<]]< move left
  82. ]
  83. >[- >> [>>] >> [>>] + [<<] << [<<]]< move right
  84. ]
  85. >
  86. [- left hand bracket
  87. >> [>>] >> [>>]< move to data cell
  88. [>+>>+<<<-]> make double copy and move to first
  89. [<+>-] restore original data cell value
  90. >>[<<+>>[-]]+ This and the following achieves
  91. <<[>>-<<-] x = not x
  92. >> go to flag cell (0 or 1)
  93. Some tricky stuff here: set up (not flag) also so we can later choose
  94. appropriate instruction sequence to get back to code area in one pass
  95. In one case we set flags at the other end (data greater than 0) but
  96. for the other we just go back without setting any flags (data equals 0)
  97. [<<+>>>>+<<-] make two copes of flag
  98. >>[<<+>>-]
  99. <<[>>+<<-]+ This and the following achieves
  100. >>[<<->>-]<< x = not x
  101. << so we now have (data) '(flag) (?) (not flag)
  102. [ if flag set then
  103. -<< [<<] << [<<]< clear and return to code section where we save
  104. << << ++ a 2 meaning we need (later) to match left bracket
  105. >> stop in zero cell for now
  106. ]
  107. >> if we executed code above then now at switch flag
  108. else it will put us ready to return from data area
  109. [-<<<<<<[<<]<<[<<]<] move back to switch flag without setting anything
  110. >
  111. ]
  112. <
  113. ]
  114. >
  115. [- right hand bracket
  116. >> [>>] >> [>>]< move to data cell
  117. [>+>>+<<<-]> make double copy and move to first
  118. [[<+>-]>>[-]+<<] restore data from first then zero second and set flag
  119. >> go to flag cell (0 or 1)
  120. Some tricky stuff here: set up (not flag) also so we can later choose
  121. appropriate instruction sequence to get back to code area in one pass
  122. In one case we set flags at the other end (data greater than 0) but
  123. for the other we just go back without setting any flags (data equals 0)
  124. [<<+>>>>+<<-] make two copes of flag
  125. >>[<<+>>-]
  126. <<[>>+<<-]+ This and the following achieves
  127. >>[<<->>-]<< x = not x
  128. << so we now have (data) '(flag) (?) (not flag)
  129. [ if flag set then
  130. -<< [<<] << [<<]< clear and return to code section where we save
  131. << << + a 1 meaning we need (later) to match right bracket
  132. >> stop in zero cell for now
  133. ]
  134. >> if we executed code above then now at switch flag
  135. else it will put us ready to return from data area
  136. [-<<<<<<[<<]<<[<<]<] move back to switch flag without setting anything
  137. >
  138. ]
  139. >[<+>-] restore original instruction code
  140. *** We are positioned in the cell immediately to the right of the ***
  141. *** instruction that has just been "executed" in the switch above ***
  142. *** The following code is to handle finding matching brackets ***
  143. *** because code above has only set a cell value to 1 or 2 to show ***
  144. *** what kind of loop scanning is required (1=scan left 2 = right) ***
  145. << << << position to cell showing if matching required
  146. [ if non zero we need to find a matching bracket
  147. >> + set up "done" flag for switch and
  148. << - decrement switch value so now is 0 or 1
  149. [ if 1 we are looking for matching right bracket
  150. - >> - >> + clear switch value & "done" & set level to 1
  151. [ while level is more than 0
  152. >>>[-<+>>+<] double copy
  153. +<- set flag and prepare for switch
  154. [
  155. -[
  156. [-]
  157. > [- other] < do nothing except clear flag
  158. ]
  159. > [- 1 <<< + >>>] < increment level
  160. ]
  161. > [- 0 <<< - >>>] decrement level
  162. >[-<+>]<< restore instruction code
  163. << go to level
  164. [>>+<<-] if level then move right one instruction
  165. >>
  166. ]
  167. << << << go back to switch value cell
  168. ]
  169. >> go to switch done flag and if still set then
  170. [ we must be looking for a matching left bracket
  171. - << + clear switch value & "done" & set level to 1
  172. [ repeat while level is more than 0
  173. >>>[-<+>>+<] double copy
  174. +<- set flag and prepare for switch
  175. [
  176. -[
  177. [-]
  178. > [- other] < do nothing except clear flag
  179. ]
  180. > [- 1 <<< - >>>] < decrement level
  181. ]
  182. > [- 0 <<< + >>>] increment level
  183. >[-<+>]<< restore instruction code
  184. << go to level
  185. [<<+>>-] if level then move left one instruction
  186. <<
  187. ]
  188. ]
  189. ]
  190. >> >> >>
  191. > move forward to next instruction
  192. ]