public static Ast_CSharp mapCodeO2References(this Ast_CSharp astCSharp, CSharp_FastCompiler_CompilerOptions compilerOptions) { compilerOptions.ExtraSourceCodeFilesToCompile = new List <string>(); var compilationUnit = astCSharp.CompilationUnit; compilerOptions.ReferencedAssemblies = new List <string>(); // this should be cleared in a more global location compilerOptions.Extra_Using_Statements = new List <string>(); // same here var filesToDownload = new List <string>(); var currentUsingDeclarations = new List <string>(); foreach (var usingDeclaration in astCSharp.AstDetails.UsingDeclarations) { currentUsingDeclarations.Add(usingDeclaration.Text); } compilerOptions.mapOptionsDefinedInsideComments(astCSharp.AstDetails.comments()) .mapNuGetReferences() .Extra_Using_Statements.forEach(value => astCSharp.CompilationUnit.add_Using(value)); //resolve location of ExtraSourceCodeFilesToCompile astCSharp.resolveFileLocationsOfExtraSourceCodeFilesToCompile(compilerOptions); CompileEngine.handleReferencedAssembliesInstallRequirements(astCSharp.AstDetails.CSharpCode); //use the same technique to download files that are needed for this script (for example *.zip files or other unmanaged/support files) CompileEngine.tryToResolveReferencesForCompilation(filesToDownload, compilerOptions.WorkOffline); if (compilerOptions.onlyAddReferencedAssemblies.isFalse()) { foreach (var defaultRefAssembly in CompileEngine.DefaultReferencedAssemblies) { if (compilerOptions.ReferencedAssemblies.Contains(defaultRefAssembly).isFalse()) { compilerOptions.ReferencedAssemblies.add(defaultRefAssembly); } } foreach (var usingStatement in CompileEngine.DefaultUsingStatements) { if (false == currentUsingDeclarations.Contains(usingStatement)) { compilationUnit.add_Using(usingStatement); } } } //make sure the referenced assemblies are in the current execution directory CompileEngine.tryToResolveReferencesForCompilation(compilerOptions.ReferencedAssemblies, compilerOptions.WorkOffline); return(astCSharp); }
public static Ast_CSharp resolveFileLocationsOfExtraSourceCodeFilesToCompile(this Ast_CSharp astCSharp, CSharp_FastCompiler_CompilerOptions compilerOptions) { if (compilerOptions.ExtraSourceCodeFilesToCompile.size() > 0) { // try to resolve local file references try { if (compilerOptions.SourceCodeFile.isNull()) // in case this is not set (note: this should not be done here, better to do it on the REPL code) { compilerOptions.SourceCodeFile = PublicDI.CurrentScript; } for (int i = 0; i < compilerOptions.ExtraSourceCodeFilesToCompile.size(); i++) { var fileToResolve = compilerOptions.ExtraSourceCodeFilesToCompile[i].trim(); var resolved = false; // try using SourceCodeFile.directoryName() if (fileToResolve.fileExists().isFalse()) { if (compilerOptions.SourceCodeFile.valid() && compilerOptions.SourceCodeFile.isFile()) { var resolvedFile = compilerOptions.SourceCodeFile.directoryName().pathCombine(fileToResolve); if (resolvedFile.fileExists()) { compilerOptions.ExtraSourceCodeFilesToCompile[i] = resolvedFile; resolved = true; } } } // try using the localscripts folders var localMapping = fileToResolve.local(); if (localMapping.valid()) { compilerOptions.ExtraSourceCodeFilesToCompile[i] = localMapping; resolved = true; } if (resolved.isFalse() && fileToResolve.fileExists().isFalse()) { compilerOptions.ExtraSourceCodeFilesToCompile[i] = compilerOptions.ExtraSourceCodeFilesToCompile[i].fullPath(); } } } catch (Exception ex) { ex.log("in compileExtraSourceCodeReferencesAndUpdateReferencedAssemblies while resolving ExtraSourceCodeFilesToCompile"); } } return(astCSharp); }
public void compileSourceCode(string sourceCode) { if (sourceCode.valid().isFalse()) { "in CSharp_FastCompiler,compileSourceCode, provided sourceCode code was empty".error(); this.raise_OnCompileFail(); } else { if (getCachedAssemblyForCode_and_RaiseEvents(sourceCode)) { return; } // we need to do make sure we include any extra references included in the code var astCSharp = new Ast_CSharp(sourceCode); astCSharp.mapCodeO2References(CompilerOptions); compileSourceCode(sourceCode, false); } }
public void updateView(string sourceCode) { AstCSharp = new Ast_CSharp(sourceCode); AstDetails = AstCSharp.AstDetails; ast_TreeView.beginUpdate(); ast_TreeView.show_Ast(AstCSharp); if (this.AutoExpand_AstTreeView) { ast_TreeView.expandAll(); } ast_TreeView.endUpdate(); types_TreeView.show_List(AstDetails.Types, "Text"); usingDeclarations_TreeView.show_List(AstDetails.UsingDeclarations, "Text"); methods_TreeView.show_List(AstDetails.Methods, "Text"); fields_TreeView.show_List(AstDetails.Fields, "Text"); properties_TreeView.show_List(AstDetails.Properties, "Text"); comments_TreeView.show_List(AstDetails.Comments, "Text"); errors_RichTextBox.set_Text(AstCSharp.Errors); rewritenCSharpCode_SourceCodeEditor.setDocumentContents(AstDetails.CSharpCode, ".cs"); rewritenVBNet_SourceCodeEditor.setDocumentContents(AstDetails.VBNetCode, ".vb"); }
public void mapCodeO2References(Ast_CSharp astCSharp) { bool onlyAddReferencedAssemblies = false; ExtraSourceCodeFilesToCompile = new List <string>(); var compilationUnit = astCSharp.CompilationUnit; ReferencedAssemblies = new List <string>(); var filesToDownload = new List <string>(); var currentUsingDeclarations = new List <string>(); foreach (var usingDeclaration in astCSharp.AstDetails.UsingDeclarations) { currentUsingDeclarations.Add(usingDeclaration.Text); } foreach (var comment in astCSharp.AstDetails.Comments) { comment.Text.eq("O2Tag_OnlyAddReferencedAssemblies", () => onlyAddReferencedAssemblies = true); comment.Text.starts("using ", false, value => astCSharp.CompilationUnit.add_Using(value)); comment.Text.starts(new [] { "ref ", "O2Ref:" }, false, value => ReferencedAssemblies.Add(value)); comment.Text.starts(new[] { "Download:", "download:", "O2Download:" }, false, value => filesToDownload.Add(value)); comment.Text.starts(new[] { "include", "file ", "O2File:" }, false, value => ExtraSourceCodeFilesToCompile.Add(value)); comment.Text.starts(new[] { "dir ", "O2Dir:" }, false, value => ExtraSourceCodeFilesToCompile.AddRange(value.files("*.cs", true))); comment.Text.starts(new[] { "O2:debugSymbols", "generateDebugSymbols", "debugSymbols" }, true, (value) => generateDebugSymbols = true); comment.Text.starts(new[] { "SetInvocationParametersToDynamic" }, (value) => ResolveInvocationParametersType = false); comment.Text.starts(new[] { "DontSetInvocationParametersToDynamic" }, (value) => ResolveInvocationParametersType = true); comment.Text.eq("StaThread", () => { ExecuteInStaThread = true; }); comment.Text.eq("MtaThread", () => { ExecuteInMtaThread = true; }); comment.Text.eq("WorkOffline", () => { WorkOffline = true; }); } //resolve location of ExtraSourceCodeFilesToCompile resolveFileLocationsOfExtraSourceCodeFilesToCompile(); CompileEngine.handleReferencedAssembliesInstallRequirements(astCSharp.AstDetails.CSharpCode); //use the same technique to download files that are needed for this script (for example *.zip files or other unmanaged/support files) CompileEngine.tryToResolveReferencesForCompilation(filesToDownload, WorkOffline); if (onlyAddReferencedAssemblies.isFalse()) { foreach (var defaultRefAssembly in CompileEngine.DefaultReferencedAssemblies) { if (ReferencedAssemblies.Contains(defaultRefAssembly).isFalse()) { ReferencedAssemblies.add(defaultRefAssembly); } } foreach (var usingStatement in CompileEngine.DefaultUsingStatements) { if (false == currentUsingDeclarations.Contains(usingStatement)) { compilationUnit.add_Using(usingStatement); } } } //make sure the referenced assemblies are in the current execution directory CompileEngine.tryToResolveReferencesForCompilation(ReferencedAssemblies, WorkOffline); }
//this code needs to be completely rewritten public string tryToCreateCSharpCodeWith_Class_Method_WithMethodText(string code) { if (code.empty()) { return(null); } code = code.line(); // make sure there is an empty line at the end try { //handle special incudes in source code var lines = code.fix_CRLF().lines(); foreach (var originalLine in lines) { string line = originalLine; originalLine.starts("//O2Include:", (includeText) => { var file = includeText; var baseFile = SourceCodeFile ?? PublicDI.CurrentScript; var parentFolder = baseFile.parentFolder(); if (parentFolder.notValid()) { "[CSharpFastCompiled] in O2Include mapping, could not get parent folder of current script".error(); } var resolvedFile = CompileEngine.findFileOnLocalScriptFolder(file, parentFolder); if (resolvedFile.fileExists()) { var fileContents = resolvedFile.contents(); code = code.Replace(line, line.line().add(fileContents).line()); } else { "[CSharpFastCompiled] in O2Include mapping, could not a mapping for: {0}".error(includeText); } }); } var snippetParser = new SnippetParser(SupportedLanguage.CSharp); var parsedCode = snippetParser.Parse(code); AstErrors = snippetParser.errors(); CompilationUnit = new CompilationUnit(); if (parsedCode is BlockStatement || parsedCode is CompilationUnit) { Ast_CSharp astCSharp; if (parsedCode is BlockStatement) { // map parsedCode into a new type and method var blockStatement = (BlockStatement)parsedCode; CompilationUnit.add_Type(default_TypeName) .add_Method(default_MethodName, InvocationParameters, this.ResolveInvocationParametersType, blockStatement); // remove comments from parsed code astCSharp = new Ast_CSharp(CompilationUnit, snippetParser.Specials); // add references included in the original source code file mapCodeO2References(astCSharp); astCSharp.mapAstDetails(); astCSharp.ExtraSpecials.Clear(); var method = CompilationUnit.method(default_MethodName); var returntype = method.returnType(); var type = CompilationUnit.type(default_TypeName); type.Children.Clear(); var tempBlockStatement = new BlockStatement(); tempBlockStatement.add_Variable("a", 0); method.Body = tempBlockStatement; var newMethod = type.add_Method(default_MethodName, InvocationParameters, this.ResolveInvocationParametersType, tempBlockStatement); newMethod.TypeReference = returntype; astCSharp.mapAstDetails(); var csharpCode = astCSharp.AstDetails.CSharpCode .replace("Int32 a = 0;", code); AstDetails = new Ast_CSharp(csharpCode).AstDetails; CreatedFromSnipptet = true; DebugMode.ifDebug("Ast parsing was OK"); SourceCode = csharpCode; onAstOK.invoke(); return(csharpCode); } CompilationUnit = (CompilationUnit)parsedCode; if (CompilationUnit.Children.Count == 0) { return(null); } astCSharp = new Ast_CSharp(CompilationUnit, snippetParser.Specials); // add the comments from the original code mapCodeO2References(astCSharp); CreatedFromSnipptet = false; // create sourceCode using Ast_CSharp & AstDetails if (CompilationUnit.Children.Count > 0) { // reset the astCSharp.AstDetails object astCSharp.mapAstDetails(); // add the comments from the original code astCSharp.ExtraSpecials.AddRange(snippetParser.Specials); SourceCode = astCSharp.AstDetails.CSharpCode; //once we have the created SourceCode we need to create a new AST with it var tempAstDetails = new Ast_CSharp(SourceCode).AstDetails; //note we should try to add back the specials here (so that comments make it to the generated code AstDetails = tempAstDetails; DebugMode.ifDebug("Ast parsing was OK"); onAstOK.invoke(); return(SourceCode); } } } catch (Exception ex) { DebugMode.ifError("in createCSharpCodeWith_Class_Method_WithMethodText:{0}", ex.Message); } return(null); }
/// /// <summary> /// Returns a compilable C# file from and Snippet /// /// Dev Note:this code needs to be refactored (since it is too big and complex) /// </summary> /// <param name="code"></param> /// <returns></returns> public string tryToCreateCSharpCodeWith_Class_Method_WithMethodText(string code) { if (code.empty()) { return(null); } code = code.line(); // make sure there is an empty line at the end try { //handle special incudes in source code var lines = code.fix_CRLF().lines(); foreach (var originalLine in lines) { var line = originalLine; if (originalLine.starts("//O2Include:")) { var includeText = line.subString_After("//O2Include:"); var file = includeText; var baseFile = CompilerOptions.SourceCodeFile ?? PublicDI.CurrentScript; var parentFolder = baseFile.parentFolder(); if (parentFolder.notValid()) { "[CSharpFastCompiled] in O2Include mapping, could not get parent folder of current script".error(); } var resolvedFile = CompileEngine.findFileOnLocalScriptFolder(file, parentFolder); if (resolvedFile.fileExists()) { var fileContents = resolvedFile.contents(); code = code.Replace(line, line.line().add(fileContents).line()); } else { "[CSharpFastCompiled] in O2Include mapping, could not a mapping for: {0}".error(includeText); } } ; } var snippetParser = new SnippetParser(SupportedLanguage.CSharp); var parsedCode = snippetParser.Parse(code); this.astErrors(snippetParser.errors()); this.compilationUnit(new CompilationUnit()); if (parsedCode is BlockStatement || parsedCode is CompilationUnit) { Ast_CSharp astCSharp; if (parsedCode is BlockStatement) { // map parsedCode into a new type and method var blockStatement = (BlockStatement)parsedCode; var csharpCode = blockStatement.ast_CSharp_CreateCompilableClass(snippetParser, code, CompilerOptions, CompilerArtifacts, ExecutionOptions); this.astDetails(new Ast_CSharp(csharpCode).AstDetails); this.createdFromSnippet(true); DebugMode.ifDebug("Ast parsing was OK"); this.sourceCode(csharpCode); this.raise_OnAstOK(); return(csharpCode); } this.compilationUnit((CompilationUnit)parsedCode); if (this.compilationUnit().Children.Count == 0) { return(null); } astCSharp = new Ast_CSharp(this.compilationUnit(), snippetParser.Specials); // add the comments from the original code astCSharp.mapCodeO2References(CompilerOptions); this.createdFromSnippet(false); // create sourceCode using Ast_CSharp & AstDetails if (this.compilationUnit().Children.Count > 0) { // reset the astCSharp.AstDetails object astCSharp.mapAstDetails(); // add the comments from the original code astCSharp.ExtraSpecials.AddRange(snippetParser.Specials); this.sourceCode(astCSharp.AstDetails.CSharpCode); //once we have the created SourceCode we need to create a new AST with it var tempAstDetails = new Ast_CSharp(this.sourceCode()).AstDetails; //note we should try to add back the specials here (so that comments make it to the generated code this.astDetails(tempAstDetails); DebugMode.ifDebug("Ast parsing was OK"); this.raise_OnAstOK(); return(this.sourceCode()); } } } catch (Exception ex) { DebugMode.ifError("in createCSharpCodeWith_Class_Method_WithMethodText:{0}", ex.Message); } return(null); }
public static string csharpCode(this Ast_CSharp astCSharp) { return((astCSharp.notNull() && astCSharp.SourceCode.valid()) ? astCSharp.SourceCode : ""); }
public static List <AstValue <MethodDeclaration> > methods(this Ast_CSharp astCSharp) { return(astCSharp.astDetails().methods()); }
public static AstDetails astDetails(this Ast_CSharp astCSharp) { return(astCSharp.notNull() ? astCSharp.AstDetails : null); }