コード例 #1
0
        Func <InvocationContext, object, bool> CompileSwitchCase(CompileContext ctx, SwitchCase switchCase)
        {
            int valueLength = switchCase.TestValues.Count;
            var valueEvals  = new CompiledDelegate[valueLength];

            for (int i = 0; i < valueLength; i++)
            {
                valueEvals[i] = switchCase.TestValues[i].Compile(ctx);
            }
            var bodyEval = switchCase.Body.Compile(ctx);

            return((invoke, value) =>
            {
                bool isMatch = false;
                for (int i = 0; i < valueLength; i++)
                {
                    if (object.Equals(value, valueEvals[i](invoke)))
                    {
                        isMatch = true;
                        break;
                    }
                }
                if (!isMatch)
                {
                    return false;
                }
                bodyEval(invoke);
                return true;
            });
        }
コード例 #2
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var valueEval  = testValue.Compile(ctx);
            int caseLength = cases.Count;
            var caseEvals  = new Func <InvocationContext, object, bool> [caseLength];
            CompiledDelegate defaultBodyEval = null;

            for (int i = 0; i < caseLength; i++)
            {
                caseEvals[i] = CompileSwitchCase(ctx, cases[i]);
            }

            if (defaultBody != null)
            {
                defaultBodyEval = defaultBody.Compile(ctx);
            }

            return((invoke) =>
            {
                var _caseLength = caseLength;
                var _caseEvals = caseEvals;
                var value = valueEval(invoke);
                for (int i = 0; i < _caseLength; i++)
                {
                    if (_caseEvals[i](invoke, value))
                    {
                        return null;
                    }
                }
                defaultBodyEval?.Invoke(invoke);
                return null;
            });
        }
コード例 #3
0
        public override Action <InvocationContext, object> CompileSetValue(CompileContext ctx)
        {
            string name = this.name;

            return((invoke, value) =>
            {
                invoke.SetVariable(name, value);
            });
        }
コード例 #4
0
        //private MemberInfo GetMember()
        //{
        //    if (member != null)
        //        return member;
        //    Type memberType = this.instanceType;
        //    if (memberType == null)
        //    {
        //        if (instance == null)
        //            throw new ExpressionException("Membr Instance Null. Member: {0}".FormatArgs(memberName));
        //        memberType = instance.ValueType;
        //        if (memberType == null)
        //            throw new ExpressionException("Membr Type Null. Member: {0}".FormatArgs(memberName));
        //    }

        //    if (isProperty == null || isProperty.Value)
        //    {
        //        PropertyInfo pInfo = memberType.GetProperty(memberName);
        //        if (pInfo != null)
        //            return pInfo;
        //        if (isProperty != null && pInfo == null) throw new ExpressionException(Resource1.PropertyNotFound.FormatArgs(memberName));
        //    }

        //    if (isProperty == null || !isProperty.Value)
        //    {
        //        FieldInfo fInfo = memberType.GetField(memberName);
        //        if (fInfo != null)
        //            return fInfo;
        //        if (isProperty != null && fInfo == null) throw new ExpressionException(Resource1.FieldNotFound.FormatArgs(memberName));
        //    }

        //    throw new ExpressionException(Resource1.PropertyOrFieldNotFound.FormatArgs(memberName));
        //}

        private CompiledDelegate CompileTarget(CompileContext ctx)
        {
            if (instance == null)
            {
                throw new MemberAccessException("Target Null");
            }
            var targetEval = instance.Compile(ctx);

            return(targetEval);
        }
コード例 #5
0
        public override Action <InvocationContext, object> CompileSetValue(CompileContext ctx)
        {
            CompiledDelegate targetEval;

            var property = member as PropertyInfo;

            if (property != null)
            {
                var setter = property.GetSetMethod();
                if (setter == null)
                {
                    throw new MemberAccessException(Resource1.PropertyNotWritable.FormatArgs(member.Name));
                }

                if (setter.IsStatic)
                {
                    return((invoke, value) =>
                    {
                        setter.Invoke(null, new object[] { value });
                    });
                }
                else
                {
                    targetEval = CompileTarget(ctx);
                    return((invoke, value) =>
                    {
                        setter.Invoke(targetEval(invoke), new object[] { value });
                    });
                }
            }


            var field = member as FieldInfo;

            if (field.IsInitOnly)
            {
                throw new MemberAccessException(Resource1.FieldNotWritable.FormatArgs(member.Name));
            }
            if (field.IsStatic)
            {
                return((invoke, value) =>
                {
                    field.SetValue(null, value);
                });
            }
            else
            {
                targetEval = CompileTarget(ctx);
                return((invoke, value) =>
                {
                    var obj = targetEval(invoke);
                    field.SetValue(obj, value);
                });
            }
        }
