Example #1
0
        public static ConditionalExpression Parse(ref string[] program)
        {
            ConditionalExpression ret  = null;
            Expression            expr = null;

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

            if (token1.Equals("(", StringComparison.Ordinal))
            {
                ret = new ConditionalBrackets();
                (ret as ConditionalBrackets).Parse(ref program);
            }
            else if (type1 == TokenType.UnaryBooleanOp)
            {
                ret = WooScript.GetUnaryBooleanOp(token1);
                (ret as UnaryBooleanOp).Parse(ref program);
            }
            else if (type1 == TokenType.floatVar ||
                     type1 == TokenType.floatNum ||
                     type1 == TokenType.floatFunction)
            {
                expr = ExpressionBuilder.Parse(ref program);
            }
            else
            {
                throw new ParseException("Unrecognised expression \"" + token1 + "\"");
            }

            string    token2 = ParseUtils.GetToken(ref program);
            TokenType type2  = WooScript.GetTokenType(token2);

            if (type2 == TokenType.ConditionalOp)
            {
                if (type1 != TokenType.floatNum && type1 != TokenType.floatVar && type1 != TokenType.floatFunction)
                {
                    throw new ParseException("Conditional operation only takes floating point parameters");
                }

                ConditionalOp condOp = WooScript.GetConditionalOp(token2);
                condOp._Arg1 = expr;
                Expression arg2 = ExpressionBuilder.Parse(ref program);

                if (arg2.GetExpressionType() != VarType.varFloat)
                {
                    throw new ParseException("Conditional operation only takes floating point parameters");
                }

                condOp._Arg2 = arg2;

                // no need for precedence check on conditional operator
                ret = condOp;

                string    token3 = ParseUtils.PeekToken(program);
                TokenType type3  = WooScript.GetTokenType(token3);
                if (type3 == TokenType.BooleanOp)
                {
                    BooleanOp boolOp = WooScript.GetBooleanOp(token3);
                    boolOp._Arg1 = condOp;

                    ConditionalExpression condArg2 = ConditionBuilder.Parse(ref program);
                    boolOp._Arg2 = condArg2;

                    // operator precedence check
                    if (condArg2 is BooleanOp)
                    {
                        if ((condArg2 as BooleanOp).GetPrecedence() < boolOp.GetPrecedence())
                        {
                            // shuffle args
                            boolOp._Arg2 = (condArg2 as BooleanOp)._Arg1;
                            (condArg2 as BooleanOp)._Arg1 = boolOp;
                            boolOp = (condArg2 as BooleanOp);
                        }
                    }

                    ret = boolOp;
                }
            }

            if (ret == null)
            {
                throw new ParseException("Malformed conditional expression, expected conditional operation");
            }
            return(ret);
        }