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

                if (!ifstring.Equals("if", StringComparison.Ordinal))
                {
                    throw new ParseException("If Statement should always start with if, found " + ifstring + " instead.");
                }

                string openbracket = ParseUtils.GetToken(ref program);

                if (!openbracket.Equals("(", StringComparison.Ordinal))
                {
                    throw new ParseException("if statement must be followed by a condition in brackets, found " + openbracket + " instead.");
                }
                _Condition = ConditionBuilder.Parse(ref program);
                string closebracket = ParseUtils.GetToken(ref program);

                if (!closebracket.Equals(")", StringComparison.Ordinal))
                {
                    throw new ParseException("if statement found a conditional expression without a closing bracket, found " + closebracket + " instead.");
                }

                _Block = new RuleBlock();
                _Block.Parse(ref program);
            }
Example #2
0
        public void Parse(ref string[] program)
        {
            string repeatstring = ParseUtils.GetToken(ref program);

            if (!repeatstring.Equals("repeat", StringComparison.Ordinal))
            {
                throw new ParseException("repeat Statement should always start with repeat, found " + repeatstring + " instead.");
            }

            string openbracket = ParseUtils.GetToken(ref program);

            if (!openbracket.Equals("(", StringComparison.Ordinal))
            {
                throw new ParseException("if statement must be followed by a condition in brackets, found " + openbracket + " instead.");
            }

            _Expression = ExpressionBuilder.Parse(ref program);
            if (_Expression.GetExpressionType() != VarType.varFloat)
            {
                throw new ParseException("repeat statement requires a float parameter");
            }

            string closebracket = ParseUtils.GetToken(ref program);

            if (!closebracket.Equals(")", StringComparison.Ordinal))
            {
                throw new ParseException("if statement found a conditional expression without a closing bracket, found " + closebracket + " instead.");
            }

            _RepeatBlock = new RuleBlock();
            _RepeatBlock.Parse(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)
        {
            _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 #5
0
        public void Parse(ref string[] program)
        {
            string floatFunctionName = ParseUtils.GetToken(ref program);

            _Function = WooScript.GetFloatFunction(floatFunctionName);
            _Function.Parse(ref program);
        }
Example #6
0
        public static string PeekToken(string[] lines)
        {
            string[] tempLines = lines.Clone() as string[];
            string   token     = ParseUtils.GetToken(ref tempLines);

            return(token);
        }
Example #7
0
        public void ConsumeRule(ref string[] program)
        {
            string token = ParseUtils.GetToken(ref program);

            if (!token.Equals("{", StringComparison.Ordinal))
            {
                throw new ParseException("rules/shaders not currently supported with arguments, expected {, found " + token + " instead.");
            }
            int braces = 1;

            while (braces > 0)
            {
                token = ParseUtils.GetToken(ref program);
                if (token.Equals("{", StringComparison.Ordinal))
                {
                    braces++;
                }
                if (token.Equals("}", StringComparison.Ordinal))
                {
                    braces--;
                }
                if (token.Equals("rule", StringComparison.Ordinal))
                {
                    throw new ParseException("missing }, found " + token + " instead.");
                }
                if (token.Equals("shader", StringComparison.Ordinal))
                {
                    throw new ParseException("missing }, found " + token + " instead.");
                }
            }
        }
Example #8
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 #9
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 #10
0
 public void Parse(ref string[] program)
 {
     _DistanceFunction = ParseUtils.GetToken(ref program);
     if (WooScript.IsShader(_DistanceFunction))
     {
         _ShaderMode = true;
     }
     else
     {
         _ShaderMode = false;
     }
 }
Example #11
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 #12
0
        public void Parse(ref string[] program)
        {
            string openbrace = ParseUtils.GetToken(ref program);

            if (!openbrace.Equals("(", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \"(\" at start of function parameters");
            }

            _XExpr = ExpressionBuilder.Parse(ref program);
            if (_XExpr.GetExpressionType() != VarType.varFloat)
            {
                throw new ParseException("parameter one to vec() is not a float");
            }

            string comma = ParseUtils.GetToken(ref program);

            if (!comma.Equals(",", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \",\"");
            }

            _YExpr = ExpressionBuilder.Parse(ref program);
            if (_YExpr.GetExpressionType() != VarType.varFloat)
            {
                throw new ParseException("parameter two to vec() is not a float");
            }

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

            _ZExpr = ExpressionBuilder.Parse(ref program);
            if (_ZExpr.GetExpressionType() != VarType.varFloat)
            {
                throw new ParseException("parameter three to vec() is not a float");
            }

            string closebrace = ParseUtils.GetToken(ref program);

            if (!closebrace.Equals(")", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \")\" at end of function parameters");
            }
        }
Example #13
0
        public void Parse(ref string[] program)
        {
            string token = ParseUtils.GetToken(ref program);

            if (token.Length > 0)
            {
                _MaterialFunction = token;
                if (!WooScript.IsShader(_MaterialFunction))
                {
                    throw new ParseException("materialfunction call expected a shader, sadly " + _MaterialFunction + " isn't one...");
                }
            }
            else
            {
                _MaterialFunction = "";
            }
        }
Example #14
0
        public void Parse(ref string[] program)
        {
            _ColourExpr = ExpressionBuilder.Parse(ref program);
            if (_ColourExpr.GetExpressionType() != VarType.varVector)
            {
                throw new ParseException("malformed directional light colour");
            }

            string comma = ParseUtils.GetToken(ref program);

            if (!comma.Equals(",", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \",\"");
            }

            _DirectionExpr = ExpressionBuilder.Parse(ref program);
            if (_DirectionExpr.GetExpressionType() != VarType.varVector)
            {
                throw new ParseException("malformed directional light direction");
            }

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

            _AreaExpr = ExpressionBuilder.Parse(ref program);
            if (_AreaExpr.GetExpressionType() != VarType.varFloat)
            {
                throw new ParseException("malformed directional light area");
            }

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

            _SamplesExpr = ExpressionBuilder.Parse(ref program);
            if (_SamplesExpr.GetExpressionType() != VarType.varFloat)
            {
                throw new ParseException("malformed directional light samples");
            }
        }
Example #15
0
        public void Parse(ref string[] program)
        {
            _Rotate1 = ExpressionBuilder.Parse(ref program);
            if (_Rotate1.GetExpressionType() != VarType.varVector)
            {
                throw new ParseException("_Rotate1 must be of type vector");
            }

            string comma = ParseUtils.GetToken(ref program);

            if (!comma.Equals(",", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \",\"");
            }

            _Rotate2 = ExpressionBuilder.Parse(ref program);
            if (_Rotate2.GetExpressionType() != VarType.varVector)
            {
                throw new ParseException("_Rotate2 must be of type vector");
            }

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

            _Scale = ExpressionBuilder.Parse(ref program);
            if (_Scale.GetExpressionType() != VarType.varFloat)
            {
                throw new ParseException("_Scale must be of type float");
            }

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

            _Offset = ExpressionBuilder.Parse(ref program);
            if (_Offset.GetExpressionType() != VarType.varVector)
            {
                throw new ParseException("_Offset must be of type vector");
            }
        }
Example #16
0
        public void Parse(ref string[] program)
        {
            string token1 = ParseUtils.GetToken(ref program);

            if (!token1.Equals("(", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \"(\" in Brackets");
            }

            _Expression = ExpressionBuilder.Parse(ref program);

            string token2 = ParseUtils.GetToken(ref program);

            if (!token2.Equals(")", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \")\" in Brackets");
            }
        }
Example #17
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 + "\"");
            }
        }