コード例 #6
0
 public void Dispose()
 {
     if (isDisposed)
     {
         return;
     }
     current    = origin;
     origin     = null;
     isDisposed = true;
     System.Threading.Monitor.Exit(lockObj);
 }
コード例 #7
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var lhs = operand.Compile(ctx);

            if (isOperandCompiled)
            {
                return(lhs);
            }

            var method = this.method;

            return((invoke) => method.Invoke(null, new object[] { lhs(invoke) }));
        }
コード例 #8
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var lhs    = left.Compile(ctx);
            var rhs    = right.Compile(ctx);
            var method = this.method;

            if (isEvalBefore)
            {
                return((invoke) => method.Invoke(null, new object[] { invoke, lhs, rhs }));
            }

            return((invoke) => method.Invoke(null, new object[] { lhs(invoke), rhs(invoke) }));
        }
コード例 #9
0
        //OperatorInfo OperInfo => CompileContext.Current.GetOperatorInfo(ExpressionType, typeof(object), typeof(Type));

        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var typeOperand = this.typeOperand;
            var lhs         = Operand.Compile(ctx);

            var method = this.Method;

            if (ExpressionType == ExpressionType.Convert && method.GetParameters().Length == 2)
            {
                return((invoke) => System.Convert.ChangeType(lhs(invoke), typeOperand));
            }
            return((invoke) => method.Invoke(null, new object[] { lhs(invoke) }));
        }
コード例 #10
0
        public Action <InvocationContext> CompileInitValue(CompileContext ctx)
        {
            Type   valueType = this.type;
            string name      = this.name;

            if (defaultValue == null)
            {
                return((invoke) => ((ExpressionContext)invoke.Context).AddVariable(valueType, name));
            }

            var valueEval = defaultValue.Compile(ctx);

            return((invoke) => ((ExpressionContext)invoke.Context).AddVariable(valueType, name, valueEval(invoke)));
        }
コード例 #11
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var lhs         = operand.Compile(ctx);
            var typeOperand = this.typeOperand;

            var oper   = OperInfo;
            var method = oper.method;

            switch (ExpressionType)
            {
            case ExpressionType.TypeAs:
                return((invoke) => DefaultOperators.TypeAs(lhs(invoke), typeOperand));

            case ExpressionType.TypeIs:
                return((invoke) => DefaultOperators.TypeIs(lhs(invoke), typeOperand));
            }
            throw new NotImplementedException();
        }
コード例 #12
0
 private CompileContext(CompileContext parent, IExpressionContext context, BlockType blockType, bool isDefault)
 {
     variables        = new Dictionary <string, VariableInfo>();
     variableMetadata = new Dictionary <string, object>();
     this.context     = context;
     this.blockType   = blockType;
     if (parent == null)
     {
         if (isDefault)
         {
             InitDefault();
         }
         else
         {
             this.parent = Default;
         }
     }
     else
     {
         this.parent = parent;
     }
 }
コード例 #13
0
        private Func <FunctionInvoker, Delegate> CompileFunction(CompileContext ctx)
        {
            if (!string.IsNullOrEmpty(name))
            {
                ctx.AddVariable(ValueType, name);
            }

            CompiledDelegate evalBody;

            var arguments = this.arguments;
            var child     = new CompileContext(ctx, BlockType.Method);

            foreach (var arg in arguments)
            {
                child.AddVariable(arg.ValueType, arg.Name);
            }
            evalBody = body.Compile(child);

            return((funcInvoke) =>
            {
                funcInvoke.body = evalBody;
                funcInvoke.arguments = arguments;

                switch (arguments.Count)
                {
                case 0: return Func0(funcInvoke);

                case 1: return Func1(funcInvoke);

                case 2: return Func2(funcInvoke);

                case 3: return Func3(funcInvoke);

                case 4: return Func4(funcInvoke);
                }
                return null;
            });
        }
コード例 #14
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var gotoType = this.gotoType;

            if (gotoType == GotoType.Return)
            {
                var valueEval = value.Compile(ctx);
                return((invoke) =>
                {
                    object returnValue = valueEval(invoke);
                    invoke.SetGoto(GotoType.Return, returnValue);
                    return returnValue;
                });
            }
            else
            {
                return((invoke) =>
                {
                    invoke.SetGoto(gotoType, null);
                    return null;
                });
            }
        }
