Beispiel #1
0
        public override Result GetC3D(Ent e, bool funcion, bool ciclo, bool isObjeto, LinkedList <Error> errores)
        {
            Result result = new Result();

            if (Tipo.IsInt() || Tipo.IsDouble() || Tipo.IsBoolean())
            {
                result.Valor = Valor.ToString();
            }
            else if (Tipo.IsNone())
            {
                result.Valor   = NuevoTemporal();
                result.Codigo += result.Valor + " = 0 - 1;\n";
            }
            else if (Tipo.IsString())
            {
                /*
                 * string tmp;
                 * int cont = 0;
                 * foreach (char c in Valor.ToString().Substring(1, Valor.ToString().Length - 2))
                 * {
                 *  tmp = NuevoTemporal();
                 *  result.Codigo += tmp + " = H + " + cont++ + ";\n";
                 *  result.Codigo += "heap[" + tmp + "] = " + (int)c + ";\n";
                 *
                 * }
                 * tmp = NuevoTemporal();
                 * result.Codigo += tmp + " = H + " + cont++ + ";\n";
                 * result.Codigo += "heap[" + tmp + "] = 0;\n";
                 *
                 * result.Valor = NuevoTemporal();
                 * result.Codigo += result.Valor + " = H;\n";
                 * result.Codigo += "H = H + " + cont + ";\n";
                 */
                result.Valor   = NuevoTemporal();
                result.Codigo += result.Valor + " = H;\n";

                foreach (char c in Valor.ToString().Substring(1, Valor.ToString().Length - 2))
                {
                    result.Codigo += "heap[H] = " + (int)c + ";\n";
                    result.Codigo += "H = H + 1;\n";
                }
                result.Codigo += "heap[H] = 0;\n";
                result.Codigo += "H = H + 1;\n";
            }

            return(result);
        }
Beispiel #2
0
 public void TipoDominante(Tipo op1, Tipo op2)
 {
     if (!op1.IsIndefinido() && !op2.IsIndefinido())
     {
         if (!op1.IsNone() && !op2.IsNone())
         {
             if (op1.IsString() || op2.IsString())
             {
                 if (Op == Operador.SUMA)
                 {
                     Tipo.Tip = Tipo.Type.STRING;
                     return;
                 }
             }
             else //if (!op1.IsBoolean() && !op2.IsBoolean())
             {
                 if (op1.IsDouble() || op2.IsDouble())
                 {
                     Tipo.Tip = Tipo.Type.DOUBLE;
                     return;
                 }
                 else if (op1.IsInt() || op2.IsInt())
                 {
                     Tipo.Tip = Tipo.Type.INT;
                     return;
                 }
                 else if (op1.IsBoolean() || op2.IsBoolean())
                 {
                     Tipo.Tip = Tipo.Type.INT;
                     return;
                 }
             }
         }
     }
     Tipo.Tip = Tipo.Type.INDEFINIDO;
 }
