예제 #1
0
        public object compilar(Entorno ent, Errores errores)
        {
            //CREO MIS UTILIDADES
            //ASIGNACION INICIAL
            AsignacionId target            = new AsignacionId(id, null, linea, columna);
            Asignacion   asignacionInicial = new Asignacion(target, primero, linea, columna);
            //ACTUALIZACION DE LA VARIABLE Y CONDICION A EVALUAR
            Asignacion actualizarVariable;
            Primitivo  valorFAD = new Primitivo(Tipos.INTEGER, "1", linea, columna);
            Expresion  condicion;
            AccessId   left = new AccessId(id, null, linea, columna);

            if (fad.Equals("to"))
            {
                Less menorIgual = new Less(true, left, segundo, linea, columna);
                condicion = (Expresion)menorIgual;
                Suma suma = new Suma(left, valorFAD, linea, columna);
                actualizarVariable = new Asignacion(target, (Expresion)suma, linea, columna);
            }
            else   //downto
            {
                Greater mayorIgual = new Greater(true, left, segundo, linea, columna);
                condicion = (Expresion)mayorIgual;
                Resta resta = new Resta(left, valorFAD, linea, columna);
                actualizarVariable = new Asignacion(target, (Expresion)resta, linea, columna);
            }
            //INICIO LA COMPILACION
            try
            {
                Generator generator = Generator.getInstance();
                generator.addComment("Inicia FOR");
                asignacionInicial.compilar(ent, errores);
                string lblFor = generator.newLabel();
                generator.addLabel(lblFor);
                Retorno retcondicion = condicion.compilar(ent);
                if (retcondicion.type.tipo == Tipos.BOOLEAN)
                {
                    ent.fors.AddLast(new IteFor(true, actualizarVariable));
                    ent.ybreak.AddLast(retcondicion.falseLabel);
                    ent.ycontinue.AddLast(lblFor);
                    generator.addLabel(retcondicion.trueLabel);
                    foreach (Instruccion sentencia in sentencias)
                    {
                        sentencia.compilar(ent, errores);
                    }
                    actualizarVariable.compilar(ent, errores);
                    generator.addGoto(lblFor);
                    generator.addLabel(retcondicion.falseLabel);
                    ent.fors.RemoveLast();
                    ent.ybreak.RemoveLast();
                    ent.ycontinue.RemoveLast();
                    generator.addComment("Finaliza FOR");
                }
                else
                {
                    throw new Error("Semántico", "La condicion a evaluar en el for no es de tipo Boolean", ent.obtenerAmbito(), linea, columna);
                }
            } catch (Error ex)
            {
                errores.agregarError(ex);
            }
            return(null);
        }
예제 #2
0
        public object compilar(Entorno ent, Errores errores)
        {
            try
            {
                Generator generator = Generator.getInstance();
                generator.addComment("Inicia Asignacion");

                if (ent.ambito.Equals("FUNCTION"))
                {
                    SimboloFunction symFunc = ent.actualFunc;
                    if (symFunc != null)
                    {
                        AsignacionId mytarget = (AsignacionId)this.target;
                        if (mytarget.id.ToLower().Equals(symFunc.id.ToLower())) //Es un Return
                        {
                            Return @return = new Return(this.value, linea, columna);
                            @return.isAsignacion = true;
                            return(@return.compilar(ent, errores));
                        }
                    }
                }

                Retorno target = this.target.compilar(ent);
                Retorno value  = this.value.compilar(ent);

                Simbolo symbol = target.symbol;
                if (symbol.isConst)
                {
                    throw new Error("Semántico", "No se puede cambiar el valor de una constante", ent.obtenerAmbito(), linea, columna);
                }
                if (!sameType(target.type, value.type))
                {
                    throw new Error("Semántico", "No coincide el tipo de dato de la variable con el tipo de valor a asignar", ent.obtenerAmbito(), linea, columna);
                }
                if (symbol.isHeap == false) //ES GLOBAL
                {
                    if (target.type.tipo == Tipos.BOOLEAN)
                    {
                        //Mi modificacion
                        if (value.getValue().Equals("1"))
                        {
                            generator.addGoto(value.trueLabel);
                        }
                        else
                        {
                            generator.addGoto(value.falseLabel);
                        }

                        string templabel = generator.newLabel();
                        generator.addLabel(value.trueLabel);
                        generator.addSetStack(target.getValue(), "1");
                        generator.addGoto(templabel);
                        generator.addLabel(value.falseLabel);
                        generator.addSetStack(target.getValue(), "0");
                        generator.addLabel(templabel);
                    }
                    else
                    {
                        generator.addSetStack(target.getValue(), value.getValue());
                    }
                }
                else if (symbol.isHeap)
                {
                    if (target.type.tipo == Tipos.BOOLEAN)
                    {
                        //Mi modificacion
                        if (value.getValue().Equals("1"))
                        {
                            generator.addGoto(value.trueLabel);
                        }
                        else
                        {
                            generator.addGoto(value.falseLabel);
                        }

                        string templabel = generator.newLabel();
                        generator.addLabel(value.trueLabel);
                        generator.addSetHeap(target.getValue(), "1");
                        generator.addGoto(templabel);
                        generator.addLabel(value.falseLabel);
                        generator.addSetHeap(target.getValue(), "0");
                        generator.addLabel(templabel);
                    }
                    else
                    {
                        generator.addSetHeap(target.getValue(), value.getValue());
                    }
                }
                generator.addComment("Finaliza Asignacion");
            } catch (Error ex)
            {
                errores.agregarError(ex);
            }
            return(null);
        }