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