-
Notifications
You must be signed in to change notification settings - Fork 0
/
legographs.rb
executable file
·48 lines (42 loc) · 1.1 KB
/
legographs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
#!/usr/bin/env ruby
require_relative 'bc'
module Tools
class BytecodePrinter
def initialize
@bc_lookup_hash = generate_reverse_hash
end
def generate_reverse_hash
bytecodes = []
Bytecode.constants.each { |bc|
bytecodes << { (Bytecode.const_get bc) => bc }
}
bytecodes.reduce({}, :merge)
end
def pretty_print(bytes)
bytes.each { |byte|
if @bc_lookup_hash[byte] then
puts @bc_lookup_hash[byte].to_s + "\t\t0x#{byte.to_s(16).upcase}"
else
puts "DATA" + "\t\t0x#{byte.to_s(16).upcase}" + "\t\td: #{byte.to_s(10)}" + "\t\ta: #{byte.chr}"
end
}
end
def run
output_file?
load_file
end
def output_file?
if ARGV[1]
$stdout.reopen(ARGV[1].to_s, "w")
end
end
def load_file
file_name = ARGV[0].to_s
raise RuntimeError.new("No file was given.") unless File.exist?(file_name) && File.readable?(file_name)
file = IO.read(file_name)
pretty_print(file.bytes)
end
end
end
legographs = Tools::BytecodePrinter.new
legographs.run