private CompiledMethod ParseAndCompileSingleMethod(string source) { var sourceBytes = Encoding.UTF8.GetBytes(source); var diagnostics = new BenchmarkDiagnosticsSink(); // Parse the source const string sourceFilename = "test.cle"; var syntaxTree = SyntaxParser.Parse(sourceBytes.AsMemory(), sourceFilename, diagnostics); if (syntaxTree is null || syntaxTree.Functions.Count != 1) { throw new InvalidOperationException("Expected a single method"); } // Compile the declaration var declarationProvider = new NullDeclarationProvider(); var declaration = MethodDeclarationCompiler.Compile(syntaxTree.Functions[0], syntaxTree.Namespace, sourceFilename, 0, declarationProvider, diagnostics); // Compile the method body var result = new MethodCompiler(declarationProvider, diagnostics) .CompileBody(syntaxTree.Functions[0], declaration !, syntaxTree.Namespace, sourceFilename); if (diagnostics.DiagnosticCount > 0) { throw new InvalidOperationException("Expected no diagnostics"); } return(result !); }
public void Compile_parameterless_int32_method_succeeds() { var position = new TextPosition(280, 14, 8); var syntax = MakeParameterlessMethod(Visibility.Private, "int32", position); var diagnostics = new TestingDiagnosticSink(); var declarationProvider = new TestingSingleFileDeclarationProvider(); var result = MethodDeclarationCompiler.Compile(syntax, "long::ns", "int32.cle", 8, declarationProvider, diagnostics); Assert.That(diagnostics.Diagnostics, Is.Empty); Assert.That(result, Is.InstanceOf <NativeMethodDeclaration>()); Assert.That((result as NativeMethodDeclaration) !.IsEntryPoint, Is.False); Assert.That(result !.ReturnType, Is.EqualTo(SimpleType.Int32)); Assert.That(result.Visibility, Is.EqualTo(Visibility.Private)); Assert.That(result.FullName, Is.EqualTo("long::ns::MethodName")); Assert.That(result.DefiningFilename, Is.EqualTo("int32.cle")); Assert.That(result.DefinitionPosition, Is.EqualTo(position)); Assert.That(result.BodyIndex, Is.EqualTo(8)); }
public void Compile_parameterless_bool_method_succeeds() { var position = new TextPosition(13, 3, 4); var syntax = MakeParameterlessMethod(Visibility.Public, "bool", position); var diagnostics = new TestingDiagnosticSink(); var declarationProvider = new TestingSingleFileDeclarationProvider(); var result = MethodDeclarationCompiler.Compile(syntax, "Namespace", "bool.cle", 7, declarationProvider, diagnostics); Assert.That(diagnostics.Diagnostics, Is.Empty); Assert.That(result, Is.InstanceOf <NativeMethodDeclaration>()); Assert.That((result as NativeMethodDeclaration) !.IsEntryPoint, Is.False); Assert.That(result !.ReturnType, Is.EqualTo(SimpleType.Bool)); Assert.That(result.Visibility, Is.EqualTo(Visibility.Public)); Assert.That(result.FullName, Is.EqualTo("Namespace::MethodName")); Assert.That(result.DefiningFilename, Is.EqualTo("bool.cle")); Assert.That(result.DefinitionPosition, Is.EqualTo(position)); Assert.That(result.BodyIndex, Is.EqualTo(7)); }
/// <summary> /// Parses the given source code, compiles the method declarations and /// returns the result of <see cref="MethodCompiler.CompileBody"/> on the first method. /// </summary> protected CompiledMethod?TryCompileFirstMethod(string source, out TestingDiagnosticSink diagnostics) { var sourceBytes = Encoding.UTF8.GetBytes(source); diagnostics = new TestingDiagnosticSink(); // Parse the source const string sourceFilename = "test.cle"; var syntaxTree = SyntaxParser.Parse(sourceBytes.AsMemory(), sourceFilename, diagnostics); Assert.That(syntaxTree, Is.Not.Null, "Source file was not parsed successfully."); // Parse the declarations var declarationProvider = new TestingSingleFileDeclarationProvider(); MethodDeclaration?firstDeclaration = null; foreach (var functionSyntax in syntaxTree !.Functions) { var declaration = MethodDeclarationCompiler.Compile(functionSyntax, syntaxTree.Namespace, sourceFilename, declarationProvider.Methods.Count, declarationProvider, diagnostics); Assert.That(declaration, Is.Not.Null, $"Method declaration for {functionSyntax.Name} was not compiled successfully."); if (firstDeclaration is null) { firstDeclaration = declaration; } declarationProvider.Methods.Add(functionSyntax.Name, declaration !); } Assert.That(firstDeclaration, Is.Not.Null, "No methods were declared."); // Then compile the first method return(new MethodCompiler(declarationProvider, diagnostics) .CompileBody(syntaxTree.Functions[0], firstDeclaration !, syntaxTree.Namespace, sourceFilename)); }