Esempio n. 1
0
        // ******************** CREACION DE FUNCIONES

        public static Nodo Funciones(ParseTreeNode entrada)
        {
            int linea   = entrada.ChildNodes[1].Span.Location.Line;
            int columna = entrada.ChildNodes[1].Span.Location.Column;

            string nombre_funcion = entrada.ChildNodes[1].Token.Text;

            LinkedList <Nodo> parametros = Obtener_parametros(entrada.ChildNodes[3]);

            Objeto.TipoObjeto tipo_retorno   = Variables.getTipo(entrada.ChildNodes[6]);
            string            nombre_retorno = Variables.Nombre_del_tipo(entrada.ChildNodes[6]);


            if (entrada.ChildNodes.Count == 10)
            {
                LinkedList <Nodo> variables = new LinkedList <Nodo>();
                Variables.Variables_Objeto(entrada.ChildNodes[8], variables);
                Instrucciones_Funcion(entrada.ChildNodes[9], variables);

                return(new CreacionFuncion(linea, columna, nombre_funcion, variables, parametros, nombre_retorno, tipo_retorno, Variables.getObjeto(tipo_retorno)));
            }
            else if (entrada.ChildNodes.Count == 9)
            {
                LinkedList <Nodo> variables = new LinkedList <Nodo>();
                // chilnode[6] instrucciones
                Instrucciones_Funcion(entrada.ChildNodes[8], variables);
                return(new CreacionFuncion(linea, columna, nombre_funcion, variables, parametros, nombre_retorno, tipo_retorno, Variables.getObjeto(tipo_retorno)));
            }
            return(null);
        }
 public Declaracion_Arreglo(int linea, int columna, Nodo[] dim, string nombre, Objeto.TipoObjeto tipo, string nombre_tipo) : base(linea, columna)
 {
     this.dimensiones = dim;
     this.nombre      = nombre;
     this.tipo        = tipo;
     this.nombre_tipo = nombre_tipo;
 }
Esempio n. 3
0
 public Declaracion(int linea, int columna, string[] ids, Nodo exp, Objeto.TipoObjeto tipo, string nombre) : base(linea, columna)
 {
     this.ids         = ids;
     this.expresion   = exp;
     this.tipo        = tipo;
     this.nombre_tipo = nombre;
 }
Esempio n. 4
0
        public override Objeto execute(Entorno entorno)
        {
            Objeto res_left  = left.execute(entorno);
            Objeto res_right = right.execute(entorno);

            Objeto.TipoObjeto tipo_dominante = TablaTipo.tabla[res_left.getTipo().GetHashCode(), res_right.getTipo().GetHashCode()];

            if (tipo_dominante == Objeto.TipoObjeto.BOOLEAN)
            {
                if (bool.Parse(res_left.getValor().ToString()) == true && bool.Parse(res_right.getValor().ToString()) == true)
                {
                    return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, true));
                }
                else
                {
                    return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, false));
                }
            }
            else
            {
                Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                        "No se puede operar AND en tipos" + res_left.getTipo().ToString() + "con" + res_right.getTipo().ToString());
                Maestra.getInstancia.addError(error);
            }

            throw new Exception("La operacion logica OR solo puede hacerse con valores booleanos");
        }
