1
0
mirror of https://github.com/safiire/n65.git synced 2024-12-12 15:29:12 +00:00

Linted front_end.rb

This commit is contained in:
Saf 2020-08-30 12:06:22 -07:00
parent 520a5198af
commit 9f5f71cd4a

View File

@ -1,73 +1,63 @@
# frozen_string_literal: true
require 'optparse' require 'optparse'
require_relative '../n65' require_relative '../n65'
module N65 module N65
# This class handles the front end aspects,
#### # parsing the commandline options and running the assembler
## This class handles the front end aspects,
## parsing the commandline options and running the assembler
class FrontEnd class FrontEnd
####
## Initialize with ARGV commandline
def initialize(argv) def initialize(argv)
@options = {output_file: nil, write_symbol_table: false, quiet: false, cycle_count: false} @options = { output_file: nil, write_symbol_table: false, quiet: false, cycle_count: false }
@argv = argv.dup @argv = argv.dup
end end
# Run the assembler
####
## Run the assembler
def run def run
## First use the option parser
parser = create_option_parser parser = create_option_parser
parser.parse!(@argv) parser.parse!(@argv)
## Whatever is leftover in argv the input files
if @argv.size.zero? if @argv.size.zero?
STDERR.puts("No input files") warn('No input files')
exit(1) exit(1)
end end
## Only can assemble one file at once for now # Only can assemble one file at once for now
if @argv.size != 1 if @argv.size != 1
STDERR.puts "Can only assemble one input file at once, but you can use .inc and .incbin directives" warn('Can only assemble one input file at once, but you can use .inc and .incbin directives')
exit(1) exit(1)
end end
input_file = @argv.shift input_file = @argv.shift
## Make sure the input file exists # Make sure the input file exists
unless File.exists?(input_file) unless File.exist?(input_file)
STDERR.puts "Input file #{input_file} does not exist" warn("Input file #{input_file} does not exist")
exit(1) exit(1)
end end
## Maybe they didn't provide an output file name, so we'll guess # Maybe they didn't provide an output file name, so we'll guess
if @options[:output_file].nil? if @options[:output_file].nil?
ext = File.extname(input_file) ext = File.extname(input_file)
@options[:output_file] = input_file.gsub(ext, '') + '.nes' @options[:output_file] = "#{input_file.gsub(ext, '')}.nes"
end end
if @options.values.any?(&:nil?) if @options.values.any?(&:nil?)
STDERR.puts "Missing options try --help" warn('Missing options try --help')
exit(1) exit(1)
end end
N65::Assembler.from_file(input_file, @options) N65::Assembler.from_file(input_file, @options)
end end
private private
####
## Create a commandline option parser
def create_option_parser def create_option_parser
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] <input_file.asm>" opts.banner = "Usage: #{$PROGRAM_NAME} [options] <input_file.asm>"
opts.on('-o', '--outfile filename', 'outfile') do |output_file| opts.on('-o', '--outfile filename', 'outfile') do |output_file|
@options[:output_file] = output_file; @options[:output_file] = output_file
end end
opts.on('-s', '--symbols', 'Outputs a symbol map') do opts.on('-s', '--symbols', 'Outputs a symbol map') do
@ -91,11 +81,7 @@ module N65
puts opts puts opts
exit exit
end end
end end
end end
end end
end end