Esempio n. 1
0
        private bool HasHigherPrecedence(LexicToken current, LexicToken prev)
        {
            int currentPrecedence = this.GetPrecedence(current);
            int prevPrecedence    = this.GetPrecedence(prev);

            return(currentPrecedence > prevPrecedence || (current is PowerLexicToken));
        }
Esempio n. 2
0
        private void EvaluateToken(LexicToken token, List <LexicToken> output, Stack <LexicToken> operators)
        {
            if (token is WhitespaceLexicToken || token is ArgumentSeparatorLexicToken)
            {
                return;
            }
            // if constant, factorial or variable add to output
            if (token is ConstantLexicToken || token is FactorialLexicToken || token is VariableLexicToken)
            {
                output.Add(token);
                return;
            }
            if (token is LeftParenLexicToken)
            {
                operators.Push(token);
                return;
            }
            if (token is RightParenLexicToken)
            {
                LexicToken op;
                while (operators.CanPop())
                {
                    op = operators.Pop();
                    if (op is LeftParenLexicToken)
                    {
                        break;
                    }
                    output.Add(op);
                }
                return;
            }
            if (!operators.Any())
            {
                operators.Push(token);
                return;
            }
            LexicToken prev = operators.Peek();

            if (prev is LeftParenLexicToken || this.HasHigherPrecedence(token, prev))
            {
                operators.Push(token);
            }
            else
            {
                output.Add(operators.Pop());
                operators.Push(token);
            }
        }
Esempio n. 3
0
        private void ValidateLexingOutput(IList <LexicToken> tokens)
        {
            if (tokens.Count < 2)
            {
                return;
            }
            LexicToken t1 = null;
            LexicToken t2 = tokens[0];

            for (int i = 1; i < tokens.Count; i++)
            {
                t1 = t2;
                t2 = tokens[i];
                if ((t1 is ConstantLexicToken || t1 is VariableLexicToken) && (t2 is VariableLexicToken || t2 is FunctionLexicToken || t2 is LeftParenLexicToken))
                {
                    tokens.Insert(i++, new MultiplyLexicToken());
                }
            }
        }
Esempio n. 4
0
 private int GetPrecedence(LexicToken token)
 {
     if (token is AddLexicToken)
     {
         return(OperationPriorities.Addition);
     }
     else if (token is SubtractLexicToken)
     {
         return(OperationPriorities.Subtraction);
     }
     else if (token is MultiplyLexicToken)
     {
         return(OperationPriorities.Multiplication);
     }
     else if (token is DivideLexicToken)
     {
         return(OperationPriorities.Division);
     }
     else if (token is ModulusLexicToken)
     {
         return(OperationPriorities.Modulus);
     }
     else if (token is PowerLexicToken)
     {
         return(OperationPriorities.Power);
     }
     else if (token is FunctionLexicToken)
     {
         return(OperationPriorities.Function);
     }
     else if (token is FactorialLexicToken)
     {
         return(OperationPriorities.Function);
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Esempio n. 5
0
        public LexerResult Evaluate(ILexerContext input)
        {
            InputStream       stream = input.Stream;
            List <LexicToken> tokens = new List <LexicToken>();

            while (!stream.IsEmpty)
            {
                InputStream copy = stream;
                foreach (TokenReader reader in input.Readers)
                {
                    LexicToken token = null;
                    stream = reader.TryRead(stream, out token);
                    if (token != null && !(token is WhitespaceLexicToken))
                    {
                        if (token is NamedLexicToken)
                        {
                            NamedLexicToken nlt = (NamedLexicToken)token;
                            if (input.IsFunction(nlt.Name))
                            {
                                token = new FunctionLexicToken(nlt.Name);
                            }
                            else
                            {
                                token = new VariableLexicToken(nlt.Name);
                            }
                        }
                        tokens.Add(token);
                        break;
                    }
                }
                if (stream == copy)
                {
                    throw new ParserException($"Unexpected character '{stream.Content[0]}' at position {stream.Position}");
                }
            }
            this.ValidateLexingOutput(tokens);
            return(new LexerResult(tokens));
        }