Class: Brainfucktt::REPL

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeREPL

Returns a new instance of REPL.



20
21
22
23
24
# File 'lib/brainfucktt/repl.rb', line 20

def initialize
  trap('INT') { self.exit }
  @input, @output = nil
  @parser = Brainfucktt::Parser.new
end

Class Method Details

.instanceObject



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

def instance
  @instance ||= new
end

.runObject



13
14
15
16
# File 'lib/brainfucktt/repl.rb', line 13

def run
  puts instance.help
  instance.loop
end

Instance Method Details

#asciiObject



89
90
91
# File 'lib/brainfucktt/repl.rb', line 89

def ascii
  @parser.data.bytes.collect(&:to_ascii).join
end

#binaryObject



93
94
95
# File 'lib/brainfucktt/repl.rb', line 93

def binary
  @parser.data.bytes.collect(&:to_binary).join(' ')
end

#decimalObject



97
98
99
# File 'lib/brainfucktt/repl.rb', line 97

def decimal
  @parser.data.bytes.collect(&:to_i).join(' ')
end

#evalObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/brainfucktt/repl.rb', line 31

def eval
  case @input.strip
  when 'help' then puts help
  when '?' then puts help
  when 'quit' then self.exit
  when 'exit' then self.exit
  when 'hex' then puts hex
  when 'ascii' then puts ascii
  when 'binary' then puts binary
  when 'decimal' then puts decimal
  else; @parser.run(@input)
  end
end

#exitObject



80
81
82
83
# File 'lib/brainfucktt/repl.rb', line 80

def exit
  puts
  Kernel.exit
end

#helpObject



70
71
72
73
74
75
76
77
78
# File 'lib/brainfucktt/repl.rb', line 70

def help
  result = ''
  result << "Type 'help' or '?' for help.\n"
  result << "Type 'hex'     to output the data buffer in hexadecimal.\n"
  result << "Type 'ascii'   to output the data buffer in ASCII.\n"
  result << "Type 'binary'  to output the data buffer in binary.\n"
  result << "Type 'decimal' to output the data buffer in decimal.\n"
  result << "Type 'exit' or 'quit' or press CTRL-C to exit.\n\n"
end

#hexObject



85
86
87
# File 'lib/brainfucktt/repl.rb', line 85

def hex
  @parser.data.bytes.collect(&:to_hex).join(' ')
end

#loopObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/brainfucktt/repl.rb', line 58

def loop
  Kernel.loop do
    # begin
      read
      eval
      print
    # rescue
    #   # Do squat
    # end
  end
end


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/brainfucktt/repl.rb', line 45

def print
  puts @parser.data.bytes.collect { |byte|
    byte_str = ''
    byte_str << (@parser.byte == byte ? '>' : '|')
    byte_str << byte.to_hex
    byte_str << (@parser.byte == byte ? '<' : '|')
    
    byte_str
  }.join(' ')
  
  @input = nil
end

#readObject



26
27
28
29
# File 'lib/brainfucktt/repl.rb', line 26

def read
  Kernel.print '> '
  @input = gets
end