Example #1
0
 public void Visit(Minus min)
 {
     _sb.Append("-");
     min.Operand.Accept(this);
 }
 public virtual void VisitMinus(Minus n)
 {
 }
Example #3
0
 public virtual void Visit(Minus node)
 {
 }
Example #4
0
 public void Visit(Minus min)
 {
     min.Operand.Accept(this);
     min.Type = MustBeInt(string.Format("The operand of {0} must be integer", min));
 }
 public void Visit(Minus min)
 {
     // Nothing to do here...
 }
Example #6
0
        Expr ParseExpr()
        {
            var OpStack = new Stack<Operator>();
            var CStack = new Stack<Expr>();
            ExprNext NextCanBe = ExprNext.UExpr | ExprNext.BExpr | ExprNext.Literal | ExprNext.Bracket;
            while (Index < Tokens.Count)
            {
                // token is literal (double, int, bool, string, function call)
                if (NextCanBe.HasFlag(ExprNext.Literal))
                {
                    if (Tokens[Index].isSB)
                    {
                        var stringLiteral = new StringLiteral();
                        stringLiteral.Value = ((StringBuilder)Tokens[Index]).ToString();
                        Index++;
                        CStack.Push(stringLiteral);
                        NextCanBe = ExprNext.BExpr;
                        continue;
                    }
                    else if (Tokens[Index].isInt)
                    {
                        var intLiteral = new IntLiteral();
                        intLiteral.Value = (int)Tokens[Index];
                        Index++;
                        CStack.Push(intLiteral);
                        NextCanBe = ExprNext.BExpr;
                        continue;
                    }
                    else if (Tokens[Index].isDbl)
                    {
                        var dblLiteral = new DblLiteral();
                        dblLiteral.Value = (double)Tokens[Index];
                        NextCanBe = ExprNext.BExpr;
                        Index++;
                        CStack.Push(dblLiteral);
                        continue;
                    }
                    else if (Tokens[Index].isStr && FuncDef.ContainsKey((string)Tokens[Index]))
                    {    // If function is exists
                        var function = new ExprFunction();
                        function.Identity = (string)Tokens[Index];
                        var P = FuncDef[(string)Tokens[Index]].Parameters.Count;
                        int i = 0;
                        Index++;
                        while (Index < Tokens.Count && i < P)
                        {
                            function.Parameters.Add(ParseExpr());
                            i++;
                        }
                        if (Index == Tokens.Count && i < P)
                        {
                            throw new RPExeption(29, App.Current.TryFindResource("x_fparnm").ToString(), Line, Column);
                        }
                        CStack.Push(function);
                        NextCanBe = ExprNext.BExpr;
                        continue;
                    }
                }

                // if token is not expression either break
                if (!NextCanBe.HasFlag(ExprNext.UExpr) && !NextCanBe.HasFlag(ExprNext.BExpr) && !NextCanBe.HasFlag(ExprNext.Bracket) || !Tokens[Index].isStr) break;
                // else token is an operator
                switch ((string)Tokens[Index])
                {
                    case Scanner.BracketStart:
                        if (NextCanBe.HasFlag(ExprNext.Bracket))
                        {
                            Index++;
                            OpStack.Push(new Group());
                            NextCanBe = ExprNext.UExpr | ExprNext.BExpr | ExprNext.Literal | ExprNext.Bracket;
                            continue;
                        }
                        break;
                    case Scanner.BracketEnd:
                        {
                            Index++;
                            var o = (OpStack.Count > 0) ? OpStack.Peek() : null;
                            while (OpStack.Count > 0 && !(o is Group))
                            {
                                if (o.IsBinary)
                                {
                                    var expr = new BinaryExpression();
                                    expr.BinaryOperator = OpStack.Pop();
                                    if (expr.BinaryOperator.Level >= OpStack.Peek().Level)
                                    {
                                        expr.Right = CStack.Pop();
                                        expr.Left = CStack.Pop();
                                    }
                                    else
                                    {
                                        expr.Left = CStack.Pop();
                                        expr.Right = CStack.Pop();
                                    }
                                    CStack.Push(expr);
                                }
                                else
                                {
                                    var expr = new UnaryExpression();
                                    expr.Right = CStack.Pop();
                                    expr.UnaryOperator = OpStack.Pop();
                                    CStack.Push(expr);
                                }
                                if (OpStack.Count > 0) o = OpStack.Peek();
                            }
                            if (OpStack.Count > 0)
                            {
                                OpStack.Pop();
                                NextCanBe = ExprNext.BExpr;
                            }
                            else NextCanBe = 0;
                            continue;
                        }
                    case Scanner.ValueOf:
                        if (NextCanBe.HasFlag(ExprNext.UExpr))
                        {
                            Index++;
                            var var = new Variable();
                            var.Identity = (string)Tokens[Index];
                            CStack.Push(var);
                            OpStack.Push(new ValueOf());
                            NextCanBe = ExprNext.BExpr;
                            Index++;
                            continue;
                        }
                        break;
                    case Scanner.AsIs:
                        if (NextCanBe.HasFlag(ExprNext.UExpr))
                        {
                            Index++;
                            var strL = new StringLiteral();
                            strL.Value = (string)Tokens[Index];
                            CStack.Push(strL);
                            OpStack.Push(new AsIs());
                            NextCanBe = ExprNext.BExpr;
                            Index++;
                            continue;
                        }
                        break;
                    case Scanner.And:
                    case Scanner.Or:
                    case Scanner.Xor:
                    case Scanner.Not:
                    case Scanner.Add:
                    case Scanner.Subtract:
                    case Scanner.Multiply:
                    case Scanner.Divide:
                    case Scanner.Equal:
                    case Scanner.Bigger:
                    case Scanner.BiggerEqual:
                    case Scanner.Smaller:
                    case Scanner.SmallerEqual:
                    case Scanner.NotEqual:
                        {
                            var op = Operator.Parse((string)Tokens[Index]);
                            var o = (OpStack.Count > 0) ? OpStack.Peek() : op;
                            while (OpStack.Count > 0 && op.Level >= o.Level && !(o is Group))
                            {
                                if (o.IsBinary)
                                {
                                    var expr = new BinaryExpression();
                                    expr.Right = CStack.Pop();
                                    expr.BinaryOperator = OpStack.Pop();
                                    expr.Left = CStack.Pop();
                                    CStack.Push(expr);
                                }
                                else
                                {
                                    var expr = new UnaryExpression();
                                    expr.Right = CStack.Pop();
                                    expr.UnaryOperator = OpStack.Pop();
                                    CStack.Push(expr);
                                }
                                if (OpStack.Count > 0) o = OpStack.Peek();
                            }
                            if (CStack.Count == 0 && op is Sub || Diff(CStack, OpStack) && op is Sub) op = new Minus();        // If there was no literal before or previous token was also an operator
                            OpStack.Push(op);
                            NextCanBe = ExprNext.BExpr | ExprNext.UExpr | ExprNext.Literal | ExprNext.Bracket;
                            Index++;
                            continue;
                        }
                }
                break;
            }
            while (OpStack.Count > 0)
            {
                var o = OpStack.Peek();
                if (o.IsBinary)
                {
                    var expr = new BinaryExpression();
                    expr.Right = CStack.Pop();
                    expr.BinaryOperator = OpStack.Pop();
                    expr.Left = CStack.Pop();
                    CStack.Push(expr);
                }
                else
                {
                    var expr = new UnaryExpression();
                    expr.Right = CStack.Pop();
                    expr.UnaryOperator = OpStack.Pop();
                    CStack.Push(expr);
                }
            }
            return CStack.Pop();
        }
