public static IExpression CreateFromExpression(string expression)
        {
            IExpression result = null;

            if (ConstantExpression.IsMatch(expression))
            {
                result = new ConstantExpression();
            }
            else if (AddExpression.IsMatch(expression))
            {
                result = new AddExpression();
            }
            else if (SubExpression.IsMatch(expression))
            {
                result = new SubExpression();
            }
            else if (MulExpression.IsMatch(expression))
            {
                result = new MulExpression();
            }
            else if (DivExpression.IsMatch(expression))
            {
                result = new DivExpression();
            }
            else if (LetExpression.IsMatch(expression))
            {
                result = new LetExpression();
            }
            else if (VariableExpression.IsMatch(expression))
            {
                result = new VariableExpression();
            }

            return(result);
        }
        private static RawExpression Exp2(char **input)
        {
            RawExpression e = Exp1(input);

            while (true)
            {
                if (Char(input, '*'))
                {
                    e = new MulExpression
                    {
                        Left  = e,
                        Right = Exp1(input),
                    };
                }
                else if (Char(input, '/'))
                {
                    e = new DivExpression
                    {
                        Left  = e,
                        Right = Exp1(input),
                    };
                }
                else
                {
                    break;
                }
            }
            return(e);
        }
Exemple #3
0
        public void EvaluateTests(string expression, int expectedResult)
        {
            var mulExpression = new MulExpression();

            int result = mulExpression.Evaluate(expression);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
Exemple #4
0
        public void ValidateExpressionTests(string expression, bool expectedResult, string expectedError)
        {
            var mulExpression = new MulExpression();

            bool result = mulExpression.ValidateExpression(expression, out string error);

            Assert.That(result, Is.EqualTo(expectedResult));
            Assert.That(error, Is.EqualTo(expectedError));
        }
        public void interpreterTest()
        {
            MulExpression mulExpression = new MulExpression(new VarExpression(0), new VarExpression(2));
            List <string> list          = new List <string>();

            list.Add("1");
            list.Add("+");
            list.Add("3");
            list.Add("-");
            list.Add("1");
            int i = mulExpression.interpreter(list);

            Assert.AreEqual(3, i);
        }
Exemple #6
0
 public static int Evaluate(Expression exp, int var)
 {
     if (exp is ConstantExpression)
     {
         ConstantExpression tExp = exp as ConstantExpression;
         return((int)(exp as ConstantExpression).Value);
     }
     else if (exp is VariableExpression)
     {
         return(var);
     }
     else if (exp is AddExpression)
     {
         AddExpression nExp = (AddExpression)exp;
         return(Evaluate(nExp.OperandA, var) + Evaluate(nExp.OperandB, var));
     }
     else if (exp is SubExpression)
     {
         SubExpression nExp = (SubExpression)exp;
         return(Evaluate(nExp.OperandA, var) - Evaluate(nExp.OperandB, var));
     }
     else if (exp is MulExpression)
     {
         MulExpression nExp = (MulExpression)exp;
         return(Evaluate(nExp.OperandA, var) * Evaluate(nExp.OperandB, var));
     }
     else if (exp is DivExpression)
     {
         DivExpression nExp = (DivExpression)exp;
         return(Evaluate(nExp.OperandA, var) / Evaluate(nExp.OperandB, var));
     }
     else if (exp is NegExpression)
     {
         NegExpression nExp = (NegExpression)exp;
         return(-Evaluate(nExp.Value, var));
     }
     else if (exp is InvExpression)
     {
         InvExpression nExp = (InvExpression)exp;
         return(~Evaluate(nExp.Value, var));
     }
     else if (exp is XorExpression)
     {
         XorExpression nExp = (XorExpression)exp;
         return(Evaluate(nExp.OperandA, var) ^ Evaluate(nExp.OperandB, var));
     }
     throw new NotSupportedException();
 }
Exemple #7
0
        public void IsMatchTests(string expression, bool expectedResult)
        {
            bool result = MulExpression.IsMatch(expression);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
Exemple #8
0
        public void ErrorEvaluateTests(string expression)
        {
            var mulExpression = new MulExpression();

            Assert.Throws(typeof(ArithmeticException), () => mulExpression.Evaluate(expression));
        }
		public void Visit(MulExpression x)
		{
			
		}
		public ISymbolValue Visit(MulExpression x)
		{
			return E_MathOp(x);
		}
Exemple #11
0
        /// <summary>
        /// Note: Add, Multiply as well as Cat Expressions are parsed in this method.
        /// </summary>
        IExpression AddExpression(IBlockNode Scope = null)
        {
            var left = UnaryExpression(Scope);

            OperatorBasedExpression ae = null;

            switch (laKind)
            {
                case Plus:
                case Minus:
                    ae = new AddExpression(laKind == Minus);
                    break;
                case Tilde:
                    ae = new CatExpression();
                    break;
                case Times:
                case Div:
                case Mod:
                    ae = new MulExpression(laKind);
                    break;
                default:
                    return left;
            }

            Step();

            ae.LeftOperand = left;
            ae.RightOperand = AddExpression(Scope);
            return ae;
        }
 private static RawExpression Exp2(char** input)
 {
     RawExpression e = Exp1(input);
     while (true)
     {
         if (Char(input, '*'))
         {
             e = new MulExpression
             {
                 Left = e,
                 Right = Exp1(input),
             };
         }
         else if (Char(input, '/'))
         {
             e = new DivExpression
             {
                 Left = e,
                 Right = Exp1(input),
             };
         }
         else
         {
             break;
         }
     }
     return e;
 }
 public ISymbolValue Visit(MulExpression x)
 {
     return(E_MathOp(x));
 }
 public static double ReverseEvaluate(Expression exp, double val)
 {
     if (exp is ConstantExpression)
     {
         ConstantExpression tExp = exp as ConstantExpression;
         return((exp as ConstantExpression).Value);
     }
     else if (exp is VariableExpression)
     {
         return(val);
     }
     else if (exp is AddExpression)
     {
         AddExpression nExp = (AddExpression)exp;
         if (nExp.OperandB.HasVariable)
         {
             return(ReverseEvaluate(nExp.OperandB, val - ReverseEvaluate(nExp.OperandA, val)));
         }
         else if (nExp.OperandA.HasVariable)
         {
             return(ReverseEvaluate(nExp.OperandA, val - ReverseEvaluate(nExp.OperandB, val)));
         }
         else
         {
             return(Evaluate(nExp, val));
         }
     }
     else if (exp is SubExpression)
     {
         SubExpression nExp = (SubExpression)exp;
         if (nExp.OperandB.HasVariable)
         {
             return(ReverseEvaluate(nExp.OperandB, ReverseEvaluate(nExp.OperandA, val) - val));
         }
         else if (nExp.OperandA.HasVariable)
         {
             return(ReverseEvaluate(nExp.OperandA, val + ReverseEvaluate(nExp.OperandB, val)));
         }
         else
         {
             return(Evaluate(nExp, val));
         }
     }
     else if (exp is MulExpression)
     {
         MulExpression nExp = (MulExpression)exp;
         if (nExp.OperandB.HasVariable)
         {
             return(ReverseEvaluate(nExp.OperandB, val / ReverseEvaluate(nExp.OperandA, val)));
         }
         else if (nExp.OperandA.HasVariable)
         {
             return(ReverseEvaluate(nExp.OperandA, val / ReverseEvaluate(nExp.OperandB, val)));
         }
         else
         {
             return(Evaluate(nExp, val));
         }
     }
     else if (exp is NegExpression)
     {
         NegExpression nExp = (NegExpression)exp;
         return(-ReverseEvaluate(nExp.Value, val));
     }
     else if (exp is DivExpression)
     {
         DivExpression nExp = (DivExpression)exp;
         if (nExp.OperandB.HasVariable)
         {
             return(ReverseEvaluate(nExp.OperandB, ReverseEvaluate(nExp.OperandA, val) / val));
         }
         else if (nExp.OperandA.HasVariable)
         {
             return(ReverseEvaluate(nExp.OperandA, val * ReverseEvaluate(nExp.OperandB, val)));
         }
         else
         {
             return(Evaluate(nExp, val));
         }
     }
     throw new NotSupportedException();
 }
 public AbstractType Visit(MulExpression x)
 {
     return(OpExpressionType(x));
 }
Exemple #16
0
 IExpression ParseAsmMulExpression(IBlockNode Scope, IStatement Parent)
 {
     IExpression left = ParseAsmBracketExpression(Scope, Parent);
     while (laKind == Times || laKind == Div || laKind == Mod)
     {
         Step();
         var e = new MulExpression(t.Kind);
         e.LeftOperand = left;
         e.RightOperand = ParseAsmBracketExpression(Scope, Parent);
         left = e;
     }
     return left;
 }
 public void Visit(MulExpression x)
 {
 }
        public void MulExpressionTest_1()
        {
            MulExpression mulExpression = new MulExpression(new VarExpression(1), new VarExpression(2));

            Assert.IsNotNull(mulExpression);
        }