예제 #1
0
파일: NaryType.cs 프로젝트: Ancestry/DotQL
        public override ExpressionContext CompileBinaryExpression(Compiler compiler, Frame frame, ExpressionContext left, Parse.BinaryExpression expression, BaseType typeHint)
        {
            switch (expression.Operator)
            {
                case Parse.Operator.Dereference:
                    return CompileDereference(compiler, frame, left, expression, typeHint);

                default: return base.CompileBinaryExpression(compiler, frame, left, expression, typeHint);
            }
        }
예제 #2
0
        protected override void EmitUnaryOperator(MethodContext method, Compiler compiler, ExpressionContext inner, Parse.UnaryExpression expression)
        {
            switch (expression.Operator)
            {
                case Parse.Operator.Exists:
                case Parse.Operator.IsNull:
                case Parse.Operator.Not:
                case Parse.Operator.BitwiseNot:
                    base.EmitUnaryOperator(method, compiler, inner, expression);
                    break;

                default: throw NotSupported(expression);
            }
        }
예제 #3
0
        protected override void EmitBinaryOperator(MethodContext method, Compiler compiler, ExpressionContext left, ExpressionContext right, Parse.BinaryExpression expression)
        {
            switch (expression.Operator)
            {
                case Parse.Operator.Power:
                    var intPower = typeof(Runtime.Runtime).GetMethod("IntPower", new[] { typeof(int), typeof(int) });
                    if (intPower == null)
                        throw new NotSupportedException();
                    left.EmitGet(method);
                    right.EmitGet(method);
                    method.IL.EmitCall(OpCodes.Call, intPower, null);
                    break;

                default:
                    base.EmitBinaryOperator(method, compiler, left, right, expression);
                    break;
            }
        }
예제 #4
0
 protected override void EmitBinaryOperator(MethodContext method, Compiler compiler, ExpressionContext left, ExpressionContext right, Parse.BinaryExpression expression)
 {
     switch (expression.Operator)
     {
         case Parse.Operator.BitwiseAnd:
         case Parse.Operator.BitwiseOr:
         case Parse.Operator.BitwiseXor:
         case Parse.Operator.Xor:
         case Parse.Operator.ShiftLeft:
         case Parse.Operator.ShiftRight:
         case Parse.Operator.And:
         case Parse.Operator.Or:
         case Parse.Operator.Equal:
         case Parse.Operator.NotEqual:
         case Parse.Operator.InclusiveGreater:
         case Parse.Operator.InclusiveLess:
         case Parse.Operator.Greater:
         case Parse.Operator.Less:
             base.EmitBinaryOperator(method, compiler, left, right, expression);
             break;
         default: throw NotSupported(expression);
     }
 }
예제 #5
0
파일: BaseType.cs 프로젝트: Ancestry/DotQL
        /// <summary> Overridden to determine what operators a type supports and to change how they are implemented. </summary>
        /// <remarks> Override this rather than CompileBinaryOperator when nothing special is necessary when compiling the right-hand expression. </remarks>
        protected virtual void EmitShortCircuit(MethodContext method, Compiler compiler, Frame frame, ExpressionContext left, ExpressionContext right, Parse.BinaryExpression expression, Type.BaseType typeHint)
        {
            var leftType = left.Type.GetNative(compiler.Emitter);
            var rightType = right.Type.GetNative(compiler.Emitter);
            left.EmitGet(method);
            right.EmitGet(method);
            switch (expression.Operator)
            {
                case Parse.Operator.And:
                    if (!CallClassOp(method, "op_LogicalAnd", leftType, leftType))
                    {
                        var label = method.IL.DefineLabel();
                        method.IL.Emit(OpCodes.Dup);
                        method.IL.Emit(OpCodes.Brfalse_S, label);
                        right.EmitGet(method);
                        method.IL.Emit(OpCodes.And);
                        method.IL.MarkLabel(label);
                    }
                    break;

                case Parse.Operator.Or:
                    if (!CallClassOp(method, "op_LogicalOr", leftType, leftType))
                    {
                        var label = method.IL.DefineLabel();
                        method.IL.Emit(OpCodes.Dup);
                        method.IL.Emit(OpCodes.Brtrue_S, label);
                        right.EmitGet(method);
                        method.IL.Emit(OpCodes.Or);
                        method.IL.MarkLabel(label);
                    }
                    break;

                default: throw new NotSupportedException(String.Format("Operator {0} is not supported.", expression.Operator));
            }
        }
