wib.b 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. [
  2. Written In Brainfuck (wib) v0.96
  3. Brainfuck to C translator by Mats Linander
  4. This program reads brainfuck sourcecode from stdin, translates it into c code
  5. and outputs it to stdout. Wib assumes that EOF == -1.
  6. Windows based c compilers may have problems compiling code generated by wib
  7. as it produces unix style newlines ('\n'==0x0a).
  8. Changelog
  9. v0.96: 2003-05-??
  10. * New itoa routine
  11. * Some tinkering to reduce size
  12. v0.95: 2003-05-03
  13. * Bugfix. Wib said "++*p" where it should have been "--*p".
  14. v0.94: 2003-04-22
  15. * Wib is now officially an optimizing translator, a series of n '+' now
  16. produces an output of *p+=n;\n. The same goes for '-'.
  17. v0.93: 2002-08-22
  18. * Removed all - that were immediately followed by + and vice versa.
  19. v0.92: 2002-08-21
  20. * Changed the output for dots, EOF should be EOF and nothing else.
  21. AFAIK this means wib follows the spec.
  22. * Made wib assume EOF==EOF&&EOF!='\0'
  23. * Put this stuff within brackets. Since *p is always zero at this point,
  24. the loop will never be entered and any chars can be used here.
  25. * Changed the GPL copyright statement to the ordinary one, including special
  26. chars.
  27. v0.91: 2002-05-23
  28. * Added the GPL license statement thingie
  29. v0.90: 2002-05-20
  30. * Wib now outputs 'if((*p=getchar())==EOF)*p=0;\n' when a dot is read
  31. Noticed that almost every other bf compiler does it so why not mine?
  32. v0.80: 2002-05-19
  33. * First working version
  34. Copyright (C) 2002, 2003, 2004 Mats Linander
  35. This program is free software; you can redistribute it and/or modify
  36. it under the terms of the GNU General Public License as published by
  37. the Free Software Foundation; either version 2 of the License, or
  38. (at your option) any later version.
  39. This program is distributed in the hope that it will be useful,
  40. but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. GNU General Public License for more details.
  43. You should have received a copy of the GNU General Public License
  44. along with this program; if not, write to the Free Software
  45. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  46. ]
  47. +++++++[->>+++++<<]>>.<[-]<[-]+++++++[->+++++[->>+++<<]<]>>>.<<[-]<[-]>>>[-
  48. <<+>>>+>+>+>+>+>+<<<<<<]<<[->>+<<]>>>+++++.>------.>+++.>++++++++++++.>----
  49. -.>----.>>++++++++[-<++++>]<.>>++++++[->+++++[-<<++>>]<]<.<<[->>>+>+>+>+>+>
  50. +>+ <<<<<<<<<]>>>>> >>>[-<<<<<<<<+ >>>>>>>>]<<<<<+++
  51. +++++ ++++++.>+++++++++++ ++++. >-.>+++ +.>++++++++++.>>+
  52. ++>++ +++++++[-<<+++++>>] <<+. >.>>++ ++++[->+++++[-<<+
  53. +>>]< ]<++.>++++++++++.<< <<<< <<<<<< <<<<<<.>.>.>.>.>.
  54. >.>.> .>.>.>.>.>+++.>---- --.>> >>>[-]< [->+<]<[->+<]<[->
  55. +<]<[ ->+<]>>>>>+++++++[- >+++++++[-<<<<<<+ +>>>>>>]<]<<<<<.>
  56. .>.>. >.<<<<+.>>.<<--.<<< <<- .<<.>> >>>>>+.-------.>+
  57. ++++. ---....<++.>>>---.> .<<<< ++++++ .>>.<<--.<<<<<.<<
  58. .++++ ++++++.>>--.<+.>>>- -.>>> >>>.>. <<<<<.<++.<
  59. <.<<< ----------.>>>>>-.> >.<.< +.<<<< <++++++++.
  60. +.>>> >>>>>>>+++[->>++<<] >>-.< .<<<<< <++[- >>>>>>>>>
  61. >+>>> >+>+>+>+>+>+>+>+>>> +>>>> +>+>+> +>+>>> +<<<<<<<
  62. <<<<<< <<<<<<<<<<<<<<<<< <<<<<] <<<<[- >>>>>> >>>>>>>>
  63. >>+>>>> >>> >>> >>>>+<< <<<<<< <<<<<< <<<<<<<<
  64. <<<<<<<< ] > > >>>>>--- -[->>> >>+>+> >+>>>>>>
  65. >>>>>+>+>> +>> >>>>>>+>+> >+<<<< <<<<< <<<<<<<<
  66. <<<<<<<<<<<< <<<<] >>>[->>>>> >>+ >>>>>>>>>>
  67. >>>>+<<<<<<<<<<<<<<<<<<<<<]>[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+>+<<<<<<<<<<<<
  68. <<<<<<<<<<<<<<<<<<<]>+>->>-->-->>+++++>++++>--------->----------->---------
  69. ---->-------->--------------->++>---->-->>--->-->>+++++++>-------->------->
  70. ---->----------->---->-->>--->>++<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<[-]<[-]<[-]
  71. <[-]<[-],+[---------------------------------------------->[-]+<[>[-]<[->>+<
  72. <]]>[>>[-]>[-]<<<[>>+>[-]++++++++++[-<---------->]+<[>[-]<[-<<<+>>>]]<<<[->
  73. >>+<<<]>>>>[[-]<[-]>++++++++++[-<---------->]>>>>.<.<<.>>>>++.------------.
  74. -..+++++++++++.>.<<<<<<[-]]++++++++++[-<++++++++++>]<<,--------------------
  75. -------------------------<[-]+>[<[-]>[-<<+>>]]<<[->>+<<]>]>>>>>>>.<.<<.>>>>
  76. ++.-->>>>>>>>>>>>>>>>>>>>>>>>>>>[-]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<[->>>>>
  77. >>>>>>>>>>>>>>>>>> >>>>>>>>>>+<<<
  78. <<<<<<<<<<<<<<<<< <<<<<<<<<<<<<]>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>[->+<]
  79. >>>++++++++++[-< ++++++++++>]<<>>[-]>[-]>[-]>[-]>[-]>[- ]>[-]<<<<<<<<[
  80. ->>+>+<<<]>>>[- <<<+>>>]<<<>[->>+>+<<<]>>>[-<<<+>>>]< < <<>>>[>>[-]<<<
  81. [->>>+>>+<<<<< ] >>>>>[-<<<<<+>>>>>]<<<<[<->->>>+<<<< >> >>>>+<<<<<<[[-
  82. >>>>>+<<<<<]> >> >>>[-]<<<<<<]>>>>>[-<<<<<+>>>>>]<<< <<> >>>>>[[-]<<<<<
  83. [-]>>>>>]<<< <<] <[[->>>>>+<<<<<]>>+>>[-<<<+>>>]<<< <]>> >>>[-<<<<<+>>>
  84. >>]<<<<][-] <[-] >>[-<<+>>]>[-<<+>>]>[-]>[-]>[-]<< <<<<< <>[->>>+>+<<<<
  85. ]>>>>[-<<< <+>>> >]<<[->>+>+<<<]>>>[-<<<+>>>]<<[- >-<]>> [-]+<[>-<[-]]>
  86. [[-]<<<[- ]<+>>> >]<[-]<[-]<<<<>>>>[-]>[-]<<<<<[ ->>>>+> +<<<<<]>>>>>[-
  87. <<<<<+>> >>>]<>[ -]+<[>[-]<[-]]>[<<[-]<[-]<[->> +>+<<<]> >>[-<<<+>>>]>[
  88. -]]<<<< <>>[<<[-] <+>>>>[-<<<+>>
  89. >]++++ ++++[-<++ ++++>]<.[-]]<[-]++++++++++<>> [-]>[-]>[ -]>[-]>[-]>[-]
  90. >[-]<< <<<<<<[-> >+>+<<<]>>>[-<<<+>>>]<<<>[->> +>+<<<]>> >[-<<<+>>>]<<<
  91. <>>>[> >[-]<<<[- >>>+>>+<<<<<]>>>>>[-<<<<<+>>> >>]<<<<[< ->->>>+<<<<>>>
  92. >>>+<< <<<<[[->> >>>+<<<<<]>>>>>>[-]<<<<<<]>>> >>[-<<<<< +>>>>>]<<<<<>>
  93. >>>>[[ -]<<<<<[- ]>>>>>]<<<<<]<[[->>>>>+<<<<<] >>+>>[-<< <+>>>]<<<<]>>>
  94. >>[-<< <<<+>>>>> ]<<<<][-]<[-]>>[-<<+>>]>[-<<+ >>]>[-]>[ -]>[-]<<<<<<<<
  95. >[->>> +>+<<<<]> >>>[-<<<<+>>>>]<<[->>+>+<<<]> >>[-<<<+> >>]<<[->-<]>>[
  96. -]+<[> -<[-]]>[[ -]<<<[-]<+>>>>]<[-]<[-]<<<<>> >>[-]>[-] <<<<<[->>>>+>+
  97. <<<<<] >>>>>[-<< <<<+>>>>>]<>[-]+<[>[-]<[-]]>[ <<[-]<[-] <[->>+>+<<<]>>
  98. >[-<<< +>>>]>[-] ]<<<<<>[-]+>[<[-]<[-]>>>[-<<< +>>>]++++ ++++[-<++++++>
  99. ]<.[-] ]<[<<[[-] >>[-]++++++++[-<<++++++>>]<<. [-]]>>[-] ][-]++++++++[-
  100. <+++++ +>]<.<[-] >[-]>[-]>[-]>[-]>[-]>[-]>[-]> [-]>[-]<< <<<<<<<<<<<<<<
  101. <<<<<< <<<<<<<<< <<<<<.>.<<<<<<<<<<[-]>[-]]>[- <<+>>]<<+ +>[-]+<[>[-]<[
  102. ->>+<< ]]>[>>[-] >[-]<<<[>>+>[-]++++++++++[-<- --------- >]+<[>[-]<[-<<
  103. <+>>>] ]<<<[->>> +<<<]>>>>[[-]<
  104. [-]>++ ++++++++ [-<---------->]>>>>.<.<.>>>++. -------- ----.-..+++++++
  105. ++++.> .<<<<<< [-]]++++++++++[-<++++++++++>]<< ,------ ----------------
  106. ------ ------ ---------<[-]+>[<[-]>[-<<+>>]]<< [->>+< <]>]>>>>>>>.<.<.>
  107. >>++.- ->>>> >>>>>>>>>>>>>>>>>>>>>>>[-]<<<<<<< <<<<< <<<<<<<<<<<<<<<<<<
  108. <<<[-> >>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<< <<<< <<<<<<<<<<<<<<<<<<<
  109. <<<<<] >>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[->+< ]>> >++++++++++[-<++++++
  110. ++++>] << >>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<< <[ ->>+>+<<<]>>>[-<<<+>>
  111. >]<<<> [ ->>+>+<<<]>>>[-<<<+>>>]<<<<>>>[>>[-]< < <[->>>+>>+<<<<<]>>>>>[
  112. -<<<<< +>>>>>]<<<<[<->->>>+<<<<>>>>>>+<<<<<<[ [->>>>>+<<<<<]>>>>>>[-]
  113. <<<<<< ]>>>>>[-<<<<<+>>>>>]<<<<<>>>>>>[[-]<<<< <[-]>>>>>]<<<<<]<[[->>>>
  114. >+<<<< <]>>+>>[-<<<+>>>]<<<<]>>>
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++++++[-<<++++++>>]<<.[-]]>>[-]][-]++++++++[-<+
  129. +++++>]<.<[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<<<<<<<<<<<<<<<<<<
  130. <<<<<<<<<<.>.<<<<<<<<<<[-]>[-]]>[-<<+>>]<<-->[-]+<[>-<[->>+<<]]>[>>>>..>>>.
  131. <.>>.>.<<<<<<<<<[-]]>[-<<+>>]<<--------------->[-]+<[>-<[->>+<<]]>[>>>>>>.<
  132. <..>>>>.>.<<<<<<<<<[-]]>[-<<+>>]<<-->[-]+<[>-<[->>+<<]]>[>>>>>>.<..>>>.>.<<
  133. <<<<<<<[-]]>[-<<+>>]<<----------------------------->[-]+<[>-<[->>+<<]]>[>>>
  134. >>>>>>>>>>>>>>>>>>>>>.>.>.>.>.>.>.>.>.>.<<<<<<<<<<.<<<<<<<<<<<<<<<<<<<<<<<[
  135. -]]>[-<<+>>]<<-->[-]+<[>-<[->>+<<]]>[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.<<<
  136. <<<<<<<<.<<<<<<<<<<<<<<<<<<<<<<<[-]]>[-<<+>>]<<++++++++++++++++++++++++++++
  137. +++++++++++++++++++++>[-]+<[>-<[->>+<<]]>[>>>>>>>.<.>>++.-->>>>.>.<<.>>>.>.
  138. >.>.>.>>>.>.>.<<<<<<<<<<<<<<<<<<<<<<<[-]]>[-<<+>>]<<-->[-]+<[>-<[->>+<<]]>[
  139. >>>>>>.>>>>.>.>>>.>.>.>.>.>.>.>.>.>.<<<<<<<<<<<<<<<<<<<<<<<[-]]>[-<<+>>]<<+
  140. ++++++++++++++++++++++++++++++++++++++++++++++>[-]>[-]<+<[>[-]>,+<<[-]][-]>
  141. [>[-]<[-]]>[-<<+>>]<<]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.<<<<<<<<<<<.