User:Feuermonster/Bf2Py
Jump to navigation
Jump to search
Translates Brainfuck- to Pythoncode.
#!/usr/bin/stlisp ; Load some modules (load "/usr/lib/stlisp/mod_io.so") ; Read text from file (let (str-to-ls (extract (IO:read-all-text (IO "input.bf")))) "code") ; Output basic stuff (print "import sys") (print "tape = [0] * 32000") (print "ptr = 16000") (let (list nil) "spaces") (def "put-spaces" '(` (let "put-spaces-p") (IO:put-str (IO (put-spaces-p))) )) ; Convert (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])))")) '(`) ) ) ) (reduce "convert-bf" (code))