Class: Orenono::Brain

Inherits:
Object
  • Object
show all
Defined in:
lib/brain.rb

Overview

Brain

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, src = '') ⇒ Brain

Returns a new instance of Brain.

[View source]

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

#code_cursolObject (readonly)

Returns the value of attribute code_cursol.


9
10
11
# File 'lib/brain.rb', line 9

def code_cursol
  @code_cursol
end

#configObject (readonly)

Returns the value of attribute config.


8
9
10
# File 'lib/brain.rb', line 8

def config
  @config
end

#loop_stackObject (readonly)

Returns the value of attribute loop_stack.


9
10
11
# File 'lib/brain.rb', line 9

def loop_stack
  @loop_stack
end

#memory_cursolObject (readonly)

Returns the value of attribute memory_cursol.


9
10
11
# File 'lib/brain.rb', line 9

def memory_cursol
  @memory_cursol
end

#srcObject (readonly)

Returns the value of attribute src.


8
9
10
# File 'lib/brain.rb', line 8

def src
  @src
end

#tapeObject (readonly)

Returns the value of attribute tape.


8
9
10
# File 'lib/brain.rb', line 8

def tape
  @tape
end

#tokensObject (readonly)

Returns the value of attribute tokens.


8
9
10
# File 'lib/brain.rb', line 8

def tokens
  @tokens
end

Instance Method Details

#decrementObject

[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

#displayObject

[View source]

96
97
98
# File 'lib/brain.rb', line 96

def display
  putc @tape[@memory_cursol]
end

#end_loopObject

[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

#goto_end_loop_nextObject

[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

#incrementObject

rubocop:enable CyclomaticComplexity, MethodLength

[View source]

54
55
56
# File 'lib/brain.rb', line 54

def increment
  @tape[@memory_cursol] += 1
end

#next_cursolObject

[View source]

87
88
89
# File 'lib/brain.rb', line 87

def next_cursol
  @memory_cursol += 1
end

#previous_cursolObject

[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

#readObject

[View source]

100
101
102
# File 'lib/brain.rb', line 100

def read
  @tape[@memory_cursol] = STDIN.readchar.ord
end

#runObject

rubocop:disable CyclomaticComplexity, MethodLength

[View source]

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

#start_loopObject

[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