Debian Codegraph @

lib/Acme/Brainfuck.pm

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #
2 # See POD documentation below for description, copyright and licensing info.
3 #
4 # $Id: Brainfuck.pm,v 1.5 2002/09/23 02:26:31 jaldhar Exp $
5 #
6 package Acme::Brainfuck;
7 use Filter::Simple;
8 use strict;
9 use warnings;
10
11 #remember to change this in the POD too.
12 our $VERSION = '1.1.1';
13
14 # The memory pointer and memory cells of our Turing machine.
15 our $p = 0;
16 our @m = ();
17
18 # The basic Brainfuck instructions. Extras will be added in import().
19 our $ops = '+-<>,.[]';
20
21 # Whether or not we accept extra instructions.
22 our $verbose = 0;
23
24 # print out filtered text?
25 our $debug = 0;
26
27 sub import()
28 {
29 shift;
30 foreach (@_)
31 {
32 if (/^verbose$/)
33 {
34 $ops .= '~#';
35 $verbose = 1;
36 }
37 if (/^debug$/)
38 {
39 $debug = 1;
40 }
41 }
42 }
43
44 FILTER_ONLY code => sub
45 {
46 my $ret = $_;
47 while ($ret =~ /\s ([\Q$ops\E]+) \s/gsx)
48 {
49 my $code = $1;
50 my $len = length($1);
51 my $at = pos($ret) - ($len + 1);
52
53 $code =~ s/^/do { /g;
54 $code =~ s/$/P; }; /g;
55 $code =~ s/(\++)/"P += ".length($1).";" /eg;
56 $code =~ s/(\-+)/"P -= ".length($1).";" /eg;
57 $code =~ s/(<+)/"\$Acme::Brainfuck::p -= ".length($1).";" /eg;
58 $code =~ s/(>+)/"\$Acme::Brainfuck::p += ".length($1).";" /eg;
59 $code =~ s/\./print chr P; /g;
60 $code =~ s/,/P = ord getc;/g;
61 $code =~ s/\[/while(P){/g;
62 $code =~ s/\]/}; /g;
63 if ($verbose)
64 {
65 $code =~
66 s/~/\$Acme::Brainfuck::p = 0;\@Acme::Brainfuck::m = (); /g;
67 $code =~
68 s/\#/print STDERR sprintf\('\$p = %d \$m[\$p]= %d', \$Acme::Brainfuck::p, P\), "\\n"; /g;
69 }
70 $code =~ s/P/\$Acme::Brainfuck::m\[\$Acme::Brainfuck::p\]/g;
71 substr($ret, $at, $len, $code);
72 }
73 $_ = $ret;
74 print $_ if $debug;
75 };
76
77 1;
78
79 __END__
80
81 =pod
82
83 =head1 NAME
84
85 Acme::Brainfuck - Embed Brainfuck in your perl code
86
87 =head1 SYNOPSIS
88
89 #!/usr/bin/env perl
90 use Acme::Brainfuck;
91
92 print 'Hello world!', chr ++++++++++. ;
93
94 =head1 DESCRIPTION
95
96 Brainfuck is about the tiniest Turing-complete programming language you
97 can get. A language is Turing-complete if it can model the operations of
98 a Turing machine--an abstract model of a computer defined by the British
99 mathematician Alan Turing in 1936. A Turing machine consists only of an
100 endless sequence of memory cells and a pointer to one particular memory
101 cell. Yet it is theoretically capable of performing any computation. With
102 this module, you can embed Brainfuck instructions delimited by whitespace
103 into your perl code. It will be translated into Perl as parsed.
104 Brainfuck has just just 8 instructions (well more in this implementation,
105 see L</"Extensions to ANSI Brainfuck"> below.) which are as follows
106
107 =head2 Instructions
108
109 =over 4
110
111 =item + Increment
112
113 Increase the value of the current memory cell by one.
114
115 =item - Decrement
116
117 Decrease the value of the current memory cell by one.
118
119 =item > Forward
120
121 Move the pointer to the next memory cell.
122
123 =item < Back
124
125 Move the pointer to the previous memory cell.
126
127 =item , Input
128
129 Read a byte from Standard Input and store it in the current memory cell.
130
131 =item . Output
132
133 Write the value of the current memory cell to standard output.
134
135 =item [ Loop
136
137 If the value of the current memory cell is 0, continue to the cell after
138 the next ']'.
139
140 =item ] Next
141
142 Go back to the last previous '['.
143
144 =back
145
146 =head2 Extensions to ANSI Brainfuck
147
148 This implementation has extra instructions available. In order to avoid such
149 terrible bloat, they are only available if you use the I<verbose> pragma like
150 so:
151
152 use Acme::Brainfuck qw/verbose/;
153
154 The extra instructions are:
155
156 =over 4
157
158 =item ~ Reset
159
160 Resets the pointer to the first memory cell and clear all memory cells.
161
162 =item # Peek
163
164 Prints the values of the memory pointer and the current memory cell to
165 STDERR. See also L</"Debugging"> below.
166
167 =back
168
169 =head2 Debugging
170
171 By using the I<debug> pragma like this:
172
173 use Acme::Brainfuck qw/debug/;
174
175 you can dump out the generated perl code. (Caution: it is not pretty.)
176 The key to understanding it is that the memory pointer is represented by
177 I<$p>, and the memory array by I<@m> Therefore the value of the current
178 memory cell is I<$m[$p]>.
179
180 =head1 RETURN VALUE
181
182 Each sequence of Brainfuck instructions becomes a Perl block and returns the
183 value of the current memory cell.
184
185 =head1 EXAMPLES
186
187 =head2 JABH
188
189 #!/usr/bin/env perl
190 use Acme::Brainfuck;
191 print "Just another ";
192 ++++++[>++++++++++++++++<-]>
193 ++.--
194 >+++[<++++++>-]<.>[-]+++[<------>-]<
195 +.-
196 +++++++++.---------
197 ++++++++++++++.--------------
198 ++++++.------
199 >+++[<+++++++>-]<.>[-]+++[<------->-]<
200 +++.---
201 +++++++++++.-----------
202 print " hacker.\n";
203
204 =head2 Countdown
205
206 #!/usr/bin/env perl
207 use strict;
208 use Acme::Brainfuck qw/verbose/;
209
210 print "Countdown commencing...\n";
211 ++++++++++[>+>+<<-]
212 >>+++++++++++++++++++++++++++++++++++++++++++++++<<
213 ++++++++++[>>.-<.<-]
214 print "We have liftoff!\n";
215
216 =head2 Reverse
217
218 #!/usr/bin/env perl
219 use Acme::Brainfuck qw/verbose/;
220
221 while(1)
222 {
223 print "Say something to Backwards Man and then press enter: ";
224 +[->,----------]<
225 print 'Backwards Man says, "';
226 [+++++++++++.<]<
227 print "\" to you too.\n";
228 ~
229 }
230
231 =head2 Math
232
233 #!/usr/bin/env perl
234 use Acme::Brainfuck;
235 use strict;
236 use warnings;
237
238 my $answer = +++[>++++++<-]> ;
239
240 print "3 * 6 = $answer \n";
241
242 =head1 VERSION
243
244 1.1.1 Apr 06, 2004
245
246 =head1 AUTHOR
247
248 Jaldhar H. Vyas E<lt>[email protected]<gt>
249
250 =head1 THANKS
251
252 Urban Mueller - The inventor of Brainfuck.
253
254 Damian Conway - For twisting perl to hitherto unimaginable heights of
255 weirdness.
256
257 Marco Nippula E<lt>http://www.hut.fi/~mnippula/E<gt> - Some code in this
258 module comes from his F<brainfuck.pl>
259
260 Mr. Rock - Who has a nice Brainfuck tutorial at
261 L<http://www.cydathria.com/bf/>. Some of the example code comes from there.
262
263 =head1 COPYRIGHT AND LICENSE
264
265 Copyright (c) 2004, Consolidated Braincells Inc.
266 Licensed with no warranties under the Crowley Public License:
267
268 "Do what thou wilt shall be the whole of the license."
269
270 =cut