Ejemplo n.º 1
0
        //P is
        //    if next is a unary operator
        //         const op := unary(next)
        //         consume
        //         q := prec( op )
        //         const t := Exp( q )
        //         return mkNode( op, t )
        //    else if next = "("
        //         consume
        //         const t := Exp( 0 )
        //         expect ")"
        //         return t
        //    else if next is a v
        //         const t := mkLeaf( next )
        //         consume
        //         return t
        //    else
        //         error
        private Expression P()
        {
            Operator op = new Operator();

            if (tokens.Tok == "+")
            {
                // Skip unary +.
                tokens.Consume();
                return(P());
            }
            else if (IsUnaryPreOperator(tokens.Tok, ref op))
            {
                // Unary operator.
                tokens.Consume();
                Expression t = Exp(Precedence(op));
                return(Unary.New(op, t));
            }
            else if (tokens.Tok == "(")
            {
                // Group.
                tokens.Consume();
                Expression t = Exp(0);
                tokens.Expect(")");
                return(t);
            }
            else if (tokens.Tok == "{")
            {
                // Set.
                tokens.Consume();
                return(Set.New(L(",", "}")));
            }
            else if (tokens.Tok == "[")
            {
                // Matrix.
                tokens.Consume();
                List <List <Expression> > entries = new List <List <Expression> >();
                while (tokens.Tok == "[")
                {
                    tokens.Consume();
                    entries.Add(L(",", "]"));
                }
                tokens.Expect("]");

                return(Matrix.New(entries));
            }
            else
            {
                string tok = tokens.Consume();

                decimal dec = 0;
                double  dbl = 0.0;
                if (decimal.TryParse(tok, NumberStyles.Float, culture, out dec))
                {
                    return(Constant.New(dec));
                }
                if (double.TryParse(tok, NumberStyles.Float, culture, out dbl))
                {
                    return(Constant.New(dbl));
                }
                else if (tok == "True")
                {
                    return(Constant.New(true));
                }
                else if (tok == "False")
                {
                    return(Constant.New(false));
                }
                else if (tok == "\u221E" || tok == "oo")
                {
                    return(Constant.New(Real.Infinity));
                }
                else if (tokens.Tok == "[")
                {
                    // Bracket function call.
                    tokens.Consume();
                    List <Expression> args = L(",", "]");
                    return(Call.New(Resolve(tok, args), args));
                }
                else if (tokens.Tok == "(")
                {
                    // Paren function call.
                    tokens.Consume();
                    List <Expression> args = L(",", ")");
                    return(Call.New(Resolve(tok, args), args));
                }
                else
                {
                    return(Resolve(tok));
                }
            }
        }
Ejemplo n.º 2
0
 protected override Expression VisitUnary(Unary U)
 {
     // Logic operators are mostly constant.
     if (U.IsLogicOp)
         return 0;
     return base.VisitUnary(U);
 }
Ejemplo n.º 3
0
        protected override Expression VisitUnary(Unary U)
        {
            Expression O = Visit(U.Operand);
            Real? C = AsReal(O);
            switch (U.Operator)
            {
                case Operator.Not:
                    if (IsTrue(C))
                        return Constant.New(false);
                    else if (IsFalse(C))
                        return Constant.New(true);
                    break;
            }

            return Unary.New(U.Operator, O);
        }
Ejemplo n.º 4
0
 protected override int VisitUnary(Unary U)
 {
     return(Visit(U.Operand) + 1);
 }
Ejemplo n.º 5
0
 protected virtual T VisitUnary(Unary U)
 {
     return(VisitUnknown(U));
 }
Ejemplo n.º 6
0
 protected override bool VisitUnary(Unary U)
 {
     return(Visit(U.Operand));
 }
Ejemplo n.º 7
0
 public static LazyExpression operator -(Expression L, Expression R)
 {
     return(new LazyExpression(Sum.New(L, Unary.Negate(R))));
 }
Ejemplo n.º 8
0
 public static LazyExpression operator !(Expression O)
 {
     return(new LazyExpression(Unary.Not(O)));
 }
Ejemplo n.º 9
0
 protected override PrettyString VisitUnary(Unary U)
 {
     return(PrettyString.ConcatColumns(Unary.ToStringPrefix(U.Operator), Visit(U.Operand), Unary.ToStringPostfix(U.Operator)));
 }
Ejemplo n.º 10
0
 public Expression Solve(Expression x)
 {
     return(Unary.Negate(Sum.New(this.Where(i => !i.Key.Equals(x)).Select(i => Product.New(i.Key, i.Value)))) / this[x]);
 }
Ejemplo n.º 11
0
 protected override string VisitUnary(Unary U)
 {
     return(ToString(U.Operator) + Visit(U.Operand, Parser.Precedence(U.Operator)));
 }
Ejemplo n.º 12
0
 protected override string VisitUnary(Unary U)
 {
     return ToString(U.Operator) + Visit(U.Operand, Parser.Precedence(U.Operator));
 }
Ejemplo n.º 13
0
 public static LazyExpression operator -(LazyExpression L, LazyExpression R)
 {
     return(new LazyExpression(Sum.New(L.value, Unary.Negate(R.value))));
 }