Esempio n. 1
0
        public Word Visit(Declaration declaration)
        {
            var  idList = new List <string>();
            Word lResult;

            foreach (var lValue in declaration.LeftValues)
            {
                lResult = lValue.Accept(this);
                if (IsError(lResult))
                {
                    return(lResult);
                }
                idList.Add(((MyString)lResult).StringValue);
            }
            Word  rValue;
            PyObj rPyObj;
            int   i;

            if (declaration.Indexes.IsEmpty())
            {
                if (declaration.RightValue == null)
                {
                    rValue = MyNull.GetInstance();
                }
                else
                {
                    rValue = declaration.RightValue.Accept(this);
                }

                if (IsError(rValue))
                {
                    return(rValue);
                }
                if (IsMemoryBlock(rValue))//comentar ese if else si se hace la desereferencia en atomic expr.
                {
                    rPyObj = ((MemoryBlock)rValue).Value;
                }
                else
                {
                    rPyObj = (PyObj)rValue;
                }
                //rPyObj = (PyObj)rValue;//Descomentar esta linea si se hace la desereferencia en atomic expr.
            }
            else //Si tiene indices la declaracion significal que es un array
            {
                Word  indexResult;
                PyObj indexPyObj;
                var   intIndices = new int[declaration.Indexes.Count];
                //Obtiene la lista de ints en sus indices.
                i = 0;
                foreach (var index in declaration.Indexes)
                {
                    indexResult = index.Accept(this);
                    if (IsError(indexResult))
                    {
                        return(indexResult);
                    }
                    indexPyObj = ((IndexSegment)indexResult).Index;
                    //Chequea que sea del tipo correcto:
                    if (indexPyObj.GetMyType() == TypeConstants.INT)
                    {
                        intIndices[i] = ((MyInt)indexPyObj).Int;
                    }
                    else if (indexPyObj.GetMyType() == TypeConstants.CHAR)
                    {
                        intIndices[i] = ((MyChar)indexPyObj).CharValue;
                    }
                    else
                    {
                        return(ErrorFactory.ArrayDimensionError(index, indexPyObj));
                    }
                    //Chequea que tenga un rango valido
                    if (intIndices[i] < 0)
                    {
                        return(ErrorFactory.ArrayDimensionError(index, indexPyObj));
                    }
                    i++;
                }
                if (declaration.RightValue == null)
                {
                    rValue = MyArrayFactory.Create(intIndices);
                }
                else
                {
                    rValue = declaration.RightValue.Accept(this);
                }

                if (IsError(rValue))
                {
                    return(rValue);
                }
                if (IsMemoryBlock(rValue))//comentar ese if else si se hace la desereferencia en atomic expr.
                {
                    rPyObj = ((MemoryBlock)rValue).Value;
                }
                else
                {
                    rPyObj = (PyObj)rValue;
                }
                //rPyObj = (PyObj)rValue;//Descomentar esta linea si se hace la desereferencia en atomic expr.
                //Verifica que rValue sea un array con las dimensiones especificadas en intIndices
                if (rPyObj.GetMyType() != TypeConstants.ARRAY)
                {
                    return(ErrorFactory.ArrayTypeError(declaration, intIndices, rPyObj));
                }
                if (!((MyArray)rPyObj).IsNDimensionalArray(intIndices))
                {
                    return(ErrorFactory.ArrayTypeError(declaration, intIndices, rPyObj));
                }
            }

            Word result;

            i = 0;//Chapus minimo
            foreach (var id in idList)
            {
                result = CurrentScope.Add(id, rPyObj);
                if (IsError(result))
                {
                    ErrorFactory.Create(declaration.LeftValues[i], (MyError)result);
                }
                i++;
            }

            return(rPyObj);//Chapuz medio alto para facilitar el for
        }