Exemplo n.º 1
0
 internal static DynamicExpression Make(Type returnType, Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2, Expression arg3) {
     if (returnType == typeof(object)) {
         return new DynamicExpression4(delegateType, binder, arg0, arg1, arg2, arg3);
     } else {
         return new TypedDynamicExpression4(returnType, delegateType, binder, arg0, arg1, arg2, arg3);
     }
 }
Exemplo n.º 2
0
 public void SetCurrentLine(string line)
 {
     lineNumber++;
     currentLine = line;
     Value = null;
     if(currentLine.Count() > 0){
         localExpression = new Expression(currentLine);
         if (localExpression.NumericalEvaluation != null) {
             Value = localExpression.NumericalEvaluation;
             OutputString = localExpression.OutputString;
         } else {
             OutputString = "\""+line+"\" " + localExpression.OutputString;
         }
     }
 }
Exemplo n.º 3
0
 public void ExpressionConstructorTest()
 {
     Expression target = new Expression("3+4*334 - te 33z 34.3.4,() || ,");
     Tokens tokens = new Tokens(new System.Collections.Generic.List<Token>(){
         new Token("3", TokenType.numberLiteral),
         new Token("+", TokenType.operatorOrPunctuation),
         new Token("+4", TokenType.numberLiteral),
         new Token("*", TokenType.operatorOrPunctuation),
         new Token("334", TokenType.numberLiteral),
         new Token("-", TokenType.atomicOperatorOrPunctuation),
         new Token("te", TokenType.identifier),
         new Token("33z", TokenType.identifier),
         new Token("34.3.4", TokenType.numberLiteral),
         new Token(",", TokenType.operatorOrPunctuation),
         new Token("(", TokenType.atomicOperatorOrPunctuation),
         new Token(")", TokenType.atomicOperatorOrPunctuation),
         new Token("||", TokenType.operatorOrPunctuation),
         new Token(",", TokenType.operatorOrPunctuation)
     });
     for(int i=0; i < tokens.AsList().Count; i++){
         Assert.AreEqual<string>(tokens.AsList()[i].TokenString, target.Tokens.AsList()[i].TokenString);
         Assert.AreEqual<TokenType>(tokens.AsList()[i].TokenType, target.Tokens.AsList()[i].TokenType);
     }
 }
Exemplo n.º 4
0
 internal TypedDynamicExpression1(Type retType, Type delegateType, CallSiteBinder binder, Expression arg0)
     : base(delegateType, binder, arg0) {
     _retType = retType;
 }
Exemplo n.º 5
0
        internal override DynamicExpression Rewrite(Expression[] args) {
            Debug.Assert(args.Length == ((IArgumentProvider)this).ArgumentCount);

            return Expression.MakeDynamic(DelegateType, Binder, args);
        }
