示例#1
0
    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);
            }
        }
    }
示例#2
0
    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);
            }
        }
    }