Пример #1
0
        static void Main(string[] args)
        {
            Program.Initiation();

            while (true)
            {
                Console.Write(">> ");

                StreamReader streamReader = new StreamReader(Console.OpenStandardInput());

                try
                {
                    AntlrInputStream inputStream = new AntlrInputStream(streamReader.ReadLine());
                    if (inputStream.ToString().Contains("\u001a") || inputStream.ToString().Contains("\u0004"))
                    {
                        Console.WriteLine("EOF");
                        break;
                    }

                    FA_LanguageLexer  fA_LanguageLexer  = new FA_LanguageLexer(inputStream);
                    CommonTokenStream commonTokenStream = new CommonTokenStream(fA_LanguageLexer);
                    FA_LanguageParser fA_LanguageParser = new FA_LanguageParser(commonTokenStream);
                    IParseTree        parseTree         = fA_LanguageParser.conversation();
                    FA_Visitor        fA_Visitor        = new FA_Visitor(Program.functions);

                    // Print tree and output result
                    Console.WriteLine(parseTree.ToStringTree(fA_LanguageParser));
                    Console.WriteLine(fA_Visitor.Visit(parseTree).ToString());
                }
                catch (System.NullReferenceException e)
                {
                    Console.WriteLine("error: " + e.Message);
                    continue;
                }
            }
        }
