/examples/rpn.bf

https://github.com/FabianM/brainfuck · Brainfuck · 156 lines · 133 code · 23 blank · 0 comment · 76 complexity · 685da1f099f8d5421faf5e1bb4dfda82 MD5 · raw file

  1. rpn calculator
  2. Patrick Schultz patrickschultz@usa.net
  3. Sat, 4 Aug 2001 02:36:57 -0700
  4. Hello everybody,
  5. I discovered brainfuck about a year ago, and in my first period of
  6. interest I wrote a calculator program with addition, subtraction,
  7. multiplication and division. It uses the reverse polish notation, so it
  8. takes input like this:
  9. 56 4 / 5 13 * + (the line must end with a end line, ascii
  10. 13, or the program will infinite loop)
  11. Now I have remembered this fun little language and want to try doing
  12. something else. I found this mailing list while surfing the web, and
  13. promptly joined. I though I would first share my program in case anyone
  14. is interested. The code, both with comments and without, is at the
  15. bottom of the message. The comments are from when I was working on the
  16. program, and I never intended them for anyone but myself, so they
  17. probably won't make much sense, so I apologize. I have only run this
  18. program on the online javascript bf interpreter, so I don't know for
  19. sure if it would run elsewhere.
  20. So now I am looking for a new project. Some ideas I have had are:
  21. write an interpreter for another language in brainfuck, implement the
  22. encryption algorithm RC4, or perhaps a library of sorts, with various
  23. useful algorithms and data structures. If anybody has any ideas, things
  24. you were thinking about or wanted to see done, or even better, would
  25. like to work on something with me, I would really like to hear.
  26. Anyways, here is the code:
  27. +[ set continue flag to true and start loop
  28. -, clear continue flag and get input
  29. >+>+>+>+>+>+<<<<<< set five bools
  30. -------------
  31. [>-<------------------- if not 13 (ret) sub 19
  32. [>>-<<---------- if not 32 (space) clear first bool
  33. [>>>-<<<- if not 42 (mul) clear second bool
  34. [>>>>-<<<<-- if not 43 (add) clear third bool
  35. [>>>>>-<<<<<-- if not 45 (sub) clear fourth bool
  36. [>>>>>>[-]>+<<<<<<<-]]]]]] if not 47 (div) clear fifth
  37. bool and move remaining number (digit plus one) to sixth spot
  38. >[->->->->->-<<<<<] if input was a ret
  39. clear rest of bools and do not reset continue flag
  40. >[->->->->-<<<<<<+>>] if input was a
  41. space clear rest of bools and reset continue flag
  42. >[->->->-<<<<<<<-<- if
  43. input was (mul) clear rest of bools and go back to second to last num on
  44. stack
  45. [>[>+>+<<-]>>[<<+>>-]<<<-]+>[-]+>[<<+>>-]>>]
  46. multiply last two items together leaving answer in spot of first; reset
  47. continue flag
  48. >[->->-<<<<<<< if
  49. input was (add) clear rest of bools and go back to last num on stack
  50. -[<+>-]+>>>>] add
  51. last two items together; reset continue flag
  52. >[->-<<<<<<< if input was
  53. (sub) clear rest of bools and go back to last num on stack
  54. -[<->-]+>>>>>] sub last item from
  55. second to last; reset continue flag
  56. >[-<<<<<<< if input was (div) clear
  57. div bool and go back to second to last num on stack
  58. -[>+>+<<-]>->[<<+>>-]<<<- copy B to (1)
  59. using (2) as tmp storage
  60. [
  61. decrement through A; add one to (3) every Bth time
  62. >>>>+<< (3) is
  63. bool; says if (1)==0
  64. [>+>[-]<<-]>[<+>-] if (1) != 0
  65. set (3) to 0
  66. >[-<<<[>+>+<<-]>>[<<+>>-]>>+<] if (1)=0 reset (1)
  67. and add one to (4)
  68. <<-<<-
  69. ]
  70. +>[-]+>[-]>>>[<<<<<+>>>>>-]>> clear A and B
  71. and all temporary data and move answer in spot of A
  72. ]
  73. >[ if input was first digit of a number
  74. <+>> a one in case input is zero; Resulting
  75. number ends up in the space the "1" is in
  76. ,>++++++++[<---->-]< get digit and subtract 32 from it to
  77. test for space
  78. [ begin main input loop
  79. >+++++[<--->-] subtract 15 from remaining number
  80. leaving the inputed number plus one
  81. ,>++++++++[<---->-]< get next digit and subtract 32
  82. ]
  83. <[<]>> go back to first digit inputed after
  84. the beginning "1"
  85. [<-[>++++++++++<-]+>>] multiply digits together leaving "1"
  86. in every spot
  87. <[[<]>+[>]<-]<[-<]> move number back to begginning clear trail
  88. of ones; pntr is at num
  89. [<<<<<<+>>>>>>-] move number back to top of stack
  90. <<<<<+>>>>>>>] reset continue flag
  91. <<<<<<<]move pntr back to continue flag and end loop
  92. <[>>+<<-]> move answer two spaces forward to make sure
  93. printer has room
  94. +>-[>+<<[-]>-]>[<+>-]<if number is zero set spot before num to 1
  95. [while the remainder is not 0; call
  96. current p 0
  97. >+++++++++< (1)=9
  98. [ decrement through the number; adding one
  99. to (4) every tenth time
  100. >>>+<< (3) is a boolean; says if (1)==0; (3)=1;
  101. [>+>[-]<<-] if (1) != 0 { (3)=0 }; moves (1) to (2);
  102. >[<+>-]move (2) back to (1);
  103. >[<<++++++++++>>>+<-] if (3) == true { (1)=10; increment (4)
  104. (dividend)
  105. <<-<- dec (1) and (0);
  106. ]
  107. <++++++++++ set spot before (0) to 10
  108. >>[<<->>-]subtract rem in (1) from 9; making spot
  109. before (0) one more than correct digit
  110. >>[-]>[<<<+>>>-]clear (3) and move (4) to (1)
  111. <<<
  112. ]
  113. <[>]< go to first digit to be displayed; handles case
  114. if number was zero
  115. [->++++++++[<++++++>-]<.[-]<]>> print sequence of digits backwards;
  116. clearing each one; leaving pntr at original spot
  117. and without comments (this is much more convenient for using with the
  118. online interpreter):
  119. +
  120. [-,>+>+>+>+>+>+<<<<<<-------------[>-<-------------------[>>-<<----------[
  121. >>>-<<<-[>>>>-<<<<--[>>>>>-<<<<<--[>>>>>>[-]>+<<<<<<<-]]]]]]>
  122. [->->->->->-<<<<<]>[->->->->-<<<<<<+>>]>
  123. [->->->-<<<<<<<-<-[>[>+>+<<-]>>[<<+>>-]<<<-]+>[-]+>[<<+>>-]>>]>
  124. [->->-<<<<<<<-[<+>-]+>>>>]>[->-<<<<<<<-[<->-]+>>>>>]>
  125. [-<<<<<<<-[>+>+<<-]>->[<<+>>-]<<<-[>>>>+<<[>+>[-]<<-]>[<+>-]>
  126. [-<<<[>+>+<<-]>>[<<+>>-]>>+<]<<-<<-]+>[-]+>
  127. [-]>>>[<<<<<+>>>>>-]>>]>[<+>>,>++++++++[<---->-]<[>+++++[<--->-],>++++++++
  128. [<---->-]<]<[<]>>[<-[>++++++++++<-]+>>]<[[<]>+[>]<-]<
  129. [-<]>[<<<<<<+>>>>>>-]<<<<<+>>>>>>>]<<<<<<<]<[>>+<<-]>+>-[>+<<
  130. [-]>-]>[<+>-]<[>+++++++++<[>>>+<<[>+>
  131. [-]<<-]>[<+>-]>[<<++++++++++>>>+<-]<<-<-]<++++++++++>>[<<->>-]>>
  132. [-]>[<<<+>>>-]<<<]<[>]<[->++++++++[<++++++>-]<.[-]<]>>
  133. -Patrick