Пример #1
0
        public static DynLanCodeLine PrevLineWithLessDepth(
#if !NET20
            this
#endif
            DynLanCodeLines Lines,
            DynLanCodeLine StartLine,
            Func <DynLanCodeLine, Boolean> Predicate)
        {
            Int32 depth = (StartLine == null ? 0 : StartLine.Depth);
            Int32 index = (StartLine == null ? 0 : Lines.IndexOf(StartLine));

            if (index < 0)
            {
                return(null);
            }

            for (var i = index - 1; i >= 0; i--)
            {
                DynLanCodeLine line = Lines[i];
                if (!line.IsLineEmpty && line.Depth < depth)
                {
                    if (Predicate == null || Predicate(line))
                    {
                        return(line);
                    }
                }
            }

            return(null);
        }
Пример #2
0
        public static DynLanCodeLine NextOnSameOrHigher(
#if !NET20
            this
#endif
            DynLanCodeLines Lines,
            DynLanCodeLine StartLine)
        {
            Int32 depth = (StartLine == null ? 0 : StartLine.Depth);
            Int32 index = (StartLine == null ? 0 : Lines.IndexOf(StartLine));

            if (index < 0)
            {
                return(null);
            }

            for (var i = index + 1; i < Lines.Count; i++)
            {
                DynLanCodeLine line = Lines[i];
                if (!line.IsLineEmpty)
                {
                    if (line.Depth > depth)
                    {
                        return(line);
                    }
                    else if (line.Depth == depth)
                    {
                        return(line);
                    }
                }
            }

            return(null);
        }
Пример #3
0
        public static DynLanCodeLine NextOnSameOrLower(
#if !NET20
            this
#endif
            DynLanCodeLines Lines,
            DynLanCodeLine StartLine,
            Func <DynLanCodeLine, Boolean> Predicate)
        {
            Int32 depth = (StartLine == null ? 0 : StartLine.Depth);
            Int32 index = (StartLine == null ? 0 : Lines.IndexOf(StartLine));

            if (index < 0)
            {
                return(null);
            }

            for (var i = index + 1; i < Lines.Count; i++)
            {
                DynLanCodeLine line = Lines[i];
                if (!line.IsLineEmpty && line.Depth <= depth)
                {
                    if (Predicate == null || Predicate(line))
                    {
                        return(line);
                    }
                }
            }

            return(null);
        }
Пример #4
0
        public static DynLanCodeLine NextOnSameOrLower(
#if !NET20
            this
#endif
            DynLanCodeLines Lines,
            DynLanCodeLine StartLine)
        {
            return(NextOnSameOrLower(Lines, StartLine, null));
        }
Пример #5
0
        private static Boolean?CheckIfFinishedOrEmptyLine(
            DynContext DynLanContext,
            DynLanState currentState)
        {
            if (currentState == null || DynLanContext.IsFinished)
            {
                DynLanContext.IsFinished = true;
                return(true);
            }

            if (DynLanContext.ExceptionToThrow != null)
            {
                Exception ex = DynLanContext.ExceptionToThrow;
                DynLanContext.ExceptionToThrow = null;
                throw ex;
            }

            DynLanCodeLines lines       = currentState.GetCurrentLines();
            DynLanCodeLine  currentLine = currentState.GetCurrentLine();

            if (currentLine == null)
            {
                return(ExitCurrentContext(
                           DynLanContext,
                           null));
            }

            // jeśli linia jest pusta to przechodzimy do nastepnej
            if (currentLine.IsLineEmpty)
            {
                DynLanCodeLine nextLine = DynLanCodeLinesExtender.NextLine(lines, currentLine);
                if (nextLine == null)
                {
                    return(ExitCurrentContext(
                               DynLanContext,
                               null));
                }
                else
                {
                    currentState.CurrentLineID = nextLine.ID;
                }
                return(true);
            }
            return(null);
        }
