Example #1
0
        public void Parse(ref string[] program)
        {
            _Target = ParseUtils.PeekToken(program);
            bool isFloatVar  = ShaderScript.IsFloatVar(_Target);
            bool isVectorVar = ShaderScript.IsVectorVar(_Target);
            bool isNullFunc  = ShaderScript.IsNullFunction(_Target);

            if (!isFloatVar && !isVectorVar && !isNullFunc)
            {
                throw new ParseException("Expected \"" + _Target + "\" to be a float or vector variable or a non-return function");
            }

            if (isFloatVar || isVectorVar)
            {
                _Target = ParseUtils.GetToken(ref program);
                _IsNull = false;
                WooScript._Log.AddMsg("Found target variable \"" + _Target + "\"");
                WooScript._Log.Indent();

                _AssignOp = ParseUtils.GetToken(ref program);

                if (!ShaderScript.IsAssignOp(_AssignOp))
                {
                    throw new ParseException("Expected \"" + _AssignOp + "\" to be an assignment operation");
                }
            }

            _Argument = ShaderScript.ParseExpression(ref program);
        }
Example #2
0
        public void Parse(ref string[] program)
        {
            while (program[0].IndexOf(',') >= 0)
            {
                string rulename = ParseUtils.GetToken(ref program);
                _Rule.Add(rulename);

                string comma = ParseUtils.GetToken(ref program);
                if (!comma.Equals(",", StringComparison.Ordinal))
                {
                    throw new ParseException("Expected \",\"");
                }

                WooScript._Log.AddMsg("Rule1 : " + rulename);

                Expression expression = ExpressionBuilder.Parse(ref program);
                if (expression.GetExpressionType() != VarType.varFloat)
                {
                    throw new ParseException("Failed to convert weighting parameter to repeat method to float");
                }

                _Weight.Add(expression);
                WooScript._Log.AddMsg("Weight : " + expression);

                comma = ParseUtils.PeekToken(program);
                if (comma.Equals(",", StringComparison.Ordinal))
                {
                    comma = ParseUtils.GetToken(ref program);
                }
            }
        }
Example #3
0
        public void Parse(ref string[] program)
        {
            IfConditionBlock ifConditionBlock = new IfConditionBlock();

            ifConditionBlock.Parse(ref program);
            _IfBlock.Add(ifConditionBlock);

            string token = ParseUtils.PeekToken(program);

            while (token.Equals("else", StringComparison.Ordinal))
            {
                string elsestring = ParseUtils.GetToken(ref program);
                if (!elsestring.Equals("else", StringComparison.Ordinal))
                {
                    throw new ParseException("missing else statement, found " + elsestring + " instead.");
                }

                string iftoken = ParseUtils.PeekToken(program);
                if (iftoken.Equals("if", StringComparison.Ordinal))
                {
                    IfConditionBlock ifCondBlock = new IfConditionBlock();
                    ifCondBlock.Parse(ref program);
                    _IfBlock.Add(ifCondBlock);
                }
                else
                {
                    _ElseBlock = new RuleBlock();
                    _ElseBlock.Parse(ref program);
                    return; // no more blocks allowed...
                }
                token = ParseUtils.PeekToken(program);
            }
        }
Example #4
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);
        }
Example #5
0
        public void Parse(ref string[] program)
        {
            string token = ParseUtils.PeekToken(program);

            if (token.Equals("(", StringComparison.Ordinal))
            {
                token = ParseUtils.GetToken(ref program);

                do
                {
                    Argument arg = new Argument();
                    token = ParseUtils.GetToken(ref program);
                    if (token.Equals("float", StringComparison.Ordinal))
                    {
                        arg._Type = VarType.varFloat;
                    }
                    else if (token.Equals("vec", StringComparison.Ordinal))
                    {
                        arg._Type = VarType.varVector;
                    }
                    else
                    {
                        throw new ParseException("Expected type of parameter (float OR vec), not" + token);
                    }

                    arg._Name = ParseUtils.GetToken(ref program);
                    WooScript.ValidateName(arg._Name);

                    _Arguments.Add(arg);
                    token = ParseUtils.GetToken(ref program);
                }while (token.Equals(",", StringComparison.Ordinal));

                if (!token.Equals(")", StringComparison.Ordinal))
                {
                    throw new ParseException("Expected \")\" but found \"" + token + "\" instead. :(");
                }
            }

            foreach (Argument a in _Arguments)
            {
                a.Add();
            }
            block.Parse(ref program);
            foreach (Argument a in _Arguments)
            {
                a.Remove();
            }
        }
