2 // driver.cs: The compiler command line driver.
\r
4 // Author: Miguel de Icaza (miguel@gnu.org)
\r
6 // Licensed under the terms of the GNU GPL
\r
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
\r
14 using System.Reflection;
\r
15 using System.Reflection.Emit;
\r
16 using System.Collections;
\r
23 /// Summary description for Class1.
\r
28 // Assemblies references to be linked. Initialized with
\r
29 // mscorlib.dll here.
\r
30 ArrayList references;
\r
33 ArrayList link_paths;
\r
35 // Our parser context.
\r
38 bool yacc_verbose = false;
\r
40 int error_count = 0;
\r
42 public int parse (Tree context, string input_file)
\r
44 CSharpParser parser;
\r
45 System.IO.Stream input;
\r
49 input = System.IO.File.OpenRead (input_file);
\r
54 parser = new CSharpParser (context, input_file, input);
\r
55 parser.yacc_verbose = yacc_verbose;
\r
57 errors = parser.parse ();
\r
58 } catch (Exception ex) {
\r
59 Console.WriteLine (ex);
\r
60 Console.WriteLine ("Compilation aborted");
\r
67 public void Usage ()
\r
70 "compiler [-v] [-t tree] [-o output] [-L path] [-r reference] sources.cs\n" +
\r
71 "-v Verbose parsing\n"+
\r
72 "-o Specifies output file\n" +
\r
73 "-L Specifies path for loading assemblies\n" +
\r
74 "-r References an assembly\n");
\r
78 public IGenerator lookup_output (string name)
\r
81 return new Generator.TreeDump ();
\r
83 return new MSIL.Generator ();
\r
88 public static void error (string msg)
\r
90 Console.WriteLine ("Error: " + msg);
\r
93 public static void notice (string msg)
\r
95 Console.WriteLine (msg);
\r
98 public static int Main(string[] args)
\r
100 Driver driver = new Driver (args);
\r
102 return driver.error_count;
\r
105 public int LoadAssembly (string assembly)
\r
109 foreach (string dir in link_paths){
\r
110 string full_path = dir + "\\" + assembly;
\r
113 a = Assembly.Load (full_path);
\r
114 } catch (FileNotFoundException) {
\r
115 error ("// File not found: " + full_path);
\r
117 } catch (BadImageFormatException) {
\r
118 error ("// Bad file format: " + full_path);
\r
122 context.AddAssembly (a);
\r
127 public int LoadReferences ()
\r
131 foreach (string r in references){
\r
132 errors += LoadAssembly (r);
\r
139 public Driver (string [] args)
\r
141 Stream output_stream = Console.OpenStandardOutput ();
\r
142 IGenerator generator = null;
\r
145 context = new Tree ();
\r
146 references = new ArrayList ();
\r
147 link_paths = new ArrayList ();
\r
152 references.Add ("mscorlib.dll");
\r
153 link_paths.Add ("C://WINNT/Microsoft.Net/Framework/v1.0.2204");
\r
155 for (i = 0; i < args.Length; i++){
\r
156 string arg = args [i];
\r
158 if (arg.StartsWith ("-")){
\r
159 if (arg.StartsWith ("-v")){
\r
160 yacc_verbose = true;
\r
164 if (arg.StartsWith ("-t")){
\r
165 generator = lookup_output (args [++i]);
\r
169 if (arg.StartsWith ("-z")){
\r
170 generator.ParseOptions (args [++i]);
\r
174 if (arg.StartsWith ("-o")){
\r
176 output_stream = File.Create (args [++i]);
\r
177 } catch (Exception){
\r
178 error ("Could not write to `"+args [i]);
\r
185 if (arg.StartsWith ("-r")){
\r
186 references.Add (args [++i]);
\r
190 if (arg.StartsWith ("-L")){
\r
191 link_paths.Add (args [++i]);
\r
200 if (!arg.EndsWith (".cs")){
\r
201 error ("Do not know how to compile " + arg);
\r
206 errors += parse (context, arg);
\r
209 error ("// Parsing failed");
\r
211 notice ("// Parsing successful");
\r
214 // Load assemblies required
\r
216 errors += LoadReferences ();
\r
219 error ("// Could not load one or more assemblies");
\r
221 notice ("// Assemblies loaded");
\r
224 errors += context.BuilderInit ("Module", "Module.exe");
\r
227 // Name resolution on the tree.
\r
229 errors += context.Resolve ();
\r
232 // Code generation from the tree
\r
234 if (generator != null){
\r
235 StreamWriter output = new StreamWriter (output_stream);
\r
237 errors += generator.GenerateFromTree (context, output);
\r
240 error ("// Compilation failed");
\r
242 notice ("// Compilation successful");
\r
248 error_count = errors;
\r