PageRenderTime 75ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  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 "-wd":
  79. ca.waitDebugAttach = true;
  80. break;
  81. case "-wr":
  82. ca.waitResult = true;
  83. break;
  84. case "-o":
  85. if(null != ca.outName)
  86. Report.error(ErrorCode.InvalidCommandLine, null, true);
  87. ca.outName = args[++i];
  88. break;
  89. case "-h":
  90. case "--h":
  91. case "--help":
  92. case "-help":
  93. case "-?":
  94. ca.help = true;
  95. break;
  96. case "-t":
  97. if(targetSpecified)
  98. Report.error(ErrorCode.InvalidCommandLine, null, true);
  99. targetSpecified = true;
  100. if(args[i + 1] == "exe")
  101. ca.target = Target.Exe;
  102. else if(args[i + 1] == "dll")
  103. ca.target = Target.Dll;
  104. else
  105. Report.error(ErrorCode.InvalidCommandLine, null, true);
  106. i++;
  107. break;
  108. case "-r":
  109. if(null != ca.reportName)
  110. Report.error(ErrorCode.InvalidCommandLine, null, true);
  111. ca.reportName = args[++i];
  112. break;
  113. case "-m":
  114. if(modeSpecified)
  115. Report.error(ErrorCode.InvalidCommandLine, null, true);
  116. modeSpecified = true;
  117. switch(args[i + 1]) {
  118. case "token":
  119. ca.compMode = CompileMode.Token; break;
  120. case "parse":
  121. ca.compMode = CompileMode.Parse; break;
  122. case "check":
  123. ca.compMode = CompileMode.Check; break;
  124. case "gen":
  125. ca.compMode = CompileMode.Gen; break;
  126. default:
  127. Report.error(ErrorCode.InvalidCommandLine, null, true);
  128. break;
  129. } // end of switch()
  130. i++;
  131. break;
  132. case "-l":
  133. // get the list of assemblies
  134. if(null != ca.assNames)
  135. Report.error(ErrorCode.InvalidCommandLine, null, true);
  136. List<string> assNames = new List<string>();
  137. for(i++; i < args.Length && args[i][0] != '-'; i++)
  138. assNames.Add(args[i]);
  139. if(assNames.Count == 0)
  140. Report.error(ErrorCode.InvalidCommandLine, null, true);
  141. ca.assNames = assNames.ToArray();
  142. if(i != args.Length) i--;
  143. break;
  144. case "-s":
  145. // get the list of source files
  146. if(null != ca.sourceNames)
  147. Report.error(ErrorCode.InvalidCommandLine, null, true);
  148. List<string> srcNames = new List<string>();
  149. for(i++; i < args.Length && args[i][0] != '-'; i++)
  150. srcNames.Add(args[i]);
  151. if(srcNames.Count == 0)
  152. Report.error(ErrorCode.InvalidCommandLine, null, true);
  153. ca.sourceNames = srcNames.ToArray();
  154. if(i != args.Length) i--;
  155. break;
  156. } // end of switch()
  157. } // end of for()
  158. } catch(IndexOutOfRangeException) {
  159. Report.error(ErrorCode.InvalidCommandLine, null, true);
  160. } // end of try
  161. // check whether all necessary properties have been defined
  162. if(!ca.help) {
  163. if(null == ca.sourceNames)
  164. Report.error(ErrorCode.NoSourceFiles, null, true);
  165. if(null == ca.outName)
  166. Report.error(ErrorCode.NoTargetFile, null, true);
  167. } // end of if()
  168. if(ca.help)
  169. printUsage();
  170. return ca;
  171. } // end of fromCmdLine()
  172. /// <summary>
  173. /// Prints compiler usage text
  174. /// </summary>
  175. public static void printUsage() {
  176. Console.WriteLine("Usage: cbc [options]");
  177. Console.WriteLine("Possible options include:");
  178. Console.WriteLine("-o <filename>: specifies the output file");
  179. Console.WriteLine("-h, --h, --help, -help, -?, <empty>: " +
  180. "outputs this message");
  181. Console.WriteLine("-t exe|dll: specifies target file type");
  182. Console.WriteLine("-r <filename>: specifies the output text file");
  183. Console.WriteLine("-m token|parse|check|gen: specifies " +
  184. "compilation level");
  185. Console.WriteLine("-l <assembly1> ... <assemblyn>: " +
  186. "references assemblies");
  187. Console.WriteLine("-s <file1> ... <filen>: " +
  188. "adds source files to compile");
  189. Console.WriteLine("-wd: waits for the user to press <ENTER> at the " +
  190. "start of the program; useful for compiler debugging");
  191. Console.WriteLine("-wr: waits for the user to press <ENTER> at the " +
  192. "end of the program; useful for compiler debugging");
  193. } // end of printUsage()
  194. /// <summary>
  195. /// The paths to the source files to compile
  196. /// </summary>
  197. public string[] sourceNames;
  198. /// <summary>
  199. /// The name of the output file
  200. /// </summary>
  201. public string outName;
  202. /// <summary>
  203. /// Indicates whether the help message should be printed
  204. /// </summary>
  205. public bool help = false;
  206. /// <summary>
  207. /// The default target
  208. /// </summary>
  209. public Target target = Target.Exe;
  210. /// <summary>
  211. /// The report name
  212. /// </summary>
  213. public string reportName = null;
  214. /// <summary>
  215. /// The compilation mode
  216. /// </summary>
  217. public CompileMode compMode = CompileMode.Gen;
  218. /// <summary>
  219. /// The names of the assemblies referenced
  220. /// </summary>
  221. public string[] assNames;
  222. /// <summary>
  223. /// Whether to wait for debug attach at the beginning of the compilation
  224. /// </summary>
  225. public bool waitDebugAttach = false;
  226. /// <summary>
  227. /// Whether to wait at the end of the compilation
  228. /// </summary>
  229. public bool waitResult = false;
  230. /// <summary>
  231. /// Whether to initialize C$ runtime. Without this option, the exe program
  232. /// using C$ runtime will not work
  233. /// </summary>
  234. public bool useCbRuntime = false;
  235. } // end of CompileArgs
  236. } // end of namespace CBucks.Compiler