Пример #6
0
        private static Boolean?ExecuteCalculations(
            DynContext DynLanContext,
            DynLanState currentState,
            out Object Result)
        {
            Result = null;
            DynLanCodeLines lines       = currentState.GetCurrentLines();
            DynLanCodeLine  currentLine = currentState.GetCurrentLine();

            // wykonanie kalkulacji
            if (currentLine.ContainsAnyExpressions() &&
                currentLine.OperatorType != EOperatorType.ELSE &&
                currentLine.OperatorType != EOperatorType.PASS &&
                currentLine.OperatorType != EOperatorType.CATCH)
            {
                if (DynLanContext.CurrentState.ExpressionContext == null)
                {
                    DynLanContext.CurrentState.ExpressionContext = new ExpressionContext(currentLine.ExpressionGroup);
                }

                if (DynLanContext.CurrentState.ExpressionContext != null &&
                    DynLanContext.CurrentState.ExpressionContext.IsFinished)
                {
                    Result = DynLanContext.CurrentState.ExpressionContext.Result;
                    DynLanContext.CurrentState.ExpressionContext = null;
                }
                else
                {
                    try
                    {
                        Boolean result = ExpressionEvaluator.NextStep(
                            DynLanContext);

                        return(result);
                    }
                    catch
                    {
                        throw;
                    }
                }
            }

            return(null);
        }
Пример #7
0
        public static DynLanCodeLine ExitParentIf(
#if !NET20
            this
#endif
            DynLanCodeLines Lines,
            DynLanCodeLine StartLine)
        {
            Int32 depth = (StartLine == null ? 0 : StartLine.Depth);
            Int32 index = (StartLine == null ? 0 : Lines.IndexOf(StartLine));

            if (index < 0)
            {
                return(null);
            }

            DynLanCodeLine line = StartLine;

            while (true)
            {
                line = NextLine(
                    Lines,
                    line);

                if (line == null)
                {
                    break;
                }

                if (line.Depth < StartLine.Depth)
                {
                    return(line);
                }

                if (line.Depth == StartLine.Depth &&
                    line.OperatorType != EOperatorType.ELIF &&
                    line.OperatorType != EOperatorType.ELSE)
                {
                    return(line);
                }
            }

            return(null);
        }