예제 #6
0
파일: BaseType.cs 프로젝트: Ancestry/DotQL
        /// <summary> Overridden to determine what operators a type supports and to change how they are implemented. </summary>
        /// <remarks> Override this rather than CompileBinaryOperator when nothing special is necessary when compiling the right-hand expression. </remarks>
        protected virtual void EmitBinaryOperator(MethodContext method, Compiler compiler, ExpressionContext left, ExpressionContext right, Parse.BinaryExpression expression)
        {
            var leftType = left.Type.GetNative(compiler.Emitter);
            var rightType = right.Type.GetNative(compiler.Emitter);
            left.EmitGet(method);
            right.EmitGet(method);
            switch (expression.Operator)
            {
                case Parse.Operator.Addition:
                    if (!CallClassOp(method, "op_Addition", leftType, rightType))
                        method.IL.Emit(OpCodes.Add);
                    break;
                case Parse.Operator.Subtract:
                    if (!CallClassOp(method, "op_Subtraction", leftType, rightType))
                        method.IL.Emit(OpCodes.Sub);
                    break;
                case Parse.Operator.Multiply:
                    if (!CallClassOp(method, "op_Multiply", leftType, rightType))
                        method.IL.Emit(OpCodes.Mul);
                    break;
                case Parse.Operator.Modulo:
                    if (!CallClassOp(method, "op_Modulus", leftType, rightType))
                        method.IL.Emit(OpCodes.Rem);
                    break;
                case Parse.Operator.Divide:
                    if (!CallClassOp(method, "op_Division", leftType, rightType))
                        method.IL.Emit(OpCodes.Div);
                    break;
                case Parse.Operator.Power:
                    var mathPower = typeof(System.Math).GetMethod("Pow", new[] { left.Type.GetNative(compiler.Emitter), right.Type.GetNative(compiler.Emitter) });
                    if (mathPower == null)
                        throw new NotSupportedException();
                    method.IL.EmitCall(OpCodes.Call, mathPower, null);
                    break;
                case Parse.Operator.BitwiseAnd:
                    if (!CallClassOp(method, "op_BitwiseOr", leftType, rightType))
                        method.IL.Emit(OpCodes.And);
                    break;
                case Parse.Operator.BitwiseOr:
                    if (!CallClassOp(method, "op_Addition", leftType, rightType))
                        method.IL.Emit(OpCodes.Or);
                    break;
                case Parse.Operator.BitwiseXor:
                case Parse.Operator.Xor:
                    if (!CallClassOp(method, "op_ExclusiveOr", leftType, rightType))
                        method.IL.Emit(OpCodes.Xor);
                    break;
                case Parse.Operator.ShiftLeft:
                    if (!CallClassOp(method, "op_LeftShift", leftType, rightType))
                        method.IL.Emit(OpCodes.Shl);
                    break;
                case Parse.Operator.ShiftRight:
                    if (!CallClassOp(method, "op_RightShift", leftType, rightType))
                        method.IL.Emit(OpCodes.Shr);
                    break;

                case Parse.Operator.Equal:
                    if (!CallClassOp(method, "op_Equality", leftType, rightType))
                        method.IL.Emit(OpCodes.Ceq);
                    break;
                case Parse.Operator.NotEqual:
                    if (!CallClassOp(method, "op_Inequality", leftType, rightType))
                    {
                        method.IL.Emit(OpCodes.Ceq);
                        method.IL.Emit(OpCodes.Ldc_I4_0);
                        method.IL.Emit(OpCodes.Ceq);
                    }
                    break;
                case Parse.Operator.InclusiveGreater:
                    if (!CallClassOp(method, "op_GreaterThanOrEqual", leftType, rightType))
                    {
                        method.IL.Emit(OpCodes.Clt);
                        method.IL.Emit(OpCodes.Ldc_I4_0);
                        method.IL.Emit(OpCodes.Ceq);
                    }
                    break;
                case Parse.Operator.InclusiveLess:
                    if (!CallClassOp(method, "op_LessThanOrEqual", leftType, rightType))
                    {
                        method.IL.Emit(OpCodes.Cgt);
                        method.IL.Emit(OpCodes.Ldc_I4_0);
                        method.IL.Emit(OpCodes.Ceq);
                    }
                    break;
                case Parse.Operator.Greater:
                    if (!CallClassOp(method, "op_GreaterThan", leftType, rightType))
                        method.IL.Emit(OpCodes.Cgt);
                    break;
                case Parse.Operator.Less:
                    if (!CallClassOp(method, "op_LessThan", leftType, rightType))
                        method.IL.Emit(OpCodes.Clt);
                    break;

                default: throw NotSupported(expression);
            }
        }
