コード例 #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);
        }