Example #6
0
        public void Parse(ref string[] program)
        {
            string openbrace = ParseUtils.GetToken(ref program);

            if (!openbrace.Equals("{"))
            {
                throw new ParseException("Found \"" + openbrace + "\", but expected \"{\".");
            }

            string nexttoken = ParseUtils.PeekToken(program);

            while (nexttoken != "}")
            {
                ShaderStatement shaderStatement = new ShaderStatement();
                shaderStatement.Parse(ref program);
                _Statements.Add(shaderStatement);

                nexttoken = ParseUtils.PeekToken(program);
            }

            string closebrace = ParseUtils.GetToken(ref program);
        }
Example #7
0
        public void Parse(ref string[] program)
        {
            string ruleName = ParseUtils.GetToken(ref program);

            _RulePrototype = WooScript.GetRulePrototype(ruleName);
            WooScript._Log.AddMsg("Call Rule : " + ruleName);

            string openbracket = ParseUtils.PeekToken(program);

            if (openbracket.Equals("(", StringComparison.Ordinal) || _RulePrototype._Args.Count() > 0)
            {
                openbracket = ParseUtils.GetToken(ref program);
                if (!openbracket.Equals("(", StringComparison.Ordinal))
                {
                    throw new ParseException("rule called without required arguments, or syntax error. Found " + openbracket + ", expeceted \"(\" instead.");
                }

                for (int i = 0; i < _RulePrototype._Args.Count(); i++)
                {
                    _ArgValue.Add(ExpressionBuilder.Parse(ref program));
                    if (i + 1 < _RulePrototype._Args.Count())
                    {
                        string comma = ParseUtils.GetToken(ref program);
                        if (!comma.Equals(",", StringComparison.Ordinal))
                        {
                            throw new ParseException("Found " + comma + " but expected another argument after a \",\"");
                        }
                    }
                }

                string closebracket = ParseUtils.GetToken(ref program);
                if (!closebracket.Equals(")", StringComparison.Ordinal))
                {
                    throw new ParseException("rules not currently supported with arguments, missing ), found " + closebracket + " instead.");
                }
            }
        }
