Class: Orenono::Generator

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

Overview

Brainf*ck code generator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(char, config) ⇒ Generator

Returns a new instance of Generator.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generator.rb', line 20

def initialize(char, config)
  char_code = char
  repeat = char_code / 10
  rest = char_code % 10
  @tables = []
  @pointer = 0
  @config = config
  @init_codes = ten_increment(config) +
                config.start_loop +
                config.next_cursol +
                increment(repeat) +
                config.previous_cursol +
                config.decrement +
                config.end_loop +
                config.next_cursol +
                increment(rest)
  @tables[@pointer] = char_code
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#init_codesObject (readonly)

Returns the value of attribute init_codes.



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

def init_codes
  @init_codes
end

#pointerObject (readonly)

Returns the value of attribute pointer.



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

def pointer
  @pointer
end

#tablesObject (readonly)

Returns the value of attribute tables.



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

def tables
  @tables
end

Class Method Details

.generate(text, config) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/generator.rb', line 11

def self.generate(text, config)
  bytes = text.bytes.to_a
  bf = new(bytes[0], config)
  bytes.each_with_object([bf.init_codes]) do |char, codes|
    codes << bf.diff(char)
    codes << bf.output
  end.join
end

Instance Method Details

#decrement(count = 1) ⇒ Object



45
46
47
48
# File 'lib/generator.rb', line 45

def decrement(count = 1)
  @tables[@pointer] -= count
  @config.decrement * count
end

#diff(char_code) ⇒ Object



50
51
52
53
54
55
# File 'lib/generator.rb', line 50

def diff(char_code)
  diff_of_code = char_code - @tables[@pointer]
  return '' if diff_of_code.zero?
  @tables[@pointer] += diff_of_code
  diff_of_code > 0 ? (@config.increment * diff_of_code) : (@config.decrement * diff_of_code.abs)
end

#increment(count = 1) ⇒ Object



39
40
41
42
43
# File 'lib/generator.rb', line 39

def increment(count = 1)
  @tables[@pointer] = 0 unless @tables[@pointer]
  @tables[@pointer] += count
  @config.increment * count
end

#next_pointer(count = 1) ⇒ Object



57
58
59
60
# File 'lib/generator.rb', line 57

def next_pointer(count = 1)
  @pointer += count
  @config.next_cursol * count
end

#output(count = 1) ⇒ Object



67
68
69
# File 'lib/generator.rb', line 67

def output(count = 1)
  @config.display * count
end

#previous_pointer(count = 1) ⇒ Object



62
63
64
65
# File 'lib/generator.rb', line 62

def previous_pointer(count = 1)
  @pointer -= count
  @config.previous_cursol * count
end