Beispiel #1
0
        public static TranslationProcessResult Translate(string filename)
        {
            TranslationProcess process = TranslationProcess.Create(new List <string> {
                filename
            }, new List <string>(), new List <string>(), new TranslationConfiguration());
            var result = process.Execute();

            return(result);
        }
Beispiel #2
0
        public void TransOnly(string test)
        {
            string dir          = AppDomain.CurrentDomain.BaseDirectory;
            string fullFilename = System.IO.Path.Combine(dir, test);

            var translation = TranslationProcess.Create(new List <string>()
            {
                fullFilename
            }, new List <string>(), new List <string>(), new TranslationConfiguration());
            var result = translation.Execute();

            Assert.True(result.WasTranslationSuccessful, string.Join("\n", result.Errors));
        }
Beispiel #3
0
        public void Compare(string test)
        {
            string dir            = AppDomain.CurrentDomain.BaseDirectory;
            string fullFilename   = System.IO.Path.Combine(dir, test);
            string fullSilvername = System.IO.Path.Combine(dir,
                                                           System.IO.Path.GetDirectoryName(test),
                                                           System.IO.Path.GetFileNameWithoutExtension(test)
                                                           + ".vpr");

            var translation = TranslationProcess.Create(new List <string>()
            {
                fullFilename
            }, new List <string>(), new List <string>(), new TranslationConfiguration());
            var result = translation.Execute();

            Assert.True(result.WasTranslationSuccessful, string.Join("\n", result.Errors));

            string silverResult   = Reduce(result.Silvernode.ToString());
            string silverExpected = Reduce(System.IO.File.ReadAllText(fullSilvername));

            Assert.Equal(silverExpected, silverResult);
        }
Beispiel #4
0
        public void Sys(string test)
        {
            if (test.Contains("Simple"))
            {
            }
            string dir          = AppDomain.CurrentDomain.BaseDirectory;
            string fullFilename = System.IO.Path.Combine(dir, test);
            string csharpCode   = System.IO.File.ReadAllText(fullFilename);

            var translation = TranslationProcess.Create(
                new List <string> {
                fullFilename
            },
                new List <string>(),
                new List <string>(),
                new TranslationConfiguration()
                );

            var result = translation.Execute();

            var      lines = csharpCode.Split('\n');
            var      expectedErrorcodes = new List <Tuple <string, int> >();
            bool     syntaxOnly         = false;
            int      lineNumber         = 0;
            IBackend backend            = new CarbonNailgunBackend();

            foreach (string line in lines)
            {
                lineNumber++;
                string trimmed = line.Trim().ToLower();
                if (trimmed.StartsWith("//"))
                {
                    string[] words = trimmed.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (words.Length < 2)
                    {
                        continue;
                    }
                    if (words[1] == "use")
                    {
                        if (words.Length >= 3)
                        {
                            if (words[2] == "silicon")
                            {
                                backend = new SiliconNailgunBackend();
                            }
                            else if (words[2] == "carbon")
                            {
                                backend = new CarbonNailgunBackend();
                            }
                        }
                    }
                    if (words[1] == "syntax")
                    {
                        syntaxOnly = true;
                    }
                    if (words[1] == "expect" && words.Length >= 3)
                    {
                        string code        = words[2];
                        int    errorNumber = -1;
                        if (words.Length == 5)
                        {
                            if (words[3] == "at")
                            {
                                if (words[4] == "next")
                                {
                                    errorNumber = lineNumber + 1;
                                }
                            }
                        }
                        expectedErrorcodes.Add(new Tuple <string, int>(code, errorNumber));
                    }
                }
            }

            var errors = result.Errors;

            if (!syntaxOnly && result.Errors.Count == 0)
            {
                var verificationResult = backend.Verify(result.Silvernode);
                errors = verificationResult.Errors;
            }
            bool someKindOfFailureIsExpected = expectedErrorcodes.Any();

            Assert.True(expectedErrorcodes.Count == errors.Count, "Expected error count: " + expectedErrorcodes.Count + "\nActual errors: " + errors.Count + "\n" + string.Join("\n", errors));
            foreach (var error in errors)
            {
                var appropriateTuple =
                    expectedErrorcodes.Find(tuple => tuple.Item1 == error.Diagnostic.ErrorCode.ToLower() &&
                                            (tuple.Item2 == -1 || tuple.Item2 == error.CsharpLine));
                if (appropriateTuple != null)
                {
                    expectedErrorcodes.Remove(appropriateTuple);
                }
                else
                {
                    Assert.True(false, "Actual:\n" + string.Join("\n", errors) + "Expected:\n" + string.Join("\n", expectedErrorcodes));
                }
            }

            Assert.True(result.WasTranslationSuccessful || someKindOfFailureIsExpected, string.Join("\n", result.Errors) + "\nExpected:\n" + string.Join("\n", expectedErrorcodes));
        }
