Class: Orenono::Brain
- Inherits:
-
Object
- Object
- Orenono::Brain
- Defined in:
- lib/brain.rb
Overview
Brain
Instance Attribute Summary collapse
-
#code_cursol ⇒ Object
readonly
Returns the value of attribute code_cursol.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#loop_stack ⇒ Object
readonly
Returns the value of attribute loop_stack.
-
#memory_cursol ⇒ Object
readonly
Returns the value of attribute memory_cursol.
-
#src ⇒ Object
readonly
Returns the value of attribute src.
-
#tape ⇒ Object
readonly
Returns the value of attribute tape.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #decrement ⇒ Object
- #display ⇒ Object
- #end_loop ⇒ Object
- #goto_end_loop_next ⇒ Object
-
#increment ⇒ Object
rubocop:enable CyclomaticComplexity, MethodLength.
-
#initialize(config, src = '') ⇒ Brain
constructor
A new instance of Brain.
- #next_cursol ⇒ Object
- #previous_cursol ⇒ Object
- #read ⇒ Object
-
#run ⇒ Object
rubocop:disable CyclomaticComplexity, MethodLength.
- #start_loop ⇒ Object
Constructor Details
permalink #initialize(config, src = '') ⇒ Brain
Returns a new instance of Brain.
11 12 13 14 15 16 17 18 19 |
# File 'lib/brain.rb', line 11 def initialize(config, src = '') @config = config @src = src @tokens = @src.scan(/#{@config.token_patterns}/) @tape = Array.new(65_535) { 0 } @memory_cursol = 0 @code_cursol = 0 @loop_stack = [] end |
Instance Attribute Details
permalink #code_cursol ⇒ Object (readonly)
Returns the value of attribute code_cursol.
9 10 11 |
# File 'lib/brain.rb', line 9 def code_cursol @code_cursol end |
permalink #config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/brain.rb', line 8 def config @config end |
permalink #loop_stack ⇒ Object (readonly)
Returns the value of attribute loop_stack.
9 10 11 |
# File 'lib/brain.rb', line 9 def loop_stack @loop_stack end |
permalink #memory_cursol ⇒ Object (readonly)
Returns the value of attribute memory_cursol.
9 10 11 |
# File 'lib/brain.rb', line 9 def memory_cursol @memory_cursol end |
permalink #src ⇒ Object (readonly)
Returns the value of attribute src.
8 9 10 |
# File 'lib/brain.rb', line 8 def src @src end |
permalink #tape ⇒ Object (readonly)
Returns the value of attribute tape.
8 9 10 |
# File 'lib/brain.rb', line 8 def tape @tape end |
permalink #tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
8 9 10 |
# File 'lib/brain.rb', line 8 def tokens @tokens end |
Instance Method Details
permalink #decrement ⇒ Object
[View source]
58 59 60 61 |
# File 'lib/brain.rb', line 58 def decrement fail InvalidMemoryAccessError if @tape[@memory_cursol].zero? @tape[@memory_cursol] -= 1 end |
permalink #display ⇒ Object
[View source]
96 97 98 |
# File 'lib/brain.rb', line 96 def display putc @tape[@memory_cursol] end |
permalink #end_loop ⇒ Object
[View source]
82 83 84 85 |
# File 'lib/brain.rb', line 82 def end_loop fail InvalidLoopError, 'Invalid Loop.' if @loop_stack.empty? @code_cursol = @loop_stack.pop end |
permalink #goto_end_loop_next ⇒ Object
[View source]
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/brain.rb', line 69 def goto_end_loop_next hit_cnt = 0 index = 0 @tokens[@code_cursol..-1].each_with_index do |token, i| hit_cnt += 1 if token == @config.end_loop next unless hit_cnt == @loop_stack.size index = i + @code_cursol break end @loop_stack.pop @code_cursol = index + 1 end |
permalink #increment ⇒ Object
rubocop:enable CyclomaticComplexity, MethodLength
54 55 56 |
# File 'lib/brain.rb', line 54 def increment @tape[@memory_cursol] += 1 end |
permalink #next_cursol ⇒ Object
[View source]
87 88 89 |
# File 'lib/brain.rb', line 87 def next_cursol @memory_cursol += 1 end |
permalink #previous_cursol ⇒ Object
[View source]
91 92 93 94 |
# File 'lib/brain.rb', line 91 def previous_cursol fail InvalidCursolError, 'Invalid Cursol.' if @memory_cursol.zero? @memory_cursol -= 1 end |
permalink #read ⇒ Object
[View source]
100 101 102 |
# File 'lib/brain.rb', line 100 def read @tape[@memory_cursol] = STDIN.readchar.ord end |
permalink #run ⇒ Object
rubocop:disable CyclomaticComplexity, MethodLength
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 |
# File 'lib/brain.rb', line 22 def run loop do token = @tokens[@code_cursol] case token when @config.increment increment @code_cursol += 1 when @config.decrement decrement @code_cursol += 1 when @config.start_loop start_loop when @config.end_loop end_loop when @config.next_cursol next_cursol @code_cursol += 1 when @config.previous_cursol previous_cursol @code_cursol += 1 when @config.display display @code_cursol += 1 when @config.read read @code_cursol += 1 end break if @code_cursol >= @tokens.size end end |
permalink #start_loop ⇒ Object
[View source]
63 64 65 66 67 |
# File 'lib/brain.rb', line 63 def start_loop @loop_stack << @code_cursol return goto_end_loop_next if @tape[@memory_cursol] == 0 @code_cursol += 1 end |