public object EvaluateExpression(string expression, CodeContext codeContext, ExpressionParseType parseType = ExpressionParseType.Evaluate)
        {
//

            var result = parser.ParseExpression(expression);

            if (result.ToString() == "Null")
            {
                IEnumerable <Statement> statements = parser.ParseStatements(expression);

                Statement firstStatement = statements.First();

                return(EvaluateStatement(firstStatement, codeContext, parseType));
            }
            else
            {
                return(EvaluateExpression(result, codeContext, parseType));
            }
        }
        internal object ApplyUnaryOperation(ICSharpCode.NRefactory.CSharp.UnaryOperatorType unaryOperatorType, object originalValue, ExpressionParseType referenceOrValue)
        {
            object valueToModify = originalValue;

            if (originalValue is IAssignableReference)
            {
                valueToModify = ((IAssignableReference)originalValue).CurrentValue;
            }
            object toReturn = null;


            if (valueToModify == null)
            {
                toReturn = null;
            }

            else if (valueToModify is float)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (float)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (float)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (float)valueToModify - 1;
                }
            }
            else if (valueToModify is int)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (int)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (int)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (int)valueToModify - 1;
                }
            }
            else if (valueToModify is long)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (long)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (long)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (long)valueToModify - 1;
                }
            }
            else if (valueToModify is double)
            {
                if (unaryOperatorType == ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Minus)
                {
                    toReturn = (double)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (double)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (double)valueToModify - 1;
                }
            }
            else if (valueToModify is string)
            {

            }
            else if (valueToModify is bool)
            {
                if (unaryOperatorType == ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Not)
                {
                    toReturn = !((bool)valueToModify);
                }
            }

            if (toReturn == null)
            {

                throw new NotImplementedException();
            }
            else
            {
                // If the operator is one that modifies the object (like ++), we want to apply it back
                if (referenceOrValue == ExpressionParseType.GetReference && originalValue is IAssignableReference)
                {
                    ((IAssignableReference)originalValue).CurrentValue = toReturn;
                    toReturn = originalValue;
                }


                return toReturn;
            }
        }
        private static object GetObjectFromContainerAndMemberName(object container, string memberName, CodeContext codeContext, ExpressionParseType parseType)
        {
            object foundValue = null;
            bool   wasFound   = false;

            if (parseType == ExpressionParseType.Evaluate)
            {
                if (container is IAssignableReference)
                {
                    container = ((IAssignableReference)container).CurrentValue;
                }

                GetObjectFromContainerAndNameEvaluate(container, memberName, codeContext, ref foundValue, ref wasFound);
            }
            else
            {
                GetObjectFromContainerAndNameReference(container, memberName, codeContext, ref foundValue, ref wasFound);
            }
            return(foundValue);
        }
        private object EvaluateMemberReferenceExpression(ICSharpCode.NRefactory.CSharp.MemberReferenceExpression memberReferenceExpression, CodeContext codeContext, ExpressionParseType parseType)
        {
            object container = EvaluateExpression(memberReferenceExpression.Target, codeContext, ExpressionParseType.GetReference);

            if (container == null)
            {
                // Couldn't get a member reference, so it could be a variable on an Element, so let's try a non-reference get
                container = EvaluateExpression(memberReferenceExpression.Target, codeContext, ExpressionParseType.Evaluate);
            }
            string memberName = memberReferenceExpression.MemberName;

            object foundValue = GetObjectFromContainerAndMemberName(container, memberName, codeContext, parseType);

            return(foundValue);
        }
 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);
     }
 }
        internal object EvaluateStatement(Statement statement, CodeContext codeContext, ExpressionParseType parseType)
        {
            if (statement is VariableDeclarationStatement)
            {
                VariableDeclarationStatement vds = statement as VariableDeclarationStatement;

                var astType = vds.Type;

                Type type = TypeManager.GetTypeFromString(astType.GetText());

                var variables = vds.Variables;

                string addedVariable = null;

                foreach (var variable in variables)
                {
                    // right now we only support one declaration per line, will need to
                    // expand this later
                    addedVariable = variable.Name;
                    codeContext.VariableStack.Last().Add(addedVariable, null);
                    break;
                }

                if (parseType == ExpressionParseType.Evaluate)
                {
                    return(null); // hasn't been assigned yet
                }
                else
                {
                    StackVariableReference svr = new StackVariableReference();
                    int index;
                    codeContext.GetVariableInformation(addedVariable, out index);
                    svr.StackIndex      = index;
                    svr.VariableName    = addedVariable;
                    svr.TypeOfReference = type;
                    return(svr);
                }
            }
            else
            {
                return(null);
            }
        }
