예제 #1
0
파일: EUOParser.cs 프로젝트: DarkLotus/Klip
        Expr ParseExpr()
        {
            Expr  ret = null;
            Token t   = CurrentToken;

            if (NextToken.TokenName == Lexer.Tokens.LeftParan)
            {
                /* string ident = "";
                 *
                 * if (t.TokenName == Lexer.Tokens.Ident || t.TokenName == Lexer.Tokens.BuildInIdent)
                 * {
                 *   ident = t.TokenValue.ToString();
                 * }
                 *
                 * CurrentIndex++;
                 *
                 * if (NextToken.TokenName == Lexer.Tokens.RightParan)
                 * {
                 *   ret = new CallExpr(ident, new List<Expr>());
                 * }
                 * else
                 * {
                 *   //ret = new CallExpr(ident, ParseCallArgs());
                 * }*/
            }
            else if (t.TokenName == Lexer.Tokens.IntLiteral)
            {
                IntLiteral i = new IntLiteral(Convert.ToInt32(t.TokenValue.ToString()));
                ret = i;
            }
            else if (t.TokenName == Lexer.Tokens.StringLiteral)
            {
                StringLiteral s = new StringLiteral(t.TokenValue.ToString());
                ret = s;
            }
            else if (t.TokenName == Lexer.Tokens.Ident || t.TokenName == Lexer.Tokens.BuildInIdent)
            {
                string ident = t.TokenValue.ToString();

                Ident i = new Ident(ident);
                ret = i;
            }
            else if (t.TokenName == Lexer.Tokens.LeftParan)
            {
                CurrentIndex++;
                Expr e = ParseExpr();

                if (NextToken.TokenName == Lexer.Tokens.RightParan)
                {
                    CurrentIndex++;
                }

                ParanExpr p = new ParanExpr(e);

                if (NextToken.TokenName == Lexer.Tokens.Add)
                {
                    CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.add, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Sub)
                {
                    CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.sub, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Mul)
                {
                    CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.mul, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Div)
                {
                    CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.div, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Comma)
                {
                    CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.Concat, expr);
                }
                else
                {
                    ret = p;
                }
            }

            if (NextToken.TokenName == Lexer.Tokens.Add || NextToken.TokenName == Lexer.Tokens.Sub || NextToken.TokenName == Lexer.Tokens.Mul || NextToken.TokenName == Lexer.Tokens.Div || NextToken.TokenName == Lexer.Tokens.Comma)
            {
                Expr   lexpr = ret;
                Symbol op    = 0;

                if (NextToken.TokenName == Lexer.Tokens.Add)
                {
                    op = Symbol.add;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Sub)
                {
                    op = Symbol.sub;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Mul)
                {
                    op = Symbol.mul;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Div)
                {
                    op = Symbol.div;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Comma)
                {
                    op = Symbol.Concat;
                }
                CurrentIndex++;
                CurrentIndex++;
                Expr rexpr = ParseExpr();

                ret = new MathExpr(lexpr, op, rexpr);
            }

            CurrentIndex++;
            return(ret);
        }
