Sha256: c0495855f748bf1aa3a0503da0b4d33ce435fa46e4c66dd1f96f27bc2eea208a
Contents?: true
Size: 1.14 KB
Versions: 1
Compression:
Stored size: 1.14 KB
Contents
#!/usr/bin/env ruby class BrainSample def initialize(code) @tokens = code.scan(/(>|<|\+|\-|\.|,|\[|\])/).flatten @jumps = analyze_jumps(@tokens) end def run array = [] index = 0 now = 0 while index < @tokens.size case @tokens[index] when "+" array[now] ||= 0 array[now] += 1 when "-" array[now] ||= 0 array[now] -= 1 when ">" now += 1 when "<" now -= 1 when "." n = (array[now] || 0) print n.chr when "," array[now] = $stdin.getc when "[" index = @jumps[index] if array[now] == 0 when "]" index = @jumps[index] if array[now] != 0 end index += 1 end end private def analyze_jumps(tokens) stack = [] jumps = {} start_word = "[" end_word = "]" tokens.each_with_index do |v,i| if v == start_word stack.push(i) elsif v == end_word from = stack.pop to = i jumps[from] = to jumps[to] = from end end jumps end end BrainSample.new(ARGF.read).run
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
gen_brain-0.0.1 | samples/brain_sample.rb |