Skip to content

Instantly share code, notes, and snippets.

@greneholt
Created February 29, 2012 21:52
Show Gist options
  • Save greneholt/1944725 to your computer and use it in GitHub Desktop.
Save greneholt/1944725 to your computer and use it in GitHub Desktop.
File IO for vm translator
begin
raise VMTranslatorError.new("Only enter the name of one .vm file or the name of a directory") if ARGV.length > 1
raise VMTranslatorError.new("File does not exist") unless File.exists?(ARGV.first)
if File.directory? ARGV.first
files = Dir.glob(File.join(ARGV.first, '*.vm'))
raise VMTranslatorError.new("No .vm files in #{ARGV.first}") unless files.length > 0
outfile = File.join(ARGV.first, File.basename(ARGV.first))
else
raise VMTranslatorError.new("Not a .vm file") unless ARGV.first =~ /\A(.+)\.vm\Z/i
files = [ARGV.first]
outfile = $1
end
program = VMProgram.new
files.each do |a|
open(a) do |infile|
program.add_class(File.basename(a, '.vm'), infile.read)
end
end
open("#{outfile}.asm", 'w') do |outfile|
outfile.write(program.to_asm)
end
rescue VMTranslatorError => e
puts "Virtual machine translator error\n#{e}"
exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment