Created
July 22, 2015 04:01
-
-
Save zaltoprofen/7b1a2b6edfd30ca3c1b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with recursive zero100(z, i) as ( | |
values(0, 0) | |
union all | |
select 0, i+1 from zero100 where i+1 < 100 | |
), vm(step, sout, pc, ins, p, mem, stack) as ( | |
values(0, '', 1, '+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.------------.<++++++++.--------.+++.------.--------.>+.', | |
1, (select array_agg(z) from zero100), array[]::int[]) | |
union all | |
select | |
step + 1, | |
case substring(ins, pc, 1) | |
when '.' then | |
chr(mem[p]) | |
else | |
'' | |
end, | |
case | |
when substring(ins, pc, 1) = ']' and mem[p] <> 0 then | |
stack[1] | |
else | |
pc + 1 | |
end, | |
ins, | |
case substring(ins, pc, 1) | |
when '>' then | |
p + 1 | |
when '<' then | |
p - 1 | |
else | |
p | |
end, | |
(select array_agg(m) from (select | |
case row_number() over () | |
when p then m + case substring(ins, pc, 1) when '+' then 1 when '-' then -1 else 0 end | |
else m | |
end as m | |
from unnest(mem) as t(m)) as tmp | |
), | |
case substring(ins, pc, 1) | |
when '[' then | |
pc || stack | |
when ']' then | |
stack[2:array_length(stack, 1)] | |
else | |
stack | |
end | |
from vm where pc <= char_length(ins) | |
) | |
select array_to_string(array_agg(sout), '') from vm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment