Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'optparse'
- require 'colorize'
- opt = {}
- OptionParser.new do |opts|
- opts.banner = "Usage: example.rb [options]"
- opts.on("-c", "--compile FILE", "Specify input file [else from STDIN]") do |v|
- opt[:input] = v
- end
- opts.on("-o", "--output FILE", "Specify output file [else from STDOUT]") do |v|
- opt[:output] = v
- end
- opts.on("-t", "--type TYPE", "Specify output file type [ruby, cxx supported]") do |v|
- opt[:type] = v
- end
- opts.on("-?", "--help", "This help") do
- puts opts
- exit 0
- end
- end.parse!
- code = nil
- if opt[:input]
- code = IO.read opt[:input]
- else
- code = $stdin.read
- end
- unless opt[:type] == 'cxx' or opt[:type] == 'ruby'
- puts "#" + "HALT Wrong type, using Ruby".colorize(:background => :red).colorize(:white)
- opt[:type] = 'ruby'
- end
- rubyc = "\tbuf = [ ]\n\ti = 0\n\tbuf[0] = 0; "
- cppc = "#include <bits/stdc++.h>\nusing namespace std; int main() { vector < long long > buf; buf.resize(1000000); size_t i = 0; char timex; "
- code.split('').each { |ch|
- if ch == '>'
- rubyc += "i += 1; unless buf[i]; buf[i] = 0; end; "
- cppc += "i++; "
- end
- if ch == '<'
- rubyc += "i -= 1; unless buf[i]; buf[i] = 0; end; "
- cppc += "i--; "
- end
- if ch == '+'
- rubyc += "buf[i] += 1; "
- cppc += "buf[i]++; "
- end
- if ch == '-'
- rubyc += "buf[i] -= 1; "
- cppc += "buf[i]--; "
- end
- if ch == '.'
- rubyc += "print buf[i].chr; "
- cppc += "cout << (char)buf[i]; "
- end
- if ch == ','
- rubyc += "buf[i] = getc ; "
- cppc += "cin >> timex; buf[i] = timex; "
- end
- if ch == '['
- rubyc += "while buf[i] != 0; "
- cppc += "while(buf[i]) { "
- end
- if ch == ']'
- rubyc += "end; "
- cppc += "} "
- end
- }
- cppc += "}"
- rescode = (opt[:type] == 'cxx' ? cppc : rubyc)
- if opt[:output]
- IO.write opt[:output], rescode
- else
- puts rescode
- end
Advertisement
Add Comment
Please, Sign In to add comment