GlueViewOfficialPlugins.Scripting.ExpressionParser.EvaluateExpression C# (CSharp) Method

EvaluateExpression() private method

private EvaluateExpression ( Expression expression, CodeContext codeContext, ExpressionParseType parseType = ExpressionParseType.Evaluate ) : object
expression Expression
codeContext GlueView.Scripting.CodeContext
parseType ExpressionParseType
return object
        internal object EvaluateExpression(Expression expression, CodeContext codeContext, ExpressionParseType parseType = ExpressionParseType.Evaluate)
        {
            if (codeContext.VariableStack.Count == 0)
            {
                throw new Exception("codeContext must have at least one entry in scoped variables");
            }
            if (expression is ICSharpCode.NRefactory.CSharp.PrimitiveExpression)
            {
                return (expression as ICSharpCode.NRefactory.CSharp.PrimitiveExpression).Value;
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.BinaryOperatorExpression)
            {
                return EvaluateBinaryOperatorExpression(expression, codeContext);
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.ParenthesizedExpression)
            {
                return EvaluateExpression(((ICSharpCode.NRefactory.CSharp.ParenthesizedExpression)expression).Expression, codeContext);
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.InvocationExpression)
            {
                return EvaluateInvocationExpression(expression as ICSharpCode.NRefactory.CSharp.InvocationExpression, codeContext);
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.MemberReferenceExpression)
            {
                return EvaluateMemberReferenceExpression(expression as ICSharpCode.NRefactory.CSharp.MemberReferenceExpression, codeContext, parseType);
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.ObjectCreateExpression)
            {
                return EvaluateObjectCreateExpression(expression as ICSharpCode.NRefactory.CSharp.ObjectCreateExpression, codeContext);
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.ThisReferenceExpression)
            {
                return codeContext.ContainerInstance;
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.TypeReferenceExpression)
            {
                return TypeManager.GetTypeFromString(expression.GetText());
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.IdentifierExpression)
            {
                return GetObjectFromContainerAndMemberName(codeContext.ContainerInstance, expression.GetText(), codeContext, parseType);
            }
            else if (expression is ICSharpCode.NRefactory.CSharp.IndexerExpression)
            {
                return EvaluateIndexerExpression(expression as ICSharpCode.NRefactory.CSharp.IndexerExpression, codeContext);
            }
            else if (expression is LambdaExpression)
            {
                return EvaluateLambdaExpression(expression as LambdaExpression, codeContext);
            }
            else if (expression is UnaryOperatorExpression)
            {
                return EvaluateUnaryOperatorExpression(expression as UnaryOperatorExpression, codeContext);
            }
            else if (expression is CastExpression)
            {
                return EvaluateCastExpression(expression as CastExpression, codeContext);
            }
            else
            {

                return null;
            }
        }

Same methods

ExpressionParser::EvaluateExpression ( string expression, CodeContext codeContext, ExpressionParseType parseType = ExpressionParseType.Evaluate ) : object

Usage Example

Ejemplo n.º 1
0
        private void ApplyLine(string line, CodeContext codeContext, string[] allLines, string fileName, ref int index)
        {
            bool succeeded = true;

            mParserLog.AppendLine("--- " + line + " ---");


            if (string.IsNullOrEmpty(line))
            {
                // do nothing
                // This may be empty
                // because the split function
                // may have returned a line with
                // only a newline character.  If so
                // that becomes an empty line when trim
                // is called on it.  This could be a line
                // that was trimmed which is now empty.
            }
            else if (line.Trim().StartsWith("//"))
            {
                // comment, carry on
            }
            else if (line.Trim().StartsWith("#region") || line.Trim().StartsWith("#endregion"))
            {
                // we can skip region blocks
            }
            else if (ConditionalCodeBlock.GetBlockTypeStartingAt(allLines, index) != BlockType.None)
            {
                ElementRuntime elementRuntime = codeContext.ContainerInstance as ElementRuntime;

                IElement element = null;

                if (elementRuntime != null)
                {
                    element = elementRuntime.AssociatedIElement;
                }

                succeeded = ApplyConditionalBlocks(allLines, element, codeContext, fileName, ref index);

                if (!succeeded)
                {
                    throw new Exception();
                }
            }
            else
            {
                // here we want to identify how many lines make up the statement since
                // it could be something like
                // this.X = 3
                //       + 4;
                int numberOfLinesInStatement = GetNumberOfLinesInRegularStatement(allLines, index);

                string combined = CombineLines(allLines, index, numberOfLinesInStatement);

                string trimmedCombined = combined.Trim();
                if (trimmedCombined.StartsWith("{") && trimmedCombined.EndsWith("}"))
                {
                    combined = trimmedCombined.Substring(1, trimmedCombined.Length - 2);
                }
                CSharpParser parser     = new CSharpParser();
                var          statements = parser.ParseStatements(combined);



                // I can be incremented by this function by the number of lines.
                // If this value is incremented, then the calling function is responsible
                // for recognizing that in its loop and acting properly.  The calling function
                // assumes an increment of 1 so we won't do anything if there's only 1 line in this
                // statement.
                if (numberOfLinesInStatement != 1)
                {
                    index += numberOfLinesInStatement;
                }

                foreach (var statement in statements)
                {
                    if (statement is ExpressionStatement)
                    {
                        Expression expression = ((ExpressionStatement)statement).Expression;
                        if (expression is InvocationExpression || expression is UnaryOperatorExpression)
                        {
                            mExpressionParser.EvaluateExpression(expression, codeContext);
                        }
                        else if (expression is AssignmentExpression)
                        {
                            succeeded = ApplyAssignmentLine(expression as AssignmentExpression, codeContext);
                            if (!succeeded)
                            {
                                throw new Exception();
                            }
                        }
                        else if (expression is IdentifierExpression || expression is MemberReferenceExpression)
                        {
                            // This is probably an incomplete line, so let's tolerate it and move on...
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                    else if (statement is VariableDeclarationStatement)
                    {
                        VariableDeclarationStatement vds = statement as VariableDeclarationStatement;

                        foreach (var child in vds.Children)
                        {
                            if (child is VariableInitializer)
                            {
                                VariableInitializer variableInitializer = child as VariableInitializer;
                                ApplyAssignment(variableInitializer, vds, codeContext);
                                //variableInitializer.
                            }
                        }
                        //ApplyAssignment(codeContext, vds.Variables.First().GetText(), vds.
                        //vds.E
                        //ApplyAssignmentLine(statement.ToString(), codeContext);
                    }
                    //if ( is AssignmentExpression || result is MemberReferenceExpression)
                    //{
                    //    ApplyAssignmentLine(combined, codeContext);
                    //}
                    //else if (result is InvocationExpression)
                    //{
                    //    ApplyMethodCall(combined, codeContext);
                    //}
                    else
                    {
                        AddErrorText("Unknown line(s):  " + combined);
                    }
                }
            }
        }