Example #7
0
        internal object ApplyUnaryOperation(ICSharpCode.NRefactory.CSharp.UnaryOperatorType unaryOperatorType, object originalValue, ExpressionParseType referenceOrValue)
        {
            object valueToModify = originalValue;

            if (originalValue is IAssignableReference)
            {
                valueToModify = ((IAssignableReference)originalValue).CurrentValue;
            }
            object toReturn = null;


            if (valueToModify == null)
            {
                toReturn = null;
            }

            else if (valueToModify is float)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (float)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (float)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (float)valueToModify - 1;
                }
            }
            else if (valueToModify is int)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (int)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (int)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (int)valueToModify - 1;
                }
            }
            else if (valueToModify is long)
            {
                if (unaryOperatorType == UnaryOperatorType.Minus)
                {
                    toReturn = (long)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (long)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (long)valueToModify - 1;
                }
            }
            else if (valueToModify is double)
            {
                if (unaryOperatorType == ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Minus)
                {
                    toReturn = (double)valueToModify * -1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostIncrement)
                {
                    toReturn = (double)valueToModify + 1;
                }
                else if (unaryOperatorType == UnaryOperatorType.PostDecrement)
                {
                    toReturn = (double)valueToModify - 1;
                }
            }
            else if (valueToModify is string)
            {
            }
            else if (valueToModify is bool)
            {
                if (unaryOperatorType == ICSharpCode.NRefactory.CSharp.UnaryOperatorType.Not)
                {
                    toReturn = !((bool)valueToModify);
                }
            }

            if (toReturn == null)
            {
                throw new NotImplementedException();
            }
            else
            {
                // If the operator is one that modifies the object (like ++), we want to apply it back
                if (referenceOrValue == ExpressionParseType.GetReference && originalValue is IAssignableReference)
                {
                    ((IAssignableReference)originalValue).CurrentValue = toReturn;
                    toReturn = originalValue;
                }


                return(toReturn);
            }
        }
Example #8
0
        private static object GetObjectFromContainerAndMemberName(object container, string memberName, CodeContext codeContext, ExpressionParseType parseType)
        {
            object foundValue = null;
            bool wasFound = false;

            if (parseType == ExpressionParseType.Evaluate)
            {
                if (container is IAssignableReference)
                {
                    container = ((IAssignableReference)container).CurrentValue;
                }

                GetObjectFromContainerAndNameEvaluate(container, memberName, codeContext, ref foundValue, ref wasFound);
            }
            else
            {
                GetObjectFromContainerAndNameReference(container, memberName, codeContext, ref foundValue, ref wasFound);

            }
            return foundValue;
        }
Example #9
0
        private object EvaluateMemberReferenceExpression(ICSharpCode.NRefactory.CSharp.MemberReferenceExpression memberReferenceExpression, CodeContext codeContext, ExpressionParseType parseType)
        {
            object container = EvaluateExpression(memberReferenceExpression.Target, codeContext, ExpressionParseType.GetReference);
            if (container == null)
            {
                // Couldn't get a member reference, so it could be a variable on an Element, so let's try a non-reference get
                container = EvaluateExpression(memberReferenceExpression.Target, codeContext, ExpressionParseType.Evaluate);
            }
            string memberName = memberReferenceExpression.MemberName;

            object foundValue = GetObjectFromContainerAndMemberName(container, memberName, codeContext, parseType);

            return foundValue;
        }
Example #10
0
        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;
            }
        }
Example #11
0
        internal object EvaluateStatement(Statement statement, CodeContext codeContext, ExpressionParseType parseType)
        {
            if (statement is VariableDeclarationStatement)
            {
                VariableDeclarationStatement vds = statement as VariableDeclarationStatement;

                var astType = vds.Type;

                Type type = TypeManager.GetTypeFromString(astType.GetText());

                var variables = vds.Variables;

                string addedVariable = null;

                foreach (var variable in variables)
                {
                    // right now we only support one declaration per line, will need to
                    // expand this later
                    addedVariable = variable.Name;
                    codeContext.VariableStack.Last().Add(addedVariable, null);
                    break;
                }

                if (parseType == ExpressionParseType.Evaluate)
                {
                    return null; // hasn't been assigned yet
                }
                else
                {
                    StackVariableReference svr = new StackVariableReference();
                    int index;
                    codeContext.GetVariableInformation(addedVariable, out index);
                    svr.StackIndex = index;
                    svr.VariableName = addedVariable;
                    svr.TypeOfReference = type;
                    return svr;
                }
            }
            else
            {
                return null;
            }
        }
Example #12
0
        public object EvaluateExpression(string expression, CodeContext codeContext, ExpressionParseType parseType = ExpressionParseType.Evaluate)
        {
//            

            var result = parser.ParseExpression(expression);
            if (result.ToString() == "Null")
            {
                IEnumerable<Statement> statements = parser.ParseStatements(expression);

                Statement firstStatement = statements.First();

                return EvaluateStatement(firstStatement, codeContext, parseType);
            }
            else
            {
                return EvaluateExpression(result, codeContext, parseType);
            }
        }