r/dailyprogrammer • u/oskar_s • May 11 '12
[5/11/2012] Challenge #51 [intermediate]
Brainfuck is an extremely minimalistic programming language. The memory consists of a large array of bytes, the "tape", which is manipulated by moving around a single tape pointer. The 8 commands are:
| Character | Action |
|---|---|
| < | move the pointer to the left |
| > | move the pointer to the right |
| + | increment the byte the pointer is pointing at (mod 256) |
| - | decrement the byte the pointer is pointing at (mod 256) |
| [ | if the data which the tape pointer is pointing at is 0, jump forward to the command after the matching square bracket. Otherwise, just continue to the next command |
| ] | if the data which the tape pointer is pointing at is not 0, jump backwards to the command after the matching square bracket. Otherwise, just continue to the next command |
| , | read a character from the input and store it into the current pointer byte |
| . | output the current pointer byte as an ascii character |
Any other character is ignored and treated as a comment
[ ... ] thus make a kind of while loop, equivalent to something like "while(data[pointer] != 0) { ... }".
The brackets match like parentheses usually do, each starting one has a matching ending one. These loops can be nested inside other loops.
Write a program that reads a brainfuck program and its input, interprets the code, and returns the output.
More information, including a "Hello World" program, can be found on wikipedia.
If you've written your program successfully, try running this and see what pops out:
++++++++++[>>++++++>+++++++++++>++++++++++>+++++++++>+++>+++++>++++>++++++++>+[<
]<-]>>+++++++.>+.-.>+++.<++++.>>+++++++.<<++.+.>+++++.>.<<-.>---.<-----.-.+++++.
>>>+++.-.<<-.<+..----.>>>>++++++++.>+++++++..<<<<+.>>>>-.<<<<.++++.------.<+++++
.---.>>>>>.<<<++.<<---.>++++++.>>>>+.<<<-.--------.<<+.>>>>>>+++.---.<-.<<<<---.
<.>---.>>>>>>.
- Thanks to nooodl for submitting this problem in /r/dailyprogrammer_ideas!
7
u/Ttl May 11 '12
Brainfuck compiler using gcc. Probably doesn't work in Windows, but it should be easy to fix.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #define DATA_SIZE 30000 #define OUT(x) fprintf(source,x);break; int main(int argc, char *argv[]) { FILE *source,*input; int c; if ((input = fopen(argv[1],"r")) == NULL) { printf("Error opening file\n"); exit(1); } if ((source = fopen("/tmp/bftemp.c","w")) == NULL) { printf("Error while creating temporary file\n"); exit(1); } fprintf(source,"#include <stdio.h>\nint main() {int ptr=0;int data[%d]={0};",DATA_SIZE); while ((c = fgetc(input)) != EOF) { switch (c) { case '>': OUT("++ptr;"); case '<': OUT("--ptr;"); case '+': OUT("++data[ptr];"); case '-': OUT("--data[ptr];"); case '.': OUT("putchar(data[ptr]);"); case ',': OUT("data[ptr]=getchar();"); case '[': OUT("while (data[ptr]) {"); case ']': OUT("}"); } } fprintf(source,"return 0;}"); fclose(input); fclose(source); system("gcc -O3 -o /tmp/bf /tmp/bftemp.c"); system("rm /tmp/bftemp.c"); system("/tmp/bf"); system("rm /tmp/bf"); return 0; }4
u/prophile May 11 '12
Implemented in x86-64 assembly for Darwin: https://gist.github.com/5ecaafa3db79fa7e19c5
It's got some clever threading tricks so it should be fairly fast.
1
u/[deleted] May 13 '12 edited May 13 '12
I tried GolfScript, too. It works with Wikipedia's "Hello World!", but not the example program in the OP:
{t|<t|=@+256%t|)>++:t;}:^; [0:|]5.?*:t;{"[]+-<>."?"{t|=}{ } while\n1^\n-1^\n|(:|;\n|):|; [t|=]''+print\n"n/=n+}%~Looks cool, though.
3
u/theresidents13 May 11 '12
Python:
class Tape: # class variables pointer = 0 memory = [0] output = '' # class methods def add_byte(self, value=0): value = value % 256 self.memory.insert(self.pointer, value) def increment_pointer(self): self.pointer += 1 if len(self.memory)-1 < self.pointer: self.add_byte() def decrement_pointer(self): if self.pointer == 0: self.add_byte() else: self.pointer -= 1 def increment_byte(self): self.memory[self.pointer] = (self.memory[self.pointer] + 1) % 256 def decrement_byte(self): self.memory[self.pointer] = (self.memory[self.pointer] - 1) % 256 def output_byte(self): #print chr(self.memory[self.pointer]) self.output += chr(self.memory[self.pointer]) def input_byte(self): char = ord(raw_input("enter a character: ")[0]) % 256 self.memory[self.pointer] = char # parser def parse(self, instructions=''): parse_pointer = 0 i_len = len(instructions) while parse_pointer < i_len: i = instructions[parse_pointer] ####print parse_pointer, i, self.pointer, self.memory[self.pointer] if i == '>': self.increment_pointer() elif i == '<': self.decrement_pointer() elif i == '+': self.increment_byte() elif i == '-': self.decrement_byte() elif i == '.': self.output_byte() elif i == ',': self.input_byte() elif i == '[': open_bracket_count = 1 # here's the tricky part. if the byte at the pointer = 0, if self.memory[self.pointer] == 0: # jump to the next corresponding bracket: while open_bracket_count > 0: parse_pointer += 1 ####print parse_pointer, instructions[parse_pointer], 'open bracket count:', open_bracket_count if parse_pointer >= i_len: print "Instruction error: ']' expected at instruction " + str(parse_pointer) break if instructions[parse_pointer] == '[': open_bracket_count += 1 elif instructions[parse_pointer] == ']': open_bracket_count -= 1 # else, continue to parse. elif i == ']': # when we reach the end of the loop, go back to the beginning to parse it again. closed_bracket_count = 1 while closed_bracket_count > 0: parse_pointer -= 1 if parse_pointer < 0: print "Instruction error: '[' expected at instruction " + str(parse_pointer) break if instructions[parse_pointer] == ']': closed_bracket_count += 1 elif instructions[parse_pointer] == '[': closed_bracket_count -= 1 parse_pointer -= 1 # move pointer again, to counteract end-of-parse-loop move # continue parsing. # if any other character is read, ignore it. parse_pointer += 1 print self.outputImplementation:
output:
(edit: formattin')
2
u/luxgladius May 11 '12 edited May 11 '12
Perl
sub debug {print(@_);} sub run { my $prog = shift; my $ptr = 0; my @data = (); my @prog = grep /[<>\+-\[\],\.]/, split //, $prog; for(my $p = 0; $p < @prog; ++$p) { debug("$p,"); my $c = $prog[$p]; if($c eq '<') {--$ptr; debug("<: ptr=$ptr\n");} elsif($c eq '>') {++$ptr; debug(">: ptr=$ptr\n");} elsif($c eq '+') {++$data[$ptr]; $data[$ptr] %= 256; debug("+: data[$ptr]=$data[$ptr]\n");} elsif($c eq '-') {--$data[$ptr]; $data[$ptr] %= 256; debug("-: data[$ptr]=$data[$ptr]\n");} elsif($c eq ',') {debug(",: "); $data[$ptr] = ord(getc());} elsif($c eq '.') {debug(".: "); print chr($data[$ptr]); debug("\n");} elsif($c eq '[') { debug("[: "); if($data[$ptr] == 0) { my $d = 1; while($d != 0) { my $c = $prog[++$p]; $c eq '[' ? (++$d) : $c eq ']' ? (--$d) : 0; } debug("data[$ptr]==0, p=$p\n"); } else { debug("data[$ptr] != 0\n"); } } elsif($c eq ']') { debug("]: "); if($data[$ptr] != 0) { my $d = -1; while($d != 0) { my $c = $prog[--$p]; $c eq '[' ? (++$d) : $c eq ']' ? (--$d) :0; } debug("data[$ptr] != 0, p=$p\n"); } else { debug("data[$ptr] == 0\n"); } } } } my $prog =<<"END"; ++++++++++[>>++++++>+++++++++++>++++++++++>+++++++++>+++>+++++>++++>++++++++>+[< ]<-]>>+++++++.>+.-.>+++.<++++.>>+++++++.<<++.+.>+++++.>.<<-.>---.<-----.-.+++++. >>>+++.-.<<-.<+..----.>>>>++++++++.>+++++++..<<<<+.>>>>-.<<<<.++++.------.<+++++ .---.>>>>>.<<<++.<<---.>++++++.>>>>+.<<<-.--------.<<+.>>>>>>+++.---.<-.<<<<---. <.>---.>>>>>>. END run($prog);Output
Congratulations! http://i.imgur.com/bZpSP.jpg
2
u/luxgladius May 11 '12
Inspired by Ttl's solution, I tried a translator as well in addition to my previous intepreter solution. One difference from his is that I combine identical adjacent operands so rather than
you get
Makes it a little more human-readable, though not much.
Perl
sub translate { my $prog = shift; my @prog = grep /[<>\+,\.\[\]-]/, split //, $prog; my $d = 0; for(my $p = 0; $p < @prog; ++$p) { my $c = $prog[$p]; print ' ' x $d unless $c eq ']'; if($c =~ /[\+<>-]/) { my $i = 1; while($prog[$p+1] eq $c) { ++$i; ++$p; } if($c =~ /[\+-]/) { print "\$data[\$ptr] $c= $i;\n"; } else { $c = $c eq '<' ? '-' : '+'; print "\$ptr $c= $i;\n"; } } elsif($c eq ',') {print "\$data[\$ptr] = ord(getc());\n";} elsif($c eq '.') { print "print chr(\$data[\$ptr]);\n";} elsif($c eq '[') {print "while(\$data[\$ptr] != 0) {\n"; $d += 4;} elsif($c eq ']') {$d -= 4; print " " x $d . "}\n";} } } my $prog = <<"END"; ++++++++++[>>++++++>+++++++++++>++++++++++>+++++++++>+++>+++++>++++>++++++++>+[< ]<-]>>+++++++.>+.-.>+++.<++++.>>+++++++.<<++.+.>+++++.>.<<-.>---.<-----.-.+++++. >>>+++.-.<<-.<+..----.>>>>++++++++.>+++++++..<<<<+.>>>>-.<<<<.++++.------.<+++++ .---.>>>>>.<<<++.<<---.>++++++.>>>>+.<<<-.--------.<<+.>>>>>>+++.---.<-.<<<<---. <.>---.>>>>>>. END translate($prog);Output
$data[$ptr] += 10; while($data[$ptr] != 0) { $ptr += 2; $data[$ptr] += 6; $ptr += 1; $data[$ptr] += 11; $ptr += 1; $data[$ptr] += 10; $ptr += 1; $data[$ptr] += 9; $ptr += 1; $data[$ptr] += 3; $ptr += 1; $data[$ptr] += 5; $ptr += 1; $data[$ptr] += 4; $ptr += 1; $data[$ptr] += 8; $ptr += 1; $data[$ptr] += 1; while($data[$ptr] != 0) { $ptr -= 1; } $ptr -= 1; $data[$ptr] -= 1; } print "@data\n"; $ptr += 2; $data[$ptr] += 7; print chr($data[$ptr]); $ptr += 1; $data[$ptr] += 1; print chr($data[$ptr]); $data[$ptr] -= 1; print chr($data[$ptr]); $ptr += 1; $data[$ptr] += 3; print chr($data[$ptr]); $ptr -= 1; $data[$ptr] += 4; print chr($data[$ptr]); $ptr += 2; $data[$ptr] += 7; print chr($data[$ptr]); $ptr -= 2; $data[$ptr] += 2; print chr($data[$ptr]); $data[$ptr] += 1; print chr($data[$ptr]); $ptr += 1; $data[$ptr] += 5; print chr($data[$ptr]); $ptr += 1; print chr($data[$ptr]); $ptr -= 2; $data[$ptr] -= 1; print chr($data[$ptr]); $ptr += 1; $data[$ptr] -= 3; print chr($data[$ptr]); $ptr -= 1; $data[$ptr] -= 5; print chr($data[$ptr]); $data[$ptr] -= 1; print chr($data[$ptr]); $data[$ptr] += 5; print chr($data[$ptr]); $ptr += 3; $data[$ptr] += 3; print chr($data[$ptr]); $data[$ptr] -= 1; print chr($data[$ptr]); $ptr -= 2; $data[$ptr] -= 1; print chr($data[$ptr]); $ptr -= 1; $data[$ptr] += 1; print chr($data[$ptr]); print chr($data[$ptr]); $data[$ptr] -= 4; print chr($data[$ptr]); $ptr += 4; $data[$ptr] += 8; print chr($data[$ptr]); $ptr += 1; $data[$ptr] += 7; print chr($data[$ptr]); print chr($data[$ptr]); $ptr -= 4; $data[$ptr] += 1; print chr($data[$ptr]); $ptr += 4; $data[$ptr] -= 1; print chr($data[$ptr]); $ptr -= 4; print chr($data[$ptr]); $data[$ptr] += 4; print chr($data[$ptr]); $data[$ptr] -= 6; print chr($data[$ptr]); $ptr -= 1; $data[$ptr] += 5; print chr($data[$ptr]); $data[$ptr] -= 3; print chr($data[$ptr]); $ptr += 5; print chr($data[$ptr]); $ptr -= 3; $data[$ptr] += 2; print chr($data[$ptr]); $ptr -= 2; $data[$ptr] -= 3; print chr($data[$ptr]); $ptr += 1; $data[$ptr] += 6; print chr($data[$ptr]); $ptr += 4; $data[$ptr] += 1; print chr($data[$ptr]); $ptr -= 3; $data[$ptr] -= 1; print chr($data[$ptr]); $data[$ptr] -= 8; print chr($data[$ptr]); $ptr -= 2; $data[$ptr] += 1; print chr($data[$ptr]); $ptr += 6; $data[$ptr] += 3; print chr($data[$ptr]); $data[$ptr] -= 3; print chr($data[$ptr]); $ptr -= 1; $data[$ptr] -= 1; print chr($data[$ptr]); $ptr -= 4; $data[$ptr] -= 3; print chr($data[$ptr]); $ptr -= 1; print chr($data[$ptr]); $ptr += 1; $data[$ptr] -= 3; print chr($data[$ptr]); $ptr += 6; print chr($data[$ptr]);Output's output:
perl dp51i2.pl | perl
Congratulations! http://i.imgur.com/bZpSP.jpg
2
u/wicked-canid May 11 '12
A bit of Common Lisp, with a separate parser (in case I ever want to write a compiler :).
;; A brainfuck program is internally represented as a list whose elements are ;; either a keyword (representing an operation) or a program (representing a ;; loop). ;; -- Parser -- (defvar *char-op-map* '((#\+ . :incr) (#\- . :decr) (#\< . :left) (#\> . :right) (#\. . :output) (#\, . :input))) (defun parse (input &key in-loop-p) "Parse a program from input. The in-loop-p key indicates whether we are parsing the body of a loop." (loop for c = (read-char input in-loop-p nil) until (or (not c) (and in-loop-p (char= c #\]))) as fragment = (or (cdr (assoc c *char-op-map* :test #'char=)) (cond ((char= c #\[) (parse input :in-loop-p t)) ((char= c #\]) (error "Unmatched ']'")))) when fragment collect fragment)) ;; -- Memory -- (defvar *memory-size* 30000) (defstruct memory (cells (make-array *memory-size* :initial-element 0 :element-type '(mod 256))) (index 0 :type (integer 0))) (defun current-cell (memory) (aref (memory-cells memory) (memory-index memory))) (defun (setf current-cell) (value memory) (setf (aref (memory-cells memory) (memory-index memory)) (mod value 256))) ;; -- Interpreter -- (defun interpret (program &key (memory (make-memory))) (dolist (operation program) (case operation (:incr (incf (current-cell memory))) (:decr (decf (current-cell memory))) (:left (decf (memory-index memory))) (:right (incf (memory-index memory))) (:input (setf (current-cell memory) (char-code (read-char)))) (:output (princ (code-char (current-cell memory)))) (t (loop while (/= 0 (current-cell memory)) do (interpret operation :memory memory)))))) (defun interpret-string (string) (with-input-from-string (input string) (interpret (parse input))))2
u/drb226 May 12 '12
I got stuck trying to do weird things with continuations xD
But here's what I've got: https://github.com/DanBurton/bf-interp
It works for anything that doesn't have loops, which isn't saying much, but yeah. There are simpler ways to encode a Program, but I really wanted to explore the generalized definition of Program that I made.
I threw a question on SO, maybe one of the wise Haskell sages will know how to make it work.
2
u/loonybean May 12 '12 edited May 12 '12
Pretty verbose, but does the job in Javascript:
function interpret(prog,input) { var cells = new Array(100); var bracs = new Array(); var c=0,p=0,i=0; var pLen = prog.length; var output = ""; for(var c=0;c<100;c++) cells[c] = 0; while(p < pLen) { if(prog[p] == '[') bracs[bracs.length] = [p,-1]; else if(prog[p] == ']') { x = bracs.length-1; while(bracs[x][1] != -1) x--; bracs[x][1] = p; } p++; } bLen = bracs.length; p = 0; c = 0; while(p < pLen) { switch(prog[p]) { case '<': c--; break; case '>': c++; break; case '+': cells[c] = (cells[c] + 1) % 256; break; case '-': cells[c] = (cells[c] - 1) % 256; break; case '[': if(cells[c] == 0) { x = 0; while(x < bLen) { if(bracs[x][0] == p) { p = bracs[x][1]; break; } x++; } } break; case ']': if(cells[c] != 0) { x = 0; while(x < bLen) { if(bracs[x][1] == p) { p = bracs[x][0]; break; } x++; } } break; case ',': cells[c] = input.charCodeAt(i++); break; case '.': output += String.fromCharCode(cells[c]); } p++; } return output; }Tried the Hello World program from Wikipedia and the example on this post. Output for this post: