Esempio n. 1
0
        public Boolean declaracionParametros(ParseTreeNode raiz, Resultado resultado)
        {
            String tipoDato = raiz.ChildNodes[0].ChildNodes[0].Token.Text.ToLower();
            String nombre   = raiz.ChildNodes[1].Token.Text.ToLower().Replace("@", "");

            resultado = comprobarTipos(tipoDato, resultado.tipo, resultado, raiz.Span.Location.Line, raiz.Span.Location.Column);
            if (!resultado.tipo.Equals("Error"))
            {
                Simbolo variable = new Simbolo(tipoDato, nombre, resultado.valor);
                Boolean estado   = Interprete.tabla.setSimbolo3(variable);
                if (!estado)
                {
                    agregarError("Semantico", "La variable " + nombre + " ya existe", raiz.Span.Location.Line, raiz.Span.Location.Column);
                }
            }
            return(false);
        }
Esempio n. 2
0
        private Object getVariable(ParseTreeNode nodoVariables)
        {
            int     contador  = 0;
            object  resultado = null;
            Simbolo s         = null;
            Object  actual    = null;

            foreach (ParseTreeNode var in nodoVariables.ChildNodes)
            {
                String nombre = var.Token.Text.ToLower();
                if (contador == 0)
                {
                    s = Interprete.tabla.getSimbolo2(nombre);
                    if (s != null)
                    {
                        actual    = s.valor;
                        resultado = s;
                    }
                    else
                    {
                        agregarError("Semantico", "Variable " + nombre + " no declarada", var.Span.Location.Line, var.Span.Location.Column);
                        return(new Resultado("Error", null));
                    }
                }
                else
                {
                    Objeto   objeto   = (Objeto)actual;
                    Atributo atributo = objeto.getAtributo(nombre);
                    if (atributo != null)
                    {
                        actual    = atributo.valor;
                        resultado = atributo;
                    }
                    else
                    {
                        agregarError("Semantico", "Variable " + nombre + " no declarada", var.Span.Location.Line, var.Span.Location.Column);
                        return(new Resultado("Error", null));
                    }
                }
                contador++;
            }

            return(resultado);
        }
Esempio n. 3
0
        public Boolean asignar(ParseTreeNode raiz)
        {
            Object variable = getVariable(raiz.ChildNodes[0].ChildNodes[0]);

            opL = new Logica();
            Resultado resultado = opL.operar(raiz.ChildNodes[1]);

            if (variable.GetType().Name.Equals("Simbolo"))
            {
                Simbolo s = (Simbolo)variable;
                s.valor = resultado.valor;;
            }
            else if (variable.GetType().Name.Equals("Atributo"))
            {
                Atributo a = (Atributo)variable;
                a.valor = resultado.valor;
            }
            return(false);
        }
