public FunctionInfo(string name, Type returnType, string body, bool isTemplateFunction, SyntaxEditorHelper.ScriptLanguageTypes scriptLanguage, string description, string templateReturnLanguage, string category) { Name = name; ReturnType = returnType; Parameters = new List<ParamInfo>(); Body = body; IsTemplateFunction = isTemplateFunction; ScriptLanguage = scriptLanguage; Description = description; TemplateReturnLanguage = templateReturnLanguage; Category = category; }
/// <summary> /// Adds debug symbols to the code of the function. /// </summary> /// <param name="code"></param> /// <param name="functionName"></param> /// <param name="language"></param> /// <returns></returns> public static string AddDebugSymbols(string code, string functionName, SyntaxEditorHelper.ScriptLanguageTypes language) { if (code.IndexOf("¬") < 0) { code = code.Replace("\n", "¬"); } else { throw new Exception("Special substitution character (¬) has been used in code. Line end substitutions can't be performed."); } string[] lines = code.Split('¬'); var sb2 = new StringBuilder(code.Length + lines.Length * 15); for (int lineCounter = 1; lineCounter <= lines.Length; lineCounter++) { switch (language) { case SyntaxEditorHelper.ScriptLanguageTypes.CSharp: // TODO: Investigate whether inserting at end is faster than appending, also whether using string.format is faster, also use AppendLine rather sb2.Append(lines[lineCounter - 1] + "/*DEBUG:" + lineCounter + "<" + functionName + ">*/" + Environment.NewLine); break; case SyntaxEditorHelper.ScriptLanguageTypes.VbNet: sb2.Append(lines[lineCounter - 1] + "'DEBUG:" + lineCounter + "<" + functionName + ">*/" + Environment.NewLine); break; default: throw new NotImplementedException("Language not catered for yet."); } } // Remove the last linebreak; if (sb2[sb2.Length - 1] == '\n') { sb2.Remove(sb2.Length - 1, 1); if (sb2[sb2.Length - 1] == '\r') { sb2.Remove(sb2.Length - 1, 1); } } else if (sb2[sb2.Length - 1] == '\r') { sb2.Remove(sb2.Length - 1, 1); } return sb2.ToString(); }
public FunctionInfo(string name, Type returnType, string body, bool isTemplateFunction, SyntaxEditorHelper.ScriptLanguageTypes scriptLanguage, string description, string templateReturnLanguage, string category, bool isExtensionMethod, string extendedType) : this(name, returnType, body, isTemplateFunction, scriptLanguage, description, templateReturnLanguage, category) { IsExtensionMethod = isExtensionMethod; ExtendedType = extendedType; }
private static string AddDebugSymbols(string code, string functionName, SyntaxEditorHelper.ScriptLanguageTypes language) { if (code.IndexOf("¬") < 0) { code = code.Replace("\n", "¬"); } else { throw new Exception("Special substitution character (¬) has been used in code. Line end substitutions can't be performed."); } string[] lines = code.Split('¬'); // Add a 10-space buffer to the start of every line, so that we can determine correct column offsets // even when "Write(" is prepended to certain calls for (int i = 0; i < lines.Length; i++) lines[i] = " " + lines[i]; var sb2 = new StringBuilder(code.Length + lines.Length * 15); for (int lineCounter = 1; lineCounter <= lines.Length; lineCounter++) { switch (language) { case SyntaxEditorHelper.ScriptLanguageTypes.CSharp: // TODO: Investigate whether inserting at end is faster than appending, also whether using string.format is faster, also use AppendLine rather sb2.AppendFormat("{0}/*DEBUG:{1}:{2}*/\n", lines[lineCounter - 1], functionName, lineCounter); break; case SyntaxEditorHelper.ScriptLanguageTypes.VbNet: sb2.AppendFormat("{0}'DEBUG:{1}:{2}*/\n", lines[lineCounter - 1], functionName, lineCounter); break; default: throw new NotImplementedException("Language not catered for yet."); } } // Remove the last linebreak; if (sb2[sb2.Length - 1] == '\n') { sb2.Remove(sb2.Length - 1, 1); if (sb2[sb2.Length - 1] == '\r') { sb2.Remove(sb2.Length - 1, 1); } } else if (sb2[sb2.Length - 1] == '\r') { sb2.Remove(sb2.Length - 1, 1); } return sb2.ToString(); }
public static bool CompileAndExecuteFile(string[] files, string[] args, string[] filesToEmbed, SyntaxEditorHelper.ScriptLanguageTypes language) { List<string> arrExtraFiles = new List<string>(); string sourceExtension; switch (language) { case SyntaxEditorHelper.ScriptLanguageTypes.CSharp: sourceExtension = ".cs"; break; case SyntaxEditorHelper.ScriptLanguageTypes.VbNet: sourceExtension = ".vb"; break; default: throw new NotImplementedException("Invalid script language."); } for (int i = 0; i < files.Length; i++) { if (Utility.StringsAreEqual(Path.GetExtension(files[i]), sourceExtension, false)) { arrExtraFiles.Add(files[i]); } } files = arrExtraFiles.ToArray(); string outputFileName = ""; string nrfFileName = ""; foreach (string arg in args) { if (arg.IndexOf("outputFileName=") >= 0) { outputFileName = arg.Substring("outputFileName=".Length); } else if (arg.IndexOf("nrfFileName=") >= 0) { nrfFileName = arg.Substring("nrfFileName=".Length); } } if (outputFileName.Length == 0) { throw new ApplicationException("Output file type is missing."); } bool isExe = (outputFileName.IndexOf(".exe") > 0); //Currently only csharp scripting is supported CodeDomProvider provider; switch (sourceExtension) { case ".cs": case ".ncs": //provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v2.0" } }); //provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v3.5" } }); provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }); //provider = new CSharpCodeProvider(); break; case ".vb": case ".nvb": provider = CodeDomProvider.CreateProvider("VisualBasic"); break; case ".njs": case ".js": provider = (CodeDomProvider)Activator.CreateInstance("Microsoft.JScript", "Microsoft.JScript.JScriptCodeProvider").Unwrap(); break; default: throw new UnsupportedLanguageExecption(sourceExtension); } CompilerParameters compilerparams = new CompilerParameters(); compilerparams.GenerateInMemory = false; // Set to true if you just want to compile and run, false to write to disk. //compilerparams.GenerateInMemory = CompileHelper.DebugVersion; // Set to true if you just want to compile and run, false to write to disk. compilerparams.GenerateExecutable = isExe; compilerparams.OutputAssembly = outputFileName; compilerparams.IncludeDebugInformation = true; // Embed resource file string optionPath = Path.Combine(Path.GetTempPath(), "options.xml"); string allCompilerOptions = "/res:\"" + optionPath + "\" "; // Embed extra files as resources. foreach (string filePath in filesToEmbed) { if (!File.Exists(filePath) && filePath.Length > 0) { System.Windows.Forms.MessageBox.Show(Controller.Instance.MainForm, "File not found: " + filePath + ". Compile aborted.", "Missing file", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return false; } allCompilerOptions += "/res:\"" + filePath + "\" "; } allCompilerOptions += "/nowarn:1687 "; //if (DebugVersion) //{ // allCompilerOptions += @" /debug "; //} // Remove the trailing comma compilerparams.CompilerOptions = allCompilerOptions; //Add assembly references from nscript.nrf or <file>.nrf if (File.Exists(nrfFileName)) { AddReferencesFromFile(compilerparams, nrfFileName); } else { //Use nscript.nrf string nrfFile = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "nscript.nrf"); if (File.Exists(nrfFile)) { AddReferencesFromFile(compilerparams, nrfFile); } } if (language == SyntaxEditorHelper.ScriptLanguageTypes.VbNet) { compilerparams.ReferencedAssemblies.Add(outputFileName.Replace("_VB", "_CS")); } // Replace the Namespace placeholder with the filename string body; using (var sr2 = new StreamReader(files[0])) { // Namespaces can only consist of letters and numbers, and the first character must be a letter body = sr2.ReadToEnd();//.Replace("#NAMESPACE#", Project.Instance.ProjectName); ParsedCode = body; sr2.Close(); } File.Delete(files[0]); Controller.Instance.ParsedCode = body; using (var sw2 = new StreamWriter(files[0])) { sw2.Write(body); sw2.Flush(); sw2.Close(); } CompilerResults results = provider.CompileAssemblyFromFile(compilerparams, files); CompiledAssemblyFileName = results.PathToAssembly; if (results.Errors.HasErrors) { List<CompilerError> templist = new List<CompilerError>(); body = Utility.StandardizeLineBreaks(body, Utility.LineBreaks.Unix); string[] lines = body.Split('\n'); foreach (System.CodeDom.Compiler.CompilerError error in results.Errors) { if (error.ErrorText.IndexOf("The type or namespace name") >= 0 && lines[error.Line - 1].IndexOf("using ") >= 0) { string invalidNamespace = lines[error.Line - 1].Substring("using ".Length); invalidNamespace = invalidNamespace.Substring(0, invalidNamespace.Length - 1); error.ErrorText = "Namespace (" + invalidNamespace + ") can't be found in referenced assemblies. On the Project Details screen, add the required assembly or remove the invalid namespace."; error.Line = -1; error.FileName = "<Project Details>"; } templist.Add(new CompilerError(error)); } OnCompilerError(templist.ToArray()); return false; } return true; }