Ejemplo n.º 1
0
        public static int  Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            var commandLineParser = new CommandLineParser.CommandLineParser();

            var sourcePath = new FileArgument('i', "input", "Source file path")
            {
                Optional = false
            };
            var outPath = new FileArgument('o', "output", "Output file path")
            {
                Optional = true, FileMustExist = false
            };

            var lexicalAnalysis = new SwitchArgument('l', "lexical", false);
            var syntaxAnalysis  = new SwitchArgument('s', "syntax", false);
            var semanticsCheck  = new SwitchArgument('c', "semantics", "turn off semantics check", true);
            var codeGeneration  = new SwitchArgument('a', "assembler", "generate assembler", false);
            var optimization    = new SwitchArgument('O', "optimization", "optimization", false);

            commandLineParser.Arguments.Add(sourcePath);
            commandLineParser.Arguments.Add(outPath);
            commandLineParser.Arguments.Add(lexicalAnalysis);
            commandLineParser.Arguments.Add(syntaxAnalysis);
            commandLineParser.Arguments.Add(semanticsCheck);
            commandLineParser.Arguments.Add(codeGeneration);
            commandLineParser.Arguments.Add(optimization);

            var compileStageGroupCertification = new ArgumentGroupCertification("l,s,a", EArgumentGroupCondition.ExactlyOneUsed);

            commandLineParser.Certifications.Add(compileStageGroupCertification);

            try {
                commandLineParser.ParseCommandLine(args);
            }
            catch (CommandLineException) {
                commandLineParser.ShowUsage();
                return(1);
            }
            using (var output = outPath.Value == null ? Console.Out : new StreamWriter(outPath.StringValue))
                using (var input = new StreamReader(sourcePath.OpenFileRead())) {
                    if (lexicalAnalysis.Value)
                    {
                        PerformLexicalAnalysis(input, output);
                    }

                    if (syntaxAnalysis.Value)
                    {
                        PerformSyntaxAnalysis(input, output, semanticsCheck.Value);
                    }

                    if (codeGeneration.Value)
                    {
                        PerformCodeGeneration(input, output, optimization.Value);
                    }
                }

            return(0);
        }
        public void GroupCertifications_All1()
        {
            ArgumentGroupCertification au = new ArgumentGroupCertification("j,k", EArgumentGroupCondition.AllUsed);

            var commandLineParser = InitGroupCertifications();

            commandLineParser.Certifications.Clear();
            commandLineParser.Certifications.Add(au);


            string[] args = new[] { "-j", "-k" };
            commandLineParser.ParseCommandLine(args);
        }
        public void GroupCertifications_OneOrNone2()
        {
            // exactly one of the arguments x, o, c must be used
            ArgumentGroupCertification oon = new ArgumentGroupCertification("f,u", EArgumentGroupCondition.OneOreNoneUsed);

            var commandLineParser = InitGroupCertifications();

            commandLineParser.Certifications.Clear();
            commandLineParser.Certifications.Add(oon);

            string[] args = new[] { "-x", "file" };
            commandLineParser.ParseCommandLine(args);
        }
        public void GroupCertifications_All3()
        {
            ArgumentGroupCertification au = new ArgumentGroupCertification("j,k", EArgumentGroupCondition.AllUsed);

            var commandLineParser = InitGroupCertifications();

            commandLineParser.Certifications.Clear();
            commandLineParser.Certifications.Add(au);

            string[] args = new[] { "-x", "file" };
            var      ex   = Assert.Throws <ArgumentConflictException>(() => commandLineParser.ParseCommandLine(args));

            Assert.StartsWith("All of these", ex.Message);
        }
        public void GroupCertifications_OneOrNone1()
        {
            ArgumentGroupCertification oon = new ArgumentGroupCertification("f,u", EArgumentGroupCondition.OneOreNoneUsed);
            var commandLineParser          = InitGroupCertifications();

            commandLineParser.Certifications.Clear();
            commandLineParser.Certifications.Add(oon);

            string[] args = new[] { "-f", "file" };
            commandLineParser.ParseCommandLine(args);

            args = new[] { "-u", "file" };
            commandLineParser.ParseCommandLine(args);
        }
        public void GroupCertifications_AtLeastOne2()
        {
            ArgumentGroupCertification oon = new ArgumentGroupCertification("f,u", EArgumentGroupCondition.AtLeastOneUsed);

            var commandLineParser = InitGroupCertifications();

            commandLineParser.Certifications.Clear();
            commandLineParser.Certifications.Add(oon);

            string[] args = new[] { "-x", "file" };

            var ex = Assert.Throws <ArgumentConflictException>(() => commandLineParser.ParseCommandLine(args));

            Assert.StartsWith("At least one of these", ex.Message);
        }
        public void GroupCertifications_ExactlyOneUsed3()
        {
            // exactly one of the arguments x, o, c must be used
            ArgumentGroupCertification eou = new ArgumentGroupCertification("x,o,c", EArgumentGroupCondition.ExactlyOneUsed);

            var commandLineParser = InitGroupCertifications();

            commandLineParser.Certifications.Clear();
            commandLineParser.Certifications.Add(eou);

            string[] args = new[] { "-j" };

            var ex = Assert.Throws <ArgumentConflictException>(() => commandLineParser.ParseCommandLine(args));

            Assert.Contains("One (and only one) of these arguments", ex.Message);
        }
        public void GroupCertifications_OneOrNone3()
        {
            // exactly one of the arguments x, o, c must be used
            ArgumentGroupCertification oon = new ArgumentGroupCertification("f,u", EArgumentGroupCondition.OneOreNoneUsed);

            var commandLineParser = InitGroupCertifications();

            commandLineParser.Certifications.Clear();
            commandLineParser.Certifications.Add(oon);

            string[] args = new[] { "-f", "file", "-u", "file2" };

            var ex = Assert.Throws <ArgumentConflictException>(() => commandLineParser.ParseCommandLine(args));

            Assert.StartsWith("These arguments can not be used together", ex.Message);
        }
        public void GroupCertifications_ExactlyOneUsed1()
        {
            // exactly one of the arguments x, o, c must be used
            ArgumentGroupCertification eou = new ArgumentGroupCertification("x,o,c", EArgumentGroupCondition.ExactlyOneUsed);

            var commandLineParser = InitGroupCertifications();

            commandLineParser.Certifications.Clear();
            commandLineParser.Certifications.Add(eou);

            string[] args = new[] { "-x", "file" };
            commandLineParser.ParseCommandLine(args);

            args = new[] { "-o", "file" };
            commandLineParser.ParseCommandLine(args);

            args = new[] { "-c", "file" };
            commandLineParser.ParseCommandLine(args);
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            var commandLineParser = new CommandLineParser.CommandLineParser();

            var compilerPath = new FileArgument('c', "compiler", "Path to compiler")
            {
                Optional = false
            };

            var testsDirectory = new DirectoryArgument('t', "test-dir", "Path to tests directory")
            {
                Optional = false
            };

            var testLexer     = new SwitchArgument('l', "lexer", "Test lexer", false);
            var testParser    = new SwitchArgument('p', "parser", "Test parser", false);
            var testSemantics = new SwitchArgument('s', "semantics", "Test parser with semantics", false);
            var testCodeGen   = new SwitchArgument('g', "codegen", "Test code generation", false);
            var testAll       = new SwitchArgument('a', "all", "Launch all tests", false);

            commandLineParser.Arguments.Add(compilerPath);
            commandLineParser.Arguments.Add(testsDirectory);

            commandLineParser.Arguments.Add(testLexer);
            commandLineParser.Arguments.Add(testParser);
            commandLineParser.Arguments.Add(testSemantics);
            commandLineParser.Arguments.Add(testCodeGen);
            commandLineParser.Arguments.Add(testAll);

            var testGroupCertification = new ArgumentGroupCertification("a,p,l,s,g", EArgumentGroupCondition.AtLeastOneUsed);

            commandLineParser.Certifications.Add(testGroupCertification);

            try {
                commandLineParser.ParseCommandLine(args);
            }
            catch (CommandLineException) {
                commandLineParser.ShowUsageHeader = "dotnet Tester.dll [pathToCompiler] [pathToTestsRoot]";
                commandLineParser.ShowUsage();
            }


            if (testLexer.Value || testAll.Value)
            {
                TestLexer(compilerPath.Value.FullName, testsDirectory.Value.FullName);
            }

            if (testParser.Value || testAll.Value)
            {
                TestParser(compilerPath.Value.FullName, testsDirectory.Value.FullName, "parser");
            }

            if (testSemantics.Value || testAll.Value)
            {
                TestParser(compilerPath.Value.FullName, testsDirectory.Value.FullName, "semantics", true);
            }

            if (testCodeGen.Value || testAll.Value)
            {
                string nasmPath;
                string gccPath;

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    //todo: remove hardcode?
                    nasmPath = @"C:\Program Files\NASM\nasm.exe";
                    gccPath  = @"C:\Users\Alexey\dev\toolchains\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\gcc.exe";
                }
                else
                {
                    nasmPath = "nasm";
                    gccPath  = "gcc";
                }
                TestCodeGen(compilerPath.Value.FullName, nasmPath, gccPath, testsDirectory.Value.FullName, "codegen");
            }
        }