/// <summary> /// Generates the Full SourceCode needed to compile an assembly from just the snippet. /// Adds /// </summary> /// <param name="source"></param> /// <param name="MaxStackFrameCount"></param> /// <param name="simple"></param> /// <returns></returns> internal static string GenerateFullSource(CodeExecutionRequest executionRequest, bool tryReturn, bool extract) { string tempSource = tryReturn ? ReturnStatement(executionRequest.Source) : executionRequest.Source; tempSource = CodeGenerationHelperFunctions.PrepareSource(executionRequest.Session, tempSource); //tidy the Source if (extract) //check for the simple flag. if it is not present we will look for declarations and add them to the stored values { IList<Declaration> declarations; tempSource = CodeGenerationHelperFunctions.ExtractDeclarations(tempSource, out declarations); //Add all declarations in the sourcefile to the valuestore so they persist out of scope if (declarations.Count > 0) { ValueStore.AddDeclarations(executionRequest.Session, declarations); } } return UsingDeclarations //+ FriendAssemblyDeclaration + CodeTemplates.ClassHeader + StoredValueAccessors(executionRequest.Session) + FuncHeader + tempSource + ThrowStatement + CodeTemplates.FuncFooter + CodeTemplates.ClassFooter; }
/// <summary> /// Takes some Sourcecode and builds an Assembly out of it that Contains the SourceCode /// as well as various other Code like function and class declarations to make the snippet /// a legitimate Assembly. The snippet can then be called by invoking Wrapper.Eval() in the /// newly generated Assembly /// </summary> /// <param name="sourceCode"></param> /// <returns></returns> internal static CompilerResults Build(CodeExecutionRequest executionRequest) { //first try to return the statement. however, if the user did i.e. an assign we will get compiler errors //so if we got 3 + 5 as sourcecode, we basically first try return 3 + 5; and get a legit result. string source = CodeGenerator.GenerateFullSource(executionRequest, true, false); //compile the sourcecode for real CompilerResults results = BuildSimple(source); if (!results.Errors.HasErrors) //check if we have errors. if not we can return. return results; else //we got errors. this means that it was no simple expression, so we just use the input as it was intended { source = CodeGenerator.GenerateFullSource(executionRequest, false, true); return BuildSimple(source); } }