Improved search....Updated filesystem model. Added -aggressivesearch option.
[repair.git] / Repair / RepairCompiler / MCC / CLI.java
1 package MCC;
2
3 import java.util.Vector;
4 import java.util.StringTokenizer;
5
6 /**
7  * A generic command-line interface for 6.035 compilers.  This class
8  * provides command-line parsing for student projects.  It recognizes
9  * the required <tt>-target</tt>, <tt>-debug</tt>, <tt>-opt</tt>, and
10  * <tt>-o</tt> switches, and generates a name for input and output
11  * files.
12  *
13  * @author  le01, 6.035 Staff (<tt>6.035-staff@mit.edu</tt>)
14  * @version <tt>$Id: CLI.java,v 1.4 2004/05/10 21:54:40 bdemsky Exp $</tt>
15  */
16 public class CLI {
17     /**
18      * Array indicating which optimizations should be performed.  If
19      * a particular element is true, it indicates that the optimization
20      * named in the optnames[] parameter to parse with the same index
21      * should be performed.
22      */
23     public boolean opts[];
24   
25     /**
26      * Vector of String containing the command-line arguments which could
27      * not otherwise be parsed.
28      */
29     public Vector extras;
30   
31     /**
32      * Vector of String containing the optimizations which could not be
33      * parsed.  It is okay to complain about anything in this list, even
34      * without the <tt>-debug</tt> flag.
35      */
36     public Vector extraopts;
37
38     /**
39      * Name of the file to put the output in.
40      */
41     public String outfile;
42   
43     /**
44      * Name of the file to get input from.  This is null if the user didn't
45      * provide a file name.
46      */
47     public String infile;
48
49     /**
50      * The debug flag.  This is true if <tt>-debug</tt> was passed on
51      * the command line, requesting debugging output.
52      */
53     public boolean debug;
54
55     /**
56      * Verbose output
57      */
58     public int verbose;
59
60     /**
61      * Public constructor.  Sets up default values for all of the
62      * result fields.  Specifically, sets the input and output files
63      * to null, the target to DEFAULT, and the extras and extraopts
64      * arrays to new empty Vectors.
65      */
66     public CLI() {
67         outfile = null;
68         infile = null;
69         extras = new Vector();
70         extraopts = new Vector();
71         verbose = 0;
72     }
73
74     /**
75      * Parse the command-line arguments.  Sets all of the result fields
76      * accordingly. <BR>
77      * <ul>
78      * <li><TT>-target <I>target</I></TT> sets the CLI.target field based
79      * on the <I>target</I> specified. </li>
80      * <li><TT>scan</TT> or <TT>scanner</TT> specifies CLI.SCAN</li>
81      * <li><TT>parse</TT> specifies CLI.PARSE</li>
82      * <li><TT>inter</TT> specifies CLI.INTER</li>
83      * <li><TT>lowir</TT> specifies CLI.LOWIR</li>
84      * <TT>assembly</TT> or <TT>codegen</TT> specifies CLI.ASSEMBLY</li>
85      * </ul>
86      * The boolean array opts[] indicates which, if any, of the
87      * optimizations in optnames[] should be performed; these arrays
88      * are in the same order.
89      *
90      * @param args Array of arguments passed in to the program's Main
91      *   function.
92      * @param optnames Ordered array of recognized optimization names.  */
93     public void parse(String args[]) {
94
95         String optnames[] = {};
96         int context = 0;
97         String ext = ".out";
98
99         opts = new boolean[optnames.length];
100
101         for (int i = 0; i < args.length; i++) {
102             if (args[i].equals("-debug")) {
103                 context = 0;
104                 debug = true;
105             } else if (args[i].equals("-checkonly")) {
106                 Compiler.REPAIR=false;
107             } else if (args[i].equals("-aggressivesearch")) {
108                 Compiler.AGGRESSIVESEARCH=true;
109             } else if (args[i].equals("-verbose") || args[i].equals("-v")) {
110                 context = 0;
111                 verbose++;
112             } else if (args[i].equals("-opt"))
113                 context = 1;
114             else if (args[i].equals("-o"))
115                 context = 2;
116             else if (context == 1) {
117                 boolean hit = false;
118                 for (int j = 0; j < optnames.length; j++) {
119                     if (args[i].equals("all") ||
120                         (args[i].equals(optnames[j]))) {
121                         hit = true;
122                         opts[j] = true;
123                     }
124                     if (args[i].equals("-" + optnames[j])) {
125                         hit = true;
126                         opts[j] = false;
127                     }
128                 }
129                 if (!hit)
130                     extraopts.addElement(args[i]);
131             } else if (context == 2) {
132                 outfile = args[i];
133                 context = 0;
134             } else {
135                 boolean hit = false;
136                 for (int j = 0; j < optnames.length; j++) {
137                     if (args[i].equals("-" + optnames[j])) {
138                         hit = true;
139                         opts[j] = true;
140                     }
141                 }
142                 if (!hit) {
143                     extras.addElement(args[i]);
144                 }
145             }
146         }
147
148         // grab infile and lose extra args
149         int i = 0;
150         while (infile == null && i < extras.size()) {
151             String fn = (String) extras.elementAt(i);
152           
153             if (fn.charAt(0) != '-')
154             {
155                 infile = fn;
156                 extras.removeElementAt(i);
157             }
158             i++;
159         }
160     }
161 }