Пример #1
0
        static void Main(string[] args)
        {
            StreamReader istr = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding);
            StreamWriter ostr = new StreamWriter(System.Console.OpenStandardOutput(), Console.OutputEncoding);

            if (args.Count() < 2)
            {
                Help();
                return;
            }

            bool low_opt = false, hight_opt = false;
            int index_in = 2, index_out = 3;

            switch (args[1])
            {
                case "-low":
                    low_opt = true;
                    break;
                case "-hight":
                    hight_opt = true;
                    break;
                case "-low-hight":
                    low_opt = hight_opt = true;
                    break;
                default:
                    index_in = 1;
                    index_out = 2;
                    break;
            }

            istr = new StreamReader(args[index_in]);
            if (index_out < args.Count())
            {
                ostr = new StreamWriter(args[index_out]);
            }

            Scaner scaner = null;
            Parser parser = null;
            Fasm.CodeGen codegen = null;
            switch (args[0])
            {
                case "-l":
                    scaner = new Scaner(istr);
                    Token t = null;
                    while (t == null || t.type != Token.Type.EOF)
                    {
                        try
                        {
                            t = scaner.Read();
                            Console.WriteLine(t.ToString());
                        }
                        catch (Scaner.Exception e)
                        {
                            ostr.WriteLine(e.Message);
                        }
                    }
                    break;
                case "-p":
                    parser = new Parser(new Scaner(istr));
                    parser.Parse();
                    parser.PrintTree(ostr);
                    ostr.WriteLine(parser.logger.ToString());
                    break;
                case "-c":
                    parser = new Parser(new Scaner(istr));
                    parser.Parse();
                    if (!parser.logger.isEmpty())
                    {
                        ostr.WriteLine(parser.logger.ToString());
                        break;
                    }
                    codegen = new Fasm.CodeGen(parser.tstack);
                    codegen.Generate(ostr);
                    break;
                case "-cexe":
                    parser = new Parser(new Scaner(istr));
                    parser.Parse();
                    if (!parser.logger.isEmpty())
                    {
                        ostr.WriteLine(parser.logger.ToString());
                        break;
                    }
                    codegen = new Fasm.CodeGen(parser.tstack);
                    codegen.Generate(ostr);
                    ostr.Flush();
                    ostr.Close();
                    if (index_out < args.Count())
                    {
                        Process.Start(new ProcessStartInfo
                        {
                            FileName = "C:/fasm/fasm.exe",
                            WindowStyle = ProcessWindowStyle.Hidden,
                            Arguments = string.Format("{0} {1}", args[index_out], args[index_out])
                        });
                    }
                    break;
                default:
                    Help();
                    return;
            }

            istr.Close();
            ostr.Close();
        }
Пример #2
0
        public static bool Compile(string[] args)
        {
            StreamReader istr = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding);
            StreamWriter ostr = new StreamWriter(System.Console.OpenStandardOutput(), Console.OutputEncoding);

            if (args.Count() < 2)
            {
                return(false);
            }


            int index_in = 1, index_out = 2;

            istr = new StreamReader(args[index_in]);
            if (index_out < args.Count())
            {
                ostr = new StreamWriter(args[index_out]);
            }

            Scaner scaner = null;
            Parser parser = null;

            Fasm.CodeGen codegen = null;
            switch (args[0])
            {
            case "-l":
                scaner = new Scaner(istr);
                Token t = null;
                while (t == null || t.type != Token.Type.EOF)
                {
                    try
                    {
                        t = scaner.Read();
                        Console.WriteLine(t.ToString());
                    }
                    catch (Scaner.Exception e)
                    {
                        ostr.WriteLine(e.Message);
                    }
                }
                break;

            case "-p":
                parser = new Parser(new Scaner(istr));
                parser.Parse();
                parser.PrintTree(ostr);
                ostr.WriteLine(parser.logger.ToString());
                break;

            case "-c":
                parser = new Parser(new Scaner(istr));
                parser.Parse();
                if (!parser.logger.isEmpty())
                {
                    ostr.WriteLine(parser.logger.ToString());
                    break;
                }
                codegen = new Fasm.CodeGen(parser.tstack);
                codegen.Generate(ostr);
                break;

            case "-cexe":
                parser = new Parser(new Scaner(istr));
                parser.Parse();
                if (!parser.logger.isEmpty())
                {
                    ostr.WriteLine(parser.logger.ToString());
                    break;
                }
                codegen = new Fasm.CodeGen(parser.tstack);
                codegen.Generate(ostr);
                ostr.Flush();
                ostr.Close();
                if (index_out < args.Count())
                {
                    Process.Start(new ProcessStartInfo
                    {
                        FileName    = "C:/fasm/fasm.exe",
                        WindowStyle = ProcessWindowStyle.Hidden,
                        Arguments   = string.Format("{0} {1}", args[index_out], args[index_out])
                    });
                }
                break;

            default:
                return(false);
            }

            istr.Close();
            ostr.Close();
            return(true);
        }