예제 #2
0
        Expr ParseExpr()
        {
            Expr  ret = null;
            Token t   = CurrentToken;

            /*if ( NextToken.TokenName == Lexer.Tokens.LeftParan )
             * {
             *   string ident = "";
             *
             *   if (t.TokenName == Lexer.Tokens.Ident || t.TokenName == Lexer.Tokens.BuildInIdent)
             *   {
             *       ident = t.TokenValue.ToString();
             *   }
             *
             *   CurrentIndex++;
             *
             *   if (NextToken.TokenName == Lexer.Tokens.RightParan)
             *   {
             *       ret = new CallExpr(ident, new List<Expr>());
             *   }
             *   else
             *   {
             *       //ret = new CallExpr(ident, ParseCallArgs());
             *   }
             * }
             * else*/
            if (t.TokenName == Lexer.Tokens.IntLiteral)
            {
                if (t.TokenValue.Contains("s"))
                {
                    ret = new IntLiteral(Convert.ToInt32(t.TokenValue.TrimEnd('s').ToString()) * 1000);
                }
                else
                {
                    ret = new IntLiteral(Convert.ToInt32(t.TokenValue.ToString()));
                }
            }
            else if (t.TokenName == Lexer.Tokens.StringLiteral)
            {
                StringLiteral s = new StringLiteral(t.TokenValue.ToString());
                ret = s;
            }
            else if (t.TokenName == Lexer.Tokens.Ident || t.TokenName == Lexer.Tokens.BuildInIdent)
            {
                string ident = t.TokenValue.ToString();

                Ident i = new Ident(ident);
                ret = i;
            }
            else if (t.TokenName == Lexer.Tokens.LeftParan)
            {
                CurrentIndex++;
                Expr e = ParseExpr();

                if (CurrentToken.TokenName == Lexer.Tokens.RightParan)
                {
                    //CurrentIndex++;
                }

                ParanExpr p = new ParanExpr(e);

                if (NextToken.TokenName == Lexer.Tokens.Add)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.add, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Sub)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.sub, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Mul)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.mul, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Div)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.div, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Comma)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.Concat, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Period)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.Period, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.And)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.And, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.MoreOrEqual || NextToken.TokenName == Lexer.Tokens.MoreOrEqual2)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.MoreOrEqual, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.LessOrEqual || NextToken.TokenName == Lexer.Tokens.LessOrEqual2)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.LessOrEqual, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Less)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.Less, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.More)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.More, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.Equal)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.Equal, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.In)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.In, expr);
                }
                else if (NextToken.TokenName == Lexer.Tokens.NotEqual)
                {
                    CurrentIndex++; CurrentIndex++;
                    Expr expr = ParseExpr();
                    ret = new MathExpr(p, Symbol.NotEqual, expr);
                }
                else
                {
                    ret = p;
                }
            }

            if (NextToken != null && (NextToken.TokenName == Lexer.Tokens.Add || NextToken.TokenName == Lexer.Tokens.Sub || NextToken.TokenName == Lexer.Tokens.Mul || NextToken.TokenName == Lexer.Tokens.Div ||
                                      NextToken.TokenName == Lexer.Tokens.Comma || NextToken.TokenName == Lexer.Tokens.Period || NextToken.TokenName == Lexer.Tokens.In ||
                                      NextToken.TokenName == Lexer.Tokens.LessOrEqual || NextToken.TokenName == Lexer.Tokens.LessOrEqual2 || NextToken.TokenName == Lexer.Tokens.MoreOrEqual || NextToken.TokenName == Lexer.Tokens.MoreOrEqual2 ||
                                      NextToken.TokenName == Lexer.Tokens.Less || NextToken.TokenName == Lexer.Tokens.More || NextToken.TokenName == Lexer.Tokens.Equal || NextToken.TokenName == Lexer.Tokens.NotEqual))
            {
                Expr   lexpr = ret;
                Symbol op    = 0;

                if (NextToken.TokenName == Lexer.Tokens.Add)
                {
                    op = Symbol.add;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Sub)
                {
                    op = Symbol.sub;
                }
                else if (NextToken.TokenName == Lexer.Tokens.In)
                {
                    op = Symbol.In;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Mul)
                {
                    op = Symbol.mul;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Div)
                {
                    op = Symbol.div;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Comma)
                {
                    op = Symbol.Concat;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Period)
                {
                    op = Symbol.Period;
                }
                else if (NextToken.TokenName == Lexer.Tokens.MoreOrEqual || NextToken.TokenName == Lexer.Tokens.MoreOrEqual2)
                {
                    op = Symbol.MoreOrEqual;
                }
                else if (NextToken.TokenName == Lexer.Tokens.LessOrEqual || NextToken.TokenName == Lexer.Tokens.LessOrEqual2)
                {
                    op = Symbol.LessOrEqual;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Less)
                {
                    op = Symbol.Less;
                }
                else if (NextToken.TokenName == Lexer.Tokens.More)
                {
                    op = Symbol.More;
                }
                else if (NextToken.TokenName == Lexer.Tokens.Equal)
                {
                    op = Symbol.Equal;
                }
                else if (NextToken.TokenName == Lexer.Tokens.NotEqual)
                {
                    op = Symbol.NotEqual;
                }
                CurrentIndex++;
                CurrentIndex++;
                Expr rexpr = ParseExpr();

                ret = new MathExpr(lexpr, op, rexpr);
                if (CurrentToken.TokenName == Lexer.Tokens.And)
                {
                    lexpr = ret;
                    CurrentIndex++;
                    rexpr = ParseExpr();
                    ret   = new MathExpr(lexpr, Symbol.And, rexpr);
                }
                else if (CurrentToken.TokenName == Lexer.Tokens.Or)
                {
                    lexpr = ret;
                    CurrentIndex++;
                    rexpr = ParseExpr();
                    ret   = new MathExpr(lexpr, Symbol.Or, rexpr);
                }
                else if (CurrentToken.TokenName == Lexer.Tokens.Abs)
                {
                    lexpr = ret;
                    CurrentIndex++;
                    ret = new MathExpr(lexpr, Symbol.Abs, null);
                }

                return(ret);
            }


            CurrentIndex++;
            return(ret);
        }