Пример #2
0
        public Function Run(List <Function> p)
        {
            Function function = new Function(this.functions);

            switch (this.FunkcnType)
            {
            case TYPE.Function:
                if (p.Count <Function>() == this.Parameters.Count <Function>())
                {
                    switch (this.Name)
                    {
                    case "+":
                        var li = from Function in p where Function.ReturnType == TYPE.List select Function;
                        var tp = from Function in p where Function.ReturnType == TYPE.Tuple select Function;
                        var st = from Function in p where Function.ReturnType == TYPE.String select Function;
                        var im = from Function in p where Function.ReturnType == TYPE.Imag select Function;
                        var re = from Function in p where Function.ReturnType == TYPE.Real select Function;
                        var it = from Function in p where Function.ReturnType == TYPE.Int select Function;
                        if (li.Count <Function>() != 0)
                        {
                        }
                        else if (tp.Count <Function>() != 0)
                        {
                        }
                        else if (st.Count <Function>() != 0)
                        {
                        }
                        else if (im.Count <Function>() != 0)
                        {
                            foreach (Function f in p)
                            {
                                switch (f.ReturnType)
                                {
                                case TYPE.Int:

                                    break;

                                case TYPE.Real:
                                    break;
                                }
                            }
                        }
                        else if (re.Count <Function>() != 0)
                        {
                            Console.WriteLine("real");
                        }
                        else
                        {
                            int a = int.Parse(p[0].Code);
                            int b = int.Parse(p[1].Code);

                            function = new Function("", TYPE.Int, TYPE.Int, (a + b).ToString(), new List <Function>(), this.functions);
                        }
                        switch (this.ReturnType)
                        {
                        case TYPE.Int:
                        {
                            int a = int.Parse(p[0].Code);
                            int b = int.Parse(p[1].Code);
                            function = new Function("", TYPE.Int, TYPE.Int, (a + b).ToString(), new List <Function>(), this.functions);
                        }
                        break;

                        case TYPE.Real:
                        {
                            double a = double.Parse(p[0].Code);
                            double b = double.Parse(p[1].Code);
                            function = new Function("", TYPE.Real, TYPE.Real, (a + b).ToString(), new List <Function>(), this.functions);
                        }
                        break;

                        case TYPE.Imag:
                        {
                            double ar = double.Parse(p[0].Parameters[0].Code);
                            double ai = double.Parse(p[0].Parameters[1].Code);
                            double br = double.Parse(p[1].Parameters[0].Code);
                            double bi = double.Parse(p[1].Parameters[1].Code);

                            List <Function> p_of_image = new List <Function>();
                            p_of_image.Add(new Function("", TYPE.Real, TYPE.Real, (ar + br).ToString(), new List <Function>(), this.functions));
                            p_of_image.Add(new Function("", TYPE.Real, TYPE.Real, (ai + bi).ToString(), new List <Function>(), this.functions));
                            function = new Function("", TYPE.Imag, TYPE.Imag, "", p_of_image, this.functions);
                        }
                        break;

                        case TYPE.Tuple:
                            break;

                        case TYPE.List:
                            break;
                        }
                        break;

                    case "-":
                        switch (this.ReturnType)
                        {
                        case TYPE.Int:
                        {
                            int a = int.Parse(p[0].Code);
                            int b = int.Parse(p[1].Code);
                            function = new Function("", TYPE.Int, TYPE.Int, (a - b).ToString(), new List <Function>(), this.functions);
                        }
                        break;

                        case TYPE.Real:
                        {
                            double a = double.Parse(p[0].Code);
                            double b = double.Parse(p[1].Code);
                            function = new Function("", TYPE.Real, TYPE.Real, (a - b).ToString(), new List <Function>(), this.functions);
                        }
                        break;

                        case TYPE.Imag:
                        {
                            double ar = double.Parse(p[0].Parameters[0].Code);
                            double ai = double.Parse(p[0].Parameters[1].Code);
                            double br = double.Parse(p[1].Parameters[0].Code);
                            double bi = double.Parse(p[1].Parameters[1].Code);

                            List <Function> p_of_image = new List <Function>();
                            p_of_image.Add(new Function("", TYPE.Real, TYPE.Real, (ar - br).ToString(), new List <Function>(), this.functions));
                            p_of_image.Add(new Function("", TYPE.Real, TYPE.Real, (ai - bi).ToString(), new List <Function>(), this.functions));
                            function = new Function("", TYPE.Imag, TYPE.Imag, "", p_of_image, this.functions);
                        }
                        break;

                        case TYPE.Tuple:
                            break;

                        case TYPE.List:
                            break;
                        }
                        break;

                    case "*":
                        switch (this.ReturnType)
                        {
                        case TYPE.Int:
                        {
                            int a = int.Parse(p[0].Code);
                            int b = int.Parse(p[1].Code);
                            function = new Function("", TYPE.Int, TYPE.Int, (a * b).ToString(), new List <Function>(), this.functions);
                        }
                        break;

                        case TYPE.Real:
                        {
                            double a = double.Parse(p[0].Code);
                            double b = double.Parse(p[1].Code);
                            function = new Function("", TYPE.Real, TYPE.Real, (a * b).ToString(), new List <Function>(), this.functions);
                        }
                        break;

                        case TYPE.Imag:
                        {
                            double ar = double.Parse(p[0].Parameters[0].Code);
                            double ai = double.Parse(p[0].Parameters[1].Code);
                            double br = double.Parse(p[1].Parameters[0].Code);
                            double bi = double.Parse(p[1].Parameters[1].Code);

                            List <Function> p_of_image = new List <Function>();
                            p_of_image.Add(new Function("", TYPE.Real, TYPE.Real, (ar * br - ai * bi).ToString(), new List <Function>(), this.functions));
                            p_of_image.Add(new Function("", TYPE.Real, TYPE.Real, (ar * bi + ai * br).ToString(), new List <Function>(), this.functions));
                            function = new Function("", TYPE.Imag, TYPE.Imag, "", p_of_image, this.functions);
                        }
                        break;
                        }
                        break;

                    case "/":
                        switch (this.ReturnType)
                        {
                        case TYPE.Int:
                        {
                            int a = int.Parse(p[0].Code);
                            int b = int.Parse(p[1].Code);
                            function = new Function("", TYPE.Int, TYPE.Int, (a / b).ToString(), new List <Function>(), this.functions);
                        }
                        break;

                        case TYPE.Real:
                        {
                            double a = double.Parse(p[0].Code);
                            double b = double.Parse(p[1].Code);
                            function = new Function("", TYPE.Real, TYPE.Real, (a / b).ToString(), new List <Function>(), this.functions);
                        }
                        break;

                        case TYPE.Imag:
                        {
                            double ar = double.Parse(p[0].Parameters[0].Code);
                            double ai = double.Parse(p[0].Parameters[1].Code);
                            double br = double.Parse(p[1].Parameters[0].Code);
                            double bi = double.Parse(p[1].Parameters[1].Code);

                            List <Function> p_of_image = new List <Function>();
                            p_of_image.Add(new Function("", TYPE.Real, TYPE.Real, ((ar * br + ai * bi) / (br * br + bi * bi)).ToString(), new List <Function>(), this.functions));
                            p_of_image.Add(new Function("", TYPE.Real, TYPE.Real, ((ai * br - ar * bi) / (br * br + bi * bi)).ToString(), new List <Function>(), this.functions));
                            function = new Function("", TYPE.Imag, TYPE.Imag, "", p_of_image, this.functions);
                        }
                        break;
                        }
                        break;

                    case "@":
                        break;

                    case "Int":
                        break;

                    case "Real":
                        break;

                    case "Imag":
                        break;

                    default:
                        string code = this.Code;

                        for (int a = 0; a < this.Parameters.Count <Function>(); a++)
                        {
                            code = code.Replace(this.Parameters[a].Name, p[a].Code);
                        }

                        // Run the code
                        AntlrInputStream  runStream = new AntlrInputStream(code);
                        FA_LanguageLexer  lexer     = new FA_LanguageLexer(runStream);
                        CommonTokenStream tokens    = new CommonTokenStream(lexer);
                        FA_LanguageParser parser    = new FA_LanguageParser(tokens);
                        IParseTree        tree      = parser.conversation();
                        //Console.WriteLine("run tree:");
                        //Console.WriteLine(tree.ToStringTree(parser));
                        FA_Visitor visitor = new FA_Visitor(this.functions);

                        function = visitor.Visit(tree);

                        break;
                    }
                }
                else
                {
                    throw new Exception("Function parameter not fit");
                }
                break;

            default:
                function = this;
                break;
            }

            return(function);
        }