Beispiel #3
0
        public override Result GetC3D(Ent e, bool funcion, bool ciclo, bool isDeclaracion, bool isObjeto, LinkedList <Error> errores)
        {
            if (!isDeclaracion)
            {
                Debugger(e, "Print");
            }

            Result result = new Result();

            if (!isDeclaracion)
            {
                Result rsExp   = Expresion.GetC3D(e, funcion, ciclo, isObjeto, errores);
                Tipo   tipoExp = Expresion.GetTipo();

                if (!tipoExp.IsIndefinido())
                {
                    if (rsExp.Valor != null)
                    {
                        /*verificar objeto y todas las demas*/
                        result.Codigo += rsExp.Codigo;

                        if (tipoExp.IsInt())
                        {
                            if (Expresion is Literal)
                            {
                                string tmp = NuevoTemporal();
                                result.Codigo += tmp + " = " + rsExp.Valor + ";\n";
                                result.Codigo += "print(\"%i\"," + tmp + ");\n";
                            }
                            else
                            {
                                result.Codigo += "print(\"%i\"," + rsExp.Valor + ");\n";
                            }
                        }
                        else if (tipoExp.IsDouble())
                        {
                            if (Expresion is Literal)
                            {
                                string tmp = NuevoTemporal();
                                result.Codigo += tmp + " = " + rsExp.Valor + ";\n";
                                result.Codigo += "print(\"%f\"," + tmp + ");\n";
                            }
                            else
                            {
                                result.Codigo += "print(\"%f\"," + rsExp.Valor + ");\n";
                            }
                        }
                        else if (tipoExp.IsBoolean())
                        {
                            result.EtiquetaV = NuevaEtiqueta();
                            result.EtiquetaF = NuevaEtiqueta();
                            string etqSalida = NuevaEtiqueta();
                            string tmpCiclo  = NuevoTemporal();

                            result.Codigo += "if (" + rsExp.Valor + " == 0) goto " + result.EtiquetaV + ";\n";
                            result.Codigo += "goto " + result.EtiquetaF + ";\n";
                            result.Codigo += result.EtiquetaV + ":\n";
                            result.Codigo += tmpCiclo + " = 70;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += tmpCiclo + " = 97;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += tmpCiclo + " = 108;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += tmpCiclo + " = 115;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += tmpCiclo + " = 101;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += "goto " + etqSalida + ";\n";
                            result.Codigo += result.EtiquetaF + ":\n";
                            result.Codigo += tmpCiclo + " = 84;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += tmpCiclo + " = 114;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += tmpCiclo + " = 117;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += tmpCiclo + " = 101;\n";
                            result.Codigo += "print(\"%c\", " + tmpCiclo + ");\n";
                            result.Codigo += etqSalida + ":\n";
                        }
                        else if (tipoExp.IsString())
                        {
                            result.EtiquetaV = NuevaEtiqueta();
                            result.EtiquetaF = NuevaEtiqueta();
                            string etqCiclo = NuevaEtiqueta();
                            string tmpCiclo = NuevoTemporal();

                            result.Codigo += etqCiclo + ":\n";
                            result.Codigo += tmpCiclo + " = heap[" + rsExp.Valor + "];\n";
                            result.Codigo += "if (" + tmpCiclo + " == 0) goto " + result.EtiquetaV + ";\n";
                            result.Codigo += "goto " + result.EtiquetaF + ";\n";
                            result.Codigo += result.EtiquetaF + ":\n";
                            result.Codigo += "print(\"%c\"," + tmpCiclo + ");\n";
                            result.Codigo += rsExp.Valor + " = " + rsExp.Valor + " + 1;\n";
                            result.Codigo += "goto " + etqCiclo + ";\n";
                            result.Codigo += result.EtiquetaV + ":\n";
                        }

                        string tmpSalto = NuevoTemporal();
                        result.Codigo += tmpSalto + " = 10;\n";
                        result.Codigo += "print(\"%c\"," + tmpSalto + ");\n";
                    }
                    else
                    {
                        if (tipoExp.IsNone())
                        {
                            string tmp = NuevoTemporal();
                            result.Codigo += tmp + " = 78;\n";
                            result.Codigo += "print(\"%c\", " + tmp + ");\n";
                            result.Codigo += tmp + " = 111;\n";
                            result.Codigo += "print(\"%c\", " + tmp + ");\n";
                            result.Codigo += tmp + " = 110;\n";
                            result.Codigo += "print(\"%c\", " + tmp + ");\n";
                            result.Codigo += tmp + " = 101;\n";
                            result.Codigo += "print(\"%c\", " + tmp + ");\n";

                            string tmpSalto = NuevoTemporal();
                            result.Codigo += tmpSalto + " = 10;\n";
                            result.Codigo += "print(\"%c\"," + tmpSalto + ");\n";
                        }
                        else
                        {
                            errores.AddLast(new Error("Semantico", "Valor no encontrado en print", Linea, Columna));
                        }
                    }
                }
            }

            return(result);
        }