Exemple #1
0
        public void Parse(ref string[] program)
        {
            string openbrace = ParseUtils.GetToken(ref program);

            string nexttoken = ParseUtils.PeekToken(program);

            while (nexttoken != "}")
            {
                TokenType type = WooScript.GetTokenType(nexttoken);
                if (nexttoken.Equals("if", StringComparison.Ordinal))
                {
                    IfStatement ifStatement = new IfStatement();
                    ifStatement.Parse(ref program);
                    _Statements.Add(ifStatement);
                }
                else if (nexttoken.Equals("repeat", StringComparison.Ordinal))
                {
                    RepeatStatement repeatStatement = new RepeatStatement();
                    repeatStatement.Parse(ref program);
                    _Statements.Add(repeatStatement);
                }
                else if (nexttoken.Equals("{", StringComparison.Ordinal))
                {
                    ScopeStatement scopeStatement = new ScopeStatement();
                    scopeStatement.Parse(ref program);
                    _Statements.Add(scopeStatement);
                }
                else if (type == TokenType.rule)
                {
                    CallStatement callStatement = new CallStatement();
                    callStatement.Parse(ref program);
                    _Statements.Add(callStatement);
                }
                else if (type == TokenType.nullFunction)
                {
                    NullStatement nullStatement = new NullStatement();
                    nullStatement.Parse(ref program);
                    _Statements.Add(nullStatement);
                }
                else if ((type == TokenType.vecVar) || (type == TokenType.floatVar))
                {
                    VarStatement varStatement = new VarStatement();
                    varStatement.Parse(ref program);
                    _Statements.Add(varStatement);
                }
                else
                {
                    throw new ParseException("Unexpected token \"" + nexttoken + "\" at start of expression");
                }

                nexttoken = ParseUtils.PeekToken(program);
            }

            string closebrace = ParseUtils.GetToken(ref program);
        }
Exemple #2
0
        public void Parse(ref string[] program)
        {
            _Variable = ParseUtils.GetToken(ref program);
            TokenType tokenType = WooScript.GetTokenType(_Variable);

            if (tokenType == TokenType.floatVar)
            {
                _Type = VarType.varFloat;
            }
            else if (tokenType == TokenType.vecVar)
            {
                _Type = VarType.varVector;
            }
            else
            {
                throw new ParseException("Unknown type for variable \"" + _Variable + "\"");
            }
        }
Exemple #3
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);
        }
Exemple #4
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);
        }