-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.py
68 lines (64 loc) · 3.63 KB
/
brainfuck.py
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
59
60
61
62
63
64
65
66
67
68
# Usage: brainfuck( code, input )
brainfuck = lambda cd,ip='':(
lambda c,i,m,p,o,u,st
,d,s,t:(lambda oc:[[(lambda
it:[(lambda ind:[d[ind].append
(d[ind].pop()+1)])(0 if it == '['
else (1 if it == ']' else 2)),0 ])(
chrco ) for chrco in c] ,t(
"syntax "+ "err")if d[0]
[0]> d [ 1][0]else(t
("syn" +"tax err"
)if d [0][0] < d
[1][ 0]else 0),[
None for _ in (
None for (_) in
iter(lambda
: [ oc[c[o
[0]]]()if c
[o[0] ] in
oc else 0,
o.append(o
.pop() + 1
), o[0]][-1
],len(c)))]
,''.join(u) ][
-1])( { ('+'):(
lambda : m[p[0] ]
.append( ((m[p[0]])
.pop() + 1) % 256 )),
'-': (lambda : m[p[0] ]
.append((m[p[0]].pop() -1
)%256 )),('>'): (lambda : p
.append((p.pop() + 1 ) % len(
m))),'<': (lambda : p.append((
p.pop() - 1 ) %len(m) )),'.':(
lambda:u.append(chr(m [p[0]][0
]))), ',': (lambda :m[ p[(0)]]
.append([m[p[0]].pop() , ord(i.
pop())][1] if len(i)!= 0 else 0
)),'[':(lambda:(lambda oc, c_c:
[None for (_) in (None for _ in
iter(lambda: [o.append (o.pop()
+ 1), oc.append(oc.pop( )+ 1) if
c[ o[0] ] == '[' else ( [ (c_c).
append(c_c.pop() + 1), 2 if c_c
[0] != oc[0] else 0][ -1 ] if
c[o[0] ] == ']' else 0 ) ][-1]
, 2))] if m[ p[0] ][0] ==0 else
st.append(o[0]))([0],[0 ]) ),']':
(lambda : [ o.pop() ,o. append (
st.pop()),st.append(o[0 ])] if m[
p[0]][0] != 0 else (st. pop( ) if
m[p[0]][0] == 0 else 0) )}) ) (cd,
list(ip), [[(0)] for x in range(
256)], [0] , [0] , [],[ ], [[0],[
0], [0]],__import__("s" +"y"+ "s",
globals() , locals() , [], (0)),
(lambda err : (_ for _ in () ) .
throw ( RuntimeError( err) ) ))
# Hello World to test
if __name__ == "__main__":
print(brainfuck('''++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++
.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.
------.--------.>+.>.'''))