SHOW:
|
|
- or go back to the newest paste.
1 | (defun brfk (i) | |
2 | (let ((s (coerce i'list))) | |
3 | (labels((lbf () | |
4 | (do(r x)((or x(not s))(reverse r)) | |
5 | (push (case (pop s) | |
6 | (#\> 'right) | |
7 | (#\< `left) | |
8 | (#\+ `inc) | |
9 | (#\- `dec) | |
10 | (#\. `print) | |
11 | (#\, `read) | |
12 | (#\[ `(bracketed ,@(lbf))) | |
13 | (#\] (setf x t) (go /)) | |
14 | (t (go /))) | |
15 | r)/))) | |
16 | `(brainfuck | |
17 | ,@(lbf))))) | |
18 | ||
19 | (defun simplify (bf) | |
20 | (optima:match bf | |
21 | ((cons 'brainfuck rest) `(brainfuck ,@(simplify rest))) | |
22 | ((list* (list 'add n) 'inc rest) (simplify (cons `(add ,(1+ n)) rest))) | |
23 | ((list* (list 'add n) 'dec rest) (simplify (cons `(add ,(1- n)) rest))) | |
24 | ((list* (list 'add n) x rest) `((add ,n) ,@(simplify (cons x rest)))) | |
25 | ((cons 'inc rest) (simplify (cons '(add 1) rest))) | |
26 | ((cons 'dec rest) (simplify (cons '(add -1) rest))) | |
27 | ((list* (list 'move n) 'left rest) (simplify (cons `(move ,(1- n)) rest))) | |
28 | ((list* (list 'move n) 'right rest) (simplify (cons `(move ,(1+ n)) rest))) | |
29 | ((list* (list 'move n) x rest) `((move ,n) ,@(simplify (cons x rest)))) | |
30 | ((cons 'left rest) (simplify (cons '(move -1) rest))) | |
31 | ((cons 'right rest) (simplify (cons '(move 1) rest))) | |
32 | ((cons (cons 'bracketed sub) rest) `((bracketed ,@(simplify sub)) ,@(simplify rest))) | |
33 | ((cons a rest) `(,a ,@(simplify rest))) | |
34 | (nil nil))) | |
35 | ||
36 | (defun as-ps(bf) | |
37 | (optima:match bf | |
38 | (nil nil) | |
39 | ((cons 'brainfuck rest) `(let ((a (make-array)) (p 0)) ,@(as-ps rest))) | |
40 | ((cons (list 'add 0) rest) (as-ps rest)) | |
41 | ((cons (list 'add x) rest) (cons`(setf(aref a p)(+(or (aref a p)0),x)) (as-ps rest))) | |
42 | ((cons (list 'move 0) rest) (as-ps rest)) | |
43 | ((cons (list 'move x) rest) (cons`(incf p ,x)(as-ps rest))) | |
44 | ((cons 'print rest) (cons`((ps:@ console log)((ps:@ *String from-char-code) (or(aref a p)0))) (as-ps rest))) | |
45 | ((cons 'read rest) (cons `(setf(aref a p) ((ps:@ (prompt) char-code-at) 0)) (as-ps rest))) | |
46 | ((cons (cons 'bracketed sub) rest) | |
47 | (cons `(loop until (= 0 (or (aref a p)0)) do ,@(as-ps sub)) | |
48 | - | (cons `(unless(= 0 (or(aref a p)0))(do(e)(e),@(as-ps sub)(setf e(= 0 (aref a p))))) |
48 | + | |
49 | ||
50 | (ps:ps* (as-ps (simplify (brfk "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.") ))) | |
51 | ||
52 | "(function () { | |
53 | var a = new Array(); | |
54 | var p = 0; | |
55 | a[p] = (a[p] || 0) + 10; | |
56 | while (0 !== (a[p] || 0)) { | |
57 | - | if (0 !== (a[p] || 0)) { |
57 | + | ++p; |
58 | - | var e = null; |
58 | + | a[p] = (a[p] || 0) + 7; |
59 | - | for (; !e; ) { |
59 | + | ++p; |
60 | - | ++p; |
60 | + | a[p] = (a[p] || 0) + 10; |
61 | - | a[p] = (a[p] || 0) + 7; |
61 | + | ++p; |
62 | - | ++p; |
62 | + | a[p] = (a[p] || 0) + 3; |
63 | - | a[p] = (a[p] || 0) + 10; |
63 | + | ++p; |
64 | - | ++p; |
64 | + | a[p] = (a[p] || 0) + 1; |
65 | - | a[p] = (a[p] || 0) + 3; |
65 | + | p += -4; |
66 | - | ++p; |
66 | + | a[p] = (a[p] || 0) + -1; |
67 | - | a[p] = (a[p] || 0) + 1; |
67 | + | |
68 | - | p += -4; |
68 | + | |
69 | - | a[p] = (a[p] || 0) + -1; |
69 | + | |
70 | - | e = 0 === a[p]; |
70 | + | |
71 | - | }; |
71 | + | |
72 | a[p] = (a[p] || 0) + 1; | |
73 | console.log(String.fromCharCode(a[p] || 0)); | |
74 | a[p] = (a[p] || 0) + 7; | |
75 | console.log(String.fromCharCode(a[p] || 0)); | |
76 | console.log(String.fromCharCode(a[p] || 0)); | |
77 | a[p] = (a[p] || 0) + 3; | |
78 | console.log(String.fromCharCode(a[p] || 0)); | |
79 | ++p; | |
80 | a[p] = (a[p] || 0) + 2; | |
81 | console.log(String.fromCharCode(a[p] || 0)); | |
82 | p += -2; | |
83 | a[p] = (a[p] || 0) + 15; | |
84 | console.log(String.fromCharCode(a[p] || 0)); | |
85 | ++p; | |
86 | console.log(String.fromCharCode(a[p] || 0)); | |
87 | a[p] = (a[p] || 0) + 3; | |
88 | console.log(String.fromCharCode(a[p] || 0)); | |
89 | a[p] = (a[p] || 0) + -6; | |
90 | console.log(String.fromCharCode(a[p] || 0)); | |
91 | a[p] = (a[p] || 0) + -8; | |
92 | console.log(String.fromCharCode(a[p] || 0)); | |
93 | ++p; | |
94 | a[p] = (a[p] || 0) + 1; | |
95 | console.log(String.fromCharCode(a[p] || 0)); | |
96 | ++p; | |
97 | return console.log(String.fromCharCode(a[p] || 0)); | |
98 | })();" |