Example #18
0
        public void Parse(ref string[] program)
        {
            _Callee = ParseUtils.GetToken(ref program);
            WooScript._Log.AddMsg("Callee : " + _Callee);

            string comma = ParseUtils.GetToken(ref program);

            if (!comma.Equals(",", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \",\"");
            }

            _Expr = ExpressionBuilder.Parse(ref program);
            if (_Expr.GetExpressionType() != VarType.varFloat)
            {
                throw new ParseException("Failed to convert repeat number to float");
            }

            WooScript._Log.AddMsg("Repeats : " + _Expr.ToString());
        }
Example #19
0
        public void Parse(ref string[] program)
        {
            string _PatternStr = ParseUtils.GetToken(ref program);

            if (_PatternStr.Length != 27)
            {
                throw new ParseException("argument must have 27 digits mengerpattern(101101...)");
            }

            for (int i = 0; i < 27; i++)
            {
                if (_PatternStr[i] == '1')
                {
                    _Pattern[i] = 1;
                }
                else
                {
                    _Pattern[i] = 0;
                }
            }
        }
Example #20
0
        public void Parse(ref string[] program)
        {
            _ColourExpr = ExpressionBuilder.Parse(ref program);
            if (_ColourExpr.GetExpressionType() != VarType.varVector)
            {
                throw new ParseException("malformed point light colour");
            }

            string comma = ParseUtils.GetToken(ref program);

            if (!comma.Equals(",", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \",\"");
            }

            _PositionExpr = ExpressionBuilder.Parse(ref program);
            if (_PositionExpr.GetExpressionType() != VarType.varVector)
            {
                throw new ParseException("malformed point light position");
            }
        }