Exemplo n.º 6
0
        private object _arg0;               // storage for the 1st argument or a readonly collection.  See IArgumentProvider for more info.

        internal DynamicExpression1(Type delegateType, CallSiteBinder binder, Expression arg0)
            : base(delegateType, binder) {
            _arg0 = arg0;
        }
 Expression IDynamicExpression.Rewrite(Expression[] args)
 {
     return this.Rewrite(args);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Makes a copy of this node replacing the args with the provided values.  The 
 /// number of the args needs to match the number of the current block.
 /// 
 /// This helper is provided to allow re-writing of nodes to not depend on the specific optimized
 /// subclass of DynamicExpression which is being used. 
 /// </summary>
 internal virtual DynamicExpression Rewrite(Expression[] args) {
     throw ContractUtils.Unreachable;
 }
Exemplo n.º 9
0
        internal override DynamicExpression Rewrite(Expression[] args) {
            Debug.Assert(args.Length == 4);

            return Expression.MakeDynamic(DelegateType, Binder, args[0], args[1], args[2], args[3]);
        }
Exemplo n.º 10
0
 public Opera_nd_tor(Expression expression)
 {
     this.expression = expression;
 }
Exemplo n.º 11
0
        public Expression Reduce()
        {
            int level = 0;
            while (list.Count > 1)
            {
                Require.True(level <= precedence.highestLevel);
                // needs to be a for loop, the list gets modified in action
                //forward
                Opera_nd_tor item;
                for (int j = 0; j < 2; ++j)
                {
                    bool leftToRight = j == 0;
                    int i;
                    if (leftToRight)
                        i = -1;
                    else
                        i = list.Count;                        
                    while (true)
                    {
                        if (leftToRight)
                        {
                            i++;
                            if (i == list.Count)
                                break;
                        }
                        else
                        {
                            if (i == 0)
                                break;
                            i--;
                        }
                        item = list[i];
                        if (item.IsOperator && (item.Precedence == level) && (item.LeftToRight == leftToRight))
                        {
                            switch (item.Kind)
                            {
                                case OperatorKind.Infix:
                                    {
                                        i = i - 1;
                                        Expression left = list[i].Expression;
                                        Expression right = list[i + 2].Expression;
                                        list.RemoveRange(i, 3);
                                        list.Insert(i, item.ReduceInfix(left, right));
                                        break;
                                    }
                                case OperatorKind.Prefix:
                                    {
                                        Expression right = list[i + 1].Expression;
                                        list.RemoveRange(i, 2);
                                        list.Insert(i, item.ReducePrefix(right));
                                        break;
                                    }
                                case OperatorKind.Postfix:
                                    {
                                        i = i - 1;
                                        Expression left = list[i].Expression;
                                        list.RemoveRange(i, 2);
                                        list.Insert(i, item.ReducePostFix(left));
                                        break;
                                    }
                                default:
                                    {
                                        Require.NotCalled();
                                        break;
                                    }
                            }
                        }
                    }
                }

                level++;
            }
            Expression result = list[0].Expression;
            list.Clear();
            return result;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" /> and four arguments.
        /// </summary>
        /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param>
        /// <param name="binder">The runtime binder for the dynamic operation.</param>
        /// <param name="arg0">The first argument to the dynamic operation.</param>
        /// <param name="arg1">The second argument to the dynamic operation.</param>
        /// <param name="arg2">The third argument to the dynamic operation.</param>
        /// <param name="arg3">The fourth argument to the dynamic operation.</param>
        /// <returns>
        /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to
        /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the
        /// <see cref="DynamicExpression.DelegateType">DelegateType</see>,
        /// <see cref="DynamicExpression.Binder">Binder</see>, and
        /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values.
        /// </returns>
        public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2, Expression arg3) {
            ContractUtils.RequiresNotNull(delegateType, "delegateType");
            ContractUtils.RequiresNotNull(binder, "binder");
            if (!delegateType.IsSubclassOf(typeof(MulticastDelegate))) throw Error.TypeMustBeDerivedFromSystemDelegate();

            var method = GetValidMethodForDynamic(delegateType);
            var parameters = method.GetParametersCached();

            ValidateArgumentCount(method, ExpressionType.Dynamic, 5, parameters);
            ValidateDynamicArgument(arg0);
            ValidateOneArgument(method, ExpressionType.Dynamic, arg0, parameters[1]);
            ValidateDynamicArgument(arg1);
            ValidateOneArgument(method, ExpressionType.Dynamic, arg1, parameters[2]);
            ValidateDynamicArgument(arg2);
            ValidateOneArgument(method, ExpressionType.Dynamic, arg2, parameters[3]);
            ValidateDynamicArgument(arg3);
            ValidateOneArgument(method, ExpressionType.Dynamic, arg3, parameters[4]);

            return DynamicExpression.Make(method.GetReturnType(), delegateType, binder, arg0, arg1, arg2, arg3);
        }
Exemplo n.º 13
0
 public void AddOperator(string token, Expression expression, OperatorKind kind)
 {
     list.Add(new Opera_nd_tor(precedence.precedence[ExpressionPrecedence.operatorName(kind, token)], expression));
 }
Exemplo n.º 14
0
 public void AddOperand(Expression expression)
 {
     list.Add(new Opera_nd_tor(expression));
 }
Exemplo n.º 15
0
 public Opera_nd_tor ReducePostFix(Expression left)
 {
     return new Opera_nd_tor(operatorDef.Handler(null, left, postfixExpression));
 }
Exemplo n.º 16
0
 public Opera_nd_tor ReducePrefix(Expression right)
 {
     return new Opera_nd_tor(operatorDef.Handler(token, null, right));
 }
Exemplo n.º 17
0
 public Opera_nd_tor ReduceInfix(Expression left, Expression right)
 {
     return new Opera_nd_tor(operatorDef.Handler(token, left, right));
 }
Exemplo n.º 18
0
        private readonly Expression _arg1;      // storage for the 2nd argument

        internal DynamicExpression2(Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1)
            : base(delegateType, binder) {
            _arg0 = arg0;
            _arg1 = arg1;
        }
Exemplo n.º 19
0
        /// <summary>
        /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" /> and one argument.
        /// </summary>
        /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param>
        /// <param name="binder">The runtime binder for the dynamic operation.</param>
        /// <param name="arg0">The argument to the dynamic operation.</param>
        /// <returns>
        /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to
        /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the
        /// <see cref="DynamicExpression.DelegateType">DelegateType</see>,
        /// <see cref="DynamicExpression.Binder">Binder</see>, and
        /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values.
        /// </returns>
        public static DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, Expression arg0) {
            ContractUtils.RequiresNotNull(delegateType, "delegatType");
            ContractUtils.RequiresNotNull(binder, "binder");
            ContractUtils.Requires(delegateType.IsSubclassOf(typeof(Delegate)), "delegateType", Strings.TypeMustBeDerivedFromSystemDelegate);

            var method = GetValidMethodForDynamic(delegateType);
            var parameters = method.GetParametersCached();

            ValidateArgumentCount(method, ExpressionType.Dynamic, 2, parameters);
            ValidateDynamicArgument(arg0);
            ValidateOneArgument(method, ExpressionType.Dynamic, arg0, parameters[1]);

            return DynamicExpression.Make(method.GetReturnType(), delegateType, binder, arg0);
        }
