Пример #1
0
        private void CreateAssignment()
        {
            var oprCharArray = assignOperatorToken.Represents.ToCharArray();

            if (oprCharArray.Length > 1)
            {
                try
                {
                    var assignOperator = DefaultOperator.GetBySign(oprCharArray[0].ToString());
                    result.priority = (short)assignOperator.Priority;
                }
                catch (ArgumentException ex)
                {
                    throw new ArgumentException($"Can`t create an assignment in expression: \"{lexedExpression.Initial}\". " +
                                                $"Set: \"{lexedExpression.TargetSet}\". Reason: {ex.ToString()}");
                }

                inner.EnqueueHead(new Lexema(oprCharArray[0].ToString(), Token.Binar));
                inner.EnqueueHead(variableToken);
            }
        }
Пример #2
0
        private void BuildExpression()
        {
            if (LexicalQueue.Count == 1 && LexicalQueue.First.Value.Token == Token.Value)
            {
                variables[variableToken.Represents].Value = Number.Parse(LexicalQueue.First.Value.Represents);

                return;
            }

            for (int i = 0; i < lexedExpression.LexicalQueue.Count; i++)
            {
                var lexema = lexedExpression.LexicalQueue.DequeueHead();

                IFunctor functor;

                switch (lexema.Token)
                {
                case Token.Value:

                    functor = new VoidFunctor().SetInnerArgs(new IValueHolder[]
                    {
                        new ValueHolder(Number.Parse(lexema.Represents))
                    });
                    break;

                case Token.Variable:

                    if (variables.getters.ContainsKey(lexema.Represents))
                    {
                        functor = variables.getters[lexema.Represents];
                    }
                    else
                    {
                        functor = new ValueGetter(lexema.Represents, variables);
                        variables.getters.Add(lexema.Represents, functor as ValueGetter);
                    }
                    break;

                case Token.Function:

                    functor = MathWrapper.GetFunctorFor(lexema.Represents);
                    break;

                case Token.Binar:

                    functor = DefaultOperator.GetBySign(lexema.Represents);
                    break;

                case Token.Unar:

                    functor = DefaultOperator.GetBySign(lexema.Represents, isUnar: true);
                    break;

                default:

                    throw new ArgumentException($"Wrong input token {lexema.Represents} has arived " +
                                                $"while translating \"{lexedExpression.Initial}\" on position {lexema.Position}! " +
                                                $"TargetSet: {lexedExpression.TargetSet}");
                }

                result.members.Enqueue(functor);

                lexedExpression.LexicalQueue.EnqueueTail(lexema);
            }
        }