private void StartTimer(IOKind op) { int streamTimeout; if (timeout == System.Threading.Timeout.Infinite) streamTimeout = System.Threading.Timeout.Infinite; else streamTimeout = timeout - (int)stopwatch.ElapsedMilliseconds; if (op == IOKind.Read) { if (ShouldResetStreamTimeout(lastReadTimeout, streamTimeout)) { baseStream.ReadTimeout = streamTimeout; lastReadTimeout = streamTimeout; } } else { if (ShouldResetStreamTimeout(lastWriteTimeout, streamTimeout)) { baseStream.WriteTimeout = streamTimeout; lastWriteTimeout = streamTimeout; } } if (timeout == System.Threading.Timeout.Infinite) return; stopwatch.Start(); }
private static SyntaxTree MakeProgram(string sourceFilePath, IOKind ioKind, string methodName, string className, string namespaceName) { ParsingResult result = Parsing.ParseFromFile(sourceFilePath); if (!result.Success) { throw new Exception("Syntax error in source file."); } else { if (!result.ParsedAll) { Console.WriteLine("Could not parse the whole source file, output might not work as intended."); } MethodDeclarationSyntax method = Exprs.Method(methodName, ioKind, result.SyntaxGenerators); if (ioKind == IOKind.Argument) { method = method.WithParameterList(SyntaxFactory.ParseParameterList("(IEnumerable<byte> input)")); } ClassDeclarationSyntax @class = Exprs.Class(className); @class = @class.WithMembers(SyntaxFactory.SingletonList <MemberDeclarationSyntax>(method)); NamespaceDeclarationSyntax @namespace = Exprs.Namespace(namespaceName); @namespace = @namespace.WithMembers(SyntaxFactory.SingletonList <MemberDeclarationSyntax>(@class)); CompilationUnitSyntax cu = SyntaxFactory.CompilationUnit().AddMembers(@namespace); return(SyntaxFactory.SyntaxTree(cu, path: sourceFilePath)); } }
private static IEnumerable <StatementSyntax> MethodInit(IOKind ioKind) { yield return(SyntaxFactory.ParseStatement($"int {pointerName} = 0;")); yield return(SyntaxFactory.ParseStatement($"byte[] {arrayName} = new byte[{arraySize}];")); if (ioKind == IOKind.Argument) { yield return(SyntaxFactory.ParseStatement($"IEnumerator<byte> {enumeratorName} = {inputName}.GetEnumerator();")); } }
internal static MethodDeclarationSyntax Method(string methodName, IOKind ioKind, IEnumerable <SyntaxGenerator> innerStatements) { TypeSyntax returnType = ioKind switch { IOKind.Argument => SyntaxFactory.ParseTypeName("IEnumerable<byte>"), IOKind.Console => SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), _ => throw new ArgumentException($"Must have valid {nameof(IOKind)}", nameof(ioKind)) }; MethodDeclarationSyntax method = SyntaxFactory.MethodDeclaration(returnType, methodName); method = method.WithBody(SyntaxFactory.Block(Enumerable.Concat(MethodInit(ioKind), innerStatements.Select(g => g(ioKind))))); method = method.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword))); return(method); }
private void StartTimer(IOKind op) { int streamTimeout; if (timeout == System.Threading.Timeout.Infinite) { streamTimeout = System.Threading.Timeout.Infinite; } else { streamTimeout = timeout - (int)stopwatch.ElapsedMilliseconds; } if (op == IOKind.Read) { if (ShouldResetStreamTimeout(lastReadTimeout, streamTimeout)) { #if !CF && !RT baseStream.ReadTimeout = streamTimeout; #endif lastReadTimeout = streamTimeout; } } else { if (ShouldResetStreamTimeout(lastWriteTimeout, streamTimeout)) { #if !CF && !RT baseStream.WriteTimeout = streamTimeout; #endif lastWriteTimeout = streamTimeout; } } if (timeout == System.Threading.Timeout.Infinite) { return; } stopwatch.Start(); }
private static void Compile(string sourceFilePath, string outDirectoryPath, bool isLib, IOKind ioKind, string methodName, string className, string namespaceName) { SyntaxTree tree = MakeProgram(sourceFilePath, ioKind, methodName, className, namespaceName); CSharpCompilationOptions options = new CSharpCompilationOptions( outputKind: isLib ? OutputKind.DynamicallyLinkedLibrary : OutputKind.ConsoleApplication, mainTypeName: isLib ? null : $"{namespaceName}.{className}", optimizationLevel: OptimizationLevel.Release); IEnumerable <MetadataReference> references = new[] { MetadataReference.CreateFromFile(NuGetPackageResolver.GetLatestsPath("System.Runtime")), MetadataReference.CreateFromFile(NuGetPackageResolver.GetLatestsPath("System.Runtime.Extensions")), MetadataReference.CreateFromFile(NuGetPackageResolver.GetLatestsPath("System.Console")) }; CSharpCompilation compilation = CSharpCompilation.Create(Path.GetFileNameWithoutExtension(sourceFilePath), new[] { tree }, references, options); EmitResult result = compilation.Emit(Path.Combine(outDirectoryPath, WithFileExtension(Path.GetFileNameWithoutExtension(sourceFilePath), isLib))); if (result.Success) { Console.WriteLine("Build successfull!"); } else { throw new Exception("Build errors! This is serious, please report it on GitHub."); } }
internal static void CompileDll(string sourceFilePath, string outDirectoryPath, IOKind ioKind, string methodName = defaultMethodName, string className = defaultClassName, string namespaceName = defaultNamespaceName) { Compile(sourceFilePath, outDirectoryPath, true, ioKind, methodName, className, namespaceName); }