Class: GenBrain::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Generator

Returns a new instance of Generator.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/gen_brain/generator.rb', line 6

def initialize(config)
  @title = config["title"]
  @p_inc = config["p_inc"]
  @p_dec = config["p_dec"]
  @v_inc = config["v_inc"]
  @v_dec = config["v_dec"]
  @output= config["output"]
  @input = config["input"]
  @jump_forward = config["jump_forward"]
  @jump_back = config["jump_back"]
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def input
  @input
end

#jump_backObject

Returns the value of attribute jump_back.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def jump_back
  @jump_back
end

#jump_forwardObject

Returns the value of attribute jump_forward.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def jump_forward
  @jump_forward
end

#outputObject

Returns the value of attribute output.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def output
  @output
end

#p_decObject

Returns the value of attribute p_dec.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def p_dec
  @p_dec
end

#p_incObject

Returns the value of attribute p_inc.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def p_inc
  @p_inc
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def title
  @title
end

#v_decObject

Returns the value of attribute v_dec.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def v_dec
  @v_dec
end

#v_incObject

Returns the value of attribute v_inc.



3
4
5
# File 'lib/gen_brain/generator.rb', line 3

def v_inc
  @v_inc
end

Instance Method Details

#callObject



31
32
33
34
35
36
37
# File 'lib/gen_brain/generator.rb', line 31

def call
  File.write("#{title}.rb", lang)
  File.write("#{title}_hello_world", hello_world_input_sample)
  puts "🎉 Created #{title}.rb and #{title}_hello_world"
  puts "🌈 Let's execute the following code!!"
  puts "#=> ruby #{title}.rb #{title}_hello_world"
end

#hello_world_input_sampleObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gen_brain/generator.rb', line 106

def hello_world_input_sample
  "+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.------------.<++++++++.--------.+++.------.--------.>+.".
    gsub(/\>/, p_inc).
    gsub(/\</, p_dec).
    gsub(/\+/, v_inc).
    gsub(/\-/, v_dec).
    gsub(/\./, output).
    gsub(/\,/, input).
    gsub(/\[/, jump_forward).
    gsub(/\]/, jump_back)
end

#langObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gen_brain/generator.rb', line 39

def lang
  class_name = title.split(/[^[:alnum:]]+/).map(&:capitalize).join
"#!/usr/bin/env ruby\n\nclass \#{class_name}\n  def initialize(code)\n@tokens = code.scan(/(\#{words})/).flatten\n@jumps = analyze_jumps(@tokens)\n  end\n\n  def run\narray = []\nindex = 0\nnow = 0\n\nwhile index < @tokens.size\n  case @tokens[index]\n    when \"\#{v_inc}\"\n      array[now] ||= 0\n      array[now] += 1\n    when \"\#{v_dec}\"\n      array[now] ||= 0\n      array[now] -= 1\n    when \"\#{p_inc}\"\n      now += 1\n    when \"\#{p_dec}\"\n      now -= 1\n    when \"\#{output}\"\n      n = (array[now] || 0)\n      print n.chr\n    when \"\#{input}\"\n      array[now] = $stdin.getc\n    when \"\#{jump_forward}\"\n      index = @jumps[index] if array[now] == 0\n    when \"\#{jump_back}\"\n      index = @jumps[index] if array[now] != 0\n  end\n  index += 1\nend\n  end\n\n  private\n\n  def analyze_jumps(tokens)\nstack = []\njumps = {}\nstart_word = \"\#{jump_forward}\"\nend_word = \"\#{jump_back}\"\ntokens.each_with_index do |v,i|\n  if v == start_word\n    stack.push(i)\n  elsif v == end_word\n    from = stack.pop\n    to = i\n    jumps[from] = to\n    jumps[to] = from\n  end\nend\njumps\n  end\nend\n\n\#{class_name}.new(ARGF.read).run\n"
end

#wordsObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gen_brain/generator.rb', line 18

def words
  [
    p_inc,
    p_dec,
    v_inc,
    v_dec,
    output,
    input,
    jump_forward,
    jump_back
  ].map{|word|Regexp.escape(word)}.join('|')
end