예제 #1
0
        //式の解析
        public static ExprAST CreateSikiAST(TokenStream tokenst)
        {
            tokenst.SetCheckPoint();
            ExprAST exprAST = CreateKouAST(tokenst);

            if (exprAST == null)
            {
                tokenst.Rollback();
                return(null);
            }
            while (tokenst.NowIndex < tokenst.Size)
            {
                string opstr = tokenst.Get().Str;
                if (opstr == "+" || opstr == "-")
                {
                    tokenst.Next();
                    ExprAST baseAST2 = CreateKouAST(tokenst);
                    if (baseAST2 == null)
                    {
                        tokenst.Rollback();
                        return(null);
                    }
                    if (opstr == "+")
                    {
                        exprAST = new BinaryExprAST(BinaryExprAST.Op.Add, exprAST, baseAST2);
                    }
                    else
                    {
                        exprAST = new BinaryExprAST(BinaryExprAST.Op.Sub, exprAST, baseAST2);
                    }
                }
                else
                {
                    break;
                }
            }
            return(exprAST);
        }
예제 #2
0
        //項の解析
        static ExprAST CreateKouAST(TokenStream tokenst)
        {
            ExprAST exprAST;
            bool    minusFlag = false;

            //実数
            if (tokenst.Get().Str == "-")
            {
                minusFlag = true;
                tokenst.Next();
            }
            if (tokenst.Get().TokenType == TokenType.Double)
            {
                exprAST = new DoubleAST(tokenst.Get().GetDouble());
                tokenst.Next();
            }
            //関数
            else
            {
                exprAST = CreateFuncCallAST(tokenst);

                if (exprAST == null)
                {
                    //変数
                    if (tokenst.Get().TokenType == TokenType.Identifier && variableTable.Find(tokenst.Get().Str) != null)
                    {
                        exprAST = new DoubleAST(variableTable.Find(tokenst.Get().Str).Value);
                        tokenst.Next();
                    }
                    else
                    {
                        exprAST = CreateLambdaCallAST(tokenst);
                        if (exprAST == null)
                        {
                            //(式)
                            if (tokenst.Get().TokenType == TokenType.LeftKakko)
                            {
                                tokenst.Next();
                                exprAST = CreateSikiAST(tokenst);
                                if (exprAST == null)
                                {
                                    return(null);
                                }
                                if (tokenst.Get().TokenType != TokenType.RightKakko)
                                {
                                    return(null);
                                }
                                tokenst.Next();
                            }
                            else
                            {
                                return(null);
                            }
                        }
                    }
                }
            }
            if (tokenst.NowIndex >= tokenst.Size)
            {
                return(minusFlag ?exprAST = new MinusAST(exprAST) : exprAST);
            }
            string opstr = tokenst.Get().Str;

            if (opstr == "*" || opstr == "/")
            {
                ExprAST baseAST2;
                tokenst.Next();
                baseAST2 = CreateKouAST(tokenst);
                if (baseAST2 == null)
                {
                    return(null);
                }
                if (opstr == "*")
                {
                    exprAST = new BinaryExprAST(BinaryExprAST.Op.Mul, exprAST, baseAST2);
                }
                else if (opstr == "/")
                {
                    exprAST = new BinaryExprAST(BinaryExprAST.Op.Div, exprAST, baseAST2);
                }
                return(minusFlag ? exprAST = new MinusAST(exprAST) : exprAST);
            }
            return(minusFlag ? exprAST = new MinusAST(exprAST) : exprAST);
        }