예제 #7
0
파일: BaseType.cs 프로젝트: Ancestry/DotQL
 public virtual ExpressionContext Convert(ExpressionContext expression, BaseType target)
 {
     throw new NotImplementedException(String.Format("Conversion from {0} to {1} is not supported.", expression.Type, target));
 }
예제 #8
0
파일: BaseType.cs 프로젝트: Ancestry/DotQL
        public virtual ExpressionContext CompileUnaryExpression(Compiler compiler, Frame frame, ExpressionContext inner, Parse.UnaryExpression expression, Type.BaseType typeHint)
        {
            switch (expression.Operator)
            {
                case Parse.Operator.Exists:
                case Parse.Operator.IsNull:
                    return
                        new ExpressionContext
                        (
                            expression,
                            SystemTypes.Boolean,
                            inner.Characteristics,
                            m => { EmitUnaryOperator(m, compiler, inner, expression); }
                        );

                case Parse.Operator.Negate:
                case Parse.Operator.Not:
                case Parse.Operator.BitwiseNot:
                case Parse.Operator.Successor:
                case Parse.Operator.Predicessor:
                    return
                        new ExpressionContext
                        (
                            expression,
                            inner.Type,
                            inner.Characteristics,
                            m => { EmitUnaryOperator(m, compiler, inner, expression); }
                        );

                default: throw NotSupported(expression);
            }
        }
예제 #9
0
파일: BaseType.cs 프로젝트: Ancestry/DotQL
 public virtual ExpressionContext CompileExtractExpression(Compiler compiler, Frame frame, ExpressionContext left, Parse.ExtractExpression expression, BaseType typeHint)
 {
     throw new CompilerException(expression, CompilerException.Codes.OperatorNotSupported, Parse.Keywords.Extract, GetType());
 }
예제 #10
0
파일: TupleType.cs 프로젝트: Ancestry/DotQL
        private ExpressionContext CompileDereference(Compiler compiler, Frame frame, ExpressionContext left, Parse.BinaryExpression expression, Type.BaseType typeHint)
        {
            var local = compiler.AddFrame(frame, expression);
            var native = left.ActualNative(compiler.Emitter);

            LocalBuilder valueVariable = null;

            // Create symbol for each tuple member
            foreach (var a in ((TupleType)left.Type).Attributes)
            {
                var field = native.GetField(a.Key.ToString(), BindingFlags.Public | BindingFlags.Instance);
                local.Add(expression, a.Key, field);
                compiler.ContextsBySymbol.Add
                (
                    field,
                    new ExpressionContext
                    (
                        null,
                        a.Value,
                        Characteristic.Default,
                        m =>
                        {
                            m.IL.Emit(OpCodes.Ldloc, valueVariable);
                            m.IL.Emit(OpCodes.Ldfld, field);
                        }
                    )
                );
            }

            var right = compiler.CompileExpression(local, expression.Right, typeHint);

            return
                new ExpressionContext
                (
                    expression,
                    right.Type,
                    Compiler.MergeCharacteristics(left.Characteristics, right.Characteristics),
                    m =>
                    {
                        m.IL.BeginScope();

                        left.EmitGet(m);
                        valueVariable = m.DeclareLocal(expression.Right, native, "value");
                        m.IL.Emit(OpCodes.Stloc, valueVariable);

                        right.EmitGet(m);

                        m.IL.EndScope();
                    }
                );
        }