Esempio n. 5
0
        public override Objeto execute(Entorno entorno)
        {
            Objeto res_left  = left.execute(entorno);  //izq
            Objeto res_right = right.execute(entorno); // der

            Objeto.TipoObjeto tipo_dominante = TablaTipo.tabla[res_left.getTipo().GetHashCode(), res_right.getTipo().GetHashCode()];

            if (tipo_dominante == Objeto.TipoObjeto.INTEGER)
            {
                return(new Primitivo(tipo_dominante, int.Parse(res_left.getValor().ToString()) + int.Parse(res_right.getValor().ToString())));
            }
            else if (tipo_dominante == Objeto.TipoObjeto.STRING)
            {
                return(new Primitivo(tipo_dominante, res_left.getValor().ToString() + res_right.getValor().ToString()));
            }
            else if (tipo_dominante == Objeto.TipoObjeto.REAL)
            {
                return(new Primitivo(tipo_dominante, double.Parse(res_left.getValor().ToString()) + double.Parse(res_right.getValor().ToString())));
            }
            else
            {
                Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                        "No se pueden sumar tipos de datos " + res_left.getTipo().ToString() + " con " + res_right.getTipo().ToString());
                Maestra.getInstancia.addError(error);
            }

            throw new Exception("No se pueden sumar tipos de datos " + res_left.getTipo().ToString() + " con " + res_right.getTipo().ToString());
        }
Esempio n. 6
0
        public override Objeto execute(Entorno entorno)
        {
            Objeto res_left  = left.execute(entorno);
            Objeto res_right = right.execute(entorno);

            Objeto.TipoObjeto tipo_dominante = TablaTipo.tabla[res_left.getTipo().GetHashCode(), res_right.getTipo().GetHashCode()];

            if (tipo_dominante == Objeto.TipoObjeto.INTEGER || tipo_dominante == Objeto.TipoObjeto.REAL)
            {
                if (double.Parse(res_right.getValor().ToString()) != 0)
                {
                    return(new Primitivo(Objeto.TipoObjeto.REAL, double.Parse(res_left.getValor().ToString()) / double.Parse(res_right.getValor().ToString())));
                }
                else
                {
                    Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                            "No se puede realizar la division entre 0");
                    Maestra.getInstancia.addError(error);
                    throw new Exception("No se puede realizar la division entre 0");
                }
            }
            else
            {
                Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                        "No se pueden dividir tipos de datos" + res_left.getTipo().ToString() + "con" + res_right.getTipo().ToString());
                Maestra.getInstancia.addError(error);
            }

            throw new Exception("No se pueden dividir tipos de datos" + res_left.getTipo().ToString() + "con" + res_right.getTipo().ToString());
        }
Esempio n. 7
0
 public Creacion_Parametro(int linea, int columna, string id, Objeto.TipoObjeto tipoObjeto, string nombre_tipo,
                           Parametro.Tipo_Parametro parametro, Nodo exp) : base(linea, columna)
 {
     this.id          = id;
     this.tipo        = tipoObjeto;
     this.nombre_tipo = nombre_tipo;
     this.parametro   = parametro;
     this.expresion   = exp;
 }
Esempio n. 8
0
        public static void Parametros_valor(ParseTreeNode entrada, LinkedList <Nodo> salida)
        {
            int linea   = entrada.Span.Location.Line;
            int columna = entrada.Span.Location.Column;

            Objeto.TipoObjeto tipo        = Variables.getTipo(entrada.ChildNodes[2]);
            string            nombre_type = Variables.Nombre_del_tipo(entrada.ChildNodes[2]);

            getId(entrada.ChildNodes[0], tipo, nombre_type, Parametro.Tipo_Parametro.VALOR, salida);
        }
Esempio n. 9
0
 public CreacionFuncion(int linea, int columna, string nombre, LinkedList <Nodo> instru, LinkedList <Nodo> para,
                        string tipo_retorno, Objeto.TipoObjeto tipo, Nodo exp) : base(linea, columna)
 {
     this.nombre_funcion = nombre;
     this.instrucciones  = instru;
     this.parametros     = para;
     this.tipo_retorno   = tipo_retorno;
     this.tipo           = tipo;
     this.expresion      = exp;
 }
