protected virtual AstNode _Optimize(AstNodeExprCast Cast)
        {
            //Console.WriteLine("Optimize.AstNodeExprCast: {0} : {1}", Cast.CastedType, Cast.Expr);

            // Dummy cast
            if (Cast.CastedType == Cast.Expr.Type)
            {
                //Console.WriteLine("Dummy Cast");
                return(Cast.Expr);
            }
            // Double Cast
            else if (Cast.Expr is AstNodeExprCast)
            {
                //Console.WriteLine("Double Cast");
                var FirstCastType  = (Cast.Expr as AstNodeExprCast).CastedType;
                var SecondCastType = Cast.CastedType;
                if (FirstCastType.IsPrimitive && SecondCastType.IsPrimitive)
                {
                    if (AstUtils.GetTypeSize(FirstCastType) >= AstUtils.GetTypeSize(SecondCastType))
                    {
                        return(Optimize(new AstNodeExprCast(Cast.CastedType, (Cast.Expr as AstNodeExprCast).Expr)));
                    }
                }
            }
            // Cast to immediate
            else if (Cast.Expr is AstNodeExprImm)
            {
                //Console.WriteLine("Cast to immediate");
                return(new AstNodeExprImm(AstUtils.CastType((Cast.Expr as AstNodeExprImm).Value, Cast.CastedType)));
            }

            return(Cast);
        }
示例#2
0
        private void CheckCompatibleTypes()
        {
            var compatible = !(AstUtils.GetTypeSize(LeftNode.Type) < AstUtils.GetTypeSize(RightNode.Type));

            if (OperatorRequireBoolOperands(Operator) && LeftNode.Type != typeof(bool) &&
                RightNode.Type != typeof(bool))
            {
                compatible = false;
            }

            if (!compatible)
            {
                throw new Exception(
                          $"Left.Type({LeftNode.Type}) Right.Type({RightNode.Type}) are not compatibles Operator: {Operator}");
            }
        }
        private void CheckCompatibleTypes()
        {
            bool Compatible = true;

            if (AstUtils.GetTypeSize(LeftNode.Type) < AstUtils.GetTypeSize(RightNode.Type))
            {
                Compatible = false;
            }

            if (OperatorRequireBoolOperands(Operator) && (LeftNode.Type != typeof(bool)) && (RightNode.Type != typeof(bool)))
            {
                Compatible = false;
            }

            if (!Compatible)
            {
                throw (new Exception(String.Format("Left.Type({0}) Right.Type({1}) are not compatibles Operator: {2}", LeftNode.Type, RightNode.Type, Operator)));
            }
        }
示例#4
0
        protected virtual AstNode _Optimize(AstNodeExprCast cast)
        {
            //Console.WriteLine("Optimize.AstNodeExprCast: {0} : {1}", Cast.CastedType, Cast.Expr);

            // Dummy cast
            if (cast.CastedType == cast.Expr.Type)
            {
                //Console.WriteLine("Dummy Cast");
                return(cast.Expr);
            }

            // Double Cast
            var expr = cast.Expr as AstNodeExprCast;

            if (expr != null)
            {
                //Console.WriteLine("Double Cast");
                var firstCastType  = expr.CastedType;
                var secondCastType = cast.CastedType;
                if (firstCastType.IsPrimitive && secondCastType.IsPrimitive)
                {
                    if (AstUtils.GetTypeSize(firstCastType) >= AstUtils.GetTypeSize(secondCastType))
                    {
                        return(Optimize(new AstNodeExprCast(cast.CastedType, expr.Expr)));
                    }
                }

                return(cast);
            }

            // Cast to immediate
            var imm = cast.Expr as AstNodeExprImm;

            if (imm != null)
            {
                //Console.WriteLine("Cast to immediate");
                return(new AstNodeExprImm(AstUtils.CastType(imm.Value, cast.CastedType)));
            }

            return(cast);
        }