예제 #11
0
파일: TupleType.cs 프로젝트: Ancestry/DotQL
        protected override void EmitBinaryOperator(MethodContext method, Compiler compiler, ExpressionContext left, ExpressionContext right, Parse.BinaryExpression expression)
        {
            switch (expression.Operator)
            {
                case Parse.Operator.Equal:
                case Parse.Operator.NotEqual:
                    base.EmitBinaryOperator(method, compiler, left, right, expression);
                    break;

                //// TODO: Tuple union
                //case Parse.Operator.BitwiseOr:

                default: throw NotSupported(expression);
            }
        }
예제 #12
0
파일: NaryType.cs 프로젝트: Ancestry/DotQL
        // Restriction
        public override ExpressionContext CompileExtractExpression(Compiler compiler, Frame frame, ExpressionContext left, Parse.ExtractExpression expression, BaseType typeHint)
        {
            var memberType = ((NaryType)left.Type).Of;
            var memberNative = left.NativeType ?? memberType.GetNative(compiler.Emitter);

            var local = compiler.AddFrame(frame, expression);

            // Prepare index and value symbols
            var indexSymbol = PrepareValueIndexContext(compiler, left, expression.Condition, memberType, memberNative, local);

            // Compile condition
            var condition = compiler.CompileExpression(local, expression.Condition, SystemTypes.Boolean);
            if (!(condition.Type is BooleanType))
                throw new CompilerException(expression.Condition, CompilerException.Codes.IncorrectType, condition.Type, "Boolean");

            var indexReferenced = compiler.References.ContainsKey(indexSymbol);

            return
                new ExpressionContext
                (
                    expression,
                    left.Type,
                    Compiler.MergeCharacteristics(left.Characteristics, condition.Characteristics),
                    m =>
                    {
                        // Create a new private method for the condition
                        var typeBuilder = (TypeBuilder)m.Builder.DeclaringType;
                        var innerMethod =
                            new MethodContext
                            (
                                typeBuilder.DefineMethod
                                (
                                    "Where" + expression.GetHashCode(),
                                    MethodAttributes.Private | MethodAttributes.Static,
                                    typeof(bool),	// return type
                                    indexReferenced ? new System.Type[] { memberNative, typeof(int) } : new System.Type[] { memberNative }	// param types
                                )
                            );
                        condition.EmitGet(innerMethod);
                        innerMethod.IL.Emit(OpCodes.Ret);

                        left.EmitGet(m);

                        // TODO: Force ordering to left if Set and index is referenced

                        // Instantiate a delegate pointing to the new method
                        m.IL.Emit(OpCodes.Ldnull);				// instance
                        m.IL.Emit(OpCodes.Ldftn, innerMethod.Builder);	// method
                        var funcType =
                            indexReferenced
                                ? System.Linq.Expressions.Expression.GetFuncType(memberNative, typeof(int), typeof(bool))
                                : System.Linq.Expressions.Expression.GetFuncType(memberNative, typeof(bool));
                        m.IL.Emit
                        (
                            OpCodes.Newobj,
                            funcType.GetConstructor(new[] { typeof(object), typeof(IntPtr) })
                        );

                        funcType = indexReferenced ? typeof(Func<ReflectionUtility.T, int, bool>) : typeof(Func<ReflectionUtility.T, bool>);
                        var where = typeof(System.Linq.Enumerable).GetMethodExt("Where", new System.Type[] { typeof(IEnumerable<ReflectionUtility.T>), funcType });
                        where = where.MakeGenericMethod(memberNative);

                        m.IL.EmitCall(OpCodes.Call, where, null);
                    }
                );
        }