Esempio n. 4
0
        public Boolean declaracion(ParseTreeNode raiz)
        {
            if (raiz.ChildNodes.Count == 3)
            {
                ParseTreeNodeList ids      = raiz.ChildNodes[0].ChildNodes;
                String            tipoDato = raiz.ChildNodes[1].ChildNodes[0].Token.Text.ToLower();
                opL = new Logica();

                foreach (ParseTreeNode id in ids)
                {
                    opL = new Logica();
                    Resultado resultado = opL.operar(raiz.ChildNodes[2].ChildNodes[0]);
                    resultado = comprobarTipos(tipoDato, resultado.tipo, resultado, raiz.Span.Location.Line, raiz.Span.Location.Column);
                    if (!resultado.tipo.Equals("Error"))
                    {
                        Simbolo variable = new Simbolo(tipoDato, id.Token.Text, resultado.valor);
                        Boolean estado   = Interprete.tabla.setSimbolo3(variable);
                        if (!estado)
                        {
                            agregarError("Semantico", "La variable " + id.Token.Text + " ya existe", id.Span.Location.Line, id.Span.Location.Column);
                        }
                    }
                }
            }
            else if (raiz.ChildNodes.Count == 2)
            {
                ParseTreeNodeList ids      = raiz.ChildNodes[0].ChildNodes;
                String            tipoDato = raiz.ChildNodes[1].ChildNodes[0].Token.Text.ToLower();
                foreach (ParseTreeNode id in ids)
                {
                    Simbolo  variable = null;
                    DateTime today;
                    switch (tipoDato)
                    {
                    case "text":
                        variable = new Simbolo(tipoDato, id.Token.Text, "");
                        break;

                    case "integer":
                        variable = new Simbolo(tipoDato, id.Token.Text, 0);
                        break;

                    case "double":
                        variable = new Simbolo(tipoDato, id.Token.Text, 0.0);
                        break;

                    case "bool":
                        variable = new Simbolo(tipoDato, id.Token.Text, 0);
                        break;

                    case "date":
                        today    = DateTime.Today;
                        variable = new Simbolo(tipoDato, id.Token.Text, today.ToString("dd-MM-yyyy"));
                        break;

                    case "datetime":
                        today    = DateTime.Today;
                        variable = new Simbolo(tipoDato, id.Token.Text, today.ToString("dd-MM-yyyy hh:mm:ss"));
                        break;

                    default:
                        Objeto objeto = instanciarObjeto(tipoDato, id.Span.Location.Line, id.Span.Location.Column);
                        if (objeto != null)
                        {
                            String nombreObjeto = raiz.ChildNodes[0].ChildNodes[0].Token.Text.ToLower();
                            variable = new Simbolo(tipoDato, nombreObjeto, objeto);
                        }
                        else
                        {
                            return(false);
                        }
                        break;
                    }
                    Boolean estado = Interprete.tabla.setSimbolo3(variable);
                    if (!estado)
                    {
                        agregarError("Semantico", "La variable " + id.Token.Text + " ya existe", id.Span.Location.Line, id.Span.Location.Column);
                    }
                }
            }
            return(false);
        }
Esempio n. 5
0
        public Resultado ejecutar(ParseTreeNode raiz)
        {
            Resultado resultado = null;

            ParseTreeNode nodoDeclaracion = raiz.ChildNodes[0];

            if (!nodoDeclaracion.ChildNodes[1].ChildNodes[0].Token.Text.ToLower().Equals("integer"))
            {
                //reportar error
                return(null);
            }

            //cambiar ambito
            TablaSimbolo aux = Interprete.tabla;

            Interprete.tabla          = new TablaSimbolo();
            Interprete.tabla.anterior = aux;
            ParseTreeNode SENTSPROC = new ParseTreeNode(new NonTerminal("SENTSPROC"), raiz.Span);

            SENTSPROC.ChildNodes.Add(nodoDeclaracion);
            interprete.ejecutar(SENTSPROC);

            opL = new Logica();
            Resultado     condicion      = opL.operar(raiz.ChildNodes[1]);
            String        operacion      = raiz.ChildNodes[2].ChildNodes[0].Token.Text;
            ParseTreeNode nodoSentencias = raiz.ChildNodes[3];

            if (condicion.valor != null && (condicion.tipo.Equals("integer") || (condicion.tipo.Equals("bool"))))
            {
                while (condicion.valor.ToString().Equals("1"))
                {
                    TablaSimbolo aux2 = Interprete.tabla;
                    Interprete.tabla          = new TablaSimbolo();
                    Interprete.tabla.anterior = aux2;
                    resultado        = interprete.ejecutar(nodoSentencias);
                    Interprete.tabla = aux2;
                    if (condicion.valor.ToString().Equals("1"))
                    {
                        if (resultado != null)
                        {
                            if (resultado.detener)
                            {
                                resultado = null;
                                break;
                            }
                        }

                        Simbolo simbolo = Interprete.tabla.getSimbolo2(nodoDeclaracion.ChildNodes[0].ChildNodes[0].Token.Text.Replace("@", "").ToLower());
                        if (operacion.Equals("++"))
                        {
                            simbolo.valor = Convert.ToInt64(simbolo.valor) + 1;
                        }
                        else
                        {
                            simbolo.valor = Convert.ToInt64(simbolo.valor) - 1;
                        }

                        opL       = new Logica();
                        condicion = opL.operar(raiz.ChildNodes[1]);
                    }
                }
            }
            else
            {
                agregarError("Semantico", "La condicion de una sentencia de control solo puede ser tipo Bool o Integer(0|1)", raiz.Span.Location.Line, raiz.Span.Location.Column);
                return(null);
            }

            Interprete.tabla = aux;

            return(resultado);
        }