Пример #1
0
 public Lexica()
 {
     Rules = new TokenDefinition[]
     {
         new TokenDefinition(Token.Function, MathWrapper.GetMethodsRegex(options)), //0
         new TokenDefinition(Token.Unar, unarRegex),                                //1
         new TokenDefinition(Token.Variable, variableRegex),                        //2
         new TokenDefinition(Token.Value, valueRegex),                              //3
         new TokenDefinition(Token.Binar, binarRegex),                              //4
         new TokenDefinition(Token.LeftBracket, leftBracketRegex),                  //5
         new TokenDefinition(Token.RightBracket, rightBracketRegex),                //6
         new TokenDefinition(Token.SqLeftBracket, sqLeftBracketRegex),              //7
         new TokenDefinition(Token.SqRightBracket, sqRightBracketRegex),            //8
         new TokenDefinition(Token.Divider, dividerRegex)                           //9
     };
 }
Пример #2
0
        private void PassFunction(Lexema lexema)
        {
            shuntingMachine.Push(lexema);

            var argsCount = MathWrapper.GetFunctorFor(lexema.Represents).ParamsCount;

            argumentsCountMachine.Push(argsCount);
            argumentsRecievedMachine.Push(0);

            if (wereArguments.Count != 0)
            {
                wereArguments.Pop();
                wereArguments.Push(true);
            }
            wereArguments.Push(false);
        }
Пример #3
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);
            }
        }