示例#1
0
        public override Unit VisitArrayInstantiation(ArrayInstatiationAST arrayInstatiation)
        {
            //tengo que declarar un metodo que sea el que inicialize este array. es decir que le asigne el valor de sizeexp;
            TypeInfo tI    = arrayInstatiation.CurrentScope.GetTypeInfo(arrayInstatiation.ArrayTypeIdentifier);
            Type     array = code.DefinedType[tI.CodeName];


            ILGenerator il = code.Method.GetILGenerator();

            arrayInstatiation.SizeExp.Accept(this);
            il.Emit(OpCodes.Newarr, array.GetElementType());

            //guardar el array .
            LocalBuilder localArray = il.DeclareLocal(array);

            il.Emit(OpCodes.Stloc, localArray.LocalIndex);

            //inicializar correctamente el array
            // for (i =0 to SizeExp ) array[i] = InitializationExp
            //TODO: It can be implemented with a generic method that we create (once) in the assembly: Init<T>(T [] elems, T value)
            CreateArrayInitializMethod(arrayInstatiation, localArray, array);

            il.Emit(OpCodes.Ldloc, localArray.LocalIndex);


            if (!code.PushOnStack)
            {
                il.Emit(OpCodes.Pop);
            }

            return(Unit.Create());
        }
示例#2
0
        private void CreateArrayInitializMethod(ArrayInstatiationAST arrayInstantiation, LocalBuilder localArray, Type arrayType)
        {
            ILGenerator il = code.Method.GetILGenerator();
            //--->
            bool pushOnStack = code.PushOnStack;

            //crear la variable del for.
            LocalBuilder varFor = il.DeclareLocal(typeof(int));

            //generar codigo para cargar el cero.
            il.Emit(OpCodes.Ldc_I4_0);
            // i = 0
            il.Emit(OpCodes.Stloc, varFor.LocalIndex);


            //declaracion de las etiquetas de salto
            Label evaluarCond = il.DefineLabel();
            Label endLoop     = il.DefineLabel();

            //condicion
            il.MarkLabel(evaluarCond);
            //cargas la i
            code.PushOnStack = true;
            il.Emit(OpCodes.Ldloc, varFor.LocalIndex);
            //carga la expresion from
            code.PushOnStack = true;
            arrayInstantiation.SizeExp.Accept(this);
            // tengo que ver si i<exp
            il.Emit(OpCodes.Clt);

            //salto al final si no se cumplio la condicion
            il.Emit(OpCodes.Brfalse, endLoop);

            // a[i] = InitializationExp
            code.PushOnStack = pushOnStack;
            //cargar el array
            il.Emit(OpCodes.Ldloc, localArray.LocalIndex);
            //cargar el indexer
            il.Emit(OpCodes.Ldloc, varFor.LocalIndex);
            //cargar la exp
            arrayInstantiation.InitializationExp.Accept(this);
            //hacer la asignacion
            il.Emit(OpCodes.Stelem, arrayType.IsArray ? arrayType.GetElementType() : arrayType);

            //incrementar el valor de la variable de iteracion
            il.Emit(OpCodes.Ldloc, varFor.LocalIndex);
            il.Emit(OpCodes.Ldc_I4, 1);
            il.Emit(OpCodes.Add);
            il.Emit(OpCodes.Stloc, varFor.LocalIndex);
            //salta a la condicion
            il.Emit(OpCodes.Br, evaluarCond);
            //lo que viene detras del ciclo.

            il.MarkLabel(endLoop);
            //<---
            code.PushOnStack = pushOnStack;
        }
示例#3
0
        public override bool VisitArrayInstantiation(ArrayInstatiationAST arrayInstatiation)
        {
            var other = _other as ArrayInstatiationAST;

            if (other == null)
            {
                return(false);
            }

            return(other.ArrayTypeIdentifier == arrayInstatiation.ArrayTypeIdentifier &&
                   IsEqualNodes(other.SizeExp, arrayInstatiation.SizeExp) &&
                   IsEqualNodes(other.InitializationExp, arrayInstatiation.InitializationExp));
        }
示例#4
0
        public override bool VisitArrayInstantiation(ArrayInstatiationAST arrayInstatiation)
        {
            arrayInstatiation.CurrentScope = _scope;

            arrayInstatiation.ReturnType = TigerType.GetType <ErrorType>();
            TigerType t;

            if (_scope.HasType(arrayInstatiation.ArrayTypeIdentifier, out t) != ScopeLocation.NotDeclared)
            //Chequeo si este tipo de array fue declarado
            {
                var typeArray = t as ArrayType;
                if (typeArray != null)
                {
                    arrayInstatiation.SizeExp.Accept(this);
                    if (arrayInstatiation.SizeExp.ReturnType != TigerType.GetType <IntType>())
                    {
                        //Chequeo que el length del array sea un entero
                        _errorListener.Add(new AnalysisError(AnalysisError.LoadMessage("ArrayIndex"), arrayInstatiation.Line, arrayInstatiation.Columns));
                    }
                    else
                    {
                        arrayInstatiation.InitializationExp.Accept(this);
                        if (!arrayInstatiation.InitializationExp.ReturnType.CanConvertTo(typeArray.BaseType))
                        {
                            _errorListener.Add(
                                new AnalysisError(
                                    string.Format(AnalysisError.LoadMessage("Match"), arrayInstatiation.InitializationExp.ReturnType,
                                                  typeArray.BaseType), arrayInstatiation.Line, arrayInstatiation.Columns));
                        }
                        else
                        {
                            arrayInstatiation.ReturnType = typeArray;
                            return(arrayInstatiation.AlwaysReturn = true);
                        }
                    }
                    return(false);
                }
            }
            _errorListener.Add(AnalysisError.TypeIsNotDefined(arrayInstatiation, arrayInstatiation.ArrayTypeIdentifier));
            return(false);
        }
示例#5
0
 public abstract T VisitArrayInstantiation(ArrayInstatiationAST arrayInstatiation);