Skip to content

Instantly share code, notes, and snippets.

@hone
Forked from anonymous/gist:242325
Created November 25, 2009 00:05
Show Gist options
  • Save hone/242352 to your computer and use it in GitHub Desktop.
Save hone/242352 to your computer and use it in GitHub Desktop.
require 'optparse'
require 'scanner/scanner'
module SC
class SimpleCompilerError < RuntimeError
end
class CommandOptionError < RuntimeError
end
class SimpleCompiler
def initialize(args)
begin
@opts = parse_args(args)
@input = get_input
rescue CommandOptionError => ex
raise SimpleCompilerError, ex.to_s
end
end
def parse_args(args)
opts = { :phase => :unspecified, :input => :stdin }
optparser = OptionParser.new
optparser.banner = "./sc [-s] [filename]"
optparser.on("-s", "invokes the scanner") do
opts[:phase] = :scanner
end
begin
optparser.parse!(args)
rescue OptionParser::ParseError
raise CommandOptionError, optparser.to_s
end
case
when args.length == 1 then opts[:input] = args[0]
when args.length > 1 then raise CommandOptionError, optparser.to_s
end
raise CommandOptionError, optparser.to_s if (opts[:phase] == :unspecified)
return opts
end
def get_input
io =
if @opts[:input] == :stdin
$stdin
else
File.new(@opts[:input])
end
input = io.read
io.close
return input
end
def run
case
when @opts[:phase] == :scanner then run_scanner
end
end
def run_scanner
end
public :run
private :get_input, :parse_args, :run_scanner
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment