Created
July 21, 2009 20:54
-
-
Save ivanvc/151571 to your computer and use it in GitHub Desktop.
This file contains 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
raise "No file specified" unless ARGV[0] | |
raise "File does not exist" unless File.exists?(ARGV[0]) | |
raise "File is not readable" unless File.readable?(ARGV[0]) | |
file = File.open(ARGV[0]) | |
cells = Array.new(30000, 0) | |
min_size = 0 | |
max_size = 255 | |
location = 0 | |
while not file.eof? | |
char = file.getc | |
case char | |
when ?> | |
location += 1 if location < cells.length | |
when ?< | |
location -= 1 if location > 0 | |
when ?+ | |
cells[location] += 1 | |
cells[location] = min_size if cells[location] > max_size | |
when ?- | |
cells[location] -= 1 | |
cells[location] = max_size if cells[location] < min_size | |
when ?. | |
$stdout.putc cells[location] | |
when ?, | |
cells[location] = $stdin.getc | |
when ?[ | |
if cells[location] == 0 | |
nested = 1 | |
begin | |
_char = file.getc | |
nested += 1 if _char == ?[ | |
nested -= 1 if _char == ?] | |
end until nested == 0 | |
end | |
when ?] | |
if cells[location] != 0 | |
nested = 1 | |
begin | |
file.pos = file.pos - 2 | |
_char = file.getc | |
nested += 1 if _char == ?] | |
nested -= 1 if _char == ?[ | |
end until nested == 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment