private void CompilePrepare() { if (File.Exists(mainIniFile)) { SinumerikLexer definitionLexer = new SinumerikLexer(CharStreams.fromPath(mainIniFile), null, _errorTextWriter); SinumerikParser definitionParser = new SinumerikParser(new CommonTokenStream(definitionLexer), null, _errorTextWriter); definitionParser.BuildParseTree = true; IParseTree definitionTree = definitionParser.parse(); GlobalVarsVisitor globalVars = new GlobalVarsVisitor(mainScope); globalVars.Visit(definitionTree); } foreach (var fileName in Directory.EnumerateFiles(subDir)) { SinumerikLexer subLexer = new SinumerikLexer(CharStreams.fromPath(fileName), null, _errorTextWriter); SinumerikParser subParser = new SinumerikParser(new CommonTokenStream(subLexer), null, _errorTextWriter); subParser.BuildParseTree = true; IParseTree subTree = subParser.parse(); SymbolVisitor subSymbolVisitor = new SymbolVisitor(functions); subSymbolVisitor.Visit(subTree); } mainLexer = new SinumerikLexer(CharStreams.fromstring(_text.Text), null, _errorTextWriter); }
public StringBuilder Compile() { Scope scope = new Scope(mainScope); SinumerikParser mainParser = new SinumerikParser(new CommonTokenStream(mainLexer), null, _errorTextWriter); mainParser.BuildParseTree = true; IParseTree mainTree = mainParser.parse(); // outputTextWriter.Close(); SymbolVisitor mainSymbolVisitor = new SymbolVisitor(functions); mainSymbolVisitor.Visit(mainTree); EvalVisitor visitor = new EvalVisitor(scope, functions, _gcodeOutput); visitor.Visit(mainTree); return(_gcodeOutput); }
/// <summary> /// Visits an assembly. /// </summary> /// <typeparam name="T">The type of the returned data.</typeparam> /// <param name="symbolVisitor">The symbol visitor to use.</param> /// <param name="path">The path of the assembly to visit.</param> /// <returns>The data returned by the visitor.</returns> public static T Visit <T>(this SymbolVisitor <T> symbolVisitor, string path) { MetadataReference testedAssembly = MetadataReference.CreateFromFile(path); // TODO: test other platforms (like Mono) to see what is needed. IList <MetadataReference> platformAssemblies; object trustedPlatformAssemblies = AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES"); if (trustedPlatformAssemblies != null) { // .NET Core App need this for resolving types. // TODO: see if all assemblies are needed. // https://github.com/dotnet/roslyn/wiki/Runtime-code-generation-using-Roslyn-compilations-in-.NET-Core-App platformAssemblies = trustedPlatformAssemblies .ToString() .Split(Path.PathSeparator) //.Where(t => t.Contains("System.Runtime.dll") || t.Contains("System.Private.CoreLib.dll") || t.Contains("netstandard.dll")) .Select(x => (MetadataReference)MetadataReference.CreateFromFile(x)) .ToList(); } else { // .NET Framework MetadataReference mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); platformAssemblies = new List <MetadataReference> { mscorlib }; } platformAssemblies.Add(testedAssembly); Compilation compilation = CSharpCompilation .Create(nameof(BlyrosSymbolVisitor)) .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithMetadataImportOptions(MetadataImportOptions.All)) .WithReferences(platformAssemblies); return(symbolVisitor.Visit(compilation.GetAssemblyOrModuleSymbol(testedAssembly))); }