6 require 'voodoo/generators/dummy_generator'
8 USAGE
="USAGE: voodooc [options] <input file>"
10 Input file denotes the file to compile. The special name '-' causes
11 voodooc to read from standard input. In that case, the -o option is
18 --architecture <architecture>
19 Select target architecture. Use -a help to get a list of
20 supported architectures.
24 Check program for correctness, but do not actually compile it.
28 --output-format <format>
29 Select output format. Use -a <architecture> -f help to get a list of
30 supported output formats for architecture.
33 List the features supported by this implementation. The features
34 are listed in the format <feature name><tab><version>, ordered
35 by feature name. The program exits after printing the list of
36 features; no compilation is performed.
40 Display usage information and exit. No compilation will be performed.
43 --output <output file>
44 --output-file <output file>
45 Set output file name. The special name '-' causes voodooc to write
49 Display version information and exit. No compilation will be performed.
52 architecture
= Voodoo
::Config.default_architecture
56 output_format
= Voodoo
::Config.default_format
59 # Gets the input file name from the command line.
60 def get_input_file_name
65 $stderr.puts
"no input files"
67 $stderr.puts
"Too many arguments"
75 # If error is a Voodoo::Compiler::Error, prints error messages.
76 # Else, re-raises error.
77 def handle_error error
78 # If error is a compiler error, iterate over all child errors and
79 # print user-friendly messages. Else, re-raise so that the default
80 # handling is performed.
81 if error
.kind_of
? Voodoo
::Compiler::Error
82 error
.errors
.each
do |e
|
84 if e
.respond_to
?(:start_line) && e
.start_line
!= nil
85 message
<< "#{e.start_line}: "
86 if e
.input_name
!= nil
87 message
= "#{e.input_name}:#{message}"
89 message
= "line #{message}"
94 if e
.respond_to
?(:text) && e
.text
!= nil
95 $stderr.print
"\n #{e.text.gsub("\n", "\n ")}\n"
104 # Process command line
107 opts
= GetoptLong
.new(
108 ['--arch', '--architecture', '-a', GetoptLong
::REQUIRED_ARGUMENT],
109 ['--check', '-c', GetoptLong
::NO_ARGUMENT],
110 ['--features', GetoptLong
::NO_ARGUMENT],
111 ['--help', '-h', GetoptLong
::NO_ARGUMENT],
112 ['--output-file', '--output', '-o', GetoptLong
::REQUIRED_ARGUMENT],
113 ['--output-format', '--format', '-f', GetoptLong
::REQUIRED_ARGUMENT ],
114 ['--version', GetoptLong
::NO_ARGUMENT]
118 opts
.each
do |opt
, arg
|
121 architecture
= arg
.to_sym
132 when '--output-format'
133 output_format
= arg
.to_sym
135 puts
"#{Voodoo::Config::IMPLEMENTATION_NAME} version " +
136 Voodoo
::Config::IMPLEMENTATION_VERSION
141 if architecture
== :help
142 # List supported architectures
143 puts
"Supported architectures:"
144 puts Voodoo
::CodeGenerator.architectures
.map
{|arch
| arch
.to_s
}.sort
148 if output_format
== :help
149 # List supported output formats
150 puts
"Supported output formats for architecture #{architecture}:"
151 puts Voodoo
::CodeGenerator.formats(architecture
).map
{|f
| f
.to_s
}.sort
156 input_file
= get_input_file_name
157 infile
= input_file
== '-' ? $stdin : open(input_file
)
159 parser
= Voodoo
::Parser.new infile
160 generator
= Voodoo
::DummyGenerator.new
161 compiler
= Voodoo
::Compiler.new parser
, generator
, nil
173 # Select code generator based on output format
175 generator
= Voodoo
::CodeGenerator.get_generator
:architecture => architecture
,
176 :format => output_format
177 rescue NotImplementedError
178 if Voodoo
::CodeGenerator.architecture_supported
? architecture
179 $stderr.puts
"Format #{output_format} is not supported for architecture #{architecture}"
180 $stderr.puts
"Supported formats for #{architecture} are:"
181 $stderr.puts Voodoo
::CodeGenerator.formats(architecture
)
183 $stderr.puts
"Architecture #{architecture} is not supported."
184 $stderr.puts
"Supported architectures are:"
185 $stderr.puts Voodoo
::CodeGenerator.architectures
190 # If requested, show features and exit
192 generator
.features
.to_a
.sort
{|x
,y
| x
[0].to_s
<=> y
[0].to_s
}.each
do |feature
|
193 puts
"#{feature[0]}\t#{feature[1]}"
198 input_file
= get_input_file_name
207 $stderr.puts
"The -o option is mandatory when reading from standard input"
211 infile
= open input_file
212 # Set output_file if not already set
213 output_file
= generator
.output_file_name input_file
unless output_file
216 if output_file
== '-'
219 outfile
= open output_file
, 'w'
223 parser
= Voodoo
::Parser.new infile
224 compiler
= Voodoo
::Compiler.new parser
, generator
, outfile
228 if output_file
!= '-'
229 File
::unlink(output_file
) if File
::exists?(output_file
)