Created
November 24, 2009 23:15
-
-
Save anonymous/242325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
input = String.new | |
if @opts[:input] == :stdin | |
input = $stdin.readlines.join | |
else | |
IO.foreach(@opts[:input]) do |line| | |
input += line | |
end | |
end | |
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