Example #1
0
        /* Megnézzük, hogy érvényes-e a külön-külön és együtt a range és a step.
         *
         * Ha nem, akkor nullt adunk vissza, hogy a for ciklus ne futhasson. És hibát írunk ki.
         * Különben egy háromelemű tuple-t adunk vissza, kezdet vég és step intekkel.
         * Így ha esetleg valamelyik ezek közül float-lenne, annak az alsó egészrészét vesszük és warningot kap a programozó.
         */
        public override object VisitRange_and_step([NotNull] DDD_layout_scriptParser.Range_and_stepContext context)
        {
            var     operations = context.operation();
            dynamic rangeStart = VisitOperation(operations[0]);
            dynamic rangeEnd   = VisitOperation(operations[1]);
            dynamic step       = 1;

            if (context.STEP() != null)
            {
                step = VisitOperation(operations[2]);
            }

            // ha bármelyik nem int vagy float, akkor ott hiba van
            if (!(rangeStart is double || rangeStart is int) || !(rangeEnd is double || rangeEnd is int) || !(step is int || step is double))
            {
                alerts.Add(new error(context.Start.Line, "Range and step can only hold Int or Float values"));
                return(null);
            }

            if (rangeStart is double)
            {
                alerts.Add(new warning(context.Start.Line, "The start of the range is casted from Float to Int. You may ignore this warning"));
                rangeStart = (int)rangeStart;
            }

            if (rangeEnd is double)
            {
                alerts.Add(new warning(context.Start.Line, "The end of the range is casted from Float to Int. You may ignore this warning"));
                rangeEnd = (int)rangeEnd;
            }

            if (step is double)
            {
                alerts.Add(new warning(context.Start.Line, "The step is casted from Float to Int. You may ignore this warning"));
                step = (int)step;
            }

            if (context.BRACKET_O() != null)
            {
                rangeStart += (step > 0) ? 1 : -1;
            }

            if (context.BRACKET_C() != null)
            {
                rangeEnd -= (step > 0) ? 1 : -1;
            }

            if (step == 0)
            {
                alerts.Add(new error(context.Start.Line, "Step was 0. This would cause an infinite loop"));
                return(null);
            }

            if ((step > 0 && rangeEnd - rangeStart < 0) || (step < 0 && rangeEnd - rangeStart > 0))
            {
                alerts.Add(new error(context.Start.Line, $"Infinite loop. [Range: {rangeStart}..{rangeEnd}, Step: {step}]"));
                return(null);
            }

            return(new Tuple <int, int, int>(rangeStart, rangeEnd, step));
        }