Example #8
0
        public static ScriptElement ParseExpression(ref string[] program)
        {
            ScriptElement ret   = new ScriptElement();
            string        token = ParseUtils.PeekToken(program);

            ret._Reorderable = false;

            if (token.Equals("-", StringComparison.Ordinal))
            {
                token  = ParseUtils.GetToken(ref program);
                token += ParseUtils.PeekToken(program);
            }

            if (token.Equals("(", StringComparison.Ordinal))
            {
                token            = ParseUtils.GetToken(ref program);
                ret              = ParseExpression(ref program);
                ret._Reorderable = false;

                token = ParseUtils.GetToken(ref program);
                if (!token.Equals(")", StringComparison.Ordinal))
                {
                    throw new ParseException("Missing closebracket on bracketed expression, found" + token);
                }
            }
            else if (IsFloatVar(token))
            {
                token       = ParseUtils.GetToken(ref program);
                ret._Type   = SEType.FloatVar;
                ret._String = token;
            }
            else if (IsVectorVar(token))
            {
                token       = ParseUtils.GetToken(ref program);
                ret._Type   = SEType.VectorVar;
                ret._String = token;
            }
            else if (IsFloatNum(token))
            {
                string chuck = ParseUtils.GetToken(ref program);
                ret._Type   = SEType.FloatVar;
                ret._String = token;
            }
            else if (token.Equals("{"))
            {
                ret._Type      = SEType.CodeBlock;
                ret._Codeblock = new CodeBlock();
                ret._Codeblock.Parse(ref program);
            }
            else if (token.IndexOf('.') > -1)
            {
                token = ParseUtils.GetToken(ref program);
                int dotPosition = token.IndexOf('.');

                ret._Type   = SEType.VectorVar;
                ret._String = token.Substring(0, dotPosition);

                ScriptElement getter = new ScriptElement();

                string index = token.Substring(dotPosition + 1);

                if (index.Equals("x", StringComparison.Ordinal))
                {
                    getter._String = "getx";
                }
                else if (index.Equals("y", StringComparison.Ordinal))
                {
                    getter._String = "gety";
                }
                else if (index.Equals("z", StringComparison.Ordinal))
                {
                    getter._String = "getz";
                }
                else
                {
                    throw new ParseException("Invalid subindex " + index);
                }

                getter._Arguments.Add(ret);
                getter._Type = SEType.FloatVar;
                ret          = getter;
            }
            else if (token.Equals("repeat", StringComparison.Ordinal))
            {
                token       = ParseUtils.GetToken(ref program);
                ret._String = token;

                string openBracket = ParseUtils.PeekToken(program);
                if (openBracket.Equals("(", StringComparison.Ordinal))
                {
                    openBracket = ParseUtils.GetToken(ref program);

                    ret._Arguments.Add(ParseExpression(ref program));

                    string closeBracket = ParseUtils.GetToken(ref program);
                    if (!closeBracket.Equals(")", StringComparison.Ordinal))
                    {
                        throw new ParseException("Expected close bracket, found \"" + token + "\"");
                    }
                }
                else
                {
                    throw new ParseException("repeat not followed by number, found \"" + token + "\", usage : repeat(x){}");
                }

                ret._Arguments.Add(ParseExpression(ref program));
            }
            else if (IsFunction(token))
            {
                token       = ParseUtils.GetToken(ref program);
                ret._String = token;

                string openBracket = ParseUtils.PeekToken(program);
                if (openBracket.Equals("(", StringComparison.Ordinal))
                {
                    openBracket = ParseUtils.GetToken(ref program);

                    ret._Arguments.Add(ParseExpression(ref program));

                    string comma = ParseUtils.PeekToken(program);
                    while (comma.Equals(",", StringComparison.Ordinal))
                    {
                        comma = ParseUtils.GetToken(ref program);
                        ret._Arguments.Add(ParseExpression(ref program));
                        comma = ParseUtils.PeekToken(program);
                    }

                    string closeBracket = ParseUtils.GetToken(ref program);
                    if (!closeBracket.Equals(")", StringComparison.Ordinal))
                    {
                        throw new ParseException("Expected close bracket, found \"" + token + "\"");
                    }
                }

                ValidateFunction(ret);
            }
            else
            {
                throw new ParseException("Unrecognised token found \"" + token + "\"");
            }

            string opCode = ParseUtils.PeekToken(program);

            if (IsOperator(opCode))
            {
                opCode = ParseUtils.GetToken(ref program);
                ScriptElement opElement = new ScriptElement();

                if (opCode.Equals("+", StringComparison.Ordinal))
                {
                    opElement._String = "add";
                }
                if (opCode.Equals("-", StringComparison.Ordinal))
                {
                    opElement._String = "sub";
                }
                if (opCode.Equals("*", StringComparison.Ordinal))
                {
                    opElement._String = "mul";
                }
                if (opCode.Equals("/", StringComparison.Ordinal))
                {
                    opElement._String = "div";
                }
                if (opCode.Equals("%", StringComparison.Ordinal))
                {
                    opElement._String = "mod";
                }

                opElement._Type = ret._Type;
                opElement._Arguments.Add(ret);

                ScriptElement arg2 = ParseExpression(ref program);
                opElement._Arguments.Add(arg2);

                if (arg2._Reorderable)
                {
                    if (GetPrecedence(opElement._String) < GetPrecedence(arg2._String))
                    {
                        ScriptElement Arg1Arg1 = ret;
                        ScriptElement Arg2Arg1 = arg2._Arguments.ElementAt(0);
                        ScriptElement Arg2Arg2 = arg2._Arguments.ElementAt(1);

                        arg2._Arguments.Clear();
                        arg2._Arguments.Add(opElement);
                        arg2._Arguments.Add(Arg2Arg2);
                        opElement._Arguments.Clear();
                        opElement._Arguments.Add(Arg1Arg1);
                        opElement._Arguments.Add(Arg2Arg1);

                        opElement = arg2;
                    }
                }

                ret = opElement;
                ret._Reorderable = true;

                ValidateFunction(opElement);
            }

            return(ret);
        }
