Beispiel #1
0
        //////////////////////////////////////////////

        public void RaiseProgramStart(DynLanState state, IList <DynLanMethodParam> finalParameters)
        {
            DynLanState currentState = MyCollectionsExtenders.Peek(this.Stack);

            if (this.OnProgramStart != null)
            {
                DynLanProgramChangedEventArgs args = new DynLanProgramChangedEventArgs()
                {
                    Context    = this,
                    Program    = state.Program,
                    State      = state,
                    Parameters = finalParameters
                };
                this.OnProgramStart(this, args);
                args.Clean();
            }
        }
Beispiel #2
0
        public DynLanState PopContext(Exception ex)
        {
            DynLanState currentState = MyCollectionsExtenders.Peek(this.Stack);

            if (currentState != null &&
                currentState.ContextType != DynLanContextType.GLOBAL)
            {
                RaiseProgramEnd(currentState, ex);

                currentState.Clean();

                DynLanState popedState = MyCollectionsExtenders.Pop(this.Stack);
                this.CurrentState = MyCollectionsExtenders.Peek(this.Stack);

                return(currentState);
            }
            return(null);
        }
Beispiel #3
0
        ////////////////////////////////////////////

        private DynLanProgram Compile(IList <Char> Code)
        {
            CodeLines lines = GetLines(Code);

            DynLanProgram mainProgram = new DynLanProgram()
            {
                ForceDecimals = ForceDecimals
            };
            List <DynLanProgram> methodStack = new List <DynLanProgram>();

            methodStack.Add(mainProgram);

            Int32 lineNr = 0;
            Int32 depth  = 0;

            //foreach (CodeLine line in lines)
            for (var i = 0; i < lines.Count; i++)
            {
                CodeLine line     = lines[i];
                CodeLine nextLine = i < lines.Count - 1 ? lines[i + 1] : null;

                lineNr++;
                try
                {
                    Int32 currentDepth = -1;

#if !SPACES_FOR_DEPTH
                    if (StringHelper.SequenceEqualInsensitive(line, DynLanuageSymbols.DepthBegin))
                    {
                        depth++;
                        continue;
                    }

                    else if (StringHelper.SequenceEqualInsensitive(line, DynLanuageSymbols.DepthEnd))
                    {
                        depth--;
                        continue;
                    }

                    currentDepth = depth;
#endif

                    DynLanMethod method = null;
                    method = GetMethodDefinition(line, currentDepth);

#if SPACES_FOR_DEPTH
                    if (method != null)
                    {
                        currentDepth = method.Depth;
                    }
#else
                    if (method != null)
                    {
                        if (!StringHelper.SequenceEqualInsensitive(nextLine, DynLanuageSymbols.DepthBegin))
                        {
                            throw new Exception("Method body should begin with bracket { !");
                        }
                        else
                        {
                            depth++; currentDepth++; i++;
                        }
                    }
#endif
                    DynLanClass classDefinition = null;
                    if (method == null)
                    {
                        classDefinition = GetClassDefinition(line, currentDepth);

#if SPACES_FOR_DEPTH
                        if (classDefinition != null)
                        {
                            currentDepth = classDefinition.Depth;
                        }
#else
                        if (classDefinition != null)
                        {
                            if (!StringHelper.SequenceEqualInsensitive(nextLine, DynLanuageSymbols.DepthBegin))
                            {
                                throw new Exception("Class body should begin with bracket { !");
                            }
                            else
                            {
                                depth++; currentDepth++; i++;
                            }
                        }
#endif
                    }

                    DynLanCodeLine codeLine = null;
                    if (method == null && classDefinition == null)
                    {
                        codeLine = SetCodeLine(null, line, nextLine, ref currentDepth, ref i);
                        depth    = currentDepth;
#if SPACES_FOR_DEPTH
                        if (codeLine != null)
                        {
                            currentDepth = codeLine.Depth;
                        }
#endif
                    }

                    DynLanProgram currentMethod = MyCollectionsExtenders.Peek(methodStack);
                    if (codeLine == null || !codeLine.IsLineEmpty)
                    {
                        while (currentDepth < currentMethod.Depth ||
                               (currentDepth == currentMethod.Depth && classDefinition != null && classDefinition != currentMethod) ||
                               (currentDepth == currentMethod.Depth && method != null && method != currentMethod))
                        {
                            MyCollectionsExtenders.Pop(methodStack);
                            currentMethod = MyCollectionsExtenders.Peek(methodStack);
                        }
                    }

                    if (method != null)
                    {
                        currentMethod.Methods.Remove_by_Name(method.Name);
                        currentMethod.Methods.Add(method);
                        methodStack.Add(method);
                        continue;
                    }

                    if (classDefinition != null)
                    {
                        currentMethod.Classes.Remove_by_Name(classDefinition.Name);
                        currentMethod.Classes.Add(classDefinition);
                        methodStack.Add(classDefinition);
                        continue;
                    }

                    if (codeLine != null)
                    {
                        if (codeLine.IsLineEmpty == false)
                        {
                            currentMethod.Lines.Add(codeLine);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new DynLanCompileException(
                              "Line index: " + line.LineIndex + " (" + new string(line.ToArray()) + ")" + "; " + ex.Message,
                              ex);
                }
            }

            for (var i = mainProgram.Lines.Count - 1; i >= 0; i--)
            {
                var line = mainProgram.Lines[i];

                if (line.Depth > 0)
                {
                    continue;
                }
                else if (line.OperatorType == EOperatorType.PASS)
                {
                    continue;
                }
                else if (line.OperatorType == EOperatorType.RETURN)
                {
                    break;
                }
                else if (line.OperatorType == EOperatorType.NONE)
                {
                    line.OperatorType = EOperatorType.RETURN;
                    break;
                }
                else
                {
                    break;
                }
            }

            if (depth != 0)
            {
                throw new DynLanCompileException(
                          "Incorect number of brackets. " + (depth > 0 ? ("Missing } x " + depth) : ("Too many } x " + Math.Abs(depth))));
            }

            return(mainProgram);
        }
Beispiel #4
0
        public static Boolean EvaluateQueue(
            DynContext DynLanContext)
        {
            ExpressionState   expState   = DynLanContext.CurrentExpressionState;
            ExpressionContext expContext = DynLanContext.CurrentExpressionContext;

            // policzenie nastepnego parametru
            if (expState.AreParametersCalculating)
            {
                expState.ParameterIndex++;
                if (expState.ParameterIndex < expState.ParameterTokens.Count)
                {
                    Boolean result = false;

                    ExpressionToken parameterToken = expState.ParameterTokens[expState.ParameterIndex];
                    if (parameterToken.TokenType == TokenType.VARIABLE)
                    {
                        result = ObjectValueGetter.EvaluateValue(
                            parameterToken.TokenName,
                            DynLanContext);
                    }
                    else if (parameterToken.TokenType == TokenType.VALUE)
                    {
                        ExpressionValue operationValue = StringHelper.
                                                         GetValueFromText(parameterToken.TokenChars);

                        Object value = (
                            operationValue == null ? null : operationValue.Value);

                        expState.Parameters.Add(value);
                    }
                    else
                    {
                        throw new DynLanIncorrectExpressionFormatException();
                    }

                    return(result);
                }
            }

            // czy zakończyć i zapisać wynik
            if (expState.TokenIndex >= expState.Expression.Tokens.Count)
            {
                Object finResult = null;
                if (expState.ValueStack.Count > 0)
                {
                    finResult = MyCollectionsExtenders.Pop(expState.ValueStack);
                }

                expState.ValueStack.Clear();
                expState.Finished = true;
                expState.Result   = InternalTypeConverter.ToOuter(finResult);

                MyCollectionsExtenders.Pop(expContext.Stack);

                if (expContext.Current != null)
                {
                    expContext.Current.PushValue(InternalTypeConverter.ToOuter(finResult));
                    return(false);
                }
                else
                {
                    expContext.Result     = InternalTypeConverter.ToOuter(finResult);
                    expContext.IsFinished = true;
                    return(true);
                }
            }

            Boolean         isFirstToken = expState.TokenIndex == 0;
            ExpressionToken token        = expState.
                                           Expression.
                                           Tokens[expState.TokenIndex];

            ExpressionTokens sequence = new ExpressionTokens(
                new TokenizerQueue().
                GetNextTokensOnSameLevel(expState.Expression.Tokens, expState.TokenIndex));

            Int32 originalSequenceCount = sequence.Count;

            sequence.RemoveBrackets();

            // wykonanie następnej operacji
            if (token.TokenType == TokenType.BRACKET_BEGIN)
            {
                IList <ExpressionToken> prevSequenceTokens = new TokenizerQueue().
                                                             GetPrevTokensOnSameLevel(expState.Expression.Tokens, expState.TokenIndex - 1);

                ExpressionTokens prev_sequence = prevSequenceTokens == null ?
                                                 null : new ExpressionTokens(prevSequenceTokens);

                // jeśli poprzedni operator to @
                if (
                    prev_sequence != null && prev_sequence.Count == 1 &&
                    OnpOnpTokenHelper.IsFunctionOperatorToken(prev_sequence[0]))
                {
                    Boolean result = false;

                    if (sequence.Count == 0 || expState.AreParametersCalculated)
                    {
                        Object obj = expState.ValueStack.Count == 1 ?
                                     new EmptyObject() :
                                     MyCollectionsExtenders.Peek(expState.ValueStack, 1);

                        Object methodObject = MyCollectionsExtenders.
                                              Peek(expState.ValueStack, 0);

                        result = EvaluatorForMethods.EvaluateMethod(
                            obj,
                            methodObject,
                            expState.Parameters,
                            DynLanContext);

                        expState.CleanParametersState();

                        expState.TokenIndex += originalSequenceCount;
                    }
                    else
                    {
                        expState.ParameterTokens = GetParameterTokens(sequence);
                        expState.ParameterIndex  = -1;
                        expState.Parameters      = new List <Object>();
                        result = false;
                    }

                    return(result);
                }
                // jeśli poprzedni operator to .
                else if (
                    prev_sequence != null && prev_sequence.Count == 1 &&
                    OnpOnpTokenHelper.IsPropertyOperatorToken(prev_sequence[0]))
                {
                    throw new DynLanIncorrectExpressionFormatException();
                }
                // jeśli brak poprzedniego operatora
                else if (
                    prev_sequence == null &&
                    sequence.Count == 1)
                {
                    Boolean result = false;

                    if (sequence[0].TokenType == TokenType.VARIABLE)
                    {
                        result = ObjectValueGetter.EvaluateValue(
                            sequence[0].TokenName,
                            DynLanContext);
                    }
                    else if (sequence[0].TokenType == TokenType.VALUE)
                    {
                        ExpressionValue operationValue = StringHelper.
                                                         GetValueFromText(sequence[0].TokenChars);

                        Object value = (
                            operationValue == null ? null : operationValue.Value);

                        expState.PushValue(value);
                    }
                    else
                    {
                        throw new DynLanIncorrectExpressionFormatException();
                    }

                    expState.TokenIndex += originalSequenceCount; // -1;
                    return(result);
                }
                else
                {
                    throw new DynLanInvalidExpressionException("Incorrect expression " + expState.Expression.Tokens.JoinToString(expState.TokenIndex) + "!");
                }
            }
            else if (token.TokenType == TokenType.VALUE)
            {
                ExpressionValue operationValue = StringHelper.
                                                 GetValueFromText(token.TokenChars);

                Object value = (
                    operationValue == null ? null : operationValue.Value);

                if (isFirstToken)
                {
                    expState.PushValue(value);
                }
                else
                {
                    Object prevValue = MyCollectionsExtenders.Peek(expState.ValueStack);

                    prevValue = InternalTypeConverter.
                                ToOuter(prevValue);

                    Object method = ObjectValueGetter.GetValueFromObject(
                        prevValue,
                        UniConvert.ToString(value));

                    expState.PushValue(method);
                }

                expState.TokenIndex += originalSequenceCount; // -1
            }
            else if (token.TokenType == TokenType.PROPERTY_NAME)
            {
                if (isFirstToken)
                {
                    throw new DynLanIncorrectExpressionFormatException();
                }
                else
                {
                    Object prevValue = MyCollectionsExtenders.Peek(expState.ValueStack);

                    prevValue = InternalTypeConverter.
                                ToOuter(prevValue);

                    Object value = ObjectValueGetter.GetValueFromObject(
                        prevValue,
                        token.TokenName);

                    expState.PushValue(value);
                }

                expState.TokenIndex += originalSequenceCount; // -1
            }
            else if (token.TokenType == TokenType.VARIABLE)
            {
                Boolean result = false;

                ExpressionToken next_token = expState.TokenIndex + 1 < expState.Expression.Tokens.Count ?
                                             expState.Expression.Tokens[expState.TokenIndex + 1] :
                                             null;

                Object prevValue = null;
                if (isFirstToken)
                {
                    prevValue = new EmptyObject();

                    /*result = ObjectValueGetter.EvaluateValueOrMethod(
                     *  new EMPTY_OBJECT(),
                     *  sequence[0].TokenName,
                     *  -1,
                     *  DynLanContext);*/
                }
                else
                {
                    prevValue = (
                        expState.ValueStack.Count == 0 ?
                        null :
                        MyCollectionsExtenders.Peek(expState.ValueStack));
                }

                prevValue = InternalTypeConverter.
                            ToOuter(prevValue);

                Int32 paramCount = -1;

                // jeśli następny operator to operator wołania funkcji @ to pobieramy z niego liczbę parametrów dla funkcji
                if (OnpOnpTokenHelper.IsFunctionOperatorToken(next_token) &&
                    next_token.TokenData != null &&
                    next_token.TokenData.FunctionParametersCount != null)
                {
                    paramCount = next_token.TokenData.FunctionParametersCount.Value;
                }
                else
                {
                    paramCount = -1;
                }

                result = ObjectValueGetter.EvaluateValueOrMethod(
                    prevValue,
                    sequence[0].TokenName,
                    paramCount,
                    DynLanContext);

                if (MyCollectionsExtenders.Peek(DynLanContext.CurrentExpressionState.ValueStack) == null)
                {
                    ExpressionToken next_next_token = expState.TokenIndex + 2 < expState.Expression.Tokens.Count ?
                                                      expState.Expression.Tokens[expState.TokenIndex + 2] :
                                                      null;

                    if (next_token.TokenType == TokenType.OPERATOR &&
                        OnpOnpTokenHelper.IsPropertyOperatorToken(next_token) &&
                        next_next_token != null)
                    {
                    }
                    else
                    {
                        next_next_token = null;
                    }

                    if (paramCount < 0)
                    {
                        if (next_next_token != null)
                        {
                            ExpressionToken next_next_next_next_next_token = expState.TokenIndex + 5 < expState.Expression.Tokens.Count ?
                                                                             expState.Expression.Tokens[expState.TokenIndex + 5] :
                                                                             null;

                            if (next_next_token.TokenName == "__EXT_SET" && next_next_next_next_next_token != null)
                            {
                                throw new DynLanMethodNotFoundException("Cannot find property " + next_next_next_next_next_token.TokenName + " in undefined object '" + sequence[0].TokenName + "'");
                            }
                            else
                            {
                                throw new DynLanMethodNotFoundException("Cannot call method '" + next_next_token.TokenName + "' in undefined object '" + sequence[0].TokenName + "'");
                            }
                        }
                        else
                        {
                            throw new DynLanMethodNotFoundException("Undefined object '" + sequence[0].TokenName + "'");
                        }
                    }
                    else
                    {
                        throw new DynLanMethodNotFoundException("Undefined method '" + sequence[0].TokenName + "' " + (prevValue == null ? "in undefined object" : ("in object of type " + prevValue.GetType().Name)));
                    }
                }

                expState.TokenIndex += originalSequenceCount; // -1
                return(result);
            }
            else if (token.TokenType == TokenType.OPERATOR)
            {
                if (!OnpOnpTokenHelper.IsFunctionOperatorToken(token) &&
                    !OnpOnpTokenHelper.IsPropertyOperatorToken(token))
                {
                    throw new DynLanIncorrectExpressionFormatException();
                }

                expState.TokenIndex += originalSequenceCount; // -1
            }

            if (MyCollectionsExtenders.Peek(expState.ValueStack) == null)
            {
                return(false);
            }

            return(false);
        }
        public static Boolean EvaluateOnp(
            DynContext DynLanContext)
        {
            Boolean           result     = false;
            ExpressionState   expState   = DynLanContext.CurrentExpressionState;
            ExpressionContext expContext = DynLanContext.CurrentExpressionContext;

            // czy zakończyć i zapisać wynik
            if (expState.TokenIndex >= expState.Expression.OnpTokens.Count)
            {
                Object finResult = null;
                if (expState.ValueStack.Count > 0)
                {
                    finResult = MyCollectionsExtenders.Pop(expState.ValueStack);
                }

                expState.ValueStack.Clear();
                expState.Finished = true;
                expState.Result   = InternalTypeConverter.ToOuter(finResult);

                MyCollectionsExtenders.Pop(expContext.Stack);

                if (expContext.Current != null)
                {
                    expContext.Current.PushValue(InternalTypeConverter.ToOuter(finResult));
                    return(false);
                }
                else
                {
                    expContext.Result     = InternalTypeConverter.ToOuter(finResult);
                    expContext.IsFinished = true;
                    return(true);
                }
            }

            ExpressionToken token = expState.Expression.OnpTokens[expState.TokenIndex];

            // wykonanie następnej operacji
            if (token.TokenType == TokenType.VALUE)
            {
                ExpressionValue operationValue = StringHelper.
                                                 GetValueFromText(token.TokenChars);

                Object value = operationValue == null ? null :
                               InternalTypeConverter.ToInner(operationValue.Value);

                expState.PushValue(value);
            }
            else if (token.TokenType == TokenType.PROPERTY_NAME)
            {
                expState.PushValue(token.TokenName);
            }
            if (token.TokenType == TokenType.VARIABLE)
            {
                result = ObjectValueGetter.EvaluateValue(
                    token.TokenName,
                    DynLanContext);
            }
            else if (token.TokenType == TokenType.OPERATOR)
            {
                Object valueA = InternalTypeConverter.ToInner(
                    MyCollectionsExtenders.Pop(expState.ValueStack));

                Object valueB = InternalTypeConverter.ToInner(
                    MyCollectionsExtenders.Pop(expState.ValueStack));

                Object       value        = null;
                OperatorType operatorType = OperatorTypeHelper.
                                            GetOperationType(token.TokenChars);

                try
                {
                    value = OperationHelper.Do(
                        operatorType,
                        valueB,
                        valueA,
                        DynLanContext.ForceDecimals);

                    expState.PushValue(value);
                }
                catch
                {
                    throw;
                }
            }

            expState.TokenIndex++;
            return(result);
        }