Exemplo n.º 20
0
        private readonly Expression _arg1, _arg2, _arg3;    // storage for the 2nd - 4th arguments

        internal DynamicExpression4(Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2, Expression arg3)
            : base(delegateType, binder) {
            _arg0 = arg0;
            _arg1 = arg1;
            _arg2 = arg2;
            _arg3 = arg3;
        }
Exemplo n.º 21
0
 public void ExpressionConstructorTest1()
 {
     string textOfCurrentLine = "4+-1(3)(-3)3--3";
     Expression target = new Expression(textOfCurrentLine);
     //Assert.AreEqual(target.OutputString, "34");
 }
Exemplo n.º 22
0
 internal TypedDynamicExpression4(Type retType, Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2, Expression arg3)
     : base(delegateType, binder, arg0, arg1, arg2, arg3) {
     _retType = retType;
 }
Exemplo n.º 23
0
 public Opera_nd_tor(OperatorDefinition operatorDef, Expression expression)
 {
     Require.Assigned(operatorDef);
     this.operatorDef = operatorDef;
     this.postfixExpression = expression;
 }
Exemplo n.º 24
0
        /// <summary>
        /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />.
        /// </summary>
        /// <param name="binder">The runtime binder for the dynamic operation.</param>
        /// <param name="returnType">The result type of the dynamic expression.</param>
        /// <param name="arg0">The first argument to the dynamic operation.</param>
        /// <param name="arg1">The second argument to the dynamic operation.</param>
        /// <param name="arg2">The third argument to the dynamic operation.</param>
        /// <param name="arg3">The fourth argument to the dynamic operation.</param>
        /// <returns>
        /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to
        /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the
        /// <see cref="DynamicExpression.Binder">Binder</see> and
        /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values.
        /// </returns>
        /// <remarks>
        /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the
        /// result will be inferred from the types of the arguments and the specified return type.
        /// </remarks>
        public static DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1, Expression arg2, Expression arg3) {
            ContractUtils.RequiresNotNull(binder, "binder");
            ValidateDynamicArgument(arg0);
            ValidateDynamicArgument(arg1);
            ValidateDynamicArgument(arg2);
            ValidateDynamicArgument(arg3);

            DelegateHelpers.TypeInfo info = DelegateHelpers.GetNextTypeInfo(
                returnType,
                DelegateHelpers.GetNextTypeInfo(
                    arg3.Type,
                    DelegateHelpers.GetNextTypeInfo(
                        arg2.Type,
                        DelegateHelpers.GetNextTypeInfo(
                            arg1.Type,
                            DelegateHelpers.GetNextTypeInfo(
                                arg0.Type,
                                DelegateHelpers.NextTypeInfo(typeof(CallSite))
                            )
                        )
                    )
                )
            );

            Type delegateType = info.DelegateType;
            if (delegateType == null) {
                delegateType = info.MakeDelegateType(returnType, arg0, arg1, arg2, arg3);
            }

            return DynamicExpression.Make(returnType, delegateType, binder, arg0, arg1, arg2, arg3);
        }
 /// <summary>
 /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" />.
 /// </summary>
 /// <param name="binder">The runtime binder for the dynamic operation.</param>
 /// <param name="returnType">The result type of the dynamic expression.</param>
 /// <param name="arg0">The first argument to the dynamic operation.</param>
 /// <param name="arg1">The second argument to the dynamic operation.</param>
 /// <param name="arg2">The third argument to the dynamic operation.</param>
 /// <param name="arg3">The fourth argument to the dynamic operation.</param>
 /// <returns>
 /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to
 /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the
 /// <see cref="DynamicExpression.Binder">Binder</see> and
 /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values.
 /// </returns>
 /// <remarks>
 /// The <see cref="DynamicExpression.DelegateType">DelegateType</see> property of the
 /// result will be inferred from the types of the arguments and the specified return type.
 /// </remarks>
 public static new DynamicExpression Dynamic(CallSiteBinder binder, Type returnType, Expression arg0, Expression arg1, Expression arg2, Expression arg3) {
     return Expression.Dynamic(binder, returnType, arg0, arg1, arg2, arg3);
 }