Example #9
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);
        }
Example #10
0
        public void ParseProgram(ref string[] program)
        {
            string token;

            // pre parse
            string[] preprogram = program.Clone() as string[];
            do
            {
                token = ParseUtils.GetToken(ref preprogram);

                if (string.Compare(token, "rule", true) == 0)
                {
                    string rulename = ParseUtils.GetToken(ref preprogram);
                    _Log.AddMsg("Preparser found rule " + rulename);
                    RulePrototype ruleproto = new RulePrototype(rulename);

                    string itoken = ParseUtils.PeekToken(preprogram);
                    if (itoken.Equals("(", StringComparison.Ordinal))
                    {
                        itoken = ParseUtils.GetToken(ref preprogram);
                        do
                        {
                            itoken = ParseUtils.GetToken(ref preprogram); // type
                            Argument arg = new Argument();
                            if (itoken.Equals("float", StringComparison.Ordinal))
                            {
                                arg._Type = VarType.varFloat;
                            }
                            else if (itoken.Equals("vec", StringComparison.Ordinal))
                            {
                                arg._Type = VarType.varVector;
                            }
                            else
                            {
                                throw new ParseException("Expected type of parameter (float OR vec), not" + itoken);
                            }
                            arg._Name = ParseUtils.GetToken(ref preprogram); // name

                            ruleproto._Args.Add(arg);

                            itoken = ParseUtils.GetToken(ref preprogram); // , or )
                        }while (itoken.Equals(",", StringComparison.Ordinal));
                    }
                    _RulePrototypes.Add(ruleproto);
                    ConsumeRule(ref preprogram);
                }
                else if (string.Compare(token, "shader", true) == 0)
                {
                    string shadername = ParseUtils.GetToken(ref preprogram);
                    _Log.AddMsg("Preparser found shader " + shadername);
                    _ShaderNames.Add(shadername);
                    ConsumeRule(ref preprogram);
                }
                else
                {
                    if (token.Length > 0)
                    {
                        throw new ParseException("Global statements must start with \"rule\".");
                    }
                }
            }while (token.Length > 0);

            // full parse
            do
            {
                token = ParseUtils.GetToken(ref program);

                if (string.Compare(token, "rule", true) == 0)
                {
                    string rulename = ParseUtils.GetToken(ref program);
                    _Log.AddMsg("Found rule " + rulename);
                    Rule newRule = new Rule(rulename);
                    _Log.Indent();
                    newRule.Parse(ref program);
                    _Log.UnIndent();
                    _Rules.Add(newRule);
                }
                else if (string.Compare(token, "shader", true) == 0)
                {
                    string shadername = ParseUtils.GetToken(ref program);
                    _Log.AddMsg("Found shader " + shadername);
                    Shader newShader = new Shader(shadername);
                    _Log.Indent();
                    newShader.Parse(ref program);
                    _Log.UnIndent();
                    _Shaders.Add(newShader);
                }
                else
                {
                    if (token.Length > 0)
                    {
                        throw new ParseException("Global statements must start with \"rule\".");
                    }
                }
            }while (token.Length > 0);

            // rule rulename { statement* }
            // statement = floatvar aop floatrhs | vecvar aop vecexpr | nfun ( args )
            // args = (rulename|floatexpr|vecexpr)*
            // floatexpr = ( floatexpr ) | num op floatexpr | ffun(args) op floatexpr | num | ffun(args) | floatvar
            // num = 0.x | vec.sel
            // vecexpr = Vec(floatexpr, floatexpr, floatexpr) | vec op vecexpr | vfun ( args )
            // vec = vec(x,y,z) | vec(x,y,z).rep
        }
Example #11
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);
        }