示例#1
0
        public override ValueReference Evaluate(EvaluationContext ctx, string expression, object expectedType)
        {
            expression = expression.Trim();

            if (expression.Length > 0 && expression[0] == '?')
            {
                expression = expression.Substring(1).TrimStart();
            }

            if (expression.Length > 3 && expression.StartsWith("var", StringComparison.Ordinal) && char.IsWhiteSpace(expression[3]))
            {
                expression = expression.Substring(4).TrimStart();
                string variable = null;

                for (int n = 0; n < expression.Length; n++)
                {
                    if (!char.IsLetterOrDigit(expression[n]) && expression[n] != '_')
                    {
                        variable = expression.Substring(0, n);
                        if (!expression.Substring(n).TrimStart().StartsWith("=", StringComparison.Ordinal))
                        {
                            variable = null;
                        }
                        break;
                    }

                    if (n == expression.Length - 1)
                    {
                        variable   = expression;
                        expression = null;
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(variable))
                {
                    userVariables[variable] = new UserVariableReference(ctx, variable);
                }

                if (expression == null)
                {
                    return(null);
                }
            }

            expression = ReplaceExceptionTag(expression, ctx.Options.CurrentExceptionTag);

            var expr = new CSharpParser().ParseExpression(expression);

            if (expr == null)
            {
                throw new EvaluatorException("Could not parse expression '{0}'", expression);
            }

            var evaluator = new NRefactoryExpressionEvaluatorVisitor(ctx, expression, expectedType, userVariables);

            return(expr.AcceptVisitor(evaluator));
        }
		public override ValueReference Evaluate (EvaluationContext ctx, string expression, object expectedType)
		{
			expression = expression.TrimStart ();

			if (expression.Length > 0 && expression[0] == '?')
				expression = expression.Substring (1).Trim ();

			if (expression.StartsWith ("var", StringComparison.Ordinal) && char.IsWhiteSpace (expression[3])) {
				expression = expression.Substring (4).Trim (' ', '\t');
				string variable = null;

				for (int n = 0; n < expression.Length; n++) {
					if (!char.IsLetterOrDigit (expression[n]) && expression[n] != '_') {
						variable = expression.Substring (0, n);
						if (!expression.Substring (n).Trim (' ', '\t').StartsWith ("=", StringComparison.Ordinal))
							variable = null;
						break;
					}

					if (n == expression.Length - 1) {
						variable = expression;
						expression = null;
						break;
					}
				}

				if (!string.IsNullOrEmpty (variable))
					userVariables[variable] = new UserVariableReference (ctx, variable);

				if (expression == null)
					return null;
			}

			expression = ReplaceExceptionTag (expression, ctx.Options.CurrentExceptionTag);

			var expr = new CSharpParser ().ParseExpression (expression);
			if (expr == null)
				throw new EvaluatorException ("Could not parse expression '{0}'", expression);

			var evaluator = new NRefactoryExpressionEvaluatorVisitor (ctx, expression, expectedType, userVariables);
			return expr.AcceptVisitor<ValueReference> (evaluator);
		}
        ValueReference Evaluate(IdentifierExpression t)
        {
            var visitor = new NRefactoryExpressionEvaluatorVisitor(ctx, t.Identifier, null, userVariables);

            return(t.AcceptVisitor <ValueReference> (visitor));
        }
        ValueReference Evaluate(ThisReferenceExpression t)
        {
            var visitor = new NRefactoryExpressionEvaluatorVisitor(ctx, "this", null, userVariables);

            return(t.AcceptVisitor <ValueReference> (visitor));
        }