-
Notifications
You must be signed in to change notification settings - Fork 2
/
bf2clj.clj
executable file
·58 lines (43 loc) · 1.17 KB
/
bf2clj.clj
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
;; #!/usr/bin/env clj
;; convert from Clojure to Brainfuck
(defn stdin []
(line-seq
(java.io.BufferedReader. *in* )))
(def pointer 0)
(defn init-print []
(println "
(def pointer 0)
(defn memoryname [] (str \"m\" pointer))
(defn get-value-from-memory []
(eval (symbol (memoryname))))
(defn inc-pointer
[] (def pointer (+ pointer 1)))
(defn dec-pointer
[] (def pointer (- pointer 1)))
(defn set-memory? []
(boolean (resolve (symbol (memoryname)))))
(defn set-or-zero []
(if (set-memory?) (get-value-from-memory) 0))
(defn set-pointer [set-value]
(intern *ns* (symbol (memoryname)) set-value))
(defn inc-memory [] (set-pointer (+ (set-or-zero) 1)))
(defn dec-memory [] (set-pointer (- (set-or-zero) 1)))
"))
(defn clojurefuck
[token]
(do
(cond
(= token \[) (println "
((fn []
(loop []
(if (> (set-or-zero) 0) (do ")
(= token \]) (println "(recur)) nil))))")
(= token \+) (println "(inc-memory)")
(= token \-) (println "(dec-memory)")
(= token \>) (println "(inc-pointer)")
(= token \<) (println "(dec-pointer)")
(= token \.) (println "(print (char (set-or-zero)))")))
)
(init-print)
(doall
(map clojurefuck (str (stdin))))