本文整理汇总了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);
}
}
}
示例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"));
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
}
示例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);
}
}
示例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);
}
}
}