Example #1
0
        public DB_Type GetExpType(Expression.Exp exp, Expression.Exp outer = null)
        {
            if (exp is Metadata.Expression.ConstExp)
            {
                Metadata.Expression.ConstExp e = exp as Metadata.Expression.ConstExp;

                int int_v;
                if (int.TryParse(e.value, out int_v))
                {
                    return(GetType("System.Int32"));
                }

                long long_v;
                if (long.TryParse(e.value, out long_v))
                {
                    return(GetType("System.Int64"));
                }

                bool b_v;
                if (bool.TryParse(e.value, out b_v))
                {
                    return(GetType("System.Boolean"));
                }

                float single_v;
                if (float.TryParse(e.value, out single_v))
                {
                    return(GetType("System.Single"));
                }

                double double_v;
                if (double.TryParse(e.value, out double_v))
                {
                    return(GetType("System.Double"));
                }

                if (e.value == "null")
                {
                    return(GetType("System.Object"));
                }

                if (e.value.StartsWith("'"))
                {
                    return(GetType("System.Char"));
                }

                return(GetType("System.String"));
            }

            else if (exp is Metadata.Expression.FieldExp)
            {
                Metadata.Expression.FieldExp e           = exp as Metadata.Expression.FieldExp;
                Metadata.DB_Type             caller_type = GetExpType(e.Caller);

                if (outer is MethodExp)
                {
                    MethodExp methodExp = outer as MethodExp;
                    List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>();
                    foreach (var t in methodExp.Args)
                    {
                        argTypes.Add(GetExpType(t));
                    }
                    Metadata.DB_Member member = caller_type.FindMethod(e.Name, argTypes, this);
                    return(GetType(member.typeName));
                }
                else
                {
                    DB_Member field = caller_type.FindField(e.Name, this);
                    if (field == null)
                    {
                        field = caller_type.FindProperty(e.Name, this);
                    }

                    if (field == null)
                    {
                        return(null);
                    }
                    return(GetType(field.typeName));
                }
            }

            else if (exp is Metadata.Expression.IndifierExp)
            {
                Metadata.Expression.IndifierExp e = exp as Metadata.Expression.IndifierExp;
                IndifierInfo info = GetIndifierInfo(e.Name);
                //if(outer is MethodExp)
                //{
                //    MethodExp methodExp = outer as MethodExp;
                //    List<Metadata.DB_Type> argTypes = new List<Metadata.DB_Type>();
                //    foreach (var t in methodExp.Args)
                //    {
                //        argTypes.Add(GetExpType(t));
                //    }
                //    foreach(var m in info.methods)
                //    {
                //        if(m.MatchingParameter(argTypes,this))
                //        {
                //            return GetType(m.typeName);
                //        }
                //    }

                //}
                //else
                {
                    return(info.type);
                }
            }

            else if (exp is Metadata.Expression.MethodExp)
            {
                Metadata.Expression.MethodExp me = exp as Metadata.Expression.MethodExp;

                List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>();
                foreach (var t in me.Args)
                {
                    argTypes.Add(GetExpType(t));
                }

                if (me.Caller is FieldExp)
                {
                    FieldExp         fe          = me.Caller as FieldExp;
                    Metadata.DB_Type caller_type = GetExpType(fe.Caller, fe);
                    return(GetType(caller_type.FindMethod(fe.Name, argTypes, this).typeName));
                }
                else if (me.Caller is IndifierExp)
                {
                    IndifierExp  ie   = me.Caller as IndifierExp;
                    IndifierInfo info = GetIndifierInfo(ie.Name);

                    if (info.is_method)
                    {
                        foreach (var m in info.methods)
                        {
                            if (m.MatchingParameter(argTypes, this))
                            {
                                return(GetType(m.typeName));
                            }
                        }
                    }
                    else
                    {
                        return(info.type);
                    }
                }
            }

            else if (exp is Metadata.Expression.ObjectCreateExp)
            {
                Metadata.Expression.ObjectCreateExp e = exp as Metadata.Expression.ObjectCreateExp;
                return(GetType(e.Type));
            }

            else if (exp is Metadata.Expression.BaseExp)
            {
                return(GetType(currentType.base_type));
            }

            else if (exp is Metadata.Expression.ThisExp)
            {
                return(currentType);
            }

            else if (exp is Metadata.Expression.BinaryExpressionSyntax)
            {
                Metadata.Expression.BinaryExpressionSyntax me = exp as Metadata.Expression.BinaryExpressionSyntax;
                Metadata.DB_Type        caller_type           = GetExpType(me.Left);
                List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>();
                argTypes.Add(GetExpType(me.Right));

                Metadata.DB_Member member = caller_type.FindMethod(me.OperatorToken, argTypes, this);
                return(GetType(member.typeName));
            }

            else if (exp is Metadata.Expression.PostfixUnaryExpressionSyntax)
            {
                Metadata.Expression.PostfixUnaryExpressionSyntax me = exp as Metadata.Expression.PostfixUnaryExpressionSyntax;
                Metadata.DB_Type        caller_type = GetExpType(me.Operand);
                List <Metadata.DB_Type> argTypes    = new List <Metadata.DB_Type>();
                argTypes.Add(caller_type);

                Metadata.DB_Member member = caller_type.FindMethod(me.OperatorToken, argTypes, this);
                return(GetType(member.typeName));
            }

            else if (exp is Metadata.Expression.PrefixUnaryExpressionSyntax)
            {
                Metadata.Expression.PrefixUnaryExpressionSyntax me = exp as Metadata.Expression.PrefixUnaryExpressionSyntax;
                Metadata.DB_Type        caller_type = GetExpType(me.Operand);
                List <Metadata.DB_Type> argTypes    = new List <Metadata.DB_Type>();
                argTypes.Add(caller_type);

                Metadata.DB_Member member = caller_type.FindMethod(me.OperatorToken, argTypes, this);
                return(GetType(member.typeName));
            }

            else if (exp is Metadata.Expression.ParenthesizedExpressionSyntax)
            {
                Metadata.Expression.ParenthesizedExpressionSyntax pes = exp as Metadata.Expression.ParenthesizedExpressionSyntax;
                return(GetExpType(pes.exp));
            }
            else if (exp is Metadata.Expression.ElementAccessExp)
            {
                Metadata.Expression.ElementAccessExp eae = exp as Metadata.Expression.ElementAccessExp;
                Metadata.DB_Type        caller_type      = GetExpType(eae.exp);
                List <Metadata.DB_Type> argTypes         = new List <Metadata.DB_Type>();
                foreach (var a in eae.args)
                {
                    argTypes.Add(GetExpType(a));
                }


                return(GetType(caller_type.FindProperty("Index", this).type));

                //string methodName = "";

                //if(outer is Metadata.Expression.AssignmentExpressionSyntax && ((Metadata.Expression.AssignmentExpressionSyntax)outer).Left == exp)
                //{
                //    methodName = "set_Index";
                //}
                //else
                //{
                //    methodName = "get_Index";
                //}

                //Metadata.DB_Member member = caller_type.FindMethod(methodName, argTypes, this);
                //return GetType(member.typeName);
            }
            else
            {
                Console.Error.WriteLine("无法确定表达式类型 " + exp.GetType().Name);
            }
            return(null);
        }
Example #2
0
 public void VisitExp(IMethodVisitor visitor, DB_Type type, DB_Member method, DB_StatementSyntax statement, Expression.Exp exp, Expression.Exp outer)
 {
     if (exp is Expression.IndifierExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.IndifierExp, outer);
     }
     else if (exp is Expression.FieldExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.FieldExp, outer);
     }
     else if (exp is Expression.ObjectCreateExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.ObjectCreateExp, outer);
     }
     else if (exp is Expression.ConstExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.ConstExp, outer);
     }
     else if (exp is Expression.MethodExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.MethodExp, outer);
     }
     else if (exp is Expression.ParenthesizedExpressionSyntax)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.ParenthesizedExpressionSyntax, outer);
     }
     else if (exp is Expression.ThisExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.ThisExp, outer);
     }
     else if (exp is Expression.BaseExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.BaseExp, outer);
     }
     else if (exp is Expression.AssignmentExpressionSyntax)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.AssignmentExpressionSyntax, outer);
     }
     else if (exp is Expression.BinaryExpressionSyntax)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.BinaryExpressionSyntax, outer);
     }
     else if (exp is Expression.PostfixUnaryExpressionSyntax)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.PostfixUnaryExpressionSyntax, outer);
     }
     else if (exp is Expression.PrefixUnaryExpressionSyntax)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.PrefixUnaryExpressionSyntax, outer);
     }
     else if (exp is Expression.ThrowExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.ThrowExp, outer);
     }
     else if (exp is Expression.ElementAccessExp)
     {
         visitor.VisitExp(type, method, statement, exp as Expression.ElementAccessExp, outer);
     }
     else
     {
         throw new NotSupportedException("不支持的表达式 " + exp.GetType().ToString());
     }
 }