Example #7
0
 public void Visit(Minus min)
 {
     _instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0));
     min.Operand.Accept(this);
     _instructions.Add(Instruction.Create(OpCodes.Sub_Ovf));
 }
Example #8
0
 public void TestSimpleSubstraction()
 {
     var minus = new Minus(new Property("Left"), new Property("Right"));
     Assert.That(minus.Evaluate(new Reflection(this)), Is.EqualTo(-5));
 }
        void PlusMinus(out Expression exp)
        {
            Expression second;
            MulDivMod(out exp);
            while (la.kind == 40 || la.kind == 41) {
            if (la.kind == 40) {
                Get();
            } else {
                Get();
            }
            Token tok = t;
            MulDivMod(out second);
            if (!ExpectInt(exp, tok, false)) { return; }
            if (!ExpectInt(second, tok, true)) { return; }
            if (tok.val == "+") {
                exp = new Plus((TypedExpression<int>)exp, (TypedExpression<int>)second);
            } else if (tok.val == "-") {
                exp = new Minus((TypedExpression<int>)exp, (TypedExpression<int>)second);
            }

            }
        }
 private void ParseNextPart(ref OperationClass a, ref bool isExpressionNotEnded)
 {
     if (l < expression.Length)
     {
         string s = expression.Substring(l);
         if (Regex.IsMatch(s, digits, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, digits, RegexOptions.IgnoreCase);
             s  = s.Substring(0, digit.Length);
             a  = new Value(float.Parse(s));
             l += digit.Length;
         }
         else if (Regex.IsMatch(s, cos, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, cos, RegexOptions.IgnoreCase);
             l += digit.Length;
             OperationClass b = new Value(0);
             Parse(ref b);
             a = new Cosinus(b);
         }
         else if (Regex.IsMatch(s, sin, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, sin, RegexOptions.IgnoreCase);
             l += digit.Length;
             OperationClass b = new Value(0);
             Parse(ref b);
             a = new Sinus(b);
         }
         else if (Regex.IsMatch(s, tangent, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, tangent, RegexOptions.IgnoreCase);
             l += digit.Length;
             OperationClass b = new Value(0);
             Parse(ref b);
             a = new Tangent(b);
         }
         else if (Regex.IsMatch(s, plus, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, plus, RegexOptions.IgnoreCase);
             l += digit.Length;
             Plus b = new Plus();
             b.FirstOperand = a;
             ParseNextPart(ref b.SecondOperand, ref isExpressionNotEnded);
             a = b;
         }
         else if (Regex.IsMatch(s, minus, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, minus, RegexOptions.IgnoreCase);
             l += digit.Length;
             Minus b = new Minus();
             b.FirstOperand = a;
             ParseNextPart(ref b.SecondOperand, ref isExpressionNotEnded);
             a = b;
         }
         else if (Regex.IsMatch(s, multipl, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, multipl, RegexOptions.IgnoreCase);
             l += digit.Length;
             Multiplication b = new Multiplication();
             ParseNextPart(ref b.SecondOperand, ref isExpressionNotEnded);
             if (a.Priority > 2)
             {
                 BasicOperations buff = a as BasicOperations;
                 b.FirstOperand     = buff.SecondOperand;
                 buff.SecondOperand = b;
                 a = buff;
             }
             else
             {
                 b.FirstOperand = a;
                 a = b;
             }
         }
         else if (Regex.IsMatch(s, divide, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, divide, RegexOptions.IgnoreCase);
             l += digit.Length;
             Division b = new Division();
             ParseNextPart(ref b.SecondOperand, ref isExpressionNotEnded);
             if (a.Priority > 2)
             {
                 BasicOperations buff = a as BasicOperations;
                 b.FirstOperand     = buff.SecondOperand;
                 buff.SecondOperand = b;
                 a = buff;
             }
             else
             {
                 b.FirstOperand = a;
                 a = b;
             }
         }
         else if (Regex.IsMatch(s, power, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, power, RegexOptions.IgnoreCase);
             l += digit.Length;
             Power b = new Power();
             ParseNextPart(ref b.SecondOperand, ref isExpressionNotEnded);
             if (a.Priority == -3)
             {
                 b.FirstOperand = a;
                 a = b;
             }
             //BasicOperations buff = a as BasicOperations;
             //b.FirstOperand = buff.SecondOperand;
             //buff.SecondOperand = b;
             //a = buff;
         }
         else if (Regex.IsMatch(s, e, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, e, RegexOptions.IgnoreCase);
             l += digit.Length;
             a  = new E();
         }
         else if (Regex.IsMatch(s, pi, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, pi, RegexOptions.IgnoreCase);
             l += digit.Length;
             a  = new PI();
         }
         else if (Regex.IsMatch(s, openingbracket, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, openingbracket, RegexOptions.IgnoreCase);
             l += digit.Length;
             Parse(ref a);
         }
         else if (Regex.IsMatch(s, closingbracket, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, closingbracket, RegexOptions.IgnoreCase);
             l += digit.Length;
             if (Regex.IsMatch(s, power, RegexOptions.IgnoreCase))
             {
                 digit = Regex.Match(s, power, RegexOptions.IgnoreCase);
                 l    += digit.Length;
                 Power b = new Power();
                 ParseNextPart(ref b.SecondOperand, ref isExpressionNotEnded);
                 b.FirstOperand = a;
                 a = b;
             }
             isExpressionNotEnded = false;
         }
         else if (Regex.IsMatch(s, t, RegexOptions.IgnoreCase))
         {
             Match digit = Regex.Match(s, t, RegexOptions.IgnoreCase);
             l += digit.Length;
             a  = new Tparam();
         }
     }
     else
     {
         isExpressionNotEnded = false;
     }
 }
Example #11
0
 public AbstractValue Evaluate(Minus expr)
 {
     throw new NotImplementedException();
 }
Example #12
0
 public virtual void PostVisit(Minus data)
 {
 }
Example #13
0
 public virtual void Visit(Minus data)
 {
 }
Example #14
0
 public static Expr A_Sub_A(Expr lhs, Minus m, Expr rhs)
 {
     return(new SubExpr(lhs, rhs));
 }