-
Notifications
You must be signed in to change notification settings - Fork 1
/
brainfuck postgree.sql
153 lines (151 loc) · 2.69 KB
/
brainfuck postgree.sql
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
WITH RECURSIVE
var_Input AS (
SELECT '
++++++++++[>++++++++>+++++++++++
>---------->+++>++++++++>+++++++
+++++>+++++++++++>++++++++++>+++
++++++++>+++<<<<<<<<<<-]>-.>--.>
++++.>++.>---.>---.>.>.>+.>+++.,
'::TEXT::BYTEA AS v
)
,
BF(K, PC, Skip, Stack, Head, Tape, Input, Output)
AS
(
SELECT
0 AS K,
1 AS PC,
0 AS Skip,
('x'||'00000000')::BYTEA AS Stack,
1 AS Head,
('x'||'00000000')::BYTEA AS Tape,
('x'||'00000000')::BYTEA AS Input,
'' AS Output
UNION ALL
SELECT
K + 1 AS K,
(CASE WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = ']'
THEN
SUBSTRING(Stack, 1, 4)::BIT(32)::INT
ELSE
PC + 1
END) AS PC,
(CASE WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = '['
AND
SUBSTRING(Tape, Head, 4)::BIT(32)::INT = 0
THEN
LENGTH(Stack)::INT
WHEN
Skip != 0
AND
SUBSTRING(Input, 1, 1) = '['
THEN
Skip + 4
WHEN
Skip != 0
AND
SUBSTRING(Input, 1, 1) = ']'
AND
LENGTH(Stack) < Skip
THEN
Skip - 4
WHEN
Skip != 0
AND
SUBSTRING(Input, 1, 1) = ']'
AND
LENGTH(Stack) = Skip
THEN
0
ELSE
Skip
END) AS Skip,
(CASE WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = '['
AND
SUBSTRING(Tape, Head, 4)::BIT(32)::INT != 0
THEN
PC::BIT(32)::TEXT::BYTEA || Stack
WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = ']'
THEN
SUBSTRING(Stack, 5, LENGTH(Stack) - 4)
ELSE
Stack
END) AS Stack,
(CASE WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = '<'
THEN
Head - 4
WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = '>'
THEN
Head + 4
ELSE
Head
END) AS Head,
(CASE WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = '>'
AND
Head + 4 > LENGTH(Tape)
THEN
Tape || '\\x00000000'::BYTEA
WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = '+'
THEN
(SUBSTRING(Tape, Head, 4)::BIT(32)::INT + 1)::BIT(32)::TEXT::BYTEA
|| SUBSTRING(Tape, Head + 4, LENGTH(Tape) - Head)
WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = '-'
THEN
(SUBSTRING(Tape, Head, 4)::BIT(32)::INT - 1)::BIT(32)::TEXT::BYTEA
|| SUBSTRING(Tape, Head + 4, LENGTH(Tape) - Head)
ELSE
Tape
END) AS Tape,
(CASE WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = ']'
THEN
SUBSTRING((SELECT v FROM var_Input), SUBSTRING(Stack, 1, 4)::BIT(32)::INT - 1, 1)
ELSE
SUBSTRING((SELECT v FROM var_Input), PC, 1)
END) AS Input,
(CASE WHEN
Skip = 0
AND
SUBSTRING(Input, 1, 1) = '.'
THEN
SUBSTRING(Tape, Head, 4)
ELSE
''
END) AS Output
FROM
BF
WHERE
K < 200
)
SELECT Output
FROM BF
;