Exemple #1
0
 public bool ValueEquals(VectorExpression other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Comps.SequenceEqual(other.Comps, new ExpressionValueEqualityComparer()));
 }
Exemple #2
0
        Expression ParseExpression(Precedence p, bool vectorMode)
        {
            var e = ParseUnaryExpression(vectorMode);

            while (e != null)
            {
                var op = Peek();

                if (op == TokenType.Period)
                {
                    Consume();
                    var id = Consume();
                    if (!IsIdentifier(id))
                    {
                        Error("Expected identifier after '.'");
                    }

                    e = new MemberExpression(e, id.Value);
                    continue;
                }

                if (op == TokenType.LeftSquareBrace)
                {
                    Consume();
                    var index = ParseExpression(Precedence.Invalid, true);
                    if (Peek() != TokenType.RightSquareBrace)
                    {
                        Error("Expected ]");
                    }

                    Consume();

                    e = new LookUpExpression(e, index);
                    continue;
                }

                if (p < Precedence.Assignment)
                {
                    if (TryConsume(TokenType.Colon))
                    {
                        e = new NameValuePairExpression(e, Expect(ParseExpression(Precedence.Assignment, false)));
                        continue;
                    }
                }

                if (p < Precedence.Conditional)
                {
                    if (TryConsume(TokenType.QuestionMark))
                    {
                        var case1 = Expect(ParseExpression(Precedence.Conditional, false));

                        if (!TryConsume(TokenType.Colon))
                        {
                            Error("Expected ':'");
                        }

                        var case2 = Expect(ParseExpression(Precedence.Conditional, false));

                        e = new ConditionalExpression(e, case1, case2);
                        continue;
                    }
                }

                if (p < Precedence.Multiplicative)
                {
                    if (TryConsume(TokenType.Mul))
                    {
                        e = new MultiplyExpression(e, Expect(ParseExpression(Precedence.Multiplicative, false)));
                        continue;
                    }
                    if (TryConsume(TokenType.Div))
                    {
                        e = new DivideExpression(e, Expect(ParseExpression(Precedence.Multiplicative, false)));
                        continue;
                    }
                }

                if (p < Precedence.Additive)
                {
                    if (TryConsume(TokenType.Plus))
                    {
                        e = new AddExpression(e, Expect(ParseExpression(Precedence.Additive, false)));
                        continue;
                    }
                    if (TryConsume(TokenType.Minus))
                    {
                        e = new SubtractExpression(e, Expect(ParseExpression(Precedence.Additive, false)));
                        continue;
                    }
                }

                if (p < Precedence.LogicalAnd)
                {
                    if (TryConsume(TokenType.LogAnd))
                    {
                        e = new LogicalAndExpression(e, Expect(ParseExpression(Precedence.LogicalAnd, false)));
                        continue;
                    }
                }

                if (p < Precedence.LogicalOr)
                {
                    if (TryConsume(TokenType.LogOr))
                    {
                        e = new LogicalOrExpression(e, Expect(ParseExpression(Precedence.LogicalOr, false)));
                        continue;
                    }
                }

                if (p < Precedence.Relational)
                {
                    if (TryConsume(TokenType.LessThan))
                    {
                        e = new LessThanExpression(e, Expect(ParseExpression(Precedence.Relational, false)));
                        continue;
                    }
                    if (TryConsume(TokenType.LessOrEqual))
                    {
                        e = new LessOrEqualExpression(e, Expect(ParseExpression(Precedence.Relational, false)));
                        continue;
                    }
                    if (TryConsume(TokenType.GreaterThan))
                    {
                        e = new GreaterThanExpression(e, Expect(ParseExpression(Precedence.Relational, false)));
                        continue;
                    }
                    if (TryConsume(TokenType.GreaterOrEqual))
                    {
                        e = new GreaterOrEqualExpression(e, Expect(ParseExpression(Precedence.Relational, false)));
                        continue;
                    }
                    if (TryConsume(TokenType.NotEqual))
                    {
                        e = new NotEqualExpression(e, Expect(ParseExpression(Precedence.Relational, false)));
                        continue;
                    }
                    if (TryConsume(TokenType.Equal))
                    {
                        e = new EqualExpression(e, Expect(ParseExpression(Precedence.Relational, false)));
                        continue;
                    }
                }

                if (p < Precedence.NullCoalescing)
                {
                    if (TryConsume(TokenType.DoubleQuestionMark))
                    {
                        e = new NullCoalesceExpression(e, Expect(ParseExpression(Precedence.NullCoalescing, false)));
                    }
                }

                // Vector op
                if (vectorMode && p == Precedence.Invalid && Peek() == TokenType.Comma)
                {
                    var comps = new List <Expression>();
                    comps.Add(e);
                    while (TryConsume(TokenType.Comma))
                    {
                        e = Expect(ParseExpression(Precedence.Invalid, false));
                        comps.Add(e);
                    }
                    e = new VectorExpression(comps.ToArray()).TryFold();
                }

                break;
            }

            return(e);
        }