PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/adinetz03/cbc/cbc/Common/CompileArgs.cs

#
C# | 280 lines | 165 code | 24 blank | 91 comment | 38 complexity | 7e52e5c0e64dc4e140aafc9d0d506696 MD5 | raw file
Possible License(s): AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace CBucks.Compiler {
  5. // TODO: add correct & comprehensive handling for all command
  6. // line errors
  7. /// <summary>
  8. /// The compilation target type
  9. /// </summary>
  10. public enum Target {
  11. /// <summary>
  12. /// No target type
  13. /// </summary>
  14. None,
  15. /// <summary>
  16. /// Executable file
  17. /// </summary>
  18. Exe,
  19. /// <summary>
  20. /// Dynamic Library
  21. /// </summary>
  22. Dll
  23. } // end of enum Target
  24. /// <summary>
  25. /// The compilation mode enumeration
  26. /// </summary>
  27. public enum CompileMode {
  28. /// <summary>
  29. /// No compilation
  30. /// </summary>
  31. None,
  32. /// <summary>
  33. /// The tokenize-only compilation mode
  34. /// </summary>
  35. Token,
  36. /// <summary>
  37. /// The parse-only compilation mode
  38. /// </summary>
  39. Parse,
  40. /// <summary>
  41. /// The parse & check compilation mode (no code generation)
  42. /// </summary>
  43. Check,
  44. /// <summary>
  45. /// Code generation mode. This is the default value.
  46. /// </summary>
  47. Gen
  48. } // end of CompileMode
  49. /// <summary>
  50. /// Represents arguments of the compilation
  51. /// </summary>
  52. public sealed class CompileArgs {
  53. /// <summary>
  54. /// Creates a new CompileArgs instance
  55. /// </summary>
  56. public CompileArgs() { }
  57. /// <summary>
  58. /// Creates a list of compile arguments from the command line
  59. /// </summary>
  60. /// <param name="args"> The arguments of the command line </param>
  61. /// <returns> The compilation arguments </returns>
  62. public static CompileArgs fromCmdLine(string[] args) {
  63. CompileArgs ca = new CompileArgs();
  64. ca.compMode = CompileMode.Gen;
  65. ca.target = Target.Exe;
  66. // parse the command line
  67. try {
  68. // flags to control re-specification
  69. bool targetSpecified = false, modeSpecified = false;
  70. for(int i = 0; i < args.Length; i++) {
  71. string arg = args[i];
  72. if(arg[0] != '-')
  73. Report.error(ErrorCode.InvalidCommandLine, null, true);
  74. switch(arg) {
  75. case "-cb":
  76. ca.useCbRuntime = true;
  77. break;
  78. case "-spe":
  79. ca.suppressProgramExceptions = true;
  80. break;
  81. case "-wd":
  82. ca.waitDebugAttach = true;
  83. break;
  84. case "-ce":
  85. ca.catchOuterExceptions = false;
  86. break;
  87. case "-wr":
  88. ca.waitResult = true;
  89. break;
  90. case "-o":
  91. if(null != ca.outName)
  92. Report.error(ErrorCode.InvalidCommandLine, null, true);
  93. ca.outName = args[++i];
  94. break;
  95. case "-h":
  96. case "--h":
  97. case "--help":
  98. case "-help":
  99. case "-?":
  100. ca.help = true;
  101. break;
  102. case "-t":
  103. if(targetSpecified)
  104. Report.error(ErrorCode.InvalidCommandLine, null, true);
  105. targetSpecified = true;
  106. if(args[i + 1] == "exe")
  107. ca.target = Target.Exe;
  108. else if(args[i + 1] == "dll")
  109. ca.target = Target.Dll;
  110. else
  111. Report.error(ErrorCode.InvalidCommandLine, null, true);
  112. i++;
  113. break;
  114. case "-r":
  115. if(null != ca.reportName)
  116. Report.error(ErrorCode.InvalidCommandLine, null, true);
  117. ca.reportName = args[++i];
  118. break;
  119. case "-m":
  120. if(modeSpecified)
  121. Report.error(ErrorCode.InvalidCommandLine, null, true);
  122. modeSpecified = true;
  123. switch(args[i + 1]) {
  124. case "token":
  125. ca.compMode = CompileMode.Token; break;
  126. case "parse":
  127. ca.compMode = CompileMode.Parse; break;
  128. case "check":
  129. ca.compMode = CompileMode.Check; break;
  130. case "gen":
  131. ca.compMode = CompileMode.Gen; break;
  132. default:
  133. Report.error(ErrorCode.InvalidCommandLine, null, true);
  134. break;
  135. } // end of switch()
  136. i++;
  137. break;
  138. case "-l":
  139. // get the list of assemblies
  140. if(null != ca.assNames)
  141. Report.error(ErrorCode.InvalidCommandLine, null, true);
  142. List<string> assNames = new List<string>();
  143. for(i++; i < args.Length && args[i][0] != '-'; i++)
  144. assNames.Add(args[i]);
  145. if(assNames.Count == 0)
  146. Report.error(ErrorCode.InvalidCommandLine, null, true);
  147. ca.assNames = assNames.ToArray();
  148. if(i != args.Length) i--;
  149. break;
  150. case "-s":
  151. // get the list of source files
  152. if(null != ca.sourceNames)
  153. Report.error(ErrorCode.InvalidCommandLine, null, true);
  154. List<string> srcNames = new List<string>();
  155. for(i++; i < args.Length && args[i][0] != '-'; i++)
  156. srcNames.Add(args[i]);
  157. if(srcNames.Count == 0)
  158. Report.error(ErrorCode.InvalidCommandLine, null, true);
  159. ca.sourceNames = srcNames.ToArray();
  160. if(i != args.Length) i--;
  161. break;
  162. } // end of switch()
  163. } // end of for()
  164. } catch(IndexOutOfRangeException) {
  165. Report.error(ErrorCode.InvalidCommandLine, null, true);
  166. } // end of try
  167. // check whether all necessary properties have been defined
  168. if(!ca.help) {
  169. if(null == ca.sourceNames)
  170. Report.error(ErrorCode.NoSourceFiles, null, true);
  171. if(null == ca.outName)
  172. Report.error(ErrorCode.NoTargetFile, null, true);
  173. } // end of if()
  174. if(ca.help)
  175. printUsage();
  176. return ca;
  177. } // end of fromCmdLine()
  178. /// <summary>
  179. /// Prints compiler usage text
  180. /// </summary>
  181. public static void printUsage() {
  182. Console.WriteLine("Usage: cbc [options]");
  183. Console.WriteLine("Possible options include:");
  184. Console.WriteLine("-o <filename>: specifies the output file");
  185. Console.WriteLine("-h, --h, --help, -help, -?, <empty>: " +
  186. "outputs this message");
  187. Console.WriteLine("-t exe|dll: specifies target file type");
  188. Console.WriteLine("-r <filename>: specifies the output text file");
  189. Console.WriteLine("-m token|parse|check|gen: specifies " +
  190. "compilation level");
  191. Console.WriteLine("-l <assembly1> ... <assemblyn>: " +
  192. "references assemblies");
  193. Console.WriteLine("-s <file1> ... <filen>: " +
  194. "adds source files to compile");
  195. Console.WriteLine("-wd: waits for the user to press <ENTER> at the " +
  196. "start of the program; useful for compiler debugging");
  197. Console.WriteLine("-wr: waits for the user to press <ENTER> at the " +
  198. "end of the program; useful for compiler debugging");
  199. Console.WriteLine("-ce: does not catch exceptions at the top level");
  200. Console.WriteLine("-spe: silently terminates a generated program " +
  201. "on an unhandled exception; no effect for libraries");
  202. } // end of printUsage()
  203. /// <summary>
  204. /// The paths to the source files to compile
  205. /// </summary>
  206. public string[] sourceNames;
  207. /// <summary>
  208. /// The name of the output file
  209. /// </summary>
  210. public string outName;
  211. /// <summary>
  212. /// Indicates whether the help message should be printed
  213. /// </summary>
  214. public bool help = false;
  215. /// <summary>
  216. /// The default target
  217. /// </summary>
  218. public Target target = Target.Exe;
  219. /// <summary>
  220. /// The report name
  221. /// </summary>
  222. public string reportName = null;
  223. /// <summary>
  224. /// The compilation mode
  225. /// </summary>
  226. public CompileMode compMode = CompileMode.Gen;
  227. /// <summary>
  228. /// The names of the assemblies referenced
  229. /// </summary>
  230. public string[] assNames;
  231. /// <summary>
  232. /// Whether to wait for debug attach at the beginning of the compilation
  233. /// </summary>
  234. public bool waitDebugAttach = false;
  235. /// <summary>
  236. /// Whether to wait at the end of the compilation
  237. /// </summary>
  238. public bool waitResult = false;
  239. /// <summary>
  240. /// Whether to initialize C$ runtime. Without this option, the exe program
  241. /// using C$ runtime will not work
  242. /// </summary>
  243. public bool useCbRuntime = false;
  244. /// <summary>
  245. /// Indicates whether to catch outer exceptions; turning the option off
  246. /// helps with debugging the compiler
  247. /// </summary>
  248. public bool catchOuterExceptions = true;
  249. /// <summary>
  250. /// Indicates whether the resulting program should suppress all of
  251. /// its exceptions. If used, a program silently terminates on an unhandled
  252. /// excpetion. Useful with running command-line tests.
  253. /// </summary>
  254. public bool suppressProgramExceptions = false;
  255. } // end of CompileArgs
  256. } // end of namespace CBucks.Compiler