Esempio n. 10
0
        public static void getId(ParseTreeNode entrada, Objeto.TipoObjeto tipo, string nombre_tipo,
                                 Parametro.Tipo_Parametro tipo_Parametro, LinkedList <Nodo> salida)
        {
            for (int i = 0; i < entrada.ChildNodes.Count; i++)
            {
                int    linea   = entrada.ChildNodes[i].Span.Location.Line;
                int    columna = entrada.ChildNodes[i].Span.Location.Column;
                string nombre  = entrada.ChildNodes[i].Token.Text;

                salida.AddLast(new Creacion_Parametro(linea, columna, nombre, tipo, nombre_tipo, tipo_Parametro, Variables.getObjeto(tipo)));
            }
        }
Esempio n. 11
0
        public static void Parametros_referencia(ParseTreeNode entrada, LinkedList <Nodo> salida)
        {
            ParseTreeNode referencia = entrada.ChildNodes[1];

            int linea   = referencia.Span.Location.Line;
            int columna = referencia.Span.Location.Column;

            Objeto.TipoObjeto tipo        = Variables.getTipo(referencia.ChildNodes[2]);
            string            nombre_type = Variables.Nombre_del_tipo(referencia.ChildNodes[2]);

            getId(referencia.ChildNodes[0], tipo, nombre_type, Parametro.Tipo_Parametro.REFERENCIA, salida);
        }
Esempio n. 12
0
        public override Objeto execute(Entorno entorno)
        {
            Objeto res_left  = left.execute(entorno);
            Objeto res_right = right.execute(entorno);

            Objeto.TipoObjeto tipo_dominante = TablaTipo.tabla[res_left.getTipo().GetHashCode(), res_right.getTipo().GetHashCode()];

            if (tipo_dominante == Objeto.TipoObjeto.INTEGER)
            {
                if (int.Parse(res_left.getValor().ToString()) > int.Parse(res_right.getValor().ToString()))
                {
                    return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, true));
                }
                return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, false));
            }
            else if (tipo_dominante == Objeto.TipoObjeto.REAL)
            {
                if (double.Parse(res_left.getValor().ToString()) > double.Parse(res_right.getValor().ToString()))
                {
                    return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, true));
                }
                return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, false));
            }
            else if (tipo_dominante == Objeto.TipoObjeto.STRING)
            {
                if (String.Compare(res_left.getValor().ToString(), res_right.getValor().ToString()) > 0)
                {
                    return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, true));
                }
                return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, false));
            }
            else if (tipo_dominante == Objeto.TipoObjeto.BOOLEAN)
            {
                if (bool.Parse(res_left.getValor().ToString()) == true)
                {
                    return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, true));
                }
                return(new Primitivo(Objeto.TipoObjeto.BOOLEAN, false));
            }
            else
            {
                Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                        "No se puede aplicar mayor que a tipos de datos " + res_left.getTipo().ToString() + " con " + res_right.getTipo().ToString());
                Maestra.getInstancia.addError(error);
            }
            throw new Exception("No se puede aplicar mayor que a tipos de datos " + res_left.getTipo().ToString() + " con " + res_right.getTipo().ToString());
        }
Esempio n. 13
0
        public static Nodo definicion_arreglo(ParseTreeNode entrada)
        {
            int linea   = entrada.ChildNodes[0].Span.Location.Line;
            int columna = entrada.ChildNodes[0].Span.Location.Column;

            string nombre = entrada.ChildNodes[0].Token.Text;

            Objeto.TipoObjeto tipo = getTipo(entrada.ChildNodes[7]);

            string nombre_tipo = Nombre_del_tipo(entrada.ChildNodes[7]);

            Nodo[] arreglo_dimensio;

            arreglo_dimensio = get_dimensiones(entrada.ChildNodes[4]);

            return(new Declaracion_Arreglo(linea, columna, arreglo_dimensio, nombre, tipo, nombre_tipo));
        }
