Inheritance: IExpression
Exemplo n.º 1
0
        public void RaiseIfTupleHasVariable()
        {
            Context context = new Context();
            var expr = new TupleExpression(new IExpression[] { new ConstantExpression(1), new VariableExpression(new Variable("X")), new AtomExpression(new Atom("y")) });

            try
            {
                expr.Evaluate(context);
                Assert.Fail();
            }
            catch (System.Exception ex)
            {
                Assert.AreEqual("variable 'X' is unbound", ex.Message);
            }
        }
Exemplo n.º 2
0
        public void CreateEmptyTuple()
        {
            var expr = new TupleExpression(new IExpression[] { });

            Assert.IsFalse(expr.HasVariable());

            var result = expr.Evaluate(null);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Tuple));

            var tuple = (Tuple)result;

            Assert.AreEqual(0, tuple.Arity);
        }
Exemplo n.º 3
0
        public void CreateSimpleTuple()
        {
            Context context = new Context();
            context.SetValue("X", 2);
            var expr = new TupleExpression(new IExpression[] { new ConstantExpression(1), new VariableExpression(new Variable("X")), new AtomExpression(new Atom("y")) });

            Assert.IsTrue(expr.HasVariable());

            var result = expr.Evaluate(context);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Tuple));

            var tuple = (Tuple)result;

            Assert.AreEqual(3, tuple.Arity);
            Assert.AreEqual(1, tuple.ElementAt(0));
            Assert.AreEqual(2, tuple.ElementAt(1));
            Assert.IsInstanceOfType(tuple.ElementAt(2), typeof(Atom));
            Assert.AreEqual("y", ((Atom)tuple.ElementAt(2)).Name);
        }
Exemplo n.º 4
0
        private IExpression ParseTerm()
        {
            Token token = this.NextToken();
            IExpression expression = null;

            if (token == null)
                return null;

            if (token.Type == TokenType.Variable)
            {
                expression = new VariableExpression(new Variable(token.Value));

                if (this.TryParseToken(TokenType.Separator, "("))
                {
                    var list = this.ParseExpressionList();
                    this.ParseToken(TokenType.Separator, ")");
                    expression = new CallExpression(expression, list);
                }
            }
            else if (token.Type == TokenType.Atom)
            {
                if (token.Value == "false")
                    return new ConstantExpression(false);
                if (token.Value == "true")
                    return new ConstantExpression(true);

                if (token.Value == "fun")
                    return this.ParseFunExpression();

                if (token.Value == "receive")
                    return this.ParseReceiveExpression();

                expression = new AtomExpression(new Atom(token.Value));

                if (this.TryParseToken(TokenType.Separator, "("))
                {
                    var list = this.ParseExpressionList();
                    this.ParseToken(TokenType.Separator, ")");
                    expression = new CallExpression(expression, list);
                }
                else if (this.TryParseToken(TokenType.Separator, ":"))
                {
                    var nexpression = new AtomExpression(new Atom(this.ParseAtom()));
                    this.ParseToken(TokenType.Separator, "(");
                    var list = this.ParseExpressionList();
                    this.ParseToken(TokenType.Separator, ")");
                    expression = new QualifiedCallExpression(expression, nexpression, list);
                }
            }
            else if (token.Type == TokenType.Integer)
                expression = new ConstantExpression(int.Parse(token.Value, CultureInfo.InvariantCulture));
            else if (token.Type == TokenType.Real)
                expression = new ConstantExpression(double.Parse(token.Value, CultureInfo.InvariantCulture));
            else if (token.Type == TokenType.String)
                expression = new ConstantExpression(token.Value);
            else if (token.Type == TokenType.Separator && token.Value == "(")
            {
                expression = this.ParseSimpleExpression();
                this.ParseToken(TokenType.Separator, ")");
            }
            else if (token.Type == TokenType.Separator && token.Value == "{")
            {
                var expressions = this.ParseExpressionList();
                this.ParseToken(TokenType.Separator, "}");
                expression = new TupleExpression(expressions);
            }
            else if (token.Type == TokenType.Separator && token.Value == "[")
            {
                var expressions = this.ParseExpressionList();
                IExpression tailexpression = null;

                if (this.TryParseToken(TokenType.Separator, "|"))
                    tailexpression = this.ParseSimpleExpression();

                this.ParseToken(TokenType.Separator, "]");
                expression = new ListExpression(expressions, tailexpression);
            }
            else
                this.PushToken(token);

            return expression;
        }