Skip to content

Commit

Permalink
Refactoring of tokenizer / scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
practicingruby committed Jan 25, 2012
1 parent ef649b0 commit c528515
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
16 changes: 9 additions & 7 deletions lib/turing_tarpit/scanner.rb
Expand Up @@ -9,12 +9,10 @@ def current_char
chars[index]
end

def validate_index
raise StopIteration if chars.length == index
end

def consume
def next_char
self.index = index + 1

current_char
end

def jump_forward
Expand All @@ -25,6 +23,12 @@ def jump_back
jump("]", "[", -1)
end

def validate_index
raise StopIteration if chars.length == index
end

private

def jump(from, to, step)
counter = 1
until counter == 0
Expand All @@ -37,8 +41,6 @@ def jump(from, to, step)
end
end
end

private

attr_accessor :chars, :index
end
Expand Down
11 changes: 4 additions & 7 deletions lib/turing_tarpit/tokenizer.rb
Expand Up @@ -15,23 +15,20 @@ def next(cell_value)
when "["
scanner.jump_forward if cell_value.zero?

scanner.consume
element = scanner.current_char
element = scanner.next_char
when "]"
if cell_value.zero?
while element == "]"
scanner.consume
element = scanner.current_char
element = scanner.next_char
scanner.validate_index
end
else
scanner.jump_back
scanner.consume
element = scanner.current_char
element = scanner.next_char
end
end

scanner.consume
scanner.next_char
element
end

Expand Down

0 comments on commit c528515

Please sign in to comment.