public TablaRelacional()
        {
            palabras = new Relacional[5];
            // public String relacional;
            // public int valor;

            palabras[0] = new Relacional(">", 81);
            palabras[1] = new Relacional("<", 82);
            palabras[2] = new Relacional("=", 83);
            palabras[3] = new Relacional(">=", 84);
            palabras[4] = new Relacional("<=", 85);
        }
Beispiel #2
0
        public override object Ejecutar(Entorno e, bool funcion, bool ciclo, bool sw, bool tc, LinkedList <Salida> log, LinkedList <Error> errores)
        {
            if (!Default)
            {
                if (!Continuar)
                {
                    object valExpr = Expr.GetValor(e, log, errores);

                    if (valExpr != null)
                    {
                        if (valExpr is Throw)
                        {
                            return(valExpr);
                        }

                        Relacional rel    = new Relacional(ExprSwitch, new Literal(Expr.Tipo, valExpr, Linea, Columna), Operador.IGUAL, Linea, Columna);
                        object     valRel = rel.GetValor(e, log, errores);

                        if (valRel != null)
                        {
                            if ((bool)valRel)
                            {
                                Continuar = true;
                            }
                        }
                    }
                }
            }
            else
            {
                Continuar = true;
            }

            if (Continuar)
            {
                Entorno local = new Entorno(e);
                return(Bloque.Ejecutar(local, funcion, ciclo, sw, tc, log, errores));
            }

            return(null);
        }
Beispiel #3
0
        public TablaLexica(String[] palabras, int[] lineas, TablaIdentificador tablaI, TablaConstante tablaC)
        {
            this.palabras = new Token[palabras.Length];
            //this.palabras[0].reiniciar();
            Token.sNumero = 1;


            // Creando todos los TOKENS
            for (int x = 0; x < palabras.Length; x++)
            {
                //Regex.IsMatch(palabra, @"\d+")
                if (palabras[x] != null && !Regex.IsMatch(palabras[x], @"((\W|^)\s+(\W|$))"))
                {
                    if (palabras[x] != null && Regex.IsMatch(palabras[x], @regexT1))
                    { // Tipo 1 - Reservadas
                        Reservada apuntador = this.darReservada(palabras[x]);
                        if (apuntador != null)
                        {
                            this.palabras[x] = new Token(lineas[x], palabras[x], 1, apuntador.valor);
                        }
                    }
                    else if (palabras[x] != null && Regex.IsMatch(palabras[x], @regexT6))
                    { // Tipo 6 - Cosntantes
                        Constante apuntador = this.darConstante(palabras[x], tablaC);
                        if (apuntador != null)
                        {
                            this.palabras[x] = new Token(lineas[x], "Constante", 6, apuntador.valor);
                        }
                    }
                    else if (palabras[x] != null && Regex.IsMatch(palabras[x], regexT5))
                    { // Tipo 5 - Delimitadores
                        Delimitador apuntador = this.darDelimitador(palabras[x]);
                        if (apuntador != null)
                        {
                            this.palabras[x] = new Token(lineas[x], palabras[x], 5, apuntador.valor);
                        }
                    }
                    else if (palabras[x] != null && Regex.IsMatch(palabras[x], @regexT4))
                    { // Tipo 4 - Identificador
                        Identificador apuntador = this.darIdentificador(palabras[x], tablaI);
                        if (apuntador != null)
                        {
                            this.palabras[x] = new Token(lineas[x], palabras[x], 4, apuntador.valor);
                        }
                    }
                    else if (palabras[x] != null && Regex.IsMatch(palabras[x], @regexT7))
                    { // Tipo 7 - Operadores
                        Operador apuntador = this.darOperador(palabras[x]);
                        this.palabras[x] = new Token(lineas[x], palabras[x], 7, apuntador.valor);
                    }
                    else if (palabras[x] != null && Regex.IsMatch(palabras[x], @regexT8))
                    { // Tipo 8 - Relacionales
                        Relacional apuntador = this.darRelacional(palabras[x]);
                        this.palabras[x] = new Token(lineas[x], palabras[x], 8, apuntador.valor);
                    }
                    else
                    { // Error
                        this.lError = lineas[x];
                        this.error  = true;
                        this.pError = palabras[x];

                        x = palabras.Length;
                    }
                }
            }
        }
Beispiel #4
0
        public override object GetValor(Entorno e, LinkedList <Salida> log, LinkedList <Error> errores)
        {
            Select.Mostrar = false;
            object obj = Select.Ejecutar(e, false, false, false, false, log, errores);

            if (obj != null)
            {
                if (obj is Throw)
                {
                    return(obj);
                }

                LinkedList <Entorno> data = (LinkedList <Entorno>)obj;

                Aritmetica op;
                Relacional rel;

                switch (Funcion)
                {
                case Aggregation.COUNT:
                    int count = data.Count();
                    Tipo = new Tipo(Type.INT);
                    return(count);

                case Aggregation.MIN:
                    if (data.Count() > 0)
                    {
                        object min;

                        Entorno ent1 = data.ElementAt(0);
                        Simbolo sim1 = ent1.Simbolos.ElementAt(0);

                        if (ent1.Simbolos.Count() > 1)
                        {
                            errores.AddLast(new Error("Semántico", "El Select solo debería retornar una Columna.", Linea, Columna));
                        }

                        if (sim1.Tipo.IsNumeric() || sim1.Tipo.IsDate() || sim1.Tipo.IsTime())
                        {
                            Tipo = sim1.Tipo;
                            min  = sim1.Valor;
                        }
                        else
                        {
                            errores.AddLast(new Error("Semántico", "La función de agregación MIN solo puede ser utilizada en datos numéricos, fechas y horas.", Linea, Columna));
                            return(null);
                        }

                        for (int i = 1; i < data.Count(); i++)
                        {
                            ent1 = data.ElementAt(i);
                            sim1 = ent1.Simbolos.ElementAt(0);

                            rel = new Relacional(new Literal(Tipo, min, Linea, Columna), new Literal(sim1.Tipo, sim1.Valor, Linea, Columna), Operador.MENORQUE, Linea, Columna);

                            object valRel = rel.GetValor(e, log, errores);

                            if (valRel != null)
                            {
                                if (valRel is Throw)
                                {
                                    return(valRel);
                                }

                                if (!((bool)valRel))
                                {
                                    min = sim1.Valor;
                                }
                            }
                            else
                            {
                                errores.AddLast(new Error("Semántico", "No se pudo realizar la función de agregación MIN.", Linea, Columna));
                                return(null);
                            }
                        }
                        return(min);
                    }
                    Tipo = new Tipo(Type.NULL);
                    return(new Null());

                case Aggregation.MAX:
                    if (data.Count() > 0)
                    {
                        object max;

                        Entorno ent1 = data.ElementAt(0);
                        Simbolo sim1 = ent1.Simbolos.ElementAt(0);

                        if (ent1.Simbolos.Count() > 1)
                        {
                            errores.AddLast(new Error("Semántico", "El Select solo debería retornar una Columna.", Linea, Columna));
                        }

                        if (sim1.Tipo.IsNumeric() || sim1.Tipo.IsDate() || sim1.Tipo.IsTime())
                        {
                            Tipo = sim1.Tipo;
                            max  = sim1.Valor;
                        }
                        else
                        {
                            errores.AddLast(new Error("Semántico", "La función de agregación MAX solo puede ser utilizada en datos numéricos, fechas y horas.", Linea, Columna));
                            return(null);
                        }

                        for (int i = 1; i < data.Count(); i++)
                        {
                            ent1 = data.ElementAt(i);
                            sim1 = ent1.Simbolos.ElementAt(0);

                            rel = new Relacional(new Literal(Tipo, max, Linea, Columna), new Literal(sim1.Tipo, sim1.Valor, Linea, Columna), Operador.MAYORQUE, Linea, Columna);

                            object valRel = rel.GetValor(e, log, errores);

                            if (valRel != null)
                            {
                                if (valRel is Throw)
                                {
                                    return(valRel);
                                }

                                if (!((bool)valRel))
                                {
                                    max = sim1.Valor;
                                }
                            }
                            else
                            {
                                errores.AddLast(new Error("Semántico", "No se pudo realizar la función de agregación MAX.", Linea, Columna));
                                return(null);
                            }
                        }
                        return(max);
                    }
                    Tipo = new Tipo(Type.NULL);
                    return(new Null());

                case Aggregation.SUM:
                    if (data.Count() > 0)
                    {
                        object sum;

                        Entorno ent1 = data.ElementAt(0);
                        Simbolo sim1 = ent1.Simbolos.ElementAt(0);
                        Tipo = sim1.Tipo;
                        sum  = sim1.Valor;

                        if (ent1.Simbolos.Count() > 1)
                        {
                            errores.AddLast(new Error("Semántico", "El Select solo debería retornar una Columna.", Linea, Columna));
                        }

                        if (!(Tipo.IsNumeric() || Tipo.IsString()))
                        {
                            errores.AddLast(new Error("Semántico", "La función de agregación AVG solo puede ser utilizada sobre datos numéricos y Cadenas.", Linea, Columna));
                            return(null);
                        }

                        for (int i = 1; i < data.Count(); i++)
                        {
                            ent1 = data.ElementAt(i);
                            sim1 = ent1.Simbolos.ElementAt(0);

                            op   = new Aritmetica(new Literal(Tipo, sum, Linea, Columna), new Literal(sim1.Tipo, sim1.Valor, Linea, Columna), Operador.SUMA, Linea, Columna);
                            sum  = op.GetValor(e, log, errores);
                            Tipo = op.Tipo;

                            if (sum == null)
                            {
                                errores.AddLast(new Error("Semántico", "No se pudo realizar la función de agregación SUM.", Linea, Columna));
                                return(null);
                            }

                            if (sum is Throw)
                            {
                                return(sum);
                            }
                        }

                        if (Tipo.IsString() || Tipo.IsNumeric())
                        {
                            return(sum);
                        }
                        else
                        {
                            errores.AddLast(new Error("Semántico", "No se pudo realizar la función de agregación SUM.", Linea, Columna));
                            return(null);
                        }
                    }
                    Tipo = new Tipo(Type.NULL);
                    return(new Null());

                case Aggregation.AVG:
                    if (data.Count() > 0)
                    {
                        object sum;

                        Entorno ent1 = data.ElementAt(0);
                        Simbolo sim1 = ent1.Simbolos.ElementAt(0);
                        Tipo = sim1.Tipo;
                        sum  = sim1.Valor;

                        if (ent1.Simbolos.Count() > 1)
                        {
                            errores.AddLast(new Error("Semántico", "El Select solo debería retornar una Columna.", Linea, Columna));
                        }

                        if (!Tipo.IsNumeric())
                        {
                            errores.AddLast(new Error("Semántico", "La función de agregación AVG solo puede ser utilizada sobre datos numéricos.", Linea, Columna));
                            return(null);
                        }

                        for (int i = 1; i < data.Count(); i++)
                        {
                            ent1 = data.ElementAt(i);
                            sim1 = ent1.Simbolos.ElementAt(0);

                            op   = new Aritmetica(new Literal(Tipo, sum, Linea, Columna), new Literal(sim1.Tipo, sim1.Valor, Linea, Columna), Operador.SUMA, Linea, Columna);
                            sum  = op.GetValor(e, log, errores);
                            Tipo = op.Tipo;

                            if (sum == null)
                            {
                                errores.AddLast(new Error("Semántico", "No se pudo realizar la función de agregación AVG.", Linea, Columna));
                                return(null);
                            }

                            if (sum is Throw)
                            {
                                return(sum);
                            }
                        }

                        int total = data.Count();
                        op = new Aritmetica(new Literal(Tipo, sum, Linea, Columna), new Literal(new Tipo(Type.INT), total, Linea, Columna), Operador.DIVISION, Linea, Columna);

                        sum = op.GetValor(e, log, errores);

                        if (sum != null)
                        {
                            Tipo = op.Tipo;
                            return(sum);
                        }
                        else
                        {
                            errores.AddLast(new Error("Semántico", "No se pudo realizar la función de agregación AVG.", Linea, Columna));
                            return(null);
                        }
                    }
                    Tipo = new Tipo(Type.NULL);
                    return(new Null());
                }
            }
            return(null);
        }
