Esempio n. 1
0
        public static int Main(string[] args)
        {
            CiParser      parser     = new CiParser();
            List <string> inputFiles = new List <string>();
            List <string> searchDirs = new List <string>();
            string        lang       = null;
            string        outputFile = null;
            string        namespace_ = null;

            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                if (arg[0] == '-')
                {
                    switch (arg)
                    {
                    case "--help":
                        Usage();
                        return(0);

                    case "--version":
                        Console.WriteLine("cito 1.0.0");
                        return(0);

                    case "-l":
                        lang = args[++i];
                        break;

                    case "-o":
                        outputFile = args[++i];
                        break;

                    case "-n":
                        namespace_ = args[++i];
                        break;

                    case "-D":
                        string symbol = args[++i];
                        if (symbol == "true" || symbol == "false")
                        {
                            throw new ArgumentException(symbol + " is reserved");
                        }
                        parser.PreSymbols.Add(symbol);
                        break;

                    case "-I":
                        searchDirs.Add(args[++i]);
                        break;

                    default:
                        throw new ArgumentException("Unknown option: " + arg);
                    }
                }
                else
                {
                    inputFiles.Add(arg);
                }
            }
            if (lang == null && outputFile != null)
            {
                string ext = Path.GetExtension(outputFile);
                if (ext.Length >= 2)
                {
                    lang = ext.Substring(1);
                }
            }
            if (lang == null || outputFile == null || inputFiles.Count == 0)
            {
                Usage();
                return(1);
            }
            GenBase gen;

            switch (lang)
            {
            case "c": gen = new GenC(); break;

            case "cpp": gen = new GenCpp(); break;

            case "cs": gen = new GenCs(); break;

            case "java": gen = new GenJava(); break;

            case "js": gen = new GenJs(); break;

            default: throw new ArgumentException("Unknown language: " + lang);
            }
            gen.Namespace  = namespace_;
            gen.OutputFile = outputFile;

            CiProgram program;

            try {
                foreach (string inputFile in inputFiles)
                {
                    parser.Parse(inputFile, File.OpenText(inputFile));
                }
                program = parser.Program;
                new CiResolver(program, searchDirs);
            } catch (CiException ex) {
                Console.Error.WriteLine("{0}({1}): ERROR: {2}", ex.Filename, ex.Line, ex.Message);
                return(1);
//			throw;
            }

            gen.Write(program);
            return(0);
        }
Esempio n. 2
0
File: CiTo.cs Progetto: pfusik/cito
        public static int Main(string[] args)
        {
            CiParser      parser          = new CiParser();
            List <string> inputFiles      = new List <string>();
            List <string> referencedFiles = new List <string>();
            List <string> searchDirs      = new List <string>();
            string        lang            = null;
            string        outputFile      = null;
            string        namespace_      = null;

            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                if (arg[0] == '-')
                {
                    switch (arg)
                    {
                    case "--help":
                        Usage();
                        return(0);

                    case "--version":
                        Console.WriteLine("cito 1.0.0");
                        return(0);

                    case "-l":
                        lang = args[++i];
                        break;

                    case "-o":
                        outputFile = args[++i];
                        break;

                    case "-n":
                        namespace_ = args[++i];
                        break;

                    case "-D":
                        string symbol = args[++i];
                        if (symbol == "true" || symbol == "false")
                        {
                            throw new ArgumentException(symbol + " is reserved");
                        }
                        parser.PreSymbols.Add(symbol);
                        break;

                    case "-r":
                        referencedFiles.Add(args[++i]);
                        break;

                    case "-I":
                        searchDirs.Add(args[++i]);
                        break;

                    default:
                        throw new ArgumentException("Unknown option: " + arg);
                    }
                }
                else
                {
                    inputFiles.Add(arg);
                }
            }
            if (lang == null && outputFile != null)
            {
                if (outputFile.EndsWith(".d.ts"))
                {
                    lang = "d.ts";
                }
                else
                {
                    string ext = Path.GetExtension(outputFile);
                    if (ext.Length >= 2)             // have an extension?
                    {
                        lang = ext.Substring(1);     // skip the dot
                    }
                }
            }
            if (lang == null || outputFile == null || inputFiles.Count == 0)
            {
                Usage();
                return(1);
            }
            GenBase gen;

            switch (lang)
            {
            case "c": gen = new GenC(); break;

            case "cpp": gen = new GenCpp(); break;

            case "cs": gen = new GenCs(); break;

            case "java": gen = new GenJava(); break;

            case "js": gen = new GenJs(); break;

            case "py": gen = new GenPy(); break;

            case "swift": gen = new GenSwift(); break;

            case "ts": gen = new GenTs().WithGenFullCode(); break;

            case "d.ts": gen = new GenTs(); break;

            case "cl": gen = new GenCl(); break;

            default: throw new ArgumentException("Unknown language: " + lang);
            }
            gen.Namespace  = namespace_;
            gen.OutputFile = outputFile;

            CiProgram program;

            try {
                CiScope parent = CiSystem.Value;
                if (referencedFiles.Count > 0)
                {
                    parent = ParseAndResolve(parser, parent, referencedFiles, searchDirs, lang);
                }
                program = ParseAndResolve(parser, parent, inputFiles, searchDirs, lang);
            } catch (CiException ex) {
                Console.Error.WriteLine("{0}({1}): ERROR: {2}", ex.Filename, ex.Line, ex.Message);
                return(1);
//			throw;
            }

            gen.Write(program);
            return(0);
        }