private void OnFileEvent(object sender, FileSystemEventArgs e) { Thread.Sleep(1); string name = e.Name.Substring(0, e.Name.Length - 3); name = name.Substring(name.LastIndexOf("/") + 1); name = name.Substring(name.LastIndexOf("\\") + 1); string root = watcher_to_root[sender]; string input = null; try { input = File.ReadAllText(e.FullPath); } catch (Exception) { compile_info[root][name] = string.Format("Failed to read file."); UpdateGui(); return; } TargetCppResult result = null; try { result = SugarCompiler.Compile(input, name); } catch (Exception err) { compile_info[root][name] = string.Format("Compile Error:\n{0}", err); Console.WriteLine("Compile Error!"); UpdateGui(); return; } try { File.WriteAllText(root + "/" + name + ".h", result.Header); File.WriteAllText(root + "/" + name + ".cpp", result.Implementation); } catch (Exception) { compile_info[root][name] = string.Format("Can't access file."); UpdateGui(); return; } compile_info[root][name] = null; Console.WriteLine("Compile Success!"); UpdateGui(); }
/// <summary> /// Compile code from input. /// </summary> private static void Compile(string input, string inputFileName) { int dot_pos = inputFileName.LastIndexOf("."); string file_no_ext = inputFileName.Substring(0, dot_pos); string header_name = file_no_ext + ".h"; string implementation_name = file_no_ext + ".cpp"; if (outputPath != null || outputPath == "") { if (!outputPath.EndsWith("/")) { outputPath = outputPath + "/"; } int k = header_name.LastIndexOf("/"); header_name = k == -1 ? header_name : header_name.Substring(k + 1); header_name = outputPath + header_name; k = implementation_name.LastIndexOf("/"); implementation_name = outputPath + (k == -1 ? implementation_name : implementation_name.Substring(k + 1)); } if (singleFile) { string code = SugarCompiler.Compile(input); if (printCode) { Console.WriteLine(code); } File.WriteAllText(implementation_name, code); } else { var result = SugarCompiler.Compile(input, file_no_ext); if (printCode) { Console.WriteLine(result.Header); Console.WriteLine(); Console.WriteLine(result.Implementation); } File.WriteAllText(header_name, result.Header); File.WriteAllText(implementation_name, result.Implementation); } }
private void SourceTextChanged(object sender, EventArgs e) { string input = this.Source.Text.Replace("\t", " "); File.WriteAllText("test.sc", input); try { string output = SugarCompiler.Compile(input); this.Result.Text = output; File.WriteAllText("test.cpp", output); } catch (Exception ex) { string output = string.Format("Compile Error:\n{0}", ex.Message); this.Result.Text = output; } }
private void Source_TextChanged_1(object sender, EventArgs e) { string input = this.Source.Text; File.WriteAllText("test.sc", input); try { TargetCpp sugar_cpp = new TargetCpp(); var result = SugarCompiler.Compile(input, "test"); this.Header.Text = result.Header; File.WriteAllText("test.h", this.Header.Text); this.Implementation.Text = result.Implementation; File.WriteAllText("test.cpp", this.Implementation.Text); } catch (Exception ex) { string output = string.Format("Compile Error:\n{0}", ex.Message); this.Header.Text = output; } }
internal static void DoCompile(string inputFileName, string arguments) { string cppFileName = Path.GetTempFileName(); string input = File.ReadAllText(inputFileName); TargetCppResult result = null; try { result = SugarCompiler.Compile(input, cppFileName); } catch (Exception ex) { Program.Panic(string.Format("Compile Error:\n{0}", ex.Message)); } // Write to temperory file File.Delete(cppFileName); File.WriteAllText(cppFileName + ".h", result.Header); File.WriteAllText(cppFileName + ".cpp", result.Implementation); // Execute compiler RunCommand(compiler.Command, cppFileName + ".cpp" + " " + arguments + " " + compiler.AdditionalArgs); }