Beispiel #5
0
        private static ExitCode RunVerification(
            List <string> verifiedFiles,
            List <string> assumedFiles,
            List <string> references)
        {
            TranslationProcess process;

            try
            {
                process = TranslationProcess.Create(
                    verifiedFiles,
                    assumedFiles,
                    references,
                    new TranslationConfiguration()
                {
                    Verbose             = verbose,
                    VerifyUnmarkedItems = !onlyAnnotated
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("The translation process could not be created.");
                Console.WriteLine("Error: " + ex.Message);
                return(ExitCode.InputError);
            }

            TranslationProcessResult result = process.Execute();

            if (!quiet)
            {
                Console.WriteLine(
                    result.WasTranslationSuccessful ? "Successfully translated." : $"Translation failed with {result.Errors.Count} errors."
                    );
                Console.WriteLine();
                Console.WriteLine("Resultant Silver code: ");
                Console.WriteLine("=======================");
            }
            string silvercode = result.Silvernode.ToString();

            if (lineNumbers)
            {
                int lineId = 1;
                foreach (var line in silvercode.Split('\n'))
                {
                    Console.WriteLine(lineId + ": " + line);
                    lineId++;
                }
            }
            else
            {
                Console.WriteLine(silvercode);
            }
            if (result.Errors.Count > 0)
            {
                if (!quiet)
                {
                    Console.WriteLine("=======================");
                    Console.WriteLine($"Errors: {result.Errors.Count}.");
                }
                foreach (Error error in result.Errors)
                {
                    Console.WriteLine(error.ToString());
                    if (verbose)
                    {
                        if (!String.IsNullOrEmpty(error.Diagnostic.Details))
                        {
                            Console.WriteLine("Details: " + error.Diagnostic.Details);
                        }
                        if (error.Node != null)
                        {
                            Console.WriteLine("Node source: " + error.Node.GetText().ToString().Trim());
                        }
                        Console.WriteLine();
                    }
                }
                if (!quiet)
                {
                    Console.WriteLine("=======================");
                }
            }
            // Write output to file
            if (outputSilverFile != null)
            {
                try
                {
                    System.IO.File.WriteAllText(outputSilverFile, silvercode);
                    if (!quiet)
                    {
                        Console.WriteLine($"Silver code written to {outputSilverFile}.");
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine(
                        $"Error - Silver code could not be written to {outputSilverFile}. Check that you have write permissions to that location and that the directory leading up to it exists.");
                    return(ExitCode.VerificationError);
                }
            }
            // Run verifier
            if (useSilicon || useCarbon)
            {
                if (!quiet)
                {
                    Console.WriteLine("=======================");
                }

                if (result.WasTranslationSuccessful)
                {
                    IBackend backend;
                    if (useSilicon)
                    {
                        backend = new SiliconNailgunBackend();            // new SiliconBackend();
                    }
                    else
                    {
                        backend = new CarbonNailgunBackend();  // new CarbonBackend();
                    }
                    var verificationResult = backend.Verify(result.Silvernode);
                    if (verbose)
                    {
                        Console.WriteLine(verificationResult.OriginalOutput);
                        Console.WriteLine("=======================");
                    }
                    if (verificationResult.VerificationSuccessful)
                    {
                        Console.WriteLine("Verification successful.");
                    }
                    if (verificationResult.Errors.Count > 0)
                    {
                        if (!quiet)
                        {
                            Console.WriteLine($"Errors: {verificationResult.Errors.Count}.");
                        }
                        foreach (Error error in verificationResult.Errors)
                        {
                            Console.WriteLine(error.ToString());
                            if (verbose)
                            {
                                if (!String.IsNullOrEmpty(error.Diagnostic.Details))
                                {
                                    Console.WriteLine("Details: " + error.Diagnostic.Details);
                                }
                                Console.WriteLine("Node source: " + error.Node.GetText().ToString().Trim());
                                Console.WriteLine();
                            }
                        }
                    }
                    if (verificationResult.VerificationSuccessful)
                    {
                        return(ExitCode.Success);
                    }
                    else
                    {
                        return(ExitCode.VerificationError);
                    }
                }
                else
                {
                    Console.WriteLine("The translation was not successful so a backend verifier will not be run.");
                    return(ExitCode.VerificationError);
                }
            }
            else if (result.WasTranslationSuccessful)
            {
                return(ExitCode.Success);
            }
            else
            {
                return(ExitCode.VerificationError);
            }
        }