Esempio n. 14
0
        public static Nodo getObjeto(Objeto.TipoObjeto tipo)
        {
            switch (tipo)
            {
            case Objeto.TipoObjeto.INTEGER:
                return(new Constante(0, 0, new Primitivo(tipo, ValoresDefecto(tipo))));

            case Objeto.TipoObjeto.REAL:
                return(new Constante(0, 0, new Primitivo(tipo, ValoresDefecto(tipo))));

            case Objeto.TipoObjeto.STRING:
                return(new Constante(0, 0, new Primitivo(tipo, ValoresDefecto(tipo))));

            case Objeto.TipoObjeto.BOOLEAN:
                return(new Constante(0, 0, new Primitivo(tipo, ValoresDefecto(tipo))));
            }
            return(null);
        }
Esempio n. 15
0
        public static object ValoresDefecto(Objeto.TipoObjeto tipo)
        {
            switch (tipo)
            {
            case Objeto.TipoObjeto.INTEGER:
                return(0);

            case Objeto.TipoObjeto.REAL:
                return(0.0);

            case Objeto.TipoObjeto.BOOLEAN:
                return(false);

            case Objeto.TipoObjeto.STRING:
                return("\'\'");
            }
            return(null);
        }
Esempio n. 16
0
        public bool Validar_tipos(Objeto variable, Objeto valor_nuevo)
        {
            Objeto.TipoObjeto tipo_dominante = TablaTipo.tabla[variable.getTipo().GetHashCode(), valor_nuevo.getTipo().GetHashCode()];

            if (tipo_dominante == Objeto.TipoObjeto.NULO)
            {
                Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                        "Tipos de datos incompatibles: " + variable.getTipo().ToString() + ", " + valor_nuevo.getTipo().ToString());
                Maestra.getInstancia.addError(error);
                throw new Exception("tipos de datos incompatibles");
            }
            else if (tipo_dominante == Objeto.TipoObjeto.REAL && variable.getTipo() == Objeto.TipoObjeto.INTEGER)
            {
                Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                        "Tipos de datos incompatibles: " + variable.getTipo().ToString() + ", " + valor_nuevo.getTipo().ToString());
                Maestra.getInstancia.addError(error);
                throw new Exception("tipos de datos incompatibles");
            }
            else if (tipo_dominante == Objeto.TipoObjeto.OBJECTS)
            {
                if (string.Compare(variable.getNombre(), valor_nuevo.getNombre()) != 0)
                {
                    Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                            "Tipos de datos incompatibles: " + variable.getNombre() + ", " + valor_nuevo.getNombre());
                    Maestra.getInstancia.addError(error);
                    throw new Exception("tipos de datos incompatibles");
                }
            }
            else if (tipo_dominante == Objeto.TipoObjeto.ARRAY)
            {
                if (string.Compare(variable.getNombre(), valor_nuevo.getNombre()) != 0)
                {
                    Error error = new Error(base.getLinea(), base.getColumna(), Error.Errores.Semantico,
                                            "Tipos de datos incompatibles: " + variable.getNombre() + ", " + valor_nuevo.getNombre());
                    Maestra.getInstancia.addError(error);
                    throw new Exception("tipos de datos incompatibles");
                }
            }

            return(true);
        }
Esempio n. 17
0
        public static Nodo EvaluarConstante(ParseTreeNode entrada)
        {
            if (entrada.ChildNodes.Count == 4) //Declaraciones de variables sin iniciar
            {
                int linea   = entrada.ChildNodes[0].Span.Location.Line;
                int columna = entrada.ChildNodes[0].Span.Location.Column;

                string nombre = entrada.ChildNodes[0].Token.Text;
                return(new DeclaracionConstante(linea, columna, nombre, Expresion.Expresion.evaluar(entrada.ChildNodes[2]), Objeto.TipoObjeto.CONST));
            }
            else if (entrada.ChildNodes.Count == 6) // declaracion de variable iniciada
            {
                int linea   = entrada.ChildNodes[0].Span.Location.Line;
                int columna = entrada.ChildNodes[0].Span.Location.Column;

                string            nombre = entrada.ChildNodes[0].Token.Text;
                Objeto.TipoObjeto tipo   = getTipo(entrada.ChildNodes[2]);
                return(new DeclaracionConstante(linea, columna, nombre, Expresion.Expresion.evaluar(entrada.ChildNodes[4]), tipo));
            }

            return(null);
        }
