Ejemplo n.º 1
0
        public void insertarConValor(Simbolo simbolo)
        {
            if (contiene(simbolo.identificador))
            {
                throw new Exception("La variable ha sido declarada en el mismo ambito anteriormente");
            }

            switch (simbolo.tipo)
            {
            case "Int":
                if (simbolo.valor is int)
                {
                    elementos.Add(simbolo.identificador, simbolo);
                }
                else
                {
                    throw new Exception("el valor asignado a la variable " + simbolo.identificador
                                        + " no es de tipo Int");
                }
                break;

            case "Double":
                if (simbolo.valor is double)
                {
                    elementos.Add(simbolo.identificador, simbolo);
                }
                else if (simbolo.valor is int)
                {
                    simbolo.valor = Convert.ToDouble(simbolo.valor);
                    elementos.Add(simbolo.identificador, simbolo);
                }
                else
                {
                    throw new Exception("el valor asignado a la variable " + simbolo.identificador
                                        + " no es de tipo Double");
                }
                break;

            case "String":
                if (simbolo.valor is string)
                {
                    elementos.Add(simbolo.identificador, simbolo);
                }
                else if (simbolo.valor is int)
                {
                    simbolo.valor = Convert.ToString(simbolo.valor);
                    elementos.Add(simbolo.identificador, simbolo);
                }
                break;

            case "Bool":
                if (simbolo.valor is bool)
                {
                    elementos.Add(simbolo.identificador, simbolo);
                }
                else
                {
                    throw new Exception("el valor asignado a la variable " + simbolo.identificador
                                        + " no es de tipo Bool");
                }
                break;

            case "Char":
                if (simbolo.valor is char)
                {
                    elementos.Add(simbolo.identificador, simbolo);
                }
                else
                {
                    throw new Exception("el valor asignado a la variable " + simbolo.identificador
                                        + " no es de tipo Char");
                }
                break;
            }
        }
Ejemplo n.º 2
0
        public void actualizarValor(string identificador, object valor)
        {
            if (!(contiene(identificador)))
            {
                throw new Exception("La variable " + identificador + " no existe en el contexto actual");
            }


            if (elementos[identificador] is Simbolo)
            {
                Simbolo aux = (Simbolo)elementos[identificador];

                switch (aux.tipo)
                {
                case "Int":
                    if (valor is int)
                    {
                        ((Simbolo)elementos[identificador]).valor = valor;
                    }
                    else
                    {
                        throw new Exception("el valor que intenta darle a la variable " + identificador
                                            + " no es de tipo Int");
                    }
                    break;

                case "Double":
                    if (valor is double)
                    {
                        ((Simbolo)elementos[identificador]).valor = valor;
                    }
                    else
                    {
                        throw new Exception("el valor que intenta darle a la variable " + identificador
                                            + " no es de tipo Double");
                    }
                    break;

                case "String":
                    if (valor is string)
                    {
                        ((Simbolo)elementos[identificador]).valor = valor;
                    }
                    else
                    {
                        ((Simbolo)elementos[identificador]).valor = Convert.ToString(valor);
                    }
                    break;

                case "Bool":
                    if (valor is bool)
                    {
                        ((Simbolo)elementos[identificador]).valor = valor;
                    }
                    else
                    {
                        throw new Exception("el valor que intenta darle a la variable " + identificador
                                            + " no es de tipo Bool");
                    }
                    break;

                case "Char":
                    if (valor is char)
                    {
                        ((Simbolo)elementos[identificador]).valor = valor;
                    }
                    else
                    {
                        throw new Exception("el valor que intenta darle" +
                                            " a la variable " + identificador
                                            + " no es de tipo Char");
                    }
                    break;
                }
            }
            else if (elementos[identificador] is Arreglo)
            {
                if (!(valor is Arreglo))
                {
                    throw new Exception("No se puede asignar una variable primitiva a un arreglo");
                }

                switch (((Arreglo)elementos[identificador]).tipo)
                {
                case "Int":
                    if (!((Arreglo)valor).tipo.Equals("Int"))
                    {
                        throw new Exception("Los tipos de dato de los arreglos no coinciden");
                    }
                    break;

                case "Double":
                    if (!((Arreglo)valor).tipo.Equals("Double"))
                    {
                        throw new Exception("Los tipos de dato de los arreglos no coinciden");
                    }
                    break;

                case "String":
                    if (!((Arreglo)valor).tipo.Equals("String"))
                    {
                        throw new Exception("Los tipos de dato de los arreglos no coinciden");
                    }
                    break;

                case "Bool":
                    if (!((Arreglo)valor).tipo.Equals("Bool"))
                    {
                        throw new Exception("Los tipos de dato de los arreglos no coinciden");
                    }
                    break;

                case "Char":
                    if (!((Arreglo)valor).tipo.Equals("Char"))
                    {
                        throw new Exception("Los tipos de dato de los arreglos no coinciden");
                    }
                    break;
                }

                ((Arreglo)elementos[identificador]).size = ((Arreglo)valor).size;
                for (int i = 0; i < ((Arreglo)valor).size; i++)
                {
                    ((Arreglo)elementos[identificador]).array[i] = ((Arreglo)valor).array[i];
                }
            }
        }