예제 #13
0
파일: NaryType.cs 프로젝트: Ancestry/DotQL
        private static object PrepareValueIndexContext(Compiler compiler, ExpressionContext left, Parse.Statement statement, BaseType memberType, System.Type memberNative, Frame local)
        {
            // Register value argument
            var valueSymbol = new Object();
            local.Add(statement, Name.FromComponents(Parse.ReservedWords.Value), valueSymbol);
            compiler.ContextsBySymbol.Add
            (
                valueSymbol,
                new ExpressionContext
                (
                    null,
                    memberType,
                    left.Characteristics,
                    m => { m.IL.Emit(OpCodes.Ldarg_0); }
                )
            );

            // Register index argument
            var indexSymbol = new Object();
            local.Add(statement, Name.FromComponents(Parse.ReservedWords.Index), indexSymbol);
            compiler.ContextsBySymbol.Add
            (
                indexSymbol,
                new ExpressionContext
                (
                    null,
                    SystemTypes.Int32,
                    Characteristic.Default,
                    m => { m.IL.Emit(OpCodes.Ldarg_1); }
                )
            );

            // If the members are tuples, declare locals for each field
            if (memberType is TupleType)
            {
                var tupleType = (TupleType)memberType;
                foreach (var attribute in tupleType.Attributes)
                {
                    local.Add(attribute.Key.ToID(), attribute);
                    compiler.ContextsBySymbol.Add
                    (
                        attribute,
                        new ExpressionContext
                        (
                            null,
                            attribute.Value,
                            Characteristic.Default,
                            m =>
                            {
                                m.IL.Emit(OpCodes.Ldarg_0);	// value
                                m.IL.Emit(OpCodes.Ldfld, memberNative.GetField(attribute.Key.ToString()));
                            }
                        )
                    );
                }

                // TODO: Add references
            }
            return indexSymbol;
        }
예제 #14
0
파일: NaryType.cs 프로젝트: Ancestry/DotQL
 protected override void EmitUnaryOperator(MethodContext method, Compiler compiler, ExpressionContext inner, Parse.UnaryExpression expression)
 {
     switch (expression.Operator)
     {
         case Parse.Operator.Exists:
             method.IL.EmitCall(OpCodes.Callvirt, typeof(ICollection<>).MakeGenericType(inner.Type.GetNative(compiler.Emitter)).GetProperty("Count").GetGetMethod(), null);
             method.IL.Emit(OpCodes.Ldc_I4_0);
             method.IL.Emit(OpCodes.Cgt);
             break;
         case Parse.Operator.IsNull:
             method.IL.Emit(OpCodes.Pop);
             method.IL.Emit(OpCodes.Ldc_I4_0);
             break;
         default: throw NotSupported(expression);
     }
 }
예제 #15
0
파일: NaryType.cs 프로젝트: Ancestry/DotQL
        protected virtual ExpressionContext CompileDereference(Compiler compiler, Frame frame, ExpressionContext left, Parse.BinaryExpression expression, Type.BaseType typeHint)
        {
            var memberType = ((NaryType)left.Type).Of;
            var memberNative = left.NativeType ?? memberType.GetNative(compiler.Emitter);

            var local = compiler.AddFrame(frame, expression);

            // Prepare index and value symbols
            var indexSymbol = PrepareValueIndexContext(compiler, left, expression.Right, memberType, memberNative, local);

            // Compile selection
            var selection = compiler.CompileExpression(local, expression.Right);

            var indexReferenced = compiler.References.ContainsKey(indexSymbol);

            return
                new ExpressionContext
                (
                    expression,
                    new ListType(selection.Type),
                    Compiler.MergeCharacteristics(left.Characteristics, selection.Characteristics),
                    m =>
                    {
                        // Create a new private method for the condition
                        var typeBuilder = (TypeBuilder)m.Builder.DeclaringType;
                        var innerMethod =
                            new MethodContext
                            (
                                typeBuilder.DefineMethod
                                (
                                    "Select" + expression.GetHashCode(),
                                    MethodAttributes.Private | MethodAttributes.Static,
                                    selection.ActualNative(compiler.Emitter),	// temporary return type
                                    indexReferenced ? new System.Type[] { memberNative, typeof(int) } : new System.Type[] { memberNative }	// param types
                                )
                            );
                        selection.EmitGet(innerMethod);
                        innerMethod.IL.Emit(OpCodes.Ret);

                        left.EmitGet(m);

                        // TODO: Force ordering of Left if a set and index is referenced

                        // Instantiate a delegate pointing to the new method
                        m.IL.Emit(OpCodes.Ldnull);				// instance
                        m.IL.Emit(OpCodes.Ldftn, innerMethod.Builder);	// method
                        var funcType =
                            indexReferenced
                                ? System.Linq.Expressions.Expression.GetFuncType(memberNative, typeof(int), innerMethod.Builder.ReturnType)
                                : System.Linq.Expressions.Expression.GetFuncType(memberNative, innerMethod.Builder.ReturnType);
                        m.IL.Emit
                        (
                            OpCodes.Newobj,
                            funcType.GetConstructor(new[] { typeof(object), typeof(IntPtr) })
                        );

                        funcType = indexReferenced ? typeof(Func<ReflectionUtility.T, int, ReflectionUtility.T>) : typeof(Func<ReflectionUtility.T, ReflectionUtility.T>);
                        var select =
                            typeof(Enumerable).GetMethodExt
                            (
                                "Select",
                                new System.Type[] { typeof(IEnumerable<ReflectionUtility.T>), funcType }
                            );
                        select = select.MakeGenericMethod(memberNative, innerMethod.Builder.ReturnType);

                        m.IL.EmitCall(OpCodes.Call, select, null);
                    }
                );
        }
