Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Hide all the instance variables. Gotta hide them all\!

  • Loading branch information...
commit 63a1ef4862461860db75f38e09d235fb0fedf546 1 parent 24681ef
@practicingruby practicingruby authored
View
22 lib/turing_tarpit/interpreter.rb
@@ -1,30 +1,34 @@
module TuringTarpit
class Interpreter
def initialize(tokenizer, tape)
- @tokenizer = tokenizer
- @tape = tape
+ self.tokenizer = tokenizer
+ self.tape = tape
end
def run
loop do
- case @tokenizer.next(@tape.cell_value)
+ case tokenizer.next(tape.cell_value)
when "+"
- @tape.increment_cell_value
+ tape.increment_cell_value
when "-"
- @tape.decrement_cell_value
+ tape.decrement_cell_value
when ">"
- @tape.increment_pointer
+ tape.increment_pointer
when "<"
- @tape.decrement_pointer
+ tape.decrement_pointer
when "."
- putc(@tape.cell_value)
+ putc(tape.cell_value)
when ","
value = STDIN.getch.bytes.first
next if value.zero?
- @tape.cell_value = value
+ tape.cell_value = value
end
end
end
+
+ private
+
+ attr_accessor :tape, :tokenizer
end
end
View
14 lib/turing_tarpit/scanner.rb
@@ -1,8 +1,8 @@
module TuringTarpit
class Scanner
def initialize(chars)
- @chars = chars
- @index = 0
+ self.chars = chars
+ self.index = 0
end
def current_char
@@ -14,7 +14,7 @@ def validate_index
end
def consume
- @index += 1
+ self.index = index + 1
end
def jump_forward
@@ -28,8 +28,8 @@ def jump_back
def jump(from, to, step)
counter = 1
until counter == 0
- @index += step
- case @chars[@index]
+ self.index = index + step
+ case chars[index]
when from
counter += 1
when to
@@ -37,5 +37,9 @@ def jump(from, to, step)
end
end
end
+
+ private
+
+ attr_accessor :chars, :index
end
end
View
8 lib/turing_tarpit/tape.rb
@@ -3,8 +3,8 @@ class Tape
CELL_SIZE = 256
def initialize
- @pointer_position = 0
- @cells = []
+ self.pointer_position = 0
+ self.cells = []
end
attr_reader :pointer_position
@@ -39,8 +39,8 @@ def decrement_pointer
private
- attr_reader :cells
- attr_writer :pointer_position
+ attr_accessor :cells
+ attr_writer :pointer_position
def valid_cell_value?(value)
value.kind_of?(Integer) && value.between?(0,CELL_SIZE-1)
View
28 lib/turing_tarpit/tokenizer.rb
@@ -3,36 +3,38 @@
module TuringTarpit
class Tokenizer
def initialize(source_text)
- @scanner = Scanner.new(source_text.chars.to_a)
+ self.scanner = Scanner.new(source_text.chars.to_a)
end
def next(cell_value)
- @scanner.validate_index
+ scanner.validate_index
- element = @scanner.current_char
+ element = scanner.current_char
case element
when "["
- @scanner.jump_forward if cell_value.zero?
+ scanner.jump_forward if cell_value.zero?
- @scanner.consume
- element = @scanner.current_char
+ scanner.consume
+ element = scanner.current_char
when "]"
if cell_value.zero?
while element == "]"
- @scanner.consume
- element = @scanner.current_char
- @scanner.validate_index
+ scanner.consume
+ element = scanner.current_char
+ scanner.validate_index
end
else
- @scanner.jump_back
- @scanner.consume
- element = @scanner.current_char
+ scanner.jump_back
+ scanner.consume
+ element = scanner.current_char
end
end
- @scanner.consume
+ scanner.consume
element
end
+
+ attr_accessor :scanner
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.