コード例 #15
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var parent = CompileContext.Current;

            var func = CompileFunction(ctx);

            //if (!string.IsNullOrEmpty(name))
            //{
            //    parent.AddVariable(ValueType, name);

            //}
            return((invoke) =>
            {
                FunctionInvoker funcInvoke = new FunctionInvoker(invoke);

                var del = func(funcInvoke);
                if (!string.IsNullOrEmpty(name))
                {
                    ((ExpressionContext)invoke.Context).AddVariable <Delegate>(name, del);
                }
                return del;
            });
        }
コード例 #16
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var valueEval  = Right.Compile(ctx);
            var setterEval = ((AccessableExpression)Left).CompileSetValue(ctx);

            if (isPre)
            {
                var getterEval = ((AccessableExpression)Left).Compile(ctx);
                return((invoke) =>
                {
                    var preValue = getterEval(invoke);
                    object value = valueEval(invoke);
                    setterEval(invoke, value);
                    return preValue;
                });
            }
            return((invoke) =>
            {
                object value = valueEval(invoke);
                setterEval(invoke, value);
                return value;
            });
        }
コード例 #17
0
        public bool ContainsVariable(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            CompileContext current = this;

            while (current != null)
            {
                if (current.variables != null && current.variables.ContainsKey(name))
                {
                    return(true);
                }
                if (current.context != null && current.context.ContainsVariable(name))
                {
                    return(true);
                }
                current = current.parent;
            }
            return(false);
        }
コード例 #18
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var property = member as PropertyInfo;
            CompiledDelegate targetEval;

            if (property != null)
            {
                var getter = property.GetGetMethod();
                if (getter == null)
                {
                    throw new MemberAccessException(Resource1.PropertyNotReadable.FormatArgs(member.Name));
                }
                if (getter.IsStatic)
                {
                    return((invoke) => getter.Invoke(null, InternalExtensions.EmptyObjects));
                }
                else
                {
                    targetEval = CompileTarget(ctx);
                    return((invoke) => getter.Invoke(targetEval(invoke), InternalExtensions.EmptyObjects));
                }
            }



            var field = member as FieldInfo;

            if (field.IsStatic)
            {
                return((invoke) => field.GetValue(null));
            }
            else
            {
                targetEval = CompileTarget(ctx);
                return((invoke) => field.GetValue(targetEval(invoke)));
            }
        }
コード例 #19
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            var variables = this.variables;

            CompiledDelegate[]           evals;
            Action <InvocationContext>[] evalVars;
            var child = new CompileContext(ctx, BlockType.Block);

            foreach (var variable in variables)
            {
                child.AddVariable(variable.ValueType, variable.Name);
            }

            evals    = expressions.Select(o => o.Compile(child)).ToArray();
            evalVars = variables.Select(o => o.CompileInitValue(child)).ToArray();

            return((invoke) =>
            {
                var childInvoke = invoke.CreateChild(BlockType.Block);

                foreach (var evalVar in evalVars)
                {
                    evalVar(childInvoke);
                }

                foreach (var expr in evals)
                {
                    expr(childInvoke);
                    if (childInvoke.GotoType != GotoType.None)
                    {
                        childInvoke.BubblingGoto();
                        break;
                    }
                }
                return null;
            });
        }
コード例 #20
0
 public override CompiledDelegate Compile(CompileContext ctx)
 {
     return(_Compile(ctx, value));
 }
コード例 #21
0
 public abstract Action <InvocationContext, object> CompileSetValue(CompileContext ctx);
コード例 #22
0
 public CompileContext(CompileContext parent, BlockType blockType = BlockType.Block)
     : this(parent, null, blockType, false)
 {
 }
コード例 #23
0
 public Scope(CompileContext newScope)
 {
     System.Threading.Monitor.Enter(lockObj);
     origin  = current;
     current = newScope;
 }
コード例 #24
0
 public CompileContext(CompileContext parent, IExpressionContext context, BlockType blockType = BlockType.Block)
     : this(parent, context, blockType, false)
 {
 }
コード例 #25
0
 public override CompiledDelegate Compile(CompileContext ctx)
 {
     return(Operand.Compile(ctx));
 }
コード例 #26
0
        public override CompiledDelegate Compile(CompileContext ctx)
        {
            string name = this.name;

            return((invoke) => invoke.GetVariable(name));
        }
コード例 #27
0
 public abstract CompiledDelegate Compile(CompileContext ctx);