Example #21
0
        public void Parse(ref string[] program)
        {
            string token = ParseUtils.GetToken(ref program);

            WooScript._Log.AddMsg("Found \"" + token + "\" function (null return)");
            WooScript._Log.Indent();
            _NullFunction = WooScript.GetNullFunction(token);
            string openCurl = ParseUtils.GetToken(ref program);

            if (!openCurl.Equals("(", StringComparison.Ordinal))
            {
                new ParseException("Expected \"(\" not \"" + openCurl + "\"");
            }
            _NullFunction.Parse(ref program);
            string closeCurl = ParseUtils.GetToken(ref program);

            if (!closeCurl.Equals(")", StringComparison.Ordinal))
            {
                new ParseException("Expected \")\" not \"" + closeCurl + "\"");
            }
            WooScript._Log.UnIndent();
        }
Example #22
0
        public void Parse(ref string[] program)
        {
            string token = ParseUtils.GetToken(ref program);

            WooScript._Log.AddMsg("Found function, target \"" + token + "\"");
            WooScript._Log.Indent();
            if (WooScript.IsFloatVariable(token))
            {
                _ReturnType = VarType.varFloat;
                _Var        = token;
            }
            else if (WooScript.IsVecVariable(token))
            {
                _ReturnType = VarType.varVector;
                _Var        = token;
            }
            else
            {
                throw new ParseException("Expected \"" + token + "\" to be a float or vector variable");
            }

            string assignOp = ParseUtils.GetToken(ref program);

            _AssignOp = WooScript.GetAssignOp(assignOp);

            _Expression = ExpressionBuilder.Parse(ref program);

            if (_ReturnType == VarType.varVector &&
                (_Expression.GetExpressionType() != VarType.varVector))
            {
                throw new ParseException("Target token is \"" + token + "\" which is a vector, expression isn't...");
            }

            if (_ReturnType == VarType.varFloat &&
                (_Expression.GetExpressionType() != VarType.varFloat))
            {
                throw new ParseException("Target token is \"" + token + "\" which is a float, expression isn't...");
            }
        }
Example #23
0
        public void Parse(ref string[] program)
        {
            string openbrace = ParseUtils.GetToken(ref program);

            if (!openbrace.Equals("(", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \"(\" at start of function parameters");
            }

            _Arg = ExpressionBuilder.Parse(ref program);
            if (_Arg.GetExpressionType() != VarType.varVector)
            {
                throw new ParseException("parameter to normalise() is not a vector");
            }

            string closebrace = ParseUtils.GetToken(ref program);

            if (!closebrace.Equals(")", StringComparison.Ordinal))
            {
                throw new ParseException("Expected \")\" at end of function parameters");
            }
        }
Example #24
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 #25
0
        public void Parse(ref string[] program)
        {
            string data = ParseUtils.GetToken(ref program);

            // deal with -ve numbers (bit hacky, but keeps the token parser state free)
            if (data.Length == 1 && data[0] == '-')
            {
                data += ParseUtils.GetToken(ref program);
            }

            if (data.IndexOf(':') > 0)
            {
                _RangeType = RangeTypeT.Continuous;
                WooScript._Log.AddMsg("Number type : Continuous range");
                int opPos = data.IndexOf(':');
                val1 = StringToFloat(data.Substring(0, opPos));
                val2 = StringToFloat(data.Substring(opPos + 1));
                WooScript._Log.AddMsg("Val1 : " + val1.ToString(CultureInfo.InvariantCulture));
                WooScript._Log.AddMsg("Val2 : " + val2.ToString(CultureInfo.InvariantCulture));
            }
            else if (data.IndexOf('|') > 0)
            {
                _RangeType = RangeTypeT.Binary;
                WooScript._Log.AddMsg("Number type : Binary Option");
                int opPos = data.IndexOf('|');
                val1 = StringToFloat(data.Substring(0, opPos));
                val2 = StringToFloat(data.Substring(opPos + 1));
                WooScript._Log.AddMsg("Val1 : " + val1.ToString(CultureInfo.InvariantCulture));
                WooScript._Log.AddMsg("Val2 : " + val2.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                _RangeType = RangeTypeT.Single;
                WooScript._Log.AddMsg("Number type : Single Value");
                val1 = StringToFloat(data);
                WooScript._Log.AddMsg("Val1 : " + val1.ToString(CultureInfo.InvariantCulture));
            }
        }
Example #26
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 #27
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 #28
0
        public void Parse(ref string[] program)
        {
            string op = ParseUtils.GetToken(ref program);

            _Op = WooScript.GetOp(op);
        }
Example #29
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 #30
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);
        }