November 21, 2024, 09:36:19 PM

1,531,352 Posts in 46,734 Topics by 1,523 Members
› View the most recent posts on the forum.


BRAINFUCK

Started by guff, January 29, 2009, 09:14:28 AM

previous topic - next topic

0 Members and 1 Guest are viewing this topic.

Go Down

guff

pybrain5 is a RESOUNDING SUCCESS  akudood;

Code Select
#!/usr/local/bin/python3.0
# pybrain5.py: translate Brainfuck code to C, then compile and run

def translate(brain_code):
brain_code = [char for char in brain_code if char in "<>,.[]+-"]
translated = []
brainfuck_to_c = {
'>' : '++ptr;',
'<' : '--ptr;',
'+' : '++cells[ptr];',
'-' : '--cells[ptr];',
'.' : 'putchar(cells[ptr]);',
',' : 'cells[ptr] = getchar();',
'[' : 'while (cells[ptr]) {',
']' : '}'
}
for char in brain_code:
translated.append(brainfuck_to_c[char])
return '\n'.join(translated)

def boilerplate_c(c_code):
header = '#include <stdio.h>\n'
main = 'int main() {\n'
cells = 'unsigned char cells[30000];\n'
pointer = 'unsigned int ptr = 0;'
init_cells = 'int i;\nfor(i=0;i<30000;i++){\nptr[i] = 0\n}'
end = '\n}'
return header + main + cells + pointer + c_code + end

if __name__ == '__main__':
from os import system, remove
from optparse import OptionParser
import re

parser = OptionParser()
options, args = parser.parse_args()
filename = args[0]
braincode = open(filename).read()
output_name = re.compile("(.+)\.bf?").match(filename).group(1)
# print(output_name)
c_filename = output_name + '.c'
# try:
# remove(c_filename)
# except OSError:
# pass
c_code = boilerplate_c(translate(braincode))
print(c_code)
open(c_filename, 'w').write(c_code)
#
system("gcc -O3 -o {outname} {inname}".format(outname = output_name, inname = c_filename))


i've still got a bit of work to do in terms of dealing with input and probably some other options, but the bulk of it works
it's actually a compiler (mostly based on the ideas here) rather than an interpreter like my pybrain4, in that it translates the brainfuck code to c and then compiles that

i think i can optimize it a little more if i consolidate sequential <, >, -, and +s (i.e. +++++ would normally be five lines of ++cells[ptr], so instead this would make it into cells[ptr] += 5)

at least this time i was smart enough to compartmentalize everything; i.e. sparate the parsing from compiling etc. now i just need to add the normal command line junk akudood;

Gladjaframpf

Your next challenge is to write a Brainfuck compiler IN BRAINFUCK.

guff


Gladjaframpf

It's disturbing enough that someone actually bothered to do that, but what's even worse is that they appear to have been working on it for the last four years.

And they managed to fit a naked woman into their source code. Very classy.

ncba93ivyase

Quote from: Gladjaframpf on January 29, 2009, 02:37:42 PM
And they managed to fit a naked woman into their source code. Very classy.
Oh god I just had to check it out...

And you didn't lie. goonish

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Quote from: Gladjaframpf on January 29, 2009, 02:37:42 PM
And they managed to fit a naked woman into their source code. Very classy.
well i don't think that file is actually part of the source  akudood;

but it isn't documentation either so i'm not sure what the hell it's doing there  doodhuh;

also it looks like she might be wearing panties so i'd be hesitant to say naked, just topless and with a disproportionately large navel  akudood;

ncba93ivyase

Quote from: guff on January 29, 2009, 02:52:47 PM
also it looks like she might be wearing panties so i'd be hesitant to say naked
Or she's just a little hairy

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

guff

Quote from: Pancake Persona on January 29, 2009, 03:04:27 PM
Or she's just a little hairy
then why would it be lighter than her skin  baddood;

also it would have been better if they put that file in the frontend directory akudood;

ncba93ivyase

Quote from: guff on January 29, 2009, 03:06:16 PM
then why would it be lighter than her skin  baddood;
Many women bleach their hair. badass

Quote from: ncba93ivyase on June 18, 2014, 07:58:34 PMthis isa great post i will use it in my sig

Go Up