/// <summary> /// Compile routine. Used to compile the CSharp code /// </summary> /// <param name="projectDir"></param> public static void Compile(string projectDir, CompilerFlags flags) { // Obtain the source code from the project Program file. string sourceCodeText = ReadSourceFile(projectDir + "\\Program.cs"); // Parse the source code into a syntax tree. CSharpSyntaxTree syntaxTree = Parse(sourceCodeText); // Get the root node of the syntax tree CSharpSyntaxNode rootNode = syntaxTree.GetRoot(); // Generate output or serialize compile unit // if (flags.GenerateOutput) { StringBuilder sourceCode = CPPCodeGenerator.GenerateCode(rootNode); if (flags.ArduinoSketch) { System.IO.Directory.CreateDirectory(projectDir + "\\arduino\\program"); AddSketch(sourceCode, "BlinkSample", "Program", "Main"); System.IO.File.WriteAllText(projectDir + "\\arduino\\program\\program.ino", sourceCode.ToString()); } else { System.IO.Directory.CreateDirectory(projectDir + "\\src"); AddMainEntry(sourceCode, "BlinkSample", "Program", "Main"); System.IO.File.WriteAllText(projectDir + "\\src\\program.cpp", sourceCode.ToString()); } } else { rootNode.SerializeTo(System.IO.File.OpenWrite(projectDir + "\\out\\out.cu")); } }
/// <summary> /// Parse the command line options/arguments into compiler flags and directives. /// </summary> /// <param name="args"></param> /// <returns></returns> public static CompilerFlags Parse(string[] args, ref string projectPath) { CompilerFlags parsedFlags = new CompilerFlags(); Parser.Default.ParseArguments <CLOptions>(args) .WithParsed <CLOptions>(clArgs => { Console.WriteLine(); if (clArgs.Verbose) { Console.WriteLine("> Verbose messages will be outputed during translation."); parsedFlags.Verbose = clArgs.Verbose; } if (clArgs.FrameworkName != null) { if (clArgs.FrameworkName.Contains("arduino")) { parsedFlags.ArduinoSketch = true; } } parsedFlags.GenerateOutput = clArgs.Compile; } ); return(parsedFlags); }
/// <summary> /// Main entry point. /// </summary> /// <param name="args"></param> static void Main(string[] args) { Console.WriteLine(Properties.Resources.CL_WELCOME); Console.WriteLine(Properties.Resources.CL_COPYRIGHT); string projectSourceDirectory; if (args.Length > 0) { projectSourceDirectory = args[0]; } else { projectSourceDirectory = Directory.GetCurrentDirectory(); } compilerFlags = CLParser.Parse(args, ref projectSourceDirectory); Compiler.Compile(projectSourceDirectory, compilerFlags); }