Esempio n. 18
0
 public DeclaracionConstante(int linea, int columna, string nombre, Nodo exp, Objeto.TipoObjeto tipo) : base(linea, columna)
 {
     this.nombre    = nombre;
     this.expresion = exp;
     this.tipo      = tipo;
 }
 public Sentencia_transferencia(Objeto.TipoObjeto tipo, int linea, int columna, Objeto valor) : base(tipo)
 {
     this.linea   = linea;
     this.columna = columna;
     this.valor   = valor;
 }
Esempio n. 20
0
        public static Nodo evaluar_variable(ParseTreeNode entrada)
        {
            if (entrada.ChildNodes.Count == 4) //Declaraciones de variables sin iniciar
            {
                String type    = entrada.ChildNodes[0].Term.Name;
                int    linea   = entrada.ChildNodes[0].Span.Location.Line;
                int    columna = entrada.ChildNodes[0].Span.Location.Column;

                switch (type)
                {
                case "Id":
                {
                    string []         nombre      = new string[] { entrada.ChildNodes[0].Token.Text };
                    Objeto.TipoObjeto tipo        = getTipo(entrada.ChildNodes[2]);
                    string            nombre_type = Nombre_del_tipo(entrada.ChildNodes[2]);
                    return(new Declaracion(linea, columna, nombre, getObjeto(tipo), tipo, nombre_type));
                }

                case "lista_id":
                {
                    //TODO: indicar mejor la linea y columna
                    string[]          nombre      = getId(entrada.ChildNodes[0]);
                    Objeto.TipoObjeto tipo        = getTipo(entrada.ChildNodes[2]);
                    string            nombre_type = Nombre_del_tipo(entrada.ChildNodes[2]);
                    return(new Declaracion(linea, columna, nombre, getObjeto(tipo), tipo, nombre_type));
                }

                case "id_constante":
                {
                    //hace algo?
                    string nombre = entrada.ChildNodes[0].Token.Text;
                    return(new DeclaracionConstante(linea, columna, nombre, Expresion.Expresion.evaluar(entrada.ChildNodes[2]), Objeto.TipoObjeto.CONST));
                }
                }
            }
            else if (entrada.ChildNodes.Count == 6) // declaracion de variable iniciada
            {
                String type    = entrada.ChildNodes[0].Term.Name;
                int    linea   = entrada.ChildNodes[0].Span.Location.Line;
                int    columna = entrada.ChildNodes[0].Span.Location.Column;

                switch (type)
                {
                case "Id":
                {
                    string[]          nombre      = new string[] { entrada.ChildNodes[0].Token.Text };
                    Objeto.TipoObjeto tipo        = getTipo(entrada.ChildNodes[2]);
                    string            nombre_type = Nombre_del_tipo(entrada.ChildNodes[2]);
                    return(new Declaracion(linea, columna, nombre, Expresion.Expresion.evaluar(entrada.ChildNodes[4]), tipo, nombre_type));
                }

                case "id_constante":
                {
                    //hace algo?
                    string            nombre = entrada.ChildNodes[0].Token.Text;
                    Objeto.TipoObjeto tipo   = getTipo(entrada.ChildNodes[2]);
                    return(new DeclaracionConstante(linea, columna, nombre, Expresion.Expresion.evaluar(entrada.ChildNodes[4]), tipo));
                }
                }
            }

            return(null);
        }
Esempio n. 21
0
 public Parametro(string nombre_parametro, Objeto.TipoObjeto tipo, Tipo_Parametro tipo_Parametro, Objeto valor) : base(tipo)
 {
     this.nombre = nombre_parametro;
     this.valor  = valor;
     this.tipo   = tipo_Parametro;
 }