-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompfuck.carp
42 lines (39 loc) · 1.41 KB
/
compfuck.carp
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
(defndynamic search-matching-bracket [s m]
(if (or (= (String.length s) 0) (= m 0))
0
(let [f (String.char-at s 0)]
(inc (search-matching-bracket (String.tail s) (if (= \] f) (dec m) (if (= \[ f) (inc m) m)))))))
(defndynamic compile-compf-char [c s i]
(case f
\+ (list 1 '(Array.aupdate! &t h &(fn [x] (mod (Int.inc @x) 255))))
\- (list 1 '(Array.aupdate! &t h &(fn [x] (max 0 (Int.dec @x)))))
\> (list 1 '(set! h (+ h 1)))
\< (list 1 '(set! h (- h 1)))
\. (list 1 '(IO.print &(str (Char.from-int (copy (Array.unsafe-nth &t h))))))
\, (list 1 '(Array.aset! &t h (Char.to-int (IO.get-char))))
\[
(let [end (search-matching-bracket (String.suffix s (+ i 1)) 1)]
(list (+ end 1)
`(while (/= &0 (Array.unsafe-nth &t h))
(do
%@(let [subs (String.slice s (+ i 1) end)]
(compile-compf subs 0))))))
'(1 ())))
(defndynamic compile-compf [p i]
(if (= i (String.length p))
'()
(let [f (String.char-at p i)]
(if (= \] f)
'()
(let [incr-c (compile-compf-char f p i)]
(let [incr (car incr-c)
compiled (cadr incr-c)]
(cons compiled
(compile-compf p
(+ i incr)))))))))
(defmacro compf [prog]
(eval
`(defn main []
(let-do [t (Array.replicate 30000 &0)
h 0]
%@(compile-compf prog 0)))))