예제 #16
0
파일: BaseType.cs 프로젝트: Ancestry/DotQL
        /// <summary> Overridden to determine what operators a type supports and to change how they are implemented. </summary>
        protected virtual void EmitUnaryOperator(MethodContext method, Compiler compiler, ExpressionContext inner, Parse.UnaryExpression expression)
        {
            var innerType = inner.Type.GetNative(compiler.Emitter);
            inner.EmitGet(method);
            switch (expression.Operator)
            {
                case Parse.Operator.Exists:
                    method.IL.Emit(OpCodes.Pop);
                    method.IL.Emit(OpCodes.Ldc_I4_1);	// true
                    break;
                case Parse.Operator.IsNull:
                    method.IL.Emit(OpCodes.Pop);
                    method.IL.Emit(OpCodes.Ldc_I4_0);	// false
                    break;
                case Parse.Operator.Negate:
                    if (!CallClassOp(method, "op_UnaryNegation", innerType))
                        method.IL.Emit(OpCodes.Neg);
                    break;
                case Parse.Operator.Not:
                    if (!CallClassOp(method, "op_Negation", innerType))
                        method.IL.Emit(OpCodes.Not);
                    break;
                case Parse.Operator.BitwiseNot:
                    if (!CallClassOp(method, "op_OnesComplement", innerType))
                        method.IL.Emit(OpCodes.Not);
                    break;
                case Parse.Operator.Successor:
                    method.IL.Emit(OpCodes.Ldc_I4_1);
                    method.IL.Emit(OpCodes.Add);
                    break;
                case Parse.Operator.Predicessor:
                    method.IL.Emit(OpCodes.Ldc_I4_1);
                    method.IL.Emit(OpCodes.Sub);
                    break;

                default: throw NotSupported(expression);
            }
        }