Пример #8
0
        private static Boolean GotoCatch(
            DynContext DynLanContext,
            Exception exception)
        {
            while (true)
            {
                DynLanState currentState = DynLanContext.
                                           CurrentState;

                // reset dla kontekstu obliczeń, ponieważ przechodzimy do catch'a
                currentState.ExpressionContext = null;

                DynLanCodeLines lines       = currentState.GetCurrentLines();
                DynLanCodeLine  currentLine = currentState.GetCurrentLine();

                // poszukanie poprzedniego catch'a
                DynLanCodeLine prevCatch = DynLanCodeLinesExtender.
                                           PrevLineWithLessDepth(lines, currentLine, l => l.OperatorType == EOperatorType.CATCH);

                // poszukanie poprzedniego try'a
                DynLanCodeLine prevTry = DynLanCodeLinesExtender.
                                         PrevLineWithLessDepth(lines, currentLine, l => l.OperatorType == EOperatorType.TRY);

                if (exception is DynLanAbortException)
                {
                    ExitCurrentContext(
                        DynLanContext,
                        exception);

                    if (DynLanContext.IsFinished)
                    {
                        break;
                    }
                }
                else if (prevTry == null)
                {
                    ExitCurrentContext(
                        DynLanContext,
                        exception);

                    if (DynLanContext.IsFinished)
                    {
                        break;
                    }
                }
                // jeśli znalazł try'a i nie jesteśmy w catch'u
                else if (prevTry.Depth < currentLine.Depth &&
                         (prevCatch == null || lines.IndexOf(prevCatch) < lines.IndexOf(prevTry)))
                {
                    DynLanCodeLine nextCatch = DynLanCodeLinesExtender.NextOnSameOrLower(
                        lines,
                        prevTry,
                        i => i.OperatorType == EOperatorType.CATCH);

                    if (nextCatch != null)
                    {
                        ExpressionToken variableForException = null;

                        if (nextCatch.ExpressionGroup != null &&
                            nextCatch.ExpressionGroup.MainExpression != null &&
                            nextCatch.ExpressionGroup.MainExpression.Tokens != null &&
                            nextCatch.ExpressionGroup.MainExpression.Tokens.Count > 0)
                        {
#if !NET20
                            variableForException = nextCatch.
                                                   ExpressionGroup.MainExpression.Tokens.
                                                   FirstOrDefault(i => i.TokenType != TokenType.BRACKET_BEGIN);
#else
                            variableForException = Linq2.FirstOrDefault(nextCatch.
                                                                        ExpressionGroup.MainExpression.Tokens,
                                                                        i => i.TokenType != TokenType.BRACKET_BEGIN);
#endif
                        }


                        currentState.CurrentLineID = nextCatch.ID;

                        if (variableForException != null && !String.IsNullOrEmpty(variableForException.TokenName))
                        {
                            currentState.Object[variableForException.TokenName] = exception;
                        }

                        break;
                    }
                    else
                    {
                        ExitCurrentContext(
                            DynLanContext,
                            exception);

                        if (DynLanContext.IsFinished)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    ExitCurrentContext(
                        DynLanContext,
                        exception);

                    if (DynLanContext.IsFinished)
                    {
                        break;
                    }
                }
            }
            return(false);
        }
Пример #9
0
        //////////////////////////////////////////////

        private static Boolean GotoNextLine(
            DynContext DynLanContext,
            DynLanState currentState,
            Object currentValue)
        {
            try
            {
                DynLanCodeLines lines       = currentState.GetCurrentLines();
                DynLanCodeLine  currentLine = currentState.GetCurrentLine();

                // jesli return to konczymy
                if (currentLine.OperatorType == EOperatorType.RETURN)
                {
                    return(ExitCurrentContext(
                               DynLanContext,
                               currentValue,
                               null));
                }
                // throw błędu
                else if (currentLine.OperatorType == EOperatorType.THROW)
                {
                    if (currentValue is Exception)
                    {
                        throw (Exception)currentValue;
                    }
                    else
                    {
                        String message = UniConvert.ToString(currentValue ?? "");
                        throw String.IsNullOrEmpty(message) ? new Exception() : new Exception(message);
                    }

                    /*return ExitCurrentContext(
                     *  DynLanContext,
                     *  new Exception(message));*/
                }
                // jesli return to konczymy
                else if (currentLine.OperatorType == EOperatorType.BREAK)
                {
                    return(ExitCurrentLoop(
                               DynLanContext,
                               currentValue,
                               null));
                }

                if (currentLine.OperatorType == EOperatorType.WHILE ||
                    currentLine.OperatorType == EOperatorType.IF ||
                    currentLine.OperatorType == EOperatorType.ELIF)
                {
                    Boolean conditionResult = BooleanHelper.IfTrue(currentValue);
                    if (conditionResult)
                    {
                        DynLanCodeLine nextLine = DynLanCodeLinesExtender.
                                                  NextOnSameOrHigher(lines, currentLine);

                        if (nextLine != null)
                        {
                            currentState.CurrentLineID = nextLine.ID;
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                    else
                    {
                        DynLanCodeLine nextLine = DynLanCodeLinesExtender.
                                                  NextOnSameOrLower(lines, currentLine);

                        if (nextLine == null)
                        {
                            return(ExitCurrentContext(
                                       DynLanContext,
                                       null));
                        }
                        else
                        {
                            if (nextLine.Depth < currentLine.Depth)
                            {
                                while (
                                    nextLine != null &
                                    (nextLine.OperatorType == EOperatorType.ELSE ||
                                     nextLine.OperatorType == EOperatorType.ELIF /*||
                                                                                  * nextLine.OperatorType == EOperatorType.FINALLY*/))
                                {
                                    nextLine = DynLanCodeLinesExtender.ExitParentIf(lines, nextLine);

                                    if (nextLine == null)
                                    {
                                        break;
                                    }
                                }

                                if (nextLine == null)
                                {
                                    return(ExitCurrentContext(
                                               DynLanContext,
                                               null));
                                }

                                if (nextLine.Depth < currentLine.Depth)
                                {
                                    //DynLanCodeLine prevIf = lines.
                                    //    PrevLineWithLessDepth(currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF);

                                    while (true)
                                    {
                                        DynLanCodeLine prevConditionLine = DynLanCodeLinesExtender.PrevLineWithLessDepth(
                                            lines,
                                            currentLine,
                                            l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF || l.OperatorType == EOperatorType.ELSE || l.OperatorType == EOperatorType.WHILE);

                                        if (prevConditionLine != null &&
                                            prevConditionLine.Depth >= nextLine.Depth &&
                                            prevConditionLine.OperatorType == EOperatorType.WHILE)
                                        {
                                            currentState.CurrentLineID = prevConditionLine.ID;
                                            break;
                                        }
                                        else if (prevConditionLine != null)
                                        {
                                            currentLine = prevConditionLine;
                                        }
                                        else
                                        {
                                            currentState.CurrentLineID = nextLine.ID;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    currentState.CurrentLineID = nextLine.ID;
                                }
                            }
                            else
                            {
                                currentState.CurrentLineID = nextLine.ID;
                            }
                        }
                    }
                }
                else if (
                    currentLine.OperatorType == EOperatorType.TRY ||
                    currentLine.OperatorType == EOperatorType.ELSE)
                {
                    DynLanCodeLine nextLine = DynLanCodeLinesExtender.
                                              NextOnSameOrHigher(lines, currentLine);

                    if (nextLine != null)
                    {
                        currentState.CurrentLineID = nextLine.ID;
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                else if (
                    (currentLine.OperatorType == EOperatorType.FINALLY))
                {
                    throw new NotImplementedException("FINALLY");
                }
                else if (
                    (currentLine.OperatorType == EOperatorType.CATCH))
                {
                    if (DynLanContext.Error != null)
                    {
                        DynLanContext.Error = null;
                        DynLanCodeLine nextLine = DynLanCodeLinesExtender.
                                                  NextOnSameOrHigher(lines, currentLine);

                        if (nextLine != null)
                        {
                            currentState.CurrentLineID = nextLine.ID;
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                    else
                    {
                        DynLanCodeLine nextLine = DynLanCodeLinesExtender.NextOnSameOrLower(
                            lines, currentLine);

                        if (nextLine != null)
                        {
                            currentState.CurrentLineID = nextLine.ID;
                        }
                        else
                        {
                            return(ExitCurrentContext(
                                       DynLanContext,
                                       null));
                        }
                    }
                }
                else if (currentLine.OperatorType == EOperatorType.NONE || currentLine.OperatorType == EOperatorType.PASS)
                {
                    DynLanCodeLine nextLine = DynLanCodeLinesExtender.NextLine(lines, currentLine);
                    if (nextLine != null)
                    {
                        while (
                            nextLine != null &
                            (nextLine.OperatorType == EOperatorType.ELSE ||
                             nextLine.OperatorType == EOperatorType.ELIF /*||
                                                                          * nextLine.OperatorType == EOperatorType.FINALLY*/))
                        {
                            nextLine = DynLanCodeLinesExtender.ExitParentIf(lines, nextLine);

                            if (nextLine == null)
                            {
                                return(ExitCurrentContext(
                                           DynLanContext,
                                           null));
                            }
                        }

                        if (nextLine == null)
                        {
                            return(ExitCurrentContext(
                                       DynLanContext,
                                       null));
                        }

                        if (nextLine.Depth < currentLine.Depth)
                        {
                            //DynLanCodeLine prevIf = lines.
                            //    PrevLineWithLessDepth(currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF);

                            while (true)
                            {
                                DynLanCodeLine prevConditionLine = DynLanCodeLinesExtender.
                                                                   PrevLineWithLessDepth(
                                    lines,
                                    currentLine,
                                    l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF || l.OperatorType == EOperatorType.ELSE || l.OperatorType == EOperatorType.WHILE);

                                if (prevConditionLine != null &&
                                    prevConditionLine.Depth >= nextLine.Depth &&
                                    prevConditionLine.OperatorType == EOperatorType.WHILE)
                                {
                                    currentState.CurrentLineID = prevConditionLine.ID;
                                    break;
                                }
                                else if (prevConditionLine != null)
                                {
                                    currentLine = prevConditionLine;
                                }
                                else
                                {
                                    currentState.CurrentLineID = nextLine.ID;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            currentState.CurrentLineID = nextLine.ID;
                        }
                    }
                    // jeśli ostatnia linia i jesteśmy w while'u
                    else
                    {
                        //DynLanCodeLine prevIf = lines.
                        //    PrevLineWithLessDepth(currentLine, l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF);

                        while (true)
                        {
                            DynLanCodeLine prevConditionLine = DynLanCodeLinesExtender.
                                                               PrevLineWithLessDepth(
                                lines,
                                currentLine,
                                l => l.OperatorType == EOperatorType.IF || l.OperatorType == EOperatorType.ELIF || l.OperatorType == EOperatorType.ELSE || l.OperatorType == EOperatorType.WHILE);

                            if (prevConditionLine != null &&
                                prevConditionLine.OperatorType == EOperatorType.WHILE)
                            {
                                currentState.CurrentLineID = prevConditionLine.ID;
                                break;
                            }
                            else if (prevConditionLine != null)
                            {
                                currentLine = prevConditionLine;
                            }
                            else
                            {
                                return(ExitCurrentContext(
                                           DynLanContext,
                                           null));
                            }
                        }
                    }
                }
                return(false);
            }
            catch (Exception ex)
            {
                throw;
            }
        }