/
brainfuck.rb
77 lines (64 loc) · 1.35 KB
/
brainfuck.rb
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
#!/usr/bin/ruby
class Brainfuck
class ProgramError < StandardError; end
def initialize(src)
@tokens = src.chars.to_a
end
def run
tape = []
pc = 0
cur = 0
while pc < @tokens.size
case @tokens[pc]
when "+"
tape[cur] ||= 0
tape[cur] += 1
when "-"
tape[cur] ||= 0
tape[cur] -= 1
when ">"
cur += 1
when "<"
cur -=1
raise ProgramError '開始地点より左に移動できません。' if cur < 0
when "."
n = (tape[cur] || 0)
print n.chr
when ","
tape[cur] = $stdin.getc.ord
when "["
if tape[cur] == 0
pc = @jumps[pc]
end
when "]"
if tape[cur] != 0
pc = @jumps[pc]
end
end
pc += 1
end
end
private
def analyze_jumps(tokens)
jumps = {}
starts = []
tokens.each_with_index do |c, i|
if c == "["
starts.push(i)
elsif c == "]"
raise "]が多すぎます" if starts.empty?
from = starts.pop
to = i
jumps[from] = to
jumps[to] = from
end
end
raise ProgramError, "]が多すぎます" unless starts.empty?
jumps
end
end
begin
bf = Brainfuck.new(ARGF.read).run
rescue Brainfuck::ProgramError
puts 'プログラムの実行に失敗しました。'
end