예제 #17
0
파일: BaseType.cs 프로젝트: Ancestry/DotQL
        public virtual ExpressionContext CompileBinaryExpression(Compiler compiler, Frame frame, ExpressionContext left, Parse.BinaryExpression expression, Type.BaseType typeHint)
        {
            switch (expression.Operator)
            {
                case Parse.Operator.Addition:
                case Parse.Operator.Subtract:
                case Parse.Operator.Multiply:
                case Parse.Operator.Modulo:
                case Parse.Operator.Divide:
                case Parse.Operator.Power:

                case Parse.Operator.BitwiseAnd:
                case Parse.Operator.BitwiseOr:
                case Parse.Operator.BitwiseXor:
                case Parse.Operator.Xor:
                case Parse.Operator.ShiftLeft:
                case Parse.Operator.ShiftRight:
                    {
                        var right = compiler.CompileExpression(frame, expression.Right, typeHint);
                        return
                            new ExpressionContext
                            (
                                expression,
                                left.Type,
                                Compiler.MergeCharacteristics(left.Characteristics, right.Characteristics),
                                m => { EmitBinaryOperator(m, compiler, left, right, expression); }
                            );
                    }

                case Parse.Operator.And:
                case Parse.Operator.Or:
                    {
                        var right = compiler.CompileExpression(frame, expression.Right, typeHint);
                        return
                            new ExpressionContext
                            (
                                expression,
                                left.Type,
                                Compiler.MergeCharacteristics(left.Characteristics, right.Characteristics),
                                m => { EmitShortCircuit(m, compiler, frame, left, right, expression, typeHint); }
                            );
                    }

                case Parse.Operator.Equal:
                case Parse.Operator.NotEqual:
                case Parse.Operator.InclusiveGreater:
                case Parse.Operator.InclusiveLess:
                case Parse.Operator.Greater:
                case Parse.Operator.Less:
                    {
                        var right = compiler.CompileExpression(frame, expression.Right);	// (no type hint)
                        return
                            new ExpressionContext
                            (
                                expression,
                                SystemTypes.Boolean,
                                Compiler.MergeCharacteristics(left.Characteristics, right.Characteristics),
                                m => { EmitBinaryOperator(m, compiler, left, right, expression); }
                            );
                    }

                default: throw NotSupported(expression);
            }
        }
예제 #18
0
        protected override void EmitBinaryOperator(MethodContext method, Compiler compiler, ExpressionContext left, ExpressionContext right, Parse.BinaryExpression expression)
        {
            switch (expression.Operator)
            {
                case Parse.Operator.Addition:
                    left.EmitGet(method);
                    right.EmitGet(method);
                    method.IL.EmitCall(OpCodes.Call, ReflectionUtility.StringConcat, null);
                    break;

                case Parse.Operator.Equal:
                    base.EmitBinaryOperator(method, compiler, left, right, expression);
                    break;
                case Parse.Operator.NotEqual:
                    left.EmitGet(method);
                    right.EmitGet(method);
                    method.IL.EmitCall(OpCodes.Call, ReflectionUtility.StringCompare, null);
                    method.IL.Emit(OpCodes.Ldc_I4_0);
                    method.IL.Emit(OpCodes.Ceq);
                    // Not
                    method.IL.Emit(OpCodes.Ldc_I4_0);
                    method.IL.Emit(OpCodes.Ceq);
                    break;
                case Parse.Operator.InclusiveGreater:
                    left.EmitGet(method);
                    right.EmitGet(method);
                    method.IL.EmitCall(OpCodes.Call, ReflectionUtility.StringCompare, null);
                    method.IL.Emit(OpCodes.Ldc_I4_0);
                    method.IL.Emit(OpCodes.Clt);
                    // Not
                    method.IL.Emit(OpCodes.Ldc_I4_0);
                    method.IL.Emit(OpCodes.Ceq);
                    break;
                case Parse.Operator.InclusiveLess:
                    left.EmitGet(method);
                    right.EmitGet(method);
                    method.IL.EmitCall(OpCodes.Call, ReflectionUtility.StringCompare, null);
                    method.IL.Emit(OpCodes.Ldc_I4_0);
                    method.IL.Emit(OpCodes.Cgt);
                    // Not
                    method.IL.Emit(OpCodes.Ldc_I4_0);
                    method.IL.Emit(OpCodes.Ceq);
                    break;
                case Parse.Operator.Greater:
                    left.EmitGet(method);
                    right.EmitGet(method);
                    method.IL.EmitCall(OpCodes.Call, ReflectionUtility.StringCompare, null);
                    method.IL.Emit(OpCodes.Ldc_I4_0);
                    method.IL.Emit(OpCodes.Cgt);
                    break;
                case Parse.Operator.Less:
                    left.EmitGet(method);
                    right.EmitGet(method);
                    method.IL.EmitCall(OpCodes.Call, ReflectionUtility.StringCompare, null);
                    method.IL.Emit(OpCodes.Ldc_I4_0);
                    method.IL.Emit(OpCodes.Clt);
                    break;

                default: throw NotSupported(expression);
            }
        }
