Inheritance: IExpression
Example #1
0
        public void HasVariablesWithVariable()
        {
            Variable variable = new Variable("X");
            MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123));

            Assert.IsTrue(expr.HasVariable());
        }
Example #2
0
        public void MatchOneAtomAndTwoVariablesInTupleWithConcreteTuple()
        {
            Context context = new Context();
            Atom p = new Atom("point");
            Variable x = new Variable("X");
            Variable y = new Variable("Y");

            MatchExpression expr = new MatchExpression(new TupleExpression(new IExpression[] { new AtomExpression(p), new VariableExpression(x), new VariableExpression(y) }), new TupleExpression(new IExpression[] { new AtomExpression(new Atom("point")), new ConstantExpression(1), new ConstantExpression(2) }));

            expr.Evaluate(context);

            var result1 = context.GetValue("X");
            var result2 = context.GetValue("Y");

            Assert.IsNotNull(result1);
            Assert.AreEqual(1, result1);
            Assert.IsNotNull(result2);
            Assert.AreEqual(2, result2);
        }
Example #3
0
        public void MatchTwoVariablesWithList()
        {
            Context context = new Context();
            Variable headvar = new Variable("H");
            Variable tailvar = new Variable("T");
            var list = new List(headvar, tailvar);
            var list2 = new List(1, new List(2, new List(3, null)));

            MatchExpression expr = new MatchExpression(new ConstantExpression(list), new ConstantExpression(list2));

            expr.Evaluate(context);

            var result1 = context.GetValue("H");
            var result2 = context.GetValue("T");

            Assert.IsNotNull(result1);
            Assert.AreEqual(1, result1);
            Assert.IsNotNull(result2);
            Assert.IsInstanceOfType(result2, typeof(List));
            Assert.AreEqual("[2,3]", result2.ToString());
        }
Example #4
0
        public void MatchIntegers()
        {
            MatchExpression expr = new MatchExpression(new ConstantExpression(123), new ConstantExpression(123));

            Assert.AreEqual(123, expr.Evaluate(null));
        }
Example #5
0
        public void HasVariablesWithIntegers()
        {
            MatchExpression expr = new MatchExpression(new ConstantExpression(123), new ConstantExpression(123));

            Assert.IsFalse(expr.HasVariable());
        }
Example #6
0
        public void RaiseIfMatchVariableInTupleWithTwoDifferentValues()
        {
            Context context = new Context();
            Variable x = new Variable("X");

            MatchExpression expr = new MatchExpression(new TupleExpression(new IExpression[] { new VariableExpression(x), new VariableExpression(x) }), new TupleExpression(new IExpression[] { new ConstantExpression(1), new ConstantExpression(2) }));

            try
            {
                expr.Evaluate(context);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                Assert.AreEqual("no match of right hand side value {1,2}", ex.Message);
            }
        }
Example #7
0
        public void RaiseIfMatchBoundVariableWithInteger()
        {
            Context context = new Context();
            Variable variable = new Variable("X");
            context.SetValue("X", 1);
            MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123));

            try
            {
                expr.Evaluate(context);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                Assert.AreEqual("no match of right hand side value 123", ex.Message);
            }
        }
Example #8
0
        public void NoMatchWithException()
        {
            MatchExpression expr = new MatchExpression(new ConstantExpression(123), new ConstantExpression("foo"));

            try
            {
                expr.Evaluate(null);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                Assert.AreEqual("no match of right hand side value foo", ex.Message);
            }
        }
Example #9
0
        public void MatchVariableWithInteger()
        {
            Context context = new Context();
            Variable variable = new Variable("X");
            MatchExpression expr = new MatchExpression(new VariableExpression(variable), new ConstantExpression(123));

            Assert.AreEqual(123, expr.Evaluate(context));

            Assert.AreEqual(123, context.GetValue("X"));
        }
Example #10
0
        private IExpression ParseMatchExpression()
        {
            IExpression expression = this.ParseSimpleExpression();

            Token token = this.NextToken();

            if (expression == null)
                if (token == null)
                    return null;
                else
                    throw new ParserException(string.Format("Unexpected '{0}'", token.Value));

            if (token != null && token.Type == TokenType.Operator && token.Value == "=")
            {
                expression = new MatchExpression(expression, this.ParseSimpleExpression());
                return expression;
            }
            else if (token != null && token.Type == TokenType.Operator && token.Value == "!")
            {
                expression = new SendExpression(expression, this.ParseSimpleExpression());
                return expression;
            }
            else
                this.PushToken(token);

            return expression;
        }