# New Ticket Created by Leon Brocard
# Please include the string: [perl #18566]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=18566 >
I've posted a Brainfuck interpreter on list in the past, but it kept
on getting made out of date. I think it thus makes sense to host it in
the Parrot distribution, in languages/brainfuck/, so please check out
the attached files.
Leon
ps You might be concerned about the name. Well, CPAN has a module
which matches /fuck/ too. However, if everyone really thinks
it is a problem, I don't see a problem with s/fuck/funk/g
--
Leon Brocard.............................http://www.astray.com/
scribot.................................http://www.scribot.com/
....... It's not in the manual!
-- attachment 1 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/42090/33846/6c1a24/Makefile
-- attachment 2 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/42090/33847/41f578/README
-- attachment 3 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/42090/33848/77ffa0/brainfuck.pasm
-- attachment 4 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/42090/33849/4bb8de/countdown.bf
-- attachment 5 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/42090/33850/c55c35/helloworld.bf
-- attachment 6 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/42090/33851/cbedc3/test.bf
ASSEMBLE=perl ../../assemble.pl
PARROT=../../parrot
all: build
test: build
$(PARROT) brainfuck.pbc test.bf
build: brainfuck.pasm
$(ASSEMBLE) brainfuck.pasm > brainfuck.pbc
clean:
rm -f core *.pbc *~
DESCRIPTION
This is a Brainfuck interpreter for Parrot. Brainfuck is a full
computer programming language with only eight commands. For more
information, see http://www.catseye.mb.ca/esoteric/bf/
To compile the files:
$ make
To check that the compiler is working:
$ make test
To run an example Brainfuck program:
$ ../../parrot brainfuck.pbc helloworld.bf
AUTHOR
Leon Brocard <[EMAIL PROTECTED]>
# A Brainfuck interpreter
# By Leon Brocard <[EMAIL PROTECTED]>
#
# See http://www.catseye.mb.ca/esoteric/bf/
# for more information on this silly language
# Get the brainfuck source file into S0
set S0, P0[1]
if S0, SOURCE
set S0, P0[0]
print "usage: ./parrot "
print S0
print " file.bf\n"
end
# Read the file into S1
SOURCE:
open I0, S0
SOURCE_LOOP:
readline S2, I0
concat S1, S2
if S2, SOURCE_LOOP
close I0
length I30, S1
# Initialise
set I0, 0 # Our PC
new P0, .PerlArray # Our memory
set I1, 0 # Our pointer
# The main interpreter loop
INTERP:
substr S0, S1, I0, 1
ne S0, "+", NOTPLUS
set I2, P0[I1]
inc I2
set P0[I1], I2
branch NEXT
NOTPLUS:
ne S0, "-", NOTMINUS
set I2, P0[I1]
dec I2
set P0[I1], I2
branch NEXT
NOTMINUS:
ne S0, ">", NOTGT
inc I1
branch NEXT
NOTGT:
ne S0, "<", NOTLT
dec I1
branch NEXT
NOTLT:
ne S0, "[", NOTOPEN
set I2, P0[I1]
if I2, NEXT
set I2, 0 # "depth"
OPEN_LOOP:
inc I0
substr S2, S1, I0, 1
ne S2, "[", OPEN_NOTOPEN
inc I2
branch OPEN_LOOP
OPEN_NOTOPEN:
ne S2, "]", OPEN_LOOP
eq I2, 0, NEXT
dec I2
branch OPEN_LOOP
NOTOPEN:
ne S0, "]", NOTCLOSE
set I2, 0 # "height"
CLOSE_LOOP:
dec I0
substr S2, S1, I0, 1
ne S2, "]", CLOSE_NOTCLOSE
inc I2
branch CLOSE_LOOP
CLOSE_NOTCLOSE:
ne S2, "[", CLOSE_LOOP
eq I2, 0, INTERP
dec I2
branch CLOSE_LOOP
NOTCLOSE:
ne S0, ".", NOTDOT
set I2, P0[I1]
chr S31, I2
print S31
branch NEXT
NOTDOT:
ne S0, ",", NEXT
readline S31, 0
ord I2, S31
set P0[I1], I2
branch NEXT
NEXT:
inc I0
le I0, I30, INTERP
end
++++++++++++++++++++++++++++++++[>+>+<<-]
>>+++++++++++++++++++++++++<<
++++++++++[>>.-<.<-]
>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]
<.>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[
<++++>-]<+.[-]++++++++++.
Simple Perlstyle test script by Leon Brocard <[EMAIL PROTECTED]>
+++++++++++++++++++++++++++++++++++++++++++++++++.
---.
....
+++.
---------------------------------------.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
----.
---------------------------------------------------------------------------.
+++++++++++++++++.
---------------------------------------.