예제 #19
0
파일: VoidType.cs 프로젝트: Ancestry/DotQL
 public override ExpressionContext CompileUnaryExpression(Compiler compiler, Frame frame, ExpressionContext inner, Parse.UnaryExpression expression, BaseType typeHint)
 {
     throw NotSupported(expression);
 }
예제 #20
0
        // Restriction
        public override ExpressionContext CompileExtractExpression(Compiler compiler, Frame frame, ExpressionContext left, Parse.ExtractExpression expression, BaseType typeHint)
        {
            var local = compiler.AddFrame(frame, expression);
            var alreadyOptional = left.Type is OptionalType;
            var memberNative = left.Type.GetNative(compiler.Emitter);
            var resultType = alreadyOptional ? left.Type : new OptionalType(left.Type);
            var resultNative = resultType.GetNative(compiler.Emitter);

            // Register value symbol
            LocalBuilder valueLocal = null;
            var localSymbol = new Object();
            local.Add(expression.Condition, Name.FromComponents(Parse.ReservedWords.Value), localSymbol);
            compiler.ContextsBySymbol.Add
            (
                localSymbol,
                new ExpressionContext
                (
                    null,
                    left.Type,
                    left.Characteristics,
                    m => { m.IL.Emit(OpCodes.Ldloc, valueLocal); }
                )
            );

            var condition = compiler.CompileExpression(local, expression.Condition, SystemTypes.Boolean);
            if (!(condition.Type is BooleanType))
                throw new CompilerException(expression.Condition, CompilerException.Codes.IncorrectType, condition.Type, "Boolean");

            return
                new ExpressionContext
                (
                    expression,
                    resultType,
                    Compiler.MergeCharacteristics(left.Characteristics, condition.Characteristics),
                    m =>
                    {
                        var nullLabel = m.IL.DefineLabel();
                        var endLabel = m.IL.DefineLabel();

                        // Register value argument
                        valueLocal = m.DeclareLocal(expression, memberNative, Parse.ReservedWords.Value);
                        left.EmitGet(m);
                        m.IL.Emit(OpCodes.Stloc, valueLocal);

                        condition.EmitGet(m);
                        m.IL.Emit(OpCodes.Brfalse, nullLabel);

                        // Passed condition
                        if (!alreadyOptional && memberNative.IsValueType)
                        {
                            var optionalLocal = m.DeclareLocal(expression, resultNative, Parse.ReservedWords.Value);
                            m.IL.Emit(OpCodes.Ldloca, optionalLocal);
                            m.IL.Emit(OpCodes.Ldloc, valueLocal);
                            m.IL.Emit(OpCodes.Call, resultNative.GetConstructor(new System.Type[] { left.Type.GetNative(compiler.Emitter) }));
                            m.IL.Emit(OpCodes.Ldloc, optionalLocal);
                        }
                        else
                            m.IL.Emit(OpCodes.Ldloc, valueLocal);
                        m.IL.Emit(OpCodes.Br, endLabel);

                        // Failed condition
                        m.IL.MarkLabel(nullLabel);
                        if (!alreadyOptional && memberNative.IsValueType)
                        {
                            var optionalLocal = m.DeclareLocal(expression, resultNative, Parse.ReservedWords.Value);
                            m.IL.Emit(OpCodes.Ldloca, optionalLocal);
                            m.IL.Emit(OpCodes.Initobj, resultNative);
                            m.IL.Emit(OpCodes.Ldloc, optionalLocal);
                        }
                        else if (alreadyOptional && memberNative.IsValueType)
                        {
                            m.IL.Emit(OpCodes.Ldloca, valueLocal);
                            m.IL.Emit(OpCodes.Initobj, resultNative);
                            m.IL.Emit(OpCodes.Ldloc, valueLocal);
                        }
                        else
                            m.IL.Emit(OpCodes.Ldnull);

                        m.IL.MarkLabel(endLabel);
                    }
                );
        }