Beispiel #5
0
        public Resultado operar(ParseTreeNode raiz)
        {
            Resultado r1 = null;
            Resultado r2 = null;

            switch (raiz.Term.Name)
            {
            case "EXPL":
                if (raiz.ChildNodes.Count == 3)
                {
                    r1 = operar(raiz.ChildNodes[0]);
                    r2 = operar(raiz.ChildNodes[2]);
                }
                else if (raiz.ChildNodes.Count == 2)
                {
                    //not
                    r1 = operar(raiz.ChildNodes[1]);
                    if (!r1.tipo.ToString().Equals("Error"))
                    {
                        if (r1.valor.ToString().Equals("1"))
                        {
                            return(new Resultado("bool", 0));
                        }
                        else if (r1.valor.ToString().Equals("0"))
                        {
                            return(new Resultado("bool", 1));
                        }
                    }
                    else
                    {
                        return(new Resultado("Error", null));
                    }
                }
                else
                {
                    return(operar(raiz.ChildNodes[0]));
                }
                break;

            case "EXPR":
                opR = new Relacional(tuplaActual);
                return(opR.operar(raiz));
            }


            if (!((r1.tipo.Equals("integer") || r1.tipo.Equals("bool")) && r2.tipo.Equals("integer") || r2.tipo.Equals("bool")))
            {
                agregarError("Semantico", "Bool solo puede ser 1/0", raiz.Span.Location.Line, raiz.Span.Location.Column);
                return(new Resultado("Error", null));
            }

            if (!((Convert.ToInt32(r1.valor) == 1 || Convert.ToInt32(r1.valor) == 0) && (Convert.ToInt32(r2.valor) == 1 || Convert.ToInt32(r2.valor) == 0)))
            {
                agregarError("Semantico", "Bool solo puede ser 1/0", raiz.Span.Location.Line, raiz.Span.Location.Column);
                return(new Resultado("Error", null));
            }


            switch (raiz.ChildNodes[1].Token.Text)
            {
            case "&&":
                if (Convert.ToInt32(r1.valor) == 1 && Convert.ToInt32(r2.valor) == 1)
                {
                    return(new Resultado("bool", 1));
                }
                else
                {
                    return(new Resultado("bool", 0));
                }

            case "||":
                if (Convert.ToInt32(r1.valor) == 1 || Convert.ToInt32(r2.valor) == 1)
                {
                    return(new Resultado("bool", 1));
                }
                else
                {
                    return(new Resultado("bool", 0));
                }
            }

            return(new Resultado("Error", null));
        }