Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- codeOWED_CHARS = '<>+-.,[]'
- def load_brainfuck_code(filename):
- with open(filename) as f:
- raw_code = f.read()
- code = [char for char in raw_code if char in codeOWED_CHARS]
- return code
- def write_c_code_to_file(output, template):
- with open(output, 'w') as f:
- f.write(template)
- TEMPLATE = """
- #include <stdio.h>
- int main(){
- \tchar array[30000] = {0};
- \tchar *p=array+15000;
- %s
- \treturn 0;
- }
- """
- def main(filename, output):
- bf_code = load_brainfuck_code(filename)
- print bf_code
- i = 0
- c_code = []
- code = []
- last_char = ''
- indent = 1
- for char in bf_code:
- if char == ']':
- indent -= 1
- if last_char == char and last_char in '<>+-':
- code[-1]['count'] += 1
- else:
- code.append({'code': char, 'indent': indent, 'count': 1})
- last_char = char
- if char == '[':
- indent += 1
- for el in code:
- c_code.append(el['indent'] * '\t' + el['code'])
- c_code = TEMPLATE %'\n'.join(c_code)
- write_c_code_to_file(output, c_code)
- if __name__ == '__main__':
- main(*sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement