当前位置: 首页>>代码示例>>C#>>正文


C# Expr.GetType方法代码示例

本文整理汇总了C#中Expr.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Expr.GetType方法的具体用法?C# Expr.GetType怎么用?C# Expr.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Expr的用法示例。


在下文中一共展示了Expr.GetType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenExpr

    private void GenExpr(Expr expr, System.Type expectedType)
    {
        System.Type deliveredType;

        if (expr is StringLiteral)
        {
            deliveredType = typeof(string);
            this.il.Emit(Emit.OpCodes.Ldstr, ((StringLiteral)expr).Value);
        }
        else if (expr is IntLiteral)
        {
            deliveredType = typeof(int);
            this.il.Emit(Emit.OpCodes.Ldc_I4, ((IntLiteral)expr).Value);
        }
        else if (expr is Variable)
        {
            string ident = ((Variable)expr).Ident;
            deliveredType = this.TypeOfExpr(expr);

            if (!this.symbolTable.ContainsKey(ident))
            {
                throw new System.Exception("undeclared variable '" + ident + "'");
            }

            this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[ident]);
        }
        else
        {
            throw new System.Exception("don't know how to generate " + expr.GetType().Name);
        }

        if (deliveredType != expectedType)
        {
            if (deliveredType == typeof(int) &&
                expectedType == typeof(string))
            {
                this.il.Emit(Emit.OpCodes.Box, typeof(int));
                this.il.Emit(Emit.OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
            }
            else
            {
                throw new System.Exception("can't coerce a " + deliveredType.Name + " to a " + expectedType.Name);
            }
        }
    }
开发者ID:santosh-mnrec,项目名称:Compiler,代码行数:45,代码来源:CodeGen.cs

示例2: GenerateLoadToStackForExpr

    private void GenerateLoadToStackForExpr(Expr expr, Type expectedType)
    {
        Type deliveredType;

        if (expr is StringLiteral)
        {
            deliveredType = typeof(string);
            _il.Emit(OpCodes.Ldstr, ((StringLiteral)expr).Value);
        }
        else if (expr is IntLiteral)
        {
            deliveredType = typeof(int);
            _il.Emit(OpCodes.Ldc_I4, ((IntLiteral)expr).Value);
        }
        else if (expr is Variable)
        {
            var ident = ((Variable)expr).Ident;
            deliveredType = expr.GetType();

            if (!SymbolTable.ContainsKey(ident))
            {
                throw new Exception("undeclared variable '" + ident + "'");
            }

            _il.Emit(OpCodes.Ldloc, SymbolTable[ident]);
        }
        else if (expr is ArithExpr)
        {
            var arithExpr = (ArithExpr)expr;
            var left = arithExpr.Left;
            var right = arithExpr.Right;
            deliveredType = expr.GetType();

            GenerateLoadToStackForExpr(left, expectedType);
            GenerateLoadToStackForExpr(right, expectedType);
            switch (arithExpr.Op)
            {
                case ArithOp.Add:
                    _il.Emit(OpCodes.Add);
                    break;
                case ArithOp.Sub:
                    _il.Emit(OpCodes.Sub);
                    break;
                case ArithOp.Mul:
                    _il.Emit(OpCodes.Mul);
                    break;
                case ArithOp.Div:
                    _il.Emit(OpCodes.Div);
                    break;
                default:
                    throw new NotImplementedException("Don't know how to generate il load code for " + arithExpr.Op +
                                                      " yet!");
            }
        }
        else
        {
            throw new Exception("don't know how to generate " + expr.GetType().Name);
        }

        if (deliveredType == expectedType) return;

        if (deliveredType != typeof (int) || expectedType != typeof (string))
            throw new Exception("can't coerce a " + deliveredType.Name + " to a " + expectedType.Name);

        _il.Emit(OpCodes.Box, typeof (int));
        _il.Emit(OpCodes.Callvirt, typeof (object).GetMethod("ToString"));
    }
开发者ID:jbolton209,项目名称:GFNHacking,代码行数:67,代码来源:CodeGen.cs

示例3: TypeOfExpr

 private System.Type TypeOfExpr(Expr expr)
 {
     if (expr is StringLiteral)
     {
         return typeof(string);
     }
     else if (expr is IntLiteral)
     {
         return typeof(int);
     }
     else if (expr is Variable)
     {
         Variable var = (Variable)expr;
         if (this.symbolTable.ContainsKey(var.Ident))
         {
             Emit.LocalBuilder locb = this.symbolTable[var.Ident];
             return locb.LocalType;
         }
         else
         {
             throw new System.Exception("undeclared variable '" + var.Ident + "'");
         }
     }
     else
     {
         throw new System.Exception("don't know how to calculate the type of " + expr.GetType().Name);
     }
 }
开发者ID:santosh-mnrec,项目名称:Compiler,代码行数:28,代码来源:CodeGen.cs

示例4: TypeOfExpr

    private System.Type TypeOfExpr(Expr expr)
    {
        if (expr is StringLiteral)
        {
            return typeof(string);
        }
        else if (expr is IntLiteral)
        {
            return typeof(int);
        }
        else if (expr is Variable)
        {
            Variable var = (Variable)expr;
            if (this.symbolTable.ContainsKey(var.Ident))
            {
                Emit.LocalBuilder locb = this.symbolTable[var.Ident];
                return locb.LocalType;
            }
            else
            {
                throw new System.Exception("variable no declarada '" + var.Ident + "'");
            }
        }
        //*********************
        else if (expr is BinExpr)
        {
            return typeof(int);
        }
        //*********************

        else
        {
            throw new System.Exception("no se puede calcular el tipo: " + expr.GetType().Name);
        }
    }
开发者ID:FANMixco,项目名称:Simple_Compiler,代码行数:35,代码来源:CodeGen.cs

示例5: GenExpr

    private void GenExpr(Expr expr, System.Type expectedType)
    {
        System.Type deliveredType;

        if (expr is StringLiteral)
        {
            deliveredType = typeof(string);
            this.il.Emit(Emit.OpCodes.Ldstr, ((StringLiteral)expr).Value);
        }
        else if (expr is IntLiteral)
        {
            deliveredType = typeof(int);
            this.il.Emit(Emit.OpCodes.Ldc_I4, ((IntLiteral)expr).Value);
        }
        else if (expr is Variable)
        {
            string ident = ((Variable)expr).Ident;
            deliveredType = this.TypeOfExpr(expr);

            if (!this.symbolTable.ContainsKey(ident))
            {
                throw new System.Exception("variable no declarada '" + ident + "'");
            }

            this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[ident]);
        }
        //*********************
        else if (expr is BinExpr)
        {
            Simple_Compiler.PolInv polInv = new Simple_Compiler.PolInv((BinExpr)expr);
            polInv.calcularPolaca();
            foreach (object temp in polInv.Salida)
            {
                if (temp is BinOp)
                    genCodOp((BinOp)temp);
                else
                    genCod((Expr)temp);
            }
            deliveredType = this.TypeOfExpr(expr);
        }

        //*********************

        else
        {
            throw new System.Exception("no se puede generar la expresion " + expr.GetType().Name);
        }

        if (deliveredType != expectedType)
        {
            if (deliveredType == typeof(int) &&
                expectedType == typeof(string))
            {
                this.il.Emit(Emit.OpCodes.Box, typeof(int));
                this.il.Emit(Emit.OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
            }
            else
            {
                throw new System.Exception("no se puede convertir de " + deliveredType.Name + " a un " + expectedType.Name);
            }
        }
    }
开发者ID:FANMixco,项目名称:Simple_Compiler,代码行数:62,代码来源:CodeGen.cs

示例6: TypeOfExpr

 private Type TypeOfExpr(Expr expr, Type[] argtypes)
 {
     if (expr is Literal)
     {
         return ((Literal)expr).Value.GetType();
     }
     else if (expr is Variable)
     {
         Variable var = (Variable)expr;
         string vname = var.Name[0];
         if (symbolTable.ContainsKey(vname))
         {
             if (symbolTable[vname] is LocalBuilder)
             {
                 return ((LocalBuilder)symbolTable[vname]).LocalType;
             }
             else
             {
                 int ixArg = (int)symbolTable[vname];
                 return argtypes[ixArg];
             }
         }
         else
         {
             throw new Exception("undeclared variable '" + vname + "'");
         }
     }
     else if (expr is ArithExpr)
     {
         return TypeOfExpr(((ArithExpr)expr).Left, argtypes);
     }
     else
     {
         throw new Exception("don't know how to calculate the type of " + expr.GetType().Name);
     }
 }
开发者ID:bencz,项目名称:Zinnia-lang,代码行数:36,代码来源:Program.cs

示例7: GenExpr

    private void GenExpr(Expr expr, Type expectedType, ILGenerator il, Type[] argtypes)
    {
        Type deliveredType;

        if (expr is Literal)
        {
            object val = ((Literal)expr).Value;
            deliveredType = val.GetType();
            if (val is string) il.Emit(OpCodes.Ldstr, (string)val);
            else if (val is int) il.Emit(OpCodes.Ldc_I4, (int)val);
            else if (val is double) il.Emit(OpCodes.Ldc_R8, (double)val);
        }
        else if (expr is Variable)
        {
            string ident = ((Variable)expr).Name[0];
            deliveredType = TypeOfExpr(expr, argtypes);
            if (!symbolTable.ContainsKey(ident))
            {
                throw new Exception("undeclared variable '" + ident + "'");
            }
            object var = symbolTable[ident];
            if (var is LocalBuilder) il.Emit(OpCodes.Ldloc, (LocalBuilder)var);
            else if (var is int) il.Emit(OpCodes.Ldarg, (int)var + (fStaticFunc ? 0 : 1));
            else throw new System.Exception("invalid: " + var);
        }
        else if (expr is ArithExpr)
        {
            ArithExpr arithExpr = (ArithExpr)expr;
            GenExpr(arithExpr.Left, TypeOfExpr(arithExpr.Left, argtypes), il, argtypes);
            GenExpr(arithExpr.Right, TypeOfExpr(arithExpr.Right, argtypes), il, argtypes);
            if (arithExpr.Op == "+") il.Emit(OpCodes.Add);
            else if (arithExpr.Op == "-") il.Emit(OpCodes.Sub);
            else if (arithExpr.Op == "*") il.Emit(OpCodes.Mul);
            else if (arithExpr.Op == "/") il.Emit(OpCodes.Div);
            else throw new System.Exception("unsuportOperator '" + arithExpr.Op + "'");
            deliveredType = TypeOfExpr(arithExpr, argtypes);
        }
        else
        {
            throw new System.Exception("don't know how to generate " + expr.GetType().Name);
        }
        if (deliveredType != expectedType)
        {
            if (deliveredType == typeof(int) && expectedType == typeof(string))
            {
                il.Emit(OpCodes.Box, typeof(int));
                il.Emit(OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
            }
            else
            {
                throw new Exception("can't coerce a " + deliveredType.Name + " to a " + expectedType.Name);
            }
        }
    }
开发者ID:bencz,项目名称:Zinnia-lang,代码行数:54,代码来源:Program.cs


注:本文中的Expr.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。