Exemplo n.º 26
0
 private static void ValidateDynamicArgument(Expression arg) {
     RequiresCanRead(arg, "arguments");
     var type = arg.Type;
     ContractUtils.RequiresNotNull(type, "type");
     TypeUtils.ValidateType(type);
     if (type == typeof(void)) throw Error.ArgumentTypeCannotBeVoid();
 }
 /// <summary>
 /// Creates a <see cref="DynamicExpression" /> that represents a dynamic operation bound by the provided <see cref="CallSiteBinder" /> and four arguments.
 /// </summary>
 /// <param name="delegateType">The type of the delegate used by the <see cref="CallSite" />.</param>
 /// <param name="binder">The runtime binder for the dynamic operation.</param>
 /// <param name="arg0">The first argument to the dynamic operation.</param>
 /// <param name="arg1">The second argument to the dynamic operation.</param>
 /// <param name="arg2">The third argument to the dynamic operation.</param>
 /// <param name="arg3">The fourth argument to the dynamic operation.</param>
 /// <returns>
 /// A <see cref="DynamicExpression" /> that has <see cref="NodeType" /> equal to
 /// <see cref="ExpressionType.Dynamic">Dynamic</see> and has the
 /// <see cref="DynamicExpression.DelegateType">DelegateType</see>,
 /// <see cref="DynamicExpression.Binder">Binder</see>, and
 /// <see cref="DynamicExpression.Arguments">Arguments</see> set to the specified values.
 /// </returns>
 public static new DynamicExpression MakeDynamic(Type delegateType, CallSiteBinder binder, Expression arg0, Expression arg1, Expression arg2, Expression arg3) {
     return Expression.MakeDynamic(delegateType, binder, arg0, arg1, arg2, arg3);
 }