private string Process(IList <JsStatement> stmts, IScriptSharpMetadataImporter metadata = null, INamer namer = null, IAssembly mainAssembly = null) { var obj = new DefaultLinker(metadata ?? new MockScriptSharpMetadataImporter(), namer ?? new MockNamer()); var processed = obj.Process(stmts, mainAssembly ?? new Mock <IAssembly>().Object); return(string.Join("", processed.Select(s => OutputFormatter.Format(s, allowIntermediates: false)))); }
internal Tuple<string, ICompilation, IMetadataImporter, MockErrorReporter> Compile(string source, bool includeLinq = false, bool expectErrors = false) { var sourceFile = new MockSourceFile("file.cs", source); var md = new MetadataImporter.ScriptSharpMetadataImporter(false); var n = new DefaultNamer(); var er = new MockErrorReporter(!expectErrors); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(md, er, n.GetTypeParameterName, tr => new JsTypeReferenceExpression(tr.Resolve(compilation.Compilation).GetDefinition())); var compiler = new Compiler.Compiler(md, n, rtl, er, allowUserDefinedStructs: false); var references = includeLinq ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib }; compilation = compiler.CreateCompilation(new[] { sourceFile }, references, null); var compiledTypes = compiler.Compile(compilation); if (expectErrors) { Assert.That(er.AllMessages, Is.Not.Empty, "Compile should have generated errors"); return Tuple.Create((string)null, compilation.Compilation, (IMetadataImporter)md, er); } er.AllMessagesText.Should().BeEmpty("Compile should not generate errors"); var js = new OOPEmulator.ScriptSharpOOPEmulator(compilation.Compilation, md, rtl, n, er).Process(compiledTypes, compilation.Compilation, null); js = new DefaultLinker(md, n).Process(js, compilation.Compilation.MainAssembly); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); if (Output == OutputType.GeneratedScript) Console.WriteLine(script); return Tuple.Create(script, compilation.Compilation, (IMetadataImporter)md, er); }
internal Tuple <string, ICompilation, IMetadataImporter, MockErrorReporter> Compile(string source, bool includeLinq = false, bool expectErrors = false) { var sourceFile = new MockSourceFile("file.cs", source); var md = new MetadataImporter.ScriptSharpMetadataImporter(false); var n = new DefaultNamer(); var er = new MockErrorReporter(!expectErrors); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(md, er, n.GetTypeParameterName, tr => new JsTypeReferenceExpression(tr.Resolve(compilation.Compilation).GetDefinition())); var compiler = new Compiler.Compiler(md, n, rtl, er, allowUserDefinedStructs: false); var references = includeLinq ? new[] { Common.Mscorlib, Common.Linq } : new[] { Common.Mscorlib }; compilation = compiler.CreateCompilation(new[] { sourceFile }, references, null); var compiledTypes = compiler.Compile(compilation); if (expectErrors) { Assert.That(er.AllMessages, Is.Not.Empty, "Compile should have generated errors"); return(Tuple.Create((string)null, compilation.Compilation, (IMetadataImporter)md, er)); } er.AllMessagesText.Should().BeEmpty("Compile should not generate errors"); var js = new OOPEmulator.ScriptSharpOOPEmulator(compilation.Compilation, md, rtl, n, er).Process(compiledTypes, compilation.Compilation, null); js = new DefaultLinker(md, n).Process(js, compilation.Compilation.MainAssembly); string script = string.Join("", js.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); if (Output == OutputType.GeneratedScript) { Console.WriteLine(script); } return(Tuple.Create(script, compilation.Compilation, (IMetadataImporter)md, er)); }
private string Process(IList<JsStatement> stmts, IScriptSharpMetadataImporter metadata = null, INamer namer = null, IAssembly mainAssembly = null) { var obj = new DefaultLinker(metadata ?? new MockScriptSharpMetadataImporter(), namer ?? new MockNamer()); var processed = obj.Process(stmts, mainAssembly ?? new Mock<IAssembly>().Object); return string.Join("", processed.Select(s => OutputFormatter.Format(s, allowIntermediates: false))); }
public void VisualizeAsTreeRecursion(TreeNode node, IReadOnlyTable <double> variableTable, IReadOnlyTable <FinishedFunction> functionTable, bool recursivelyVisualiseFunctions, string colorStr) { for (int i = 0; i < colorStr.Length; ++i) { if (colorStr[i] == '0') { Console.ForegroundColor = ConsoleColor.Gray; } if (colorStr[i] == '1') { Console.ForegroundColor = ConsoleColor.Red; } if (colorStr[i] == '2') { Console.ForegroundColor = ConsoleColor.Yellow; } if (colorStr[i] == '3') { Console.ForegroundColor = ConsoleColor.Magenta; } Console.Write("| "); Console.ResetColor(); } switch (node) { case NumberTreeNode lTreeNode: { Console.WriteLine(lTreeNode.Value.ToString("G7", System.Globalization.CultureInfo.InvariantCulture)); } break; case UndefinedVariableTreeNode vTreeNode: { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(vTreeNode.Name); Console.ResetColor(); } break; case FunctionParameterTreeNode fpTreeNode: { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine(String.Format("${0}", fpTreeNode.Index)); Console.ResetColor(); } break; case VariableIndexTreeNode iTreeNode: { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(String.Format("V:{0}", iTreeNode.Index)); Console.ResetColor(); } break; case FunctionIndexTreeNode fiTreeNode: { if (recursivelyVisualiseFunctions) { TreeNode clone = functionTable[fiTreeNode.Index].TopNode.Clone(); DefaultLinker linker = new DefaultLinker(); clone = linker.ReplaceParametersWithTreeNodes(clone, fiTreeNode.Parameters); VisualizeAsTreeRecursion(clone, variableTable, functionTable, recursivelyVisualiseFunctions, colorStr); } else { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine(String.Format("[F:{0}]", fiTreeNode.Index)); foreach (TreeNode child in fiTreeNode.Parameters) { VisualizeAsTreeRecursion(child, variableTable, functionTable, recursivelyVisualiseFunctions, colorStr + '3'); } Console.ResetColor(); } } break; case UndefinedFunctionTreeNode fTreeNode: { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine(String.Format("{0}()", fTreeNode.Name)); Console.ResetColor(); foreach (TreeNode child in fTreeNode.Parameters) { VisualizeAsTreeRecursion(child, variableTable, functionTable, recursivelyVisualiseFunctions, colorStr + '0'); } break; } case UnaryOperationTreeNode uTreeNode: { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(String.Format("[{0}]", uTreeNode.Operation.FunctionName)); Console.ResetColor(); VisualizeAsTreeRecursion(uTreeNode.Child, variableTable, functionTable, recursivelyVisualiseFunctions, colorStr + '1'); break; } case BinaryOperationTreeNode bTreeNode: { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(String.Format("[{0}]", bTreeNode.Operation.FunctionName)); Console.ResetColor(); VisualizeAsTreeRecursion(bTreeNode.LeftChild, variableTable, functionTable, recursivelyVisualiseFunctions, colorStr + '2'); VisualizeAsTreeRecursion(bTreeNode.RightChild, variableTable, functionTable, recursivelyVisualiseFunctions, colorStr + '2'); break; } } /* * for (int i = 0; i < level; ++i) * { * Console.Write("| "); * } * Console.WriteLine(); */ }
public bool Compile(CompilerOptions options, ErrorReporterWrapper er) { string intermediateAssemblyFile = Path.GetTempFileName(), intermediateDocFile = Path.GetTempFileName(); try { // Compile the assembly var settings = MapSettings(options, intermediateAssemblyFile, intermediateDocFile, er); if (er.HasErrors) { return(false); } if (!options.AlreadyCompiled) { // Compile the assembly var ctx = new CompilerContext(settings, new ConvertingReportPrinter(er)); var d = new Mono.CSharp.Driver(ctx); d.Compile(); if (er.HasErrors) { return(false); } } // Compile the script var md = new MetadataImporter.ScriptSharpMetadataImporter(options.MinimizeScript); var n = new DefaultNamer(); PreparedCompilation compilation = null; var rtl = new ScriptSharpRuntimeLibrary(md, er, n.GetTypeParameterName, tr => new JsTypeReferenceExpression(tr.Resolve(compilation.Compilation).GetDefinition())); var compiler = new Compiler.Compiler(md, n, rtl, er, allowUserDefinedStructs: options.References.Count == 0 /* We allow user-defined structs in mscorlib only, which can be identified by the fact that it has no references*/); var references = LoadReferences(settings.AssemblyReferences, er); if (references == null) { return(false); } compilation = compiler.CreateCompilation(options.SourceFiles.Select(f => new SimpleSourceFile(f, settings.Encoding)), references, options.DefineConstants); var compiledTypes = compiler.Compile(compilation); IMethod entryPoint = null; if (options.HasEntryPoint) { List <IMethod> candidates; if (!string.IsNullOrEmpty(options.EntryPointClass)) { var t = compilation.Compilation.MainAssembly.GetTypeDefinition(new FullTypeName(options.EntryPointClass)); if (t == null) { er.Region = DomRegion.Empty; er.Message(7950, "Could not find the entry point class " + options.EntryPointClass + "."); return(false); } candidates = t.Methods.Where(IsEntryPointCandidate).ToList(); } else { candidates = compilation.Compilation.MainAssembly.GetAllTypeDefinitions().SelectMany(t => t.Methods).Where(IsEntryPointCandidate).ToList(); } if (candidates.Count != 1) { er.Region = DomRegion.Empty; er.Message(7950, "Could not find a unique entry point."); return(false); } entryPoint = candidates[0]; } var js = new ScriptSharpOOPEmulator(compilation.Compilation, md, rtl, n, er).Process(compiledTypes, compilation.Compilation, entryPoint); js = new DefaultLinker(md, n).Process(js, compilation.Compilation.MainAssembly); if (er.HasErrors) { return(false); } string outputAssemblyPath = !string.IsNullOrEmpty(options.OutputAssemblyPath) ? options.OutputAssemblyPath : Path.ChangeExtension(options.SourceFiles[0], ".dll"); string outputScriptPath = !string.IsNullOrEmpty(options.OutputScriptPath) ? options.OutputScriptPath : Path.ChangeExtension(options.SourceFiles[0], ".js"); if (!options.AlreadyCompiled) { try { File.Copy(intermediateAssemblyFile, outputAssemblyPath, true); } catch (IOException ex) { er.Region = DomRegion.Empty; er.Message(7950, ex.Message); return(false); } if (!string.IsNullOrEmpty(options.DocumentationFile)) { try { File.Copy(intermediateDocFile, options.DocumentationFile, true); } catch (IOException ex) { er.Region = DomRegion.Empty; er.Message(7952, ex.Message); return(false); } } } if (options.MinimizeScript) { js = ((JsBlockStatement)Minifier.Process(new JsBlockStatement(js))).Statements; } string script = string.Join("", js.Select(s => options.MinimizeScript ? OutputFormatter.FormatMinified(s) : OutputFormatter.Format(s))); try { File.WriteAllText(outputScriptPath, script, settings.Encoding); } catch (IOException ex) { er.Region = DomRegion.Empty; er.Message(7951, ex.Message); return(false); } return(true); } catch (Exception ex) { er.Region = DomRegion.Empty; er.InternalError(ex.ToString()); return(false); } finally { if (!options.AlreadyCompiled) { try { File.Delete(intermediateAssemblyFile); } catch {} try { File.Delete(intermediateDocFile); } catch {} } } }