-
Notifications
You must be signed in to change notification settings - Fork 35
/
bf2c.hs
28 lines (25 loc) · 1010 Bytes
/
bf2c.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- Brainfuck to C translator
translate :: [Char] -> [Char]
translate (x:xs) = equiv x ++ translate xs
where equiv x | x == '>' = "++ptr;\n"
| x == '<' = "--ptr;\n"
| x == '+' = "++(*ptr);\n"
| x == '-' = "--(*ptr);\n"
| x == '.' = "putchar(*ptr);\n" ++
"fflush(stdout);\n"
| x == ',' = "*ptr = getchar();\n" ++
"if (*ptr == EOF) exit(0);\n"
| x == '[' = "while(*ptr) {\n"
| x == ']' = "}\n"
equiv x = ""
translate [] = ""
translator :: [Char] -> [Char]
translator input = "#include <stdio.h>\n" ++
"#include <stdlib.h>\n" ++
"int main() {\n" ++
" char mem[30000],\n" ++
" *ptr = mem;\n" ++
translate input ++
" return 0;\n" ++
"}\n"
main = interact translator