source: trunk/Examples/Others/rpn.b

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