/branches/MakLivesHere/cbc/cbc/Common/CompileArgs.cs
C# | 258 lines | 154 code | 22 blank | 82 comment | 38 complexity | 73a6ef95fd10569aae7b02d72a2f3d88 MD5 | raw file
Possible License(s): AGPL-3.0
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace CBucks.Compiler {
-
- // TODO: add correct & comprehensive handling for all command
- // line errors
-
- /// <summary>
- /// The compilation target type
- /// </summary>
- public enum Target {
- /// <summary>
- /// No target type
- /// </summary>
- None,
- /// <summary>
- /// Executable file
- /// </summary>
- Exe,
- /// <summary>
- /// Dynamic Library
- /// </summary>
- Dll
- } // end of enum Target
-
- /// <summary>
- /// The compilation mode enumeration
- /// </summary>
- public enum CompileMode {
- /// <summary>
- /// No compilation
- /// </summary>
- None,
- /// <summary>
- /// The tokenize-only compilation mode
- /// </summary>
- Token,
- /// <summary>
- /// The parse-only compilation mode
- /// </summary>
- Parse,
- /// <summary>
- /// The parse & check compilation mode (no code generation)
- /// </summary>
- Check,
- /// <summary>
- /// Code generation mode. This is the default value.
- /// </summary>
- Gen
- } // end of CompileMode
-
- /// <summary>
- /// Represents arguments of the compilation
- /// </summary>
- public sealed class CompileArgs {
-
- /// <summary>
- /// Creates a new CompileArgs instance
- /// </summary>
- public CompileArgs() { }
-
- /// <summary>
- /// Creates a list of compile arguments from the command line
- /// </summary>
- /// <param name="args"> The arguments of the command line </param>
- /// <returns> The compilation arguments </returns>
- public static CompileArgs fromCmdLine(string[] args) {
- CompileArgs ca = new CompileArgs();
- ca.compMode = CompileMode.Gen;
- ca.target = Target.Exe;
- // parse the command line
- try {
- // flags to control re-specification
- bool targetSpecified = false, modeSpecified = false;
- for(int i = 0; i < args.Length; i++) {
- string arg = args[i];
- if(arg[0] != '-')
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- switch(arg) {
- case "-cb":
- ca.useCbRuntime = true;
- break;
- case "-wd":
- ca.waitDebugAttach = true;
- break;
- case "-wr":
- ca.waitResult = true;
- break;
- case "-o":
- if(null != ca.outName)
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- ca.outName = args[++i];
- break;
- case "-h":
- case "--h":
- case "--help":
- case "-help":
- case "-?":
- ca.help = true;
- break;
- case "-t":
- if(targetSpecified)
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- targetSpecified = true;
- if(args[i + 1] == "exe")
- ca.target = Target.Exe;
- else if(args[i + 1] == "dll")
- ca.target = Target.Dll;
- else
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- i++;
- break;
- case "-r":
- if(null != ca.reportName)
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- ca.reportName = args[++i];
- break;
- case "-m":
- if(modeSpecified)
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- modeSpecified = true;
- switch(args[i + 1]) {
- case "token":
- ca.compMode = CompileMode.Token; break;
- case "parse":
- ca.compMode = CompileMode.Parse; break;
- case "check":
- ca.compMode = CompileMode.Check; break;
- case "gen":
- ca.compMode = CompileMode.Gen; break;
- default:
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- break;
- } // end of switch()
- i++;
- break;
-
- case "-l":
- // get the list of assemblies
- if(null != ca.assNames)
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- List<string> assNames = new List<string>();
- for(i++; i < args.Length && args[i][0] != '-'; i++)
- assNames.Add(args[i]);
- if(assNames.Count == 0)
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- ca.assNames = assNames.ToArray();
- if(i != args.Length) i--;
- break;
-
- case "-s":
- // get the list of source files
- if(null != ca.sourceNames)
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- List<string> srcNames = new List<string>();
- for(i++; i < args.Length && args[i][0] != '-'; i++)
- srcNames.Add(args[i]);
- if(srcNames.Count == 0)
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- ca.sourceNames = srcNames.ToArray();
- if(i != args.Length) i--;
- break;
- } // end of switch()
- } // end of for()
- } catch(IndexOutOfRangeException) {
- Report.error(ErrorCode.InvalidCommandLine, null, true);
- } // end of try
- // check whether all necessary properties have been defined
- if(!ca.help) {
- if(null == ca.sourceNames)
- Report.error(ErrorCode.NoSourceFiles, null, true);
- if(null == ca.outName)
- Report.error(ErrorCode.NoTargetFile, null, true);
- } // end of if()
- if(ca.help)
- printUsage();
- return ca;
- } // end of fromCmdLine()
-
- /// <summary>
- /// Prints compiler usage text
- /// </summary>
- public static void printUsage() {
- Console.WriteLine("Usage: cbc [options]");
- Console.WriteLine("Possible options include:");
- Console.WriteLine("-o <filename>: specifies the output file");
- Console.WriteLine("-h, --h, --help, -help, -?, <empty>: " +
- "outputs this message");
- Console.WriteLine("-t exe|dll: specifies target file type");
- Console.WriteLine("-r <filename>: specifies the output text file");
- Console.WriteLine("-m token|parse|check|gen: specifies " +
- "compilation level");
- Console.WriteLine("-l <assembly1> ... <assemblyn>: " +
- "references assemblies");
- Console.WriteLine("-s <file1> ... <filen>: " +
- "adds source files to compile");
- Console.WriteLine("-wd: waits for the user to press <ENTER> at the " +
- "start of the program; useful for compiler debugging");
- Console.WriteLine("-wr: waits for the user to press <ENTER> at the " +
- "end of the program; useful for compiler debugging");
- } // end of printUsage()
-
- /// <summary>
- /// The paths to the source files to compile
- /// </summary>
- public string[] sourceNames;
-
- /// <summary>
- /// The name of the output file
- /// </summary>
- public string outName;
-
- /// <summary>
- /// Indicates whether the help message should be printed
- /// </summary>
- public bool help = false;
-
- /// <summary>
- /// The default target
- /// </summary>
- public Target target = Target.Exe;
-
- /// <summary>
- /// The report name
- /// </summary>
- public string reportName = null;
-
- /// <summary>
- /// The compilation mode
- /// </summary>
- public CompileMode compMode = CompileMode.Gen;
-
- /// <summary>
- /// The names of the assemblies referenced
- /// </summary>
- public string[] assNames;
-
- /// <summary>
- /// Whether to wait for debug attach at the beginning of the compilation
- /// </summary>
- public bool waitDebugAttach = false;
-
- /// <summary>
- /// Whether to wait at the end of the compilation
- /// </summary>
- public bool waitResult = false;
-
- /// <summary>
- /// Whether to initialize C$ runtime. Without this option, the exe program
- /// using C$ runtime will not work
- /// </summary>
- public bool useCbRuntime = false;
-
- } // end of CompileArgs
-
- } // end of namespace CBucks.Compiler