User:Feuermonster/Bf2Py

Translates Brainfuck- to Pythoncode.


 * 1) !/usr/bin/stlisp

(load "/usr/lib/stlisp/mod_io.so")
 * Load some modules

(let (str-to-ls (extract (IO:read-all-text (IO "input.bf")))) "code")
 * Read text from file

(print "import sys") (print "tape = [0] * 32000") (print "ptr = 16000")
 * Output basic stuff

(let (list nil) "spaces")

(def "put-spaces" '(` (let "put-spaces-p") (IO:put-str (IO (put-spaces-p))) ))

(def "convert-bf" '(` (let "convert-bf-cur") (reduce "put-spaces" (spaces)) (eq (convert-bf-cur) "+"   '(IO:put-str-crlf (IO "tape[ptr] += 1"))    '(`)   ) (eq (convert-bf-cur) "-"    '(IO:put-str-crlf (IO "tape[ptr] -=  1"))     '(`)   ) (eq (convert-bf-cur) "<"    '(IO:put-str-crlf (IO "ptr -= 1"))     '(`)   ) (eq (convert-bf-cur) ">"    '(IO:put-str-crlf (IO "ptr += 1"))     '(`)   ) (eq (convert-bf-cur) "["    '(` (IO:put-str-crlf (IO "while tape[ptr] != 0:")) (let (cnat (list nil " ") (spaces)) "spaces") )     '(`)   ) (eq (convert-bf-cur) "]"    '(` (let (rest (spaces)) "spaces") (print "") )     '(`)   ) (eq (convert-bf-cur) "."    '(IO:put-str-crlf (IO "sys.stdout.write(chr(int(tape[ptr])))"))     '(`)   ) ) )
 * Convert

(reduce "convert-bf" (code))