Exemplo n.º 1
0
        public static Expression Parse(ref string[] program)
        {
            Expression ret;

            string    token1 = ParseUtils.PeekToken(program);
            TokenType type1  = WooScript.GetTokenType(token1);

            if (token1.Equals("(", StringComparison.Ordinal))
            {
                ret = new Brackets();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.floatNum)
            {
                ret = new FloatNumber();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.floatVar ||
                     type1 == TokenType.vecVar)
            {
                ret = new Variable();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.vecFunction)
            {
                ret = new VecFunctionExpr();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.floatFunction)
            {
                ret = new FloatFunctionExpr();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.Op)
            {
                //deal with -8 type numbers
                ret = new FloatNumber();
                ret.Parse(ref program);
            }
            else
            {
                throw new ParseException("Unrecognised expression \"" + token1 + "\"");
            }

/*            if (type1 == TokenType.floatFunction)
 *          {
 *              ret = new FloatFunction();
 *              ret.Parse(ref program);
 *          }
 */
            string    token2 = ParseUtils.PeekToken(program);
            TokenType type2  = WooScript.GetTokenType(token2);

            if (type2 == TokenType.Op)
            {
                FloatOperation flop = new FloatOperation();
                flop.Parse(ref program);
                flop._Argument1 = ret;
                Expression arg2 = ExpressionBuilder.Parse(ref program);
                flop._Argument2 = arg2;

                // operator precedence check
                if (arg2 is FloatOperation)
                {
                    if ((arg2 as FloatOperation).GetPrecedence() < flop.GetPrecedence())
                    {
                        // shuffle args
                        flop._Argument2 = (arg2 as FloatOperation)._Argument1;
                        (arg2 as FloatOperation)._Argument1 = flop;
                        flop = (arg2 as FloatOperation);
                    }
                }

                if (flop._Argument1.GetExpressionType() != flop._Argument2.GetExpressionType())
                {
                    throw new ParseException("Mismatch argument types on operation");
                }

                ret = flop;
            }

            return(ret);
        }
Exemplo n.º 2
0
        public static Expression Parse(ref string[] program)
        {
            Expression ret;

            string token1 = ParseUtils.PeekToken(program);
            TokenType type1 = WooScript.GetTokenType(token1);
            if (token1.Equals("(", StringComparison.Ordinal))
            {
                ret = new Brackets();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.floatNum)
            {
                ret = new FloatNumber();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.floatVar
                || type1 == TokenType.vecVar)
            {
                ret = new Variable();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.vecFunction)
            {
                ret = new VecFunctionExpr();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.floatFunction)
            {
                ret = new FloatFunctionExpr();
                ret.Parse(ref program);
            }
            else if (type1 == TokenType.Op)
            {
                //deal with -8 type numbers
                ret = new FloatNumber();
                ret.Parse(ref program);
            }
            else
                throw new ParseException("Unrecognised expression \"" + token1 + "\"");
/*            if (type1 == TokenType.floatFunction)
            {
                ret = new FloatFunction();
                ret.Parse(ref program);
            }
*/
            string token2 = ParseUtils.PeekToken(program);
            TokenType type2 = WooScript.GetTokenType(token2);
            if (type2 == TokenType.Op)
            {
                FloatOperation flop = new FloatOperation();
                flop.Parse(ref program);
                flop._Argument1 = ret;
                Expression arg2 = ExpressionBuilder.Parse(ref program);
                flop._Argument2 = arg2;

                // operator precedence check
                if (arg2 is FloatOperation)
                {
                    if ((arg2 as FloatOperation).GetPrecedence() < flop.GetPrecedence())
                    {
                        // shuffle args
                        flop._Argument2 = (arg2 as FloatOperation)._Argument1;
                        (arg2 as FloatOperation)._Argument1 = flop;
                        flop = (arg2 as FloatOperation);
                    }
                }

                if (flop._Argument1.GetExpressionType() != flop._Argument2.GetExpressionType())
                    throw new ParseException("Mismatch argument types on operation");

                ret = flop;
            }

            return ret;
        }