Ejemplo n.º 1
0
        public void IncrementFor(CodeContext codeContext, ConditionalCodeBlock ccb, ExpressionParser expressionParser)
        {
            Statement[] statements = new CSharpParser().ParseStatements(ccb.ConditionExpression) as Statement[];

            ExpressionStatement incrementStatement = statements[2] as ExpressionStatement;

            expressionParser.EvaluateExpression(incrementStatement.Expression, codeContext);
        }
Ejemplo n.º 2
0
        public bool DetermineIfShouldExecute(
            ScriptParsingPlugin plugin,
            CodeContext codeContext,
            ConditionalCodeBlock ccb,
            ExpressionParser expressionParser,
            bool isFirstExecution)
        {
            bool shouldExecute = false;

            if (ccb.BlockType == BlockType.Else)
            {
                shouldExecute = true;
            }
            else if (ccb.BlockType == BlockType.For)
            {
                Statement[] statements = new CSharpParser().ParseStatements(ccb.ConditionExpression) as Statement[];

                if (isFirstExecution)
                {
                    VariableDeclarationStatement declaration = statements[0] as VariableDeclarationStatement;

                    var variable = declaration.Variables.FirstOrDefault();

                    plugin.ApplyAssignment(variable, declaration, codeContext);
                }

                var predicateExpression =
                    (statements[1] as ExpressionStatement).Expression;
                var resultAsObject = expressionParser.EvaluateExpression(predicateExpression, codeContext);
                return((bool)resultAsObject);
            }
            else if (ccb.BlockType == BlockType.While)
            {
                shouldExecute =
                    (bool)expressionParser.EvaluateExpression(ccb.ConditionExpression, codeContext);
            }
            else
            {
                try
                {
                    shouldExecute =
                        (bool)expressionParser.EvaluateExpression(ccb.ConditionExpression, codeContext);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            return(shouldExecute);
        }
        public void TestConditionalBlocks()
        {
            var type = ConditionalCodeBlock.GetBlockTypeStartingAt(new string[] { "if (true)" }, 0);

            if (type != BlockType.If)
            {
                throw new Exception("If blocks are not being identified as such");
            }

            type = ConditionalCodeBlock.GetBlockTypeStartingAt(new string[] { "while (true)" }, 0);
            if (type != BlockType.While)
            {
                throw new Exception("If blocks are not being identified as such");
            }

            type = ConditionalCodeBlock.GetBlockTypeStartingAt(new string[] { "foreach (var item in someList)" }, 0);
            if (type != BlockType.Foreach)
            {
                throw new Exception("If blocks are not being identified as such");
            }

            type = ConditionalCodeBlock.GetBlockTypeStartingAt(new string[] { "for (int i = 0; i < 10; i++)" }, 0);
            if (type != BlockType.For)
            {
                throw new Exception("If blocks are not being identified as such");
            }


            string[] lines = new string[]
            {
                "int m = 3;",
                "for(int i = 0; i < 10; i++)",
                "{",
                "   m++;",
                "}"
            };

            CodeContext codeContext = new CodeContext(null);

            mPlugin.ApplyLinesInternal(lines, 0, lines.Length, null, codeContext);

            if ((int)(codeContext.VariableStack[0]["m"]) != 13)
            {
                throw new Exception("for loops are not working");
            }



            lines = new string[]
            {
                "while( m < 18)",
                "{",
                "   m++;",
                "}"
            };

            mPlugin.ApplyLinesInternal(lines, 0, lines.Length, null, codeContext);

            if ((int)(codeContext.VariableStack[0]["m"]) != 18)
            